org.keycloak.representations.idm.GroupRepresentation Java Examples

The following examples show how to use org.keycloak.representations.idm.GroupRepresentation. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GroupImportService.java    From keycloak-config-cli with Apache License 2.0 6 votes vote down vote up
private boolean isGroupEqual(GroupRepresentation existingGroup, GroupRepresentation patchedGroup) {
    if (!CloneUtil.deepEquals(existingGroup, patchedGroup, "subGroups")) {
        return false;
    }

    List<GroupRepresentation> importedSubGroups = patchedGroup.getSubGroups();
    List<GroupRepresentation> existingSubGroups = existingGroup.getSubGroups();

    if (importedSubGroups.isEmpty() && existingSubGroups.isEmpty()) {
        return true;
    }

    if (importedSubGroups.size() != existingSubGroups.size()) {
        return false;
    }

    return areSubGroupsEqual(existingSubGroups, importedSubGroups);
}
 
Example #2
Source File: ImportGroupsIT.java    From keycloak-config-cli with Apache License 2.0 6 votes vote down vote up
@Test
@Order(10)
void shouldUpdateRealmUpdateGroupAddRealmRole() {
    doImport("10_update_realm_update_group_add_realm_role.json");

    RealmRepresentation createdRealm = keycloakProvider.get().realm(REALM_NAME).toRepresentation();
    assertThat(createdRealm.getRealm(), is(REALM_NAME));
    assertThat(createdRealm.isEnabled(), is(true));

    GroupRepresentation updatedGroup = loadGroup("/My Group");
    assertThat("name not equal", updatedGroup.getName(), is("My Group"));
    assertThat("path not equal", updatedGroup.getPath(), is("/My Group"));

    assertThat("attributes is null", updatedGroup.getAttributes(), aMapWithSize(1));
    assertThat("attributes is null", updatedGroup.getAttributes(), hasEntry(is("my added attribute"), containsInAnyOrder("my added attribute value")));

    assertThat("realm roles is null", updatedGroup.getRealmRoles(), contains("my_realm_role"));
    assertThat("client roles is null", updatedGroup.getClientRoles(), aMapWithSize(0));
    assertThat("subgroups not empty", updatedGroup.getSubGroups(), hasSize(0));
}
 
Example #3
Source File: ImportGroupsIT.java    From keycloak-config-cli with Apache License 2.0 6 votes vote down vote up
@Test
@Order(9)
void shouldUpdateRealmUpdateGroupAddAttribute() {
    doImport("09_update_realm_update_group_add_attribute.json");

    RealmRepresentation createdRealm = keycloakProvider.get().realm(REALM_NAME).toRepresentation();
    assertThat(createdRealm.getRealm(), is(REALM_NAME));
    assertThat(createdRealm.isEnabled(), is(true));

    GroupRepresentation updatedGroup = loadGroup("/My Group");
    assertThat("name not equal", updatedGroup.getName(), is("My Group"));
    assertThat("path not equal", updatedGroup.getPath(), is("/My Group"));

    assertThat("attributes is null", updatedGroup.getAttributes(), aMapWithSize(1));
    assertThat("attributes is null", updatedGroup.getAttributes(), hasEntry(is("my added attribute"), containsInAnyOrder("my added attribute value")));

    assertThat("realm roles is null", updatedGroup.getRealmRoles(), hasSize(0));
    assertThat("client roles is null", updatedGroup.getClientRoles(), aMapWithSize(0));
    assertThat("subgroups not empty", updatedGroup.getSubGroups(), hasSize(0));
}
 
Example #4
Source File: Assert.java    From keycloak with Apache License 2.0 6 votes vote down vote up
private static String name(Object o1) {
    if (o1 instanceof String) {
        return (String) o1;
    } else if (o1 instanceof RealmRepresentation) {
        return ((RealmRepresentation) o1).getRealm();
    } else if (o1 instanceof ClientRepresentation) {
        return ((ClientRepresentation) o1).getClientId();
    } else if (o1 instanceof IdentityProviderRepresentation) {
        return ((IdentityProviderRepresentation) o1).getAlias();
    } else if (o1 instanceof RoleRepresentation) {
        return ((RoleRepresentation) o1).getName();
    } else if (o1 instanceof UserRepresentation) {
        return ((UserRepresentation) o1).getUsername();
    } else if (o1 instanceof UserFederationProviderFactoryRepresentation) {
        return ((UserFederationProviderFactoryRepresentation) o1).getId();
    } else if (o1 instanceof GroupRepresentation) {
        return ((GroupRepresentation) o1).getName();
    } else if (o1 instanceof ComponentRepresentation) {
        return ((ComponentRepresentation) o1).getName();
    } else if (o1 instanceof ClientScopeRepresentation) {
        return ((ClientScopeRepresentation) o1).getName();
    }

    throw new IllegalArgumentException();
}
 
