Skip to content

Commit

Permalink
[CLOUDTRUST-4637] Fix bug in user attribute management
Browse files Browse the repository at this point in the history
  • Loading branch information
fperot74 authored Feb 2, 2023
1 parent 3150f95 commit df59280
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
1 change: 1 addition & 0 deletions kc-cloudtrust-module/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<keycloak.version>18.0.0</keycloak.version>
<flatbuffers.version>1.10.0</flatbuffers.version>
<apache-collections.version>4.4</apache-collections.version>
<libphonenumber.version>8.13.3</libphonenumber.version>

<mockito.version>2.28.2</mockito.version>
<junit.version>5.8.2</junit.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.keycloak.admin.client.resource.IdentityProviderResource;
import org.keycloak.admin.client.resource.RealmResource;
import org.keycloak.admin.client.resource.UserResource;
import org.keycloak.admin.client.resource.UsersResource;
import org.keycloak.representations.idm.AdminEventRepresentation;
import org.keycloak.representations.idm.ClientRepresentation;
import org.keycloak.representations.idm.CredentialRepresentation;
Expand Down Expand Up @@ -282,26 +283,25 @@ public void setUserAttribute(String username, String attributeName, List<String>
}

public void setUserAttribute(String realmName, String username, String attributeName, List<String> values) {
RealmResource testRealm = getRealm(realmName);
String userId = testRealm.users().search(username).get(0).getId();
UserResource userRes = testRealm.users().get(userId);
UserRepresentation user = userRes.toRepresentation();
UsersResource kcUsers = getRealm(realmName).users();
UserRepresentation user = kcUsers.search(username).get(0);
if (user.getAttributes()==null) {
user.setAttributes(new HashMap<>());
}
user.getAttributes().put(attributeName, values);
userRes.update(user);
kcUsers.get(user.getId()).update(user);
}

public void removeUserAttribute(String username, String attributeName) {
removeUserAttribute(defaultRealmName, username, attributeName);
}

public void removeUserAttribute(String realmName, String username, String attributeName) {
RealmResource testRealm = getRealm(realmName);
UsersResource kcUsers = getRealm(realmName).users();
// remove mobile phone from user
String userId = testRealm.users().search(username).get(0).getId();
UserResource userRes = testRealm.users().get(userId);
UserRepresentation user = userRes.toRepresentation();
UserRepresentation user = kcUsers.search(username).get(0);
user.getAttributes().remove(attributeName);
userRes.update(user);
kcUsers.get(user.getId()).update(user);
}

public Stream<CredentialRepresentation> getUserCredentials(String username, Predicate<? super CredentialRepresentation> predicate) {
Expand Down

0 comments on commit df59280

Please sign in to comment.