diff --git a/services/src/main/java/org/keycloak/services/resources/admin/UsersResource.java b/services/src/main/java/org/keycloak/services/resources/admin/UsersResource.java index b04ef18297..fd38a4c2d9 100755 --- a/services/src/main/java/org/keycloak/services/resources/admin/UsersResource.java +++ b/services/src/main/java/org/keycloak/services/resources/admin/UsersResource.java @@ -186,6 +186,12 @@ public class UsersResource { for (Map.Entry attr : rep.getAttributes().entrySet()) { user.setAttribute(attr.getKey(), attr.getValue()); } + + for (String key : user.getAttributes().keySet()) { + if (!rep.getAttributes().containsKey(key)) { + user.removeAttribute(key); + } + } } } diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/admin/UserTest.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/admin/UserTest.java index 0d94657da5..2b2d70f4d0 100644 --- a/testsuite/integration/src/test/java/org/keycloak/testsuite/admin/UserTest.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/admin/UserTest.java @@ -10,6 +10,7 @@ import javax.ws.rs.core.Response; import java.util.List; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; import static org.junit.Assert.fail; /** @@ -149,4 +150,54 @@ public class UserTest extends AbstractClientTest { assertEquals(0, user.getSocialLinks().size()); } + @Test + public void attributes() { + UserRepresentation user1 = new UserRepresentation(); + user1.setUsername("user1"); + user1.attribute("attr1", "value1user1"); + user1.attribute("attr2", "value2user1"); + realm.users().create(user1); + + UserRepresentation user2 = new UserRepresentation(); + user2.setUsername("user2"); + user2.attribute("attr1", "value1user2"); + user2.attribute("attr2", "value2user2"); + realm.users().create(user2); + + user1 = realm.users().get("user1").toRepresentation(); + assertEquals(2, user1.getAttributes().size()); + assertEquals("value1user1", user1.getAttributes().get("attr1")); + assertEquals("value2user1", user1.getAttributes().get("attr2")); + + user2 = realm.users().get("user2").toRepresentation(); + assertEquals(2, user2.getAttributes().size()); + assertEquals("value1user2", user2.getAttributes().get("attr1")); + assertEquals("value2user2", user2.getAttributes().get("attr2")); + + user1.attribute("attr1", "value3user1"); + user1.attribute("attr3", "value4user1"); + + realm.users().get("user1").update(user1); + + user1 = realm.users().get("user1").toRepresentation(); + assertEquals(3, user1.getAttributes().size()); + assertEquals("value3user1", user1.getAttributes().get("attr1")); + assertEquals("value2user1", user1.getAttributes().get("attr2")); + assertEquals("value4user1", user1.getAttributes().get("attr3")); + + user1.getAttributes().remove("attr1"); + realm.users().get("user1").update(user1); + + user1 = realm.users().get("user1").toRepresentation(); + assertEquals(2, user1.getAttributes().size()); + assertEquals("value2user1", user1.getAttributes().get("attr2")); + assertEquals("value4user1", user1.getAttributes().get("attr3")); + + user1.getAttributes().clear(); + realm.users().get("user1").update(user1); + + user1 = realm.users().get("user1").toRepresentation(); + assertNull(user1.getAttributes()); + } + }