Skip to content

Commit

Permalink
Added UserBuilder support for adding custom profile attributes (#817)
Browse files Browse the repository at this point in the history
added userbuilder support for adding custom profile attributes during user creation
  • Loading branch information
arvindkrishnakumar-okta authored Feb 8, 2023
1 parent eb0d071 commit 13eadd4
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
2 changes: 2 additions & 0 deletions api/src/main/java/com/okta/sdk/resource/user/UserBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,7 @@ default UserBuilder setGroups(String... groupIds) {

UserBuilder setSha1PasswordHash(String value, String salt, String saltOrder);

UserBuilder setCustomProfileProperty(String key, Object value);

User buildAndCreate(UserApi client);
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public static void main(String[] args) {
.setPassword(password)
.setSecurityQuestion("Favorite security question?")
.setSecurityQuestionAnswer("None of them!")
//.putProfileProperty("division", "Seven") // key/value pairs predefined in the user profile schema //TODO: fix me
//.setCustomProfileProperty("foo", "bar") // pre-req: custom profile properties need to be set in your Org's Profile editor via Admin UI.
.setActive(true)
.addGroup(group.getId()) // add user to the newly created group
.buildAndCreate(userApi);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ public class DefaultUserBuilder implements UserBuilder {
private Map<String, Object> passwordHashProperties;
private String passwordHookImportType;

private final UserProfile userProfile = new UserProfile();

public UserBuilder setPassword(char[] password) {
this.password = Arrays.copyOf(password, password.length);
return this;
Expand Down Expand Up @@ -316,7 +318,7 @@ public UserBuilder setNextLogin(UserNextLogin nextLogin) {
private CreateUserRequest build() {

CreateUserRequest createUserRequest = new CreateUserRequest();
createUserRequest.setProfile(new UserProfile());
createUserRequest.setProfile(userProfile);
UserProfile userProfile = createUserRequest.getProfile();
if (Strings.hasText(firstName)) userProfile.setFirstName(firstName);
if (Strings.hasText(lastName)) userProfile.setLastName(lastName);
Expand Down Expand Up @@ -455,6 +457,12 @@ private UserBuilder setShaPasswordHash(String shaAlgorithm, String value, String
return this;
}

@Override
public UserBuilder setCustomProfileProperty(String key, Object value) {
userProfile.getAdditionalProperties().put(key, value);
return this;
}

@Override
public User buildAndCreate(UserApi client) {
return client.createUser(build(), active, provider != null, nextLogin);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -637,9 +637,9 @@ class UsersIT extends ITSupport {

// Note: Custom user profile properties can be added with something like below, but
// the respective property must first be associated to the User schema.
// You can use the Profile Editor in your Org's administrator UI or the Schemas API
// You can use the Profile Editor in your Org administrator UI or the Schemas API
// to manage schema extensions.
//userProfile.getAdditionalProperties().put("key1", "val1")
//userProfile.getAdditionalProperties().put("foo", "bar")

updateUserRequest.setProfile(userProfile)

Expand All @@ -649,6 +649,7 @@ class UsersIT extends ITSupport {

assertThat(updatedUser.lastUpdated, greaterThan(originalLastUpdated))
assertThat(updatedUser.getProfile().getProperties().get("nickName"), equalTo("Batman"))
//assertThat(userProfile.getAdditionalProperties().get("foo"), equalTo("bar"))
}

@Test (groups = "group2")
Expand Down

0 comments on commit 13eadd4

Please sign in to comment.