Example #5
Source File: GroupTest.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Test
// KEYCLOAK-2700
public void deleteRealmWithDefaultGroups() throws IOException {
    RealmRepresentation rep = new RealmRepresentation();
    rep.setRealm("foo");

    GroupRepresentation group = new GroupRepresentation();
    group.setName("default1");
    group.setPath("/default1");

    rep.setGroups(Collections.singletonList(group));
    rep.setDefaultGroups(Collections.singletonList("/default1"));

    adminClient.realms().create(rep);

    adminClient.realm(rep.getRealm()).remove();
}
 
Example #6
Source File: GroupTest.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Test
public void doNotAllowSameGroupNameAtTopLevel() throws Exception {
    RealmResource realm = adminClient.realms().realm("test");

    // creating "/test-group"
    GroupRepresentation topGroup = new GroupRepresentation();
    topGroup.setName("test-group");
    topGroup = createGroup(realm, topGroup);
    getCleanup().addGroupId(topGroup.getId());

    GroupRepresentation group2 = new GroupRepresentation();
    group2.setName("test-group");
    try (Response response = realm.groups().add(group2)) {
        assertEquals(Status.CONFLICT.getStatusCode(), response.getStatus());
    }
}
 
Example #7
Source File: ConcurrencyTest.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Override
public void run(int threadIndex, Keycloak keycloak, RealmResource realm) throws Throwable {
    String name = "g-" + uniqueIndex.getAndIncrement();
    GroupRepresentation c = new GroupRepresentation();
    c.setName(name);
    Response response = realm.groups().add(c);
    String id = ApiUtil.getCreatedId(response);
    response.close();

    c = realm.groups().group(id).toRepresentation();
    assertNotNull(c);
    assertTrue("Group " + name + " not found in group list",
      realm.groups().groups().stream()
        .map(GroupRepresentation::getName)
        .filter(Objects::nonNull)
        .anyMatch(name::equals));
}
 
Example #8
Source File: ImportUsersIT.java    From keycloak-config-cli with Apache License 2.0 6 votes vote down vote up
@Test
@Order(11)
void shouldUpdateRealmUpdateUserRemoveGroup() {
    // Create Users
    doImport("11_update_realm_update_user_remove_group.json");

    final RealmRepresentation realm = keycloakProvider.get().realm(REALM_NAME).toRepresentation();
    assertThat(realm.getRealm(), is(REALM_NAME));
    assertThat(realm.isEnabled(), is(true));

    final UserRepresentation user = keycloakRepository.getUser(REALM_NAME, "user1");
    assertThat(user.getEmail(), is("[email protected]"));
    assertThat(user.getLastName(), is("lastName1"));
    assertThat(user.getFirstName(), is("firstName1"));

    List<GroupRepresentation> userGroups = getGroupsByUser(user);
    assertThat(userGroups, hasSize(1));

    GroupRepresentation group1 = getGroupsByName(userGroups, "group1");
    assertThat(group1, nullValue());

    GroupRepresentation group2 = getGroupsByName(userGroups, "group2");
    assertThat(group2.getName(), is("group2"));
}
 
Example #9
Source File: ImportGroupsIT.java    From keycloak-config-cli with Apache License 2.0 6 votes vote down vote up
@Test
@Order(11)
void shouldUpdateRealmUpdateGroupAddClientRole() {
    doImport("11_update_realm_update_group_add_client_role.json");

    RealmRepresentation createdRealm = keycloakProvider.get().realm(REALM_NAME).toRepresentation();
    assertThat(createdRealm.getRealm(), is(REALM_NAME));
    assertThat(createdRealm.isEnabled(), is(true));

    GroupRepresentation updatedGroup = loadGroup("/My Group");
    assertThat("name not equal", updatedGroup.getName(), is("My Group"));
    assertThat("path not equal", updatedGroup.getPath(), is("/My Group"))
    ;
    assertThat("attributes is null", updatedGroup.getAttributes(), aMapWithSize(1));
    assertThat("attributes is null", updatedGroup.getAttributes(), hasEntry(is("my added attribute"), containsInAnyOrder("my added attribute value")));

    assertThat("realm roles is null", updatedGroup.getRealmRoles(), contains("my_realm_role"));
    assertThat("client roles is null", updatedGroup.getClientRoles(), aMapWithSize(1));
    assertThat("client roles is null", updatedGroup.getClientRoles(), hasEntry(is("moped-client"), containsInAnyOrder("my_client_role")));
    assertThat("subgroups not empty", updatedGroup.getSubGroups(), hasSize(0));
}
 
Example #10
Source File: ImportGroupsIT.java    From keycloak-config-cli with Apache License 2.0 6 votes vote down vote up
@Test
@Order(3)
void shouldUpdateRealmAddGroupWithRealmRole() {
    doImport("03_update_realm_add_group_with_realm_role.json");

    RealmRepresentation createdRealm = keycloakProvider.get().realm(REALM_NAME).toRepresentation();
    assertThat(createdRealm.getRealm(), is(REALM_NAME));
    assertThat(createdRealm.isEnabled(), is(true));

    GroupRepresentation addedGroup = loadGroup("/Group with realm role");

    assertThat("name not equal", addedGroup.getName(), is("Group with realm role"));
    assertThat("path not equal", addedGroup.getPath(), is("/Group with realm role"));
    assertThat("attributes is null", addedGroup.getAttributes(), aMapWithSize(0));
    assertThat("realm roles is null", addedGroup.getRealmRoles(), contains("my_realm_role"));
    assertThat("client roles is null", addedGroup.getClientRoles(), aMapWithSize(0));
    assertThat("subgroups is null", addedGroup.getSubGroups(), hasSize(0));
}
 
