use of org.keycloak.representations.idm.GroupRepresentation in project keycloak by keycloak.
the class UserStorageTest method testUpdate.
@Test
public void testUpdate() {
UserRepresentation thor = ApiUtil.findUserByUsername(testRealmResource(), "thor");
// update entity
thor.setFirstName("Stian");
thor.setLastName("Thorgersen");
thor.setEmailVerified(true);
long thorCreated = System.currentTimeMillis() - 100;
thor.setCreatedTimestamp(thorCreated);
thor.setEmail("thor@hammer.com");
thor.setAttributes(new HashMap<>());
thor.getAttributes().put("test-attribute", Arrays.asList("value"));
thor.setRequiredActions(new ArrayList<>());
thor.getRequiredActions().add(UPDATE_PROFILE.name());
testRealmResource().users().get(thor.getId()).update(thor);
// check entity
thor = ApiUtil.findUserByUsername(testRealmResource(), "thor");
Assert.assertEquals("Stian", thor.getFirstName());
Assert.assertEquals("Thorgersen", thor.getLastName());
Assert.assertEquals("thor@hammer.com", thor.getEmail());
Assert.assertTrue(thor.getAttributes().containsKey("test-attribute"));
Assert.assertEquals(1, thor.getAttributes().get("test-attribute").size());
Assert.assertEquals("value", thor.getAttributes().get("test-attribute").get(0));
Assert.assertTrue(thor.isEmailVerified());
// update group
GroupRepresentation g = new GroupRepresentation();
g.setName("my-group");
String gid = ApiUtil.getCreatedId(testRealmResource().groups().add(g));
testRealmResource().users().get(thor.getId()).joinGroup(gid);
// check group
boolean foundGroup = false;
for (GroupRepresentation ug : testRealmResource().users().get(thor.getId()).groups()) {
if (ug.getId().equals(gid)) {
foundGroup = true;
}
}
Assert.assertTrue(foundGroup);
// check required actions
assertTrue(thor.getRequiredActions().contains(UPDATE_PROFILE.name()));
// remove req. actions
thor.getRequiredActions().remove(UPDATE_PROFILE.name());
testRealmResource().users().get(thor.getId()).update(thor);
// change pass
ApiUtil.resetUserPassword(testRealmResource().users().get(thor.getId()), "lightning", false);
loginSuccessAndLogout("thor", "lightning");
// update role
RoleRepresentation r = new RoleRepresentation("foo-role", "foo role", false);
testRealmResource().roles().create(r);
ApiUtil.assignRealmRoles(testRealmResource(), thor.getId(), "foo-role");
// check role
boolean foundRole = false;
for (RoleRepresentation rr : user(thor.getId()).roles().getAll().getRealmMappings()) {
if ("foo-role".equals(rr.getName())) {
foundRole = true;
break;
}
}
assertTrue(foundRole);
// test removal of provider
testRealmResource().components().component(propProviderRWId).remove();
propProviderRWId = addComponent(newPropProviderRW());
loginSuccessAndLogout("thor", "hammer");
thor = ApiUtil.findUserByUsername(testRealmResource(), "thor");
Assert.assertNull(thor.getFirstName());
Assert.assertNull(thor.getLastName());
Assert.assertNull(thor.getEmail());
Assert.assertNull(thor.getAttributes());
Assert.assertFalse(thor.isEmailVerified());
foundGroup = false;
for (GroupRepresentation ug : testRealmResource().users().get(thor.getId()).groups()) {
if (ug.getId().equals(gid)) {
foundGroup = true;
}
}
Assert.assertFalse(foundGroup);
foundRole = false;
for (RoleRepresentation rr : user(thor.getId()).roles().getAll().getRealmMappings()) {
if ("foo-role".equals(rr.getName())) {
foundRole = true;
break;
}
}
assertFalse(foundRole);
}
use of org.keycloak.representations.idm.GroupRepresentation in project keycloak by keycloak.
the class SAMLServletAdapterTest method testUserAttributeStatementMapperUserGroupsNoAggregate.
@Test
public void testUserAttributeStatementMapperUserGroupsNoAggregate() throws Exception {
GroupRepresentation group1 = new GroupRepresentation();
group1.setName("group1");
group1.setAttributes(new HashMap<>());
group1.getAttributes().put("group-value", Arrays.asList("value1", "value2"));
ClientResource clientResource = ApiUtil.findClientResourceByClientId(testRealmResource(), AbstractSamlTest.SAML_CLIENT_ID_EMPLOYEE_2);
ProtocolMappersResource protocolMappersResource = clientResource.getProtocolMappers();
Map<String, String> config = new LinkedHashMap<>();
config.put("attribute.nameformat", "Basic");
config.put("user.attribute", "group-value");
config.put("attribute.name", "group-attribute");
try (AutoCloseable g1 = Creator.create(testRealmResource(), group1);
AutoCloseable uau = UserAttributeUpdater.forUserByUsername(testRealmResource(), "bburke").setAttribute("group-value", "user-value1").setGroups("/group1").update();
AutoCloseable c = createProtocolMapper(protocolMappersResource, "group-value", "saml", "saml-user-attribute-mapper", config)) {
employee2ServletPage.navigateTo();
assertCurrentUrlStartsWith(testRealmSAMLPostLoginPage);
testRealmSAMLPostLoginPage.form().login("bburke", "password");
driver.navigate().to(employee2ServletPage.getUriBuilder().clone().path("getAttributes").build().toURL());
waitForPageToLoad();
String body = driver.findElement(By.xpath("//body")).getText();
String[] values = parseCommaSeparatedAttributes(body, "group-attribute");
assertThat(values, arrayContaining("user-value1"));
employee2ServletPage.logout();
checkLoggedOut(employee2ServletPage, testRealmSAMLPostLoginPage);
}
}
use of org.keycloak.representations.idm.GroupRepresentation in project keycloak by keycloak.
the class SAMLServletAdapterTest method testUserAttributeStatementMapperUserGroupsAggregate.
@Test
public void testUserAttributeStatementMapperUserGroupsAggregate() throws Exception {
GroupRepresentation group1 = new GroupRepresentation();
group1.setName("group1");
group1.setAttributes(new HashMap<>());
group1.getAttributes().put("group-value", Arrays.asList("value1", "value2"));
ClientResource clientResource = ApiUtil.findClientResourceByClientId(testRealmResource(), AbstractSamlTest.SAML_CLIENT_ID_EMPLOYEE_2);
ProtocolMappersResource protocolMappersResource = clientResource.getProtocolMappers();
Map<String, String> config = new LinkedHashMap<>();
config.put("attribute.nameformat", "Basic");
config.put("user.attribute", "group-value");
config.put("attribute.name", "group-attribute");
config.put("aggregate.attrs", "true");
try (AutoCloseable g1 = Creator.create(testRealmResource(), group1);
AutoCloseable uau = UserAttributeUpdater.forUserByUsername(testRealmResource(), "bburke").setAttribute("group-value", "user-value1").setGroups("/group1").update();
AutoCloseable c = createProtocolMapper(protocolMappersResource, "group-value", "saml", "saml-user-attribute-mapper", config)) {
employee2ServletPage.navigateTo();
assertCurrentUrlStartsWith(testRealmSAMLPostLoginPage);
testRealmSAMLPostLoginPage.form().login("bburke", "password");
driver.navigate().to(employee2ServletPage.getUriBuilder().clone().path("getAttributes").build().toURL());
waitForPageToLoad();
String body = driver.findElement(By.xpath("//body")).getText();
String[] values = parseCommaSeparatedAttributes(body, "group-attribute");
assertThat(values, arrayContainingInAnyOrder("user-value1", "value1", "value2"));
employee2ServletPage.logout();
checkLoggedOut(employee2ServletPage, testRealmSAMLPostLoginPage);
}
}
use of org.keycloak.representations.idm.GroupRepresentation in project keycloak by keycloak.
the class GroupNamePolicyTest method getGroup.
private GroupRepresentation getGroup(String path) {
String[] parts = path.split("/");
RealmResource realm = getRealm();
GroupRepresentation parent = null;
for (String part : parts) {
if ("".equals(part)) {
continue;
}
if (parent == null) {
parent = realm.groups().groups().stream().filter(new Predicate<GroupRepresentation>() {
@Override
public boolean test(GroupRepresentation groupRepresentation) {
return part.equals(groupRepresentation.getName());
}
}).findFirst().get();
continue;
}
GroupRepresentation group = getGroup(part, parent.getSubGroups());
if (path.endsWith(group.getName())) {
return group;
}
parent = group;
}
return null;
}
use of org.keycloak.representations.idm.GroupRepresentation in project keycloak by keycloak.
the class GroupNamePolicyTest method configureAuthorization.
@Before
public void configureAuthorization() throws Exception {
createResource("Resource A");
createResource("Resource B");
createResource("Resource C");
createGroupPolicy("Only Group A Policy", "/Group A", true);
createGroupPolicy("Only Group B Policy", "/Group A/Group B", false);
createGroupPolicy("Only Group C Policy", "/Group A/Group B/Group C", false);
createResourcePermission("Resource A Permission", "Resource A", "Only Group A Policy");
createResourcePermission("Resource B Permission", "Resource B", "Only Group B Policy");
createResourcePermission("Resource C Permission", "Resource C", "Only Group C Policy");
RealmResource realm = getRealm();
GroupRepresentation group = getGroup("/Group A/Group B/Group C");
UserRepresentation user = realm.users().search("kolo").get(0);
realm.users().get(user.getId()).joinGroup(group.getId());
group = getGroup("/Group A/Group B");
user = realm.users().search("alice").get(0);
realm.users().get(user.getId()).joinGroup(group.getId());
}
Aggregations