Update UP via provider instead of going through the UserProfileResource

- prevents error when updating realm

Closes #34540

Signed-off-by: Stefan Guilhen <sguilhen@redhat.com>
(cherry picked from commit abf0eb7f92)
This commit is contained in:
Stefan Guilhen
2024-11-07 11:40:21 -03:00
committed by Pedro Igor
parent 1cd854fe73
commit a0318812d9
3 changed files with 38 additions and 19 deletions

View File

@@ -37,7 +37,6 @@ import org.keycloak.storage.UserStorageProviderModel;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.InternalServerErrorException;
import jakarta.ws.rs.PUT;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.core.MediaType;
@@ -96,22 +95,12 @@ public class UIRealmResource {
}
private void updateUserProfileConfiguration(UIRealmRepresentation rep) {
UPConfig upConfig = rep.getUpConfig();
if (upConfig == null) {
UserProfileResource userProfileResource = new UserProfileResource(session, auth, adminEvent);
UPConfig config = rep.getUpConfig();
if (config == null) {
return;
}
UserProfileResource userProfileResource = new UserProfileResource(session, auth, adminEvent);
if (!upConfig.equals(userProfileResource.getConfiguration())) {
Response response = userProfileResource.update(upConfig);
if (isSuccessful(response)) {
return;
}
throw new InternalServerErrorException("Failed to update user profile configuration");
}
userProfileResource.setAndGetConfiguration(config);
}
private boolean isSuccessful(Response response) {

View File

@@ -95,10 +95,18 @@ public class UserProfileResource {
@APIResponse(responseCode = "200", content = @Content(schema = @Schema(implementation = UPConfig.class)))
public Response update(UPConfig config) {
auth.realm().requireManageRealm();
UserProfileProvider t = session.getProvider(UserProfileProvider.class);
return Response.ok(setAndGetConfiguration(config)).type(MediaType.APPLICATION_JSON).build();
}
public UPConfig setAndGetConfiguration(UPConfig config) {
UserProfileProvider provider = session.getProvider(UserProfileProvider.class);
if (config != null && provider.getConfiguration().equals(config)) {
return config;
}
try {
t.setConfiguration(config);
provider.setConfiguration(config);
} catch (ComponentValidationException e) {
//show validation result containing details about error
throw ErrorResponse.error(e.getMessage(), Response.Status.BAD_REQUEST);
@@ -109,6 +117,6 @@ public class UserProfileResource {
.representation(config)
.success();
return Response.ok(t.getConfiguration()).type(MediaType.APPLICATION_JSON).build();
return provider.getConfiguration();
}
}

View File

@@ -53,6 +53,7 @@ import org.keycloak.representations.userprofile.config.UPAttribute;
import org.keycloak.representations.userprofile.config.UPAttributePermissions;
import org.keycloak.representations.userprofile.config.UPAttributeRequired;
import org.keycloak.representations.userprofile.config.UPConfig;
import org.keycloak.representations.userprofile.config.UPConfig.UnmanagedAttributePolicy;
import org.keycloak.testsuite.AbstractTestRealmKeycloakTest;
import org.keycloak.testsuite.AssertEvents;
import org.keycloak.testsuite.pages.AppPage;
@@ -289,6 +290,24 @@ public class UIRealmResourceTest extends AbstractTestRealmKeycloakTest {
assertEquals(Status.FORBIDDEN.getStatusCode(), response.getStatus());
}
@Test
public void testRenameRealm() throws IOException {
RealmRepresentation rep = testRealm().toRepresentation();
UPConfig upConfig = testRealm().users().userProfile().getConfiguration();
upConfig.setUnmanagedAttributePolicy(UnmanagedAttributePolicy.ADMIN_VIEW);
String originalRealmName = rep.getRealm();
String updatedName = originalRealmName + "changed";
try {
rep.setRealm(updatedName);
updateRealmExt(toUIRealmRepresentation(rep, upConfig), originalRealmName);
} finally {
rep.setRealm(originalRealmName);
updateRealmExt(toUIRealmRepresentation(rep, upConfig), updatedName);
assertAdminEvents.clear();
}
}
private static String getKeycloakServerUrl() {
return getAuthServerContextRoot() + "/auth";
}
@@ -311,7 +330,10 @@ public class UIRealmResourceTest extends AbstractTestRealmKeycloakTest {
}
private void updateRealmExt(UIRealmRepresentation rep) {
final var realmName = rep.getRealm();
updateRealmExt(rep, rep.getRealm());
}
private void updateRealmExt(UIRealmRepresentation rep, String realmName) {
final var request = prepareHttpRequest(realmName, "ui-ext", adminClient.tokenManager());
final var response = request