Example #11
Source File: GroupImportService.java    From keycloak-config-cli with Apache License 2.0 6 votes vote down vote up
private boolean areSubGroupsEqual(List<GroupRepresentation> existingSubGroups, List<GroupRepresentation> importedSubGroups) {
    for (GroupRepresentation importedSubGroup : importedSubGroups) {
        GroupRepresentation existingSubGroup = existingSubGroups.stream()
                .filter(group -> group.getName().equals(importedSubGroup.getName()))
                .findFirst().orElse(null);

        if (existingSubGroup == null) {
            return false;
        }

        GroupRepresentation patchedSubGroup = CloneUtil.patch(existingSubGroup, importedSubGroup);

        if (!CloneUtil.deepEquals(existingSubGroup, patchedSubGroup, "id")) {
            return false;
        }
    }

    return true;
}
 
Example #12
Source File: KeycloakMapper.java    From nexus3-keycloak-plugin with Apache License 2.0 6 votes vote down vote up
private static Set<Role> toRoles(String source, String sourceCode, List<?>[] lists, boolean forCompatible) {
    Set<Role> roles = new LinkedHashSet<>();

    for (List<?> list : lists) {
        if (list == null || list.isEmpty()) {
            continue;
        }

        for (Object representation : list) {
            if (representation instanceof RoleRepresentation) {
                if (forCompatible && ((RoleRepresentation) representation).getClientRole()) {
                    roles.add(toCompatibleRole(source, (RoleRepresentation) representation));
                }

                roles.add(toRole(source, sourceCode, (RoleRepresentation) representation));
            } else if (representation instanceof GroupRepresentation) {
                roles.add(toRole(source, sourceCode, (GroupRepresentation) representation));
            }
        }
    }
    return roles;
}
 
Example #13
Source File: KeycloakMapper.java    From nexus3-keycloak-plugin with Apache License 2.0 6 votes vote down vote up
public static Role toRole(String source, String sourceCode, GroupRepresentation representation) {
    if (representation == null) {
        return null;
    }

    Role role = new Role();
    String roleName = String.format("%s:%s%s",
                                    REALM_GROUP_PREFIX,
                                    sourceCode != null ? sourceCode + ":" : "",
                                    representation.getPath());

    role.setRoleId(roleName);
    role.setName(roleName);
    role.setReadOnly(true);
    role.setSource(source);

    return role;
}
 
Example #14
Source File: GroupImportService.java    From keycloak-config-cli with Apache License 2.0 6 votes vote down vote up
public void importGroups(RealmImport realmImport) {
    List<GroupRepresentation> groups = realmImport.getGroups();
    String realm = realmImport.getRealm();

    if (groups == null) {
        return;
    }

    List<GroupRepresentation> existingGroups = groupRepository.getGroups(realm);

    createOrUpdateGroups(groups, realm);

    if (importConfigProperties.getManaged().getGroup() == ImportManagedPropertiesValues.FULL) {
        deleteGroupsMissingInImport(realm, groups, existingGroups);
    }
}
 
Example #15
Source File: GroupPathPolicyTest.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Test
public void testAllowParentAndChildren() {
    AuthzClient authzClient = getAuthzClient();
    PermissionRequest request = new PermissionRequest("Resource A");
    String ticket = authzClient.protection().permission().create(request).getTicket();
    AuthorizationResponse response = authzClient.authorization("marta", "password").authorize(new AuthorizationRequest(ticket));

    assertNotNull(response.getToken());

    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());

    ticket = authzClient.protection().permission().create(request).getTicket();
    response = authzClient.authorization("kolo", "password").authorize(new AuthorizationRequest(ticket));

    assertNotNull(response.getToken());
}
 
Example #16
Source File: ImportGroupsIT.java    From keycloak-config-cli with Apache License 2.0 6 votes vote down vote up
@Test
@Order(0)
void shouldCreateRealmWithGroups() {
    doImport("00_create_realm_with_group.json");

    RealmRepresentation createdRealm = keycloakProvider.get().realm(REALM_NAME).toRepresentation();
    assertThat(createdRealm.getRealm(), is(REALM_NAME));
    assertThat(createdRealm.isEnabled(), is(true));

    GroupRepresentation createdGroup = loadGroup("/My Group");

    assertThat("name not equal", createdGroup.getName(), is("My Group"));
    assertThat("path not equal", createdGroup.getPath(), is("/My Group"));
    assertThat("attributes is null", createdGroup.getAttributes(), aMapWithSize(0));
    assertThat("realm roles is null", createdGroup.getRealmRoles(), hasSize(0));
    assertThat("client roles not null", createdGroup.getClientRoles(), aMapWithSize(0));
    assertThat("subgroups not empty", createdGroup.getSubGroups(), hasSize(0));
}
 
