Skip to content

Commit

Permalink
12454 allowed creating users with a given ID
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastian Schuster <[email protected]>
  • Loading branch information
sschu committed Aug 16, 2024
1 parent b0dfef0 commit 98e8156
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,11 @@ public UserModel addUser(RealmModel realm, String id, String username, boolean a

@Override
public UserModel addUser(RealmModel realm, String username) {
return addUser(realm, KeycloakModelUtils.generateId(), username.toLowerCase(), true, true);
String id = (String)session.getAttribute(UserModel.CREATE_ID_OVERRIDE);
if (id == null) {
id = KeycloakModelUtils.generateId();
}
return addUser(realm, id, username.toLowerCase(), true, true);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public interface UserModel extends RoleMapperModel {
String INCLUDE_SERVICE_ACCOUNT = "keycloak.session.realm.users.query.include_service_account";
String GROUPS = "keycloak.session.realm.users.query.groups";
String SEARCH = "keycloak.session.realm.users.query.search";
String CREATE_ID_OVERRIDE = "keycloak.session.realm.users.create.id";
String EXACT = "keycloak.session.realm.users.query.exact";
String DISABLED_REASON = "disabledReason";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ public Response createUser(final UserRepresentation rep) {
return response;
}

if (rep.getId() != null) {
session.setAttribute(UserModel.CREATE_ID_OVERRIDE, rep.getId());
}

UserModel user = profile.create();

UserResource.updateUserFromRep(profile, user, rep, session, false);
Expand All @@ -159,7 +163,7 @@ public Response createUser(final UserRepresentation rep) {

return Response.created(session.getContext().getUri().getAbsolutePathBuilder().path(user.getId()).build()).build();
} catch (ModelDuplicateException e) {
throw ErrorResponse.exists("User exists with same username or email");
throw ErrorResponse.exists("User exists with same username or email or id");
} catch (PasswordPolicyNotMetException e) {
logger.warn("Password policy not met for user " + e.getUsername(), e);
Properties messages = AdminRoot.getMessages(session, realm, auth.adminAuth().getToken().getLocale());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,21 @@ public void verifyCreateUser() {
createUser();
}

@Test
public void createUserWithId() {
String id = "01234567-89ab-cdef-0123-456789abcdef";
UserRepresentation user = createUserRepresentation(id,"user_with_id", null, null, null, null, true);
String createdId = createUser(user);
Assert.assertEquals(id, createdId);
user.setUsername("user2_with_id");
try (Response response = realm.users().create(user)) {
assertEquals(409, response.getStatus());
assertAdminEvents.assertEmpty();
ErrorRepresentation error = response.readEntity(ErrorRepresentation.class);
Assert.assertEquals("User exists with same username or email or id", error.getErrorMessage());
}
}

/**
* See KEYCLOAK-11003
*/
Expand Down Expand Up @@ -389,7 +404,7 @@ public void createDuplicateEmailWithExistingDuplicates() {
try (Response response = realm.users().create(user)) {
assertEquals(409, response.getStatus());
ErrorRepresentation error = response.readEntity(ErrorRepresentation.class);
Assert.assertEquals("User exists with same username or email", error.getErrorMessage());
Assert.assertEquals("User exists with same username or email or id", error.getErrorMessage());
assertAdminEvents.assertEmpty();
}
}
Expand Down

0 comments on commit 98e8156

Please sign in to comment.