Example #17
Source File: UsersTest.java    From keycloak with Apache License 2.0 6 votes vote down vote up
private List<GroupRepresentation> setupUsersInGroupsWithPermissions() {
    //create two groups
    GroupRepresentation grp1 = createGroupWithPermissions("grp1");
    GroupRepresentation grp2 = createGroupWithPermissions("grp2");
    //create test users
    String user1Id = createUser(realmId, "user1", "password", "user1FirstName", "user1LastName", "[email protected]");
    String user2Id = createUser(realmId, "user2", "password", "user2FirstName", "user2LastName", "[email protected]");
    String user3Id = createUser(realmId, "user3", "password", "user3FirstName", "user3LastName", "[email protected]");
    String user4Id = createUser(realmId, "user4", "password", "user4FirstName", "user4LastName", "[email protected]");
    //add users to groups
    realm.users().get(user1Id).joinGroup(grp1.getId());
    realm.users().get(user2Id).joinGroup(grp1.getId());
    realm.users().get(user3Id).joinGroup(grp1.getId());
    realm.users().get(user4Id).joinGroup(grp2.getId());

    List<GroupRepresentation> groups = new ArrayList<>();
    groups.add(grp1);
    groups.add(grp2);

    return groups;
}
 
Example #18
Source File: ImportGroupsIT.java    From keycloak-config-cli with Apache License 2.0 5 votes vote down vote up
@Test
@Order(29)
void shouldUpdateRealmUpdateGroupAddAttributeValueToSubGroup() {
    doImport("29_update_realm_update_group_add_attribute_value_to_subgroup.json");

    RealmRepresentation createdRealm = keycloakProvider.get().realm(REALM_NAME).toRepresentation();
    assertThat(createdRealm.getRealm(), is(REALM_NAME));
    assertThat(createdRealm.isEnabled(), is(true));

    GroupRepresentation updatedGroup = loadGroup("/My Group");
    assertThat("name not equal", updatedGroup.getName(), is("My Group"));
    assertThat("path not equal", updatedGroup.getPath(), is("/My Group"));

    assertThat("attributes roles is null", updatedGroup.getAttributes(), aMapWithSize(1));
    assertThat("attributes roles is null", updatedGroup.getAttributes(),
            hasEntry(is("my changed attribute"), containsInAnyOrder("my changed attribute value")));
    assertThat("realm roles is null", updatedGroup.getRealmRoles(), hasSize(0));
    assertThat("client roles is null", updatedGroup.getClientRoles(), aMapWithSize(1));
    assertThat("client roles is null", updatedGroup.getClientRoles(), hasEntry(is("second-moped-client"), containsInAnyOrder("my_client_role_of_second-moped-client", "my_second_client_role_of_second-moped-client")));

    List<GroupRepresentation> subGroups = updatedGroup.getSubGroups();
    assertThat("subgroups is empty", subGroups, hasSize(1));

    GroupRepresentation subGroup = subGroups.get(0);
    assertThat("subgroup is null", subGroup, notNullValue());
    assertThat("subgroup's name not equal", subGroup.getName(), is("My SubGroup"));
    assertThat("subgroup's path not equal", subGroup.getPath(), is("/My Group/My SubGroup"));
    assertThat("subgroup's attributes is null", subGroup.getAttributes(), aMapWithSize(1));
    assertThat("subgroup's attributes is null", subGroup.getAttributes(), hasEntry(is("my subgroup attribute"), containsInAnyOrder("my subgroup attribute value", "my subgroup attribute second value")));
    assertThat("subgroup's realm roles is null", subGroup.getRealmRoles(), hasSize(0));
    assertThat("subgroup's client roles is null", subGroup.getClientRoles(), aMapWithSize(0));
    assertThat("subgroup's subgroups is null", subGroup.getSubGroups(), hasSize(0));
}
 
Example #19
Source File: ImportGroupsIT.java    From keycloak-config-cli with Apache License 2.0 5 votes vote down vote up
@Test
@Order(28)
void shouldUpdateRealmUpdateGroupAddAttributeToSubGroup() {
    doImport("28_update_realm_update_group_add_attribute_to_subgroup.json");

    RealmRepresentation createdRealm = keycloakProvider.get().realm(REALM_NAME).toRepresentation();
    assertThat(createdRealm.getRealm(), is(REALM_NAME));
    assertThat(createdRealm.isEnabled(), is(true));

    GroupRepresentation updatedGroup = loadGroup("/My Group");
    assertThat("name not equal", updatedGroup.getName(), is("My Group"));
    assertThat("path not equal", updatedGroup.getPath(), is("/My Group"));

    assertThat("attributes roles is null", updatedGroup.getAttributes(), aMapWithSize(1));
    assertThat("attributes roles is null", updatedGroup.getAttributes(),
            hasEntry(is("my changed attribute"), containsInAnyOrder("my changed attribute value")));
    assertThat("realm roles is null", updatedGroup.getRealmRoles(), hasSize(0));
    assertThat("client roles is null", updatedGroup.getClientRoles(), aMapWithSize(1));
    assertThat("client roles is null", updatedGroup.getClientRoles(), hasEntry(is("second-moped-client"), containsInAnyOrder("my_client_role_of_second-moped-client", "my_second_client_role_of_second-moped-client")));

    assertThat("client roles is null", updatedGroup.getClientRoles(), aMapWithSize(1));
    assertThat("client roles is null", updatedGroup.getClientRoles(), hasEntry(is("second-moped-client"), containsInAnyOrder("my_client_role_of_second-moped-client", "my_second_client_role_of_second-moped-client")));

    List<GroupRepresentation> subGroups = updatedGroup.getSubGroups();
    assertThat("subgroups is empty", subGroups, hasSize(1));

    GroupRepresentation subGroup = subGroups.get(0);
    assertThat("subgroup is null", subGroup, notNullValue());
    assertThat("subgroup's name not equal", subGroup.getName(), is("My SubGroup"));
    assertThat("subgroup's path not equal", subGroup.getPath(), is("/My Group/My SubGroup"));
    assertThat("subgroup's attributes is null", subGroup.getAttributes(), aMapWithSize(1));
    assertThat("subgroup's attributes is null", subGroup.getAttributes(), hasEntry(is("my subgroup attribute"), containsInAnyOrder("my subgroup attribute value")));

    assertThat("subgroup's realm roles is null", subGroup.getRealmRoles(), hasSize(0));
    assertThat("subgroup's client roles is null", subGroup.getClientRoles(), aMapWithSize(0));
    assertThat("subgroup's subgroups is null", subGroup.getSubGroups(), hasSize(0));
}
 
Example #20
Source File: ImportGroupsIT.java    From keycloak-config-cli with Apache License 2.0 5 votes vote down vote up
@Test
@Order(27)
void shouldUpdateRealmUpdateGroupRemoveClient() {
    doImport("27_update_realm_update_group_delete_remove_client.json");

    RealmRepresentation createdRealm = keycloakProvider.get().realm(REALM_NAME).toRepresentation();
    assertThat(createdRealm.getRealm(), is(REALM_NAME));
    assertThat(createdRealm.isEnabled(), is(true));

    GroupRepresentation updatedGroup = loadGroup("/My Group");
    assertThat("name not equal", updatedGroup.getName(), is("My Group"));
    assertThat("path not equal", updatedGroup.getPath(), is("/My Group"));

    assertThat("attributes roles is null", updatedGroup.getAttributes(), aMapWithSize(1));
    assertThat("attributes roles is null", updatedGroup.getAttributes(),
            hasEntry(is("my changed attribute"), containsInAnyOrder("my changed attribute value")));
    assertThat("realm roles is null", updatedGroup.getRealmRoles(), hasSize(0));
    assertThat("client roles is null", updatedGroup.getClientRoles(), aMapWithSize(1));
    assertThat("client roles is null", updatedGroup.getClientRoles(), hasEntry(is("second-moped-client"), containsInAnyOrder("my_client_role_of_second-moped-client", "my_second_client_role_of_second-moped-client")));


    List<GroupRepresentation> subGroups = updatedGroup.getSubGroups();
    assertThat("subgroups is empty", subGroups, hasSize(1));

    GroupRepresentation subGroup = subGroups.get(0);
    assertThat("subgroup is null", subGroup, notNullValue());
    assertThat("subgroup's name not equal", subGroup.getName(), is("My SubGroup"));
    assertThat("subgroup's path not equal", subGroup.getPath(), is("/My Group/My SubGroup"));
    assertThat("subgroup's attributes is null", subGroup.getAttributes(), aMapWithSize(0));
    assertThat("subgroup's realm roles is null", subGroup.getRealmRoles(), hasSize(0));
    assertThat("subgroup's client roles is null", subGroup.getClientRoles(), aMapWithSize(0));
    assertThat("subgroup's subgroups is null", subGroup.getSubGroups(), hasSize(0));
}
 
Example #21
Source File: ImportGroupsIT.java    From keycloak-config-cli with Apache License 2.0 5 votes vote down vote up
@Test
@Order(26)
void shouldUpdateRealmUpdateGroupAddClientRolesFromSecondClient() {
    doImport("26_update_realm_update_group_delete_add_client_roles_from_second_client.json");

    RealmRepresentation createdRealm = keycloakProvider.get().realm(REALM_NAME).toRepresentation();
    assertThat(createdRealm.getRealm(), is(REALM_NAME));
    assertThat(createdRealm.isEnabled(), is(true));

    GroupRepresentation updatedGroup = loadGroup("/My Group");
    assertThat("name not equal", updatedGroup.getName(), is("My Group"));
    assertThat("path not equal", updatedGroup.getPath(), is("/My Group"));

    assertThat("attributes roles is null", updatedGroup.getAttributes(), aMapWithSize(1));
    assertThat("attributes roles is null", updatedGroup.getAttributes(),
            hasEntry(is("my changed attribute"), containsInAnyOrder("my changed attribute value")));
    assertThat("realm roles is null", updatedGroup.getRealmRoles(), hasSize(0));

    assertThat("client roles is null", updatedGroup.getClientRoles(), aMapWithSize(2));
    assertThat("client roles is null", updatedGroup.getClientRoles(), hasEntry(is("moped-client"), containsInAnyOrder("my_second_client_role")));
    assertThat("client roles is null", updatedGroup.getClientRoles(), hasEntry(is("second-moped-client"), containsInAnyOrder("my_client_role_of_second-moped-client", "my_second_client_role_of_second-moped-client")));

    List<GroupRepresentation> subGroups = updatedGroup.getSubGroups();
    assertThat("subgroups is empty", subGroups, hasSize(1));

    GroupRepresentation subGroup = subGroups.get(0);
    assertThat("subgroup is null", subGroup, notNullValue());
    assertThat("subgroup's name not equal", subGroup.getName(), is("My SubGroup"));
    assertThat("subgroup's path not equal", subGroup.getPath(), is("/My Group/My SubGroup"));
    assertThat("subgroup's attributes is null", subGroup.getAttributes(), aMapWithSize(0));
    assertThat("subgroup's realm roles is null", subGroup.getRealmRoles(), hasSize(0));
    assertThat("subgroup's client roles is null", subGroup.getClientRoles(), aMapWithSize(0));
    assertThat("subgroup's subgroups is null", subGroup.getSubGroups(), hasSize(0));
}
 
Example #22
Source File: UserTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
private GroupRepresentation createGroup(RealmResource realm, GroupRepresentation group) {
    Response response = realm.groups().add(group);
    String groupId = ApiUtil.getCreatedId(response);
    getCleanup().addGroupId(groupId);
    response.close();

    assertAdminEvents.assertEvent(realmId, OperationType.CREATE, AdminEventPaths.groupPath(groupId), group, ResourceType.GROUP);

    // Set ID to the original rep
    group.setId(groupId);
    return group;
}
 
Example #23
Source File: ImportGroupsIT.java    From keycloak-config-cli with Apache License 2.0 5 votes vote down vote up
@Test
@Order(35)
void shouldUpdateRealmUpdateGroupRemoveRealmRoleFromSubGroup() {
    doImport("35_update_realm_update_group_remove_role_from_subgroup.json");

    RealmRepresentation createdRealm = keycloakProvider.get().realm(REALM_NAME).toRepresentation();
    assertThat(createdRealm.getRealm(), is(REALM_NAME));
    assertThat(createdRealm.isEnabled(), is(true));

    GroupRepresentation updatedGroup = loadGroup("/My Group");
    assertThat("name not equal", updatedGroup.getName(), is("My Group"));
    assertThat("path not equal", updatedGroup.getPath(), is("/My Group"));

    assertThat("attributes roles is null", updatedGroup.getAttributes(), aMapWithSize(1));
    assertThat("attributes roles is null", updatedGroup.getAttributes(),
            hasEntry(is("my changed attribute"), containsInAnyOrder("my changed attribute value")));
    assertThat("realm roles is null", updatedGroup.getRealmRoles(), hasSize(0));
    assertThat("client roles is null", updatedGroup.getClientRoles(), aMapWithSize(1));
    assertThat("client roles is null", updatedGroup.getClientRoles(), hasEntry(is("second-moped-client"), containsInAnyOrder("my_client_role_of_second-moped-client", "my_second_client_role_of_second-moped-client")));

    List<GroupRepresentation> subGroups = updatedGroup.getSubGroups();
    assertThat("subgroups is empty", subGroups, hasSize(1));

    GroupRepresentation subGroup = subGroups.get(0);
    assertThat("subgroup is null", subGroup, notNullValue());
    assertThat("subgroup's name not equal", subGroup.getName(), is("My SubGroup"));
    assertThat("subgroup's path not equal", subGroup.getPath(), is("/My Group/My SubGroup"));
    assertThat("subgroup's attributes is null", subGroup.getAttributes(), aMapWithSize(1));
    assertThat("subgroup's attributes is null", subGroup.getAttributes(), hasEntry(is("my second subgroup attribute"), containsInAnyOrder("my second subgroup attribute value", "my second subgroup attribute second value")));
    assertThat("subgroup's realm roles is null", subGroup.getRealmRoles(), contains("my_second_realm_role"));
    assertThat("subgroup's client roles is null", subGroup.getClientRoles(), aMapWithSize(0));
    assertThat("subgroup's subgroups is null", subGroup.getSubGroups(), hasSize(0));
}
 
Example #24
Source File: ImportGroupsIT.java    From keycloak-config-cli with Apache License 2.0 5 votes vote down vote up
@Test
@Order(19)
void shouldUpdateRealmUpdateGroupDeleteAttribute() {
    doImport("19_update_realm_update_group_delete_attribute.json");

    RealmRepresentation createdRealm = keycloakProvider.get().realm(REALM_NAME).toRepresentation();
    assertThat(createdRealm.getRealm(), is(REALM_NAME));
    assertThat(createdRealm.isEnabled(), is(true));

    GroupRepresentation updatedGroup = loadGroup("/My Group");
    assertThat("name not equal", updatedGroup.getName(), is("My Group"));
    assertThat("path not equal", updatedGroup.getPath(), is("/My Group"));

    assertThat("attributes roles is null", updatedGroup.getAttributes(), aMapWithSize(1));
    assertThat("attributes roles is null", updatedGroup.getAttributes(), hasEntry(is("my changed attribute"), containsInAnyOrder("my changed attribute value", "my second added attribute second value")));

    assertThat("realm roles is null", updatedGroup.getRealmRoles(), contains("my_realm_role"));
    assertThat("client roles is null", updatedGroup.getClientRoles(), aMapWithSize(1));
    assertThat("client roles is null", updatedGroup.getClientRoles(), hasEntry(is("moped-client"), containsInAnyOrder("my_client_role")));

    List<GroupRepresentation> subGroups = updatedGroup.getSubGroups();
    assertThat("subgroups is empty", subGroups, hasSize(1));

    GroupRepresentation subGroup = subGroups.get(0);
    assertThat("subgroup is null", subGroup, notNullValue());
    assertThat("subgroup's name not equal", subGroup.getName(), is("My SubGroup"));
    assertThat("subgroup's path not equal", subGroup.getPath(), is("/My Group/My SubGroup"));
    assertThat("subgroup's attributes is null", subGroup.getAttributes(), aMapWithSize(0));
    assertThat("subgroup's realm roles is null", subGroup.getRealmRoles(), hasSize(0));
    assertThat("subgroup's client roles is null", subGroup.getClientRoles(), aMapWithSize(0));
    assertThat("subgroup's subgroups is null", subGroup.getSubGroups(), hasSize(0));
}
 
Example #25
Source File: ImportGroupsIT.java    From keycloak-config-cli with Apache License 2.0 5 votes vote down vote up
@Test
@Order(30)
void shouldUpdateRealmUpdateGroupAddSecondAttributeToSubGroup() {
    doImport("30_update_realm_update_group_add_second_attribute_to_subgroup.json");

    RealmRepresentation createdRealm = keycloakProvider.get().realm(REALM_NAME).toRepresentation();
    assertThat(createdRealm.getRealm(), is(REALM_NAME));
    assertThat(createdRealm.isEnabled(), is(true));

    GroupRepresentation updatedGroup = loadGroup("/My Group");
    assertThat("name not equal", updatedGroup.getName(), is("My Group"));
    assertThat("path not equal", updatedGroup.getPath(), is("/My Group"));

    assertThat("attributes roles is null", updatedGroup.getAttributes(), aMapWithSize(1));
    assertThat("attributes roles is null", updatedGroup.getAttributes(),
            hasEntry(is("my changed attribute"), containsInAnyOrder("my changed attribute value")));
    assertThat("realm roles is null", updatedGroup.getRealmRoles(), hasSize(0));
    assertThat("client roles is null", updatedGroup.getClientRoles(), aMapWithSize(1));
    assertThat("client roles is null", updatedGroup.getClientRoles(), hasEntry(is("second-moped-client"), containsInAnyOrder("my_client_role_of_second-moped-client", "my_second_client_role_of_second-moped-client")));

    List<GroupRepresentation> subGroups = updatedGroup.getSubGroups();
    assertThat("subgroups is empty", subGroups, hasSize(1));

    GroupRepresentation subGroup = subGroups.get(0);
    assertThat("subgroup is null", subGroup, notNullValue());
    assertThat("subgroup's name not equal", subGroup.getName(), is("My SubGroup"));
    assertThat("subgroup's path not equal", subGroup.getPath(), is("/My Group/My SubGroup"));

    assertThat("subgroup's attributes is null", subGroup.getAttributes(), aMapWithSize(2));
    assertThat("subgroup's attributes is null", subGroup.getAttributes(), hasEntry(is("my subgroup attribute"), containsInAnyOrder("my subgroup attribute value", "my subgroup attribute second value")));
    assertThat("subgroup's attributes is null", subGroup.getAttributes(), hasEntry(is("my second subgroup attribute"), containsInAnyOrder("my second subgroup attribute value", "my second subgroup attribute second value")));

    assertThat("subgroup's realm roles is null", subGroup.getRealmRoles(), hasSize(0));
    assertThat("subgroup's client roles is null", subGroup.getClientRoles(), aMapWithSize(0));
    assertThat("subgroup's subgroups is null", subGroup.getSubGroups(), hasSize(0));
}
 
Example #26
Source File: ImportGroupsIT.java    From keycloak-config-cli with Apache License 2.0 5 votes vote down vote up
@Test
@Order(12)
void shouldUpdateRealmUpdateGroupAddSubgroup() {
    doImport("12_update_realm_update_group_add_subgroup.json");

    RealmRepresentation createdRealm = keycloakProvider.get().realm(REALM_NAME).toRepresentation();
    assertThat(createdRealm.getRealm(), is(REALM_NAME));
    assertThat(createdRealm.isEnabled(), is(true));

    GroupRepresentation updatedGroup = loadGroup("/My Group");
    assertThat("name not equal", updatedGroup.getName(), is("My Group"));
    assertThat("path not equal", updatedGroup.getPath(), is("/My Group"));
    assertThat("attributes is null", updatedGroup.getAttributes(), aMapWithSize(1));
    assertThat("attributes is null", updatedGroup.getAttributes(), hasEntry(is("my added attribute"), containsInAnyOrder("my added attribute value")));
    assertThat("realm roles is null", updatedGroup.getRealmRoles(), contains("my_realm_role"));
    assertThat("client roles is null", updatedGroup.getClientRoles(), aMapWithSize(1));
    assertThat("client roles is null", updatedGroup.getClientRoles(), hasEntry(is("moped-client"), containsInAnyOrder("my_client_role")));

    List<GroupRepresentation> subGroups = updatedGroup.getSubGroups();
    assertThat("subgroups is empty", subGroups, hasSize(1));

    GroupRepresentation subGroup = subGroups.get(0);
    assertThat("subgroup is null", subGroup, notNullValue());
    assertThat("subgroup's name not equal", subGroup.getName(), is("My SubGroup"));
    assertThat("subgroup's path not equal", subGroup.getPath(), is("/My Group/My SubGroup"));
    assertThat("subgroup's attributes is null", subGroup.getAttributes(), aMapWithSize(0));
    assertThat("subgroup's realm roles is null", subGroup.getRealmRoles(), hasSize(0));
    assertThat("subgroup's client roles is null", subGroup.getClientRoles(), aMapWithSize(0));
    assertThat("subgroup's subgroups is null", subGroup.getSubGroups(), hasSize(0));
}
 
Example #27
Source File: ApiUtil.java    From keycloak with Apache License 2.0 5 votes vote down vote up
public static boolean groupContainsSubgroup(GroupRepresentation group, GroupRepresentation subgroup) {
    boolean contains = false;
    for (GroupRepresentation sg : group.getSubGroups()) {
        if (subgroup.getId().equals(sg.getId())) {
            contains = true;
            break;
        }
    }
    return contains;
}
 
Example #28
Source File: GroupsResource.java    From keycloak with Apache License 2.0 5 votes vote down vote up
/**
 * Get group hierarchy.  Only name and ids are returned.
 *
 * @return
 */
@GET
@NoCache
@Produces(MediaType.APPLICATION_JSON)
public List<GroupRepresentation> getGroups(@QueryParam("search") String search,
                                           @QueryParam("first") Integer firstResult,
                                           @QueryParam("max") Integer maxResults,
                                           @QueryParam("briefRepresentation") @DefaultValue("true") boolean briefRepresentation) {
    auth.groups().requireList();

    List<GroupRepresentation> results;

    if (Objects.nonNull(search)) {
        results = ModelToRepresentation.searchForGroupByName(realm, !briefRepresentation, search.trim(), firstResult, maxResults);
    } else if(Objects.nonNull(firstResult) && Objects.nonNull(maxResults)) {
        results = ModelToRepresentation.toGroupHierarchy(realm, !briefRepresentation, firstResult, maxResults);
    } else {
        results = ModelToRepresentation.toGroupHierarchy(realm, !briefRepresentation);
    }

    return results;
}
 
Example #29
Source File: ImportGroupsIT.java    From keycloak-config-cli with Apache License 2.0 5 votes vote down vote up
@Test
@Order(1)
void shouldUpdateRealmAddGroup() {
    doImport("01_update_realm_add_group.json");

    RealmRepresentation createdRealm = keycloakProvider.get().realm(REALM_NAME).toRepresentation();
    assertThat(createdRealm.getRealm(), is(REALM_NAME));
    assertThat(createdRealm.isEnabled(), is(true));

    GroupRepresentation existingGroup = loadGroup("/My Group");

    assertThat("name not equal", existingGroup.getName(), is("My Group"));
    assertThat("path not equal", existingGroup.getPath(), is("/My Group"));
    assertThat("attributes is null", existingGroup.getAttributes(), aMapWithSize(0));
    assertThat("realm roles is null", existingGroup.getRealmRoles(), hasSize(0));
    assertThat("client roles is null", existingGroup.getClientRoles(), aMapWithSize(0));
    assertThat("subgroups is null", existingGroup.getSubGroups(), hasSize(0));

    GroupRepresentation addedGroup = loadGroup("/My Added Group");

    assertThat("name not equal", addedGroup.getName(), is("My Added Group"));
    assertThat("path not equal", addedGroup.getPath(), is("/My Added Group"));
    assertThat("attributes is null", addedGroup.getAttributes(), aMapWithSize(0));
    assertThat("realm roles is null", addedGroup.getRealmRoles(), hasSize(0));
    assertThat("client roles is null", addedGroup.getClientRoles(), aMapWithSize(0));
    assertThat("subgroups is null", addedGroup.getSubGroups(), hasSize(0));
}
 
Example #30
Source File: GroupInvalidationClusterTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
protected GroupRepresentation createEntity(GroupRepresentation group, ContainerInfo node) {
    Response response = groups(node).add(group);
    String id = ApiUtil.getCreatedId(response);
    response.close();
    group.setId(id);
    return readEntity(group, node);
}