Search in sources :

Example 1 with GroupResource

use of org.keycloak.admin.client.resource.GroupResource in project keycloak by keycloak.

the class ManagementPermissionsTest method updateGroupPermissions.

@Test
public void updateGroupPermissions() {
    RealmResource realmResource = adminClient.realms().realm("test");
    GroupRepresentation group = new GroupRepresentation();
    group.setName("perm-group-test");
    Response response = realmResource.groups().add(group);
    String id = ApiUtil.getCreatedId(response);
    GroupResource groupResource = realmResource.groups().group(id);
    ManagementPermissionReference result = groupResource.setPermissions(new ManagementPermissionRepresentation(true));
    assertNotNull(result);
    assertTrue(result.isEnabled());
    result = groupResource.getPermissions();
    assertNotNull(result);
    assertTrue(result.isEnabled());
    result = groupResource.setPermissions(new ManagementPermissionRepresentation(false));
    assertNotNull(result);
    assertFalse(result.isEnabled());
    result = groupResource.getPermissions();
    assertNotNull(result);
    assertFalse(result.isEnabled());
    result = groupResource.setPermissions(new ManagementPermissionRepresentation(true));
    assertNotNull(result);
    assertTrue(result.isEnabled());
    result = groupResource.getPermissions();
    assertNotNull(result);
    assertTrue(result.isEnabled());
    result = groupResource.setPermissions(new ManagementPermissionRepresentation(true));
    assertNotNull(result);
    assertTrue(result.isEnabled());
    result = groupResource.getPermissions();
    assertNotNull(result);
    assertTrue(result.isEnabled());
    result = groupResource.setPermissions(new ManagementPermissionRepresentation(false));
    assertNotNull(result);
    assertFalse(result.isEnabled());
    result = groupResource.getPermissions();
    assertNotNull(result);
    assertFalse(result.isEnabled());
    result = groupResource.setPermissions(new ManagementPermissionRepresentation(false));
    assertNotNull(result);
    assertFalse(result.isEnabled());
    result = groupResource.getPermissions();
    assertNotNull(result);
    assertFalse(result.isEnabled());
}
Also used : Response(javax.ws.rs.core.Response) RealmResource(org.keycloak.admin.client.resource.RealmResource) GroupResource(org.keycloak.admin.client.resource.GroupResource) Test(org.junit.Test) AbstractTestRealmKeycloakTest(org.keycloak.testsuite.AbstractTestRealmKeycloakTest)

Example 2 with GroupResource

use of org.keycloak.admin.client.resource.GroupResource in project keycloak by keycloak.

the class Creator method create.

public static Creator<GroupResource> create(RealmResource realmResource, GroupRepresentation rep) {
    final GroupsResource groups = realmResource.groups();
    try (Response response = groups.add(rep)) {
        String createdId = getCreatedId(response);
        final GroupResource r = groups.group(createdId);
        LOG.debugf("Created group ID %s", createdId);
        return new Creator(createdId, r, r::remove);
    }
}
Also used : Response(javax.ws.rs.core.Response) Logger(org.jboss.logging.Logger) GroupResource(org.keycloak.admin.client.resource.GroupResource) GroupsResource(org.keycloak.admin.client.resource.GroupsResource)

Example 3 with GroupResource

use of org.keycloak.admin.client.resource.GroupResource in project keycloak by keycloak.

the class GroupTest method searchAndCountGroups.

@Test
public void searchAndCountGroups() throws Exception {
    String firstGroupId = "";
    RealmResource realm = adminClient.realms().realm("test");
    // Clean up all test groups
    for (GroupRepresentation group : realm.groups().groups()) {
        GroupResource resource = realm.groups().group(group.getId());
        resource.remove();
        assertAdminEvents.assertEvent("test", OperationType.DELETE, AdminEventPaths.groupPath(group.getId()), ResourceType.GROUP);
    }
    // Add 20 new groups with known names
    for (int i = 0; i < 20; i++) {
        GroupRepresentation group = new GroupRepresentation();
        group.setName("group" + i);
        group = createGroup(realm, group);
        if (i == 0) {
            firstGroupId = group.getId();
        }
    }
    // Get groups by search and pagination
    List<GroupRepresentation> allGroups = realm.groups().groups();
    assertEquals(20, allGroups.size());
    List<GroupRepresentation> slice = realm.groups().groups(5, 7);
    assertEquals(7, slice.size());
    List<GroupRepresentation> search = realm.groups().groups("group1", 0, 20);
    assertEquals(11, search.size());
    for (GroupRepresentation group : search) {
        assertTrue(group.getName().contains("group1"));
    }
    List<GroupRepresentation> noResultSearch = realm.groups().groups("abcd", 0, 20);
    assertEquals(0, noResultSearch.size());
    // Count
    assertEquals(new Long(allGroups.size()), realm.groups().count().get("count"));
    assertEquals(new Long(search.size()), realm.groups().count("group1").get("count"));
    assertEquals(new Long(noResultSearch.size()), realm.groups().count("abcd").get("count"));
    // Add a subgroup for onlyTopLevel flag testing
    GroupRepresentation level2Group = new GroupRepresentation();
    level2Group.setName("group1111");
    Response response = realm.groups().group(firstGroupId).subGroup(level2Group);
    response.close();
    assertAdminEvents.assertEvent("test", OperationType.CREATE, AdminEventPaths.groupSubgroupsPath(firstGroupId), level2Group, ResourceType.GROUP);
    assertEquals(new Long(allGroups.size()), realm.groups().count(true).get("count"));
    assertEquals(new Long(allGroups.size() + 1), realm.groups().count(false).get("count"));
    // add another subgroup
    GroupRepresentation level2Group2 = new GroupRepresentation();
    level2Group2.setName("group111111");
    realm.groups().group(firstGroupId).subGroup(level2Group2);
    // search and count for group with string group11 -> return 2 top level group, group11 and group0 having subgroups group1111 and group111111
    search = realm.groups().groups("group11", 0, 10);
    assertEquals(2, search.size());
    GroupRepresentation group0 = search.stream().filter(group -> "group0".equals(group.getName())).findAny().orElseGet(null);
    assertNotNull(group0);
    assertEquals(2, group0.getSubGroups().size());
    assertThat(group0.getSubGroups().stream().map(GroupRepresentation::getName).collect(Collectors.toList()), Matchers.containsInAnyOrder("group1111", "group111111"));
    assertEquals(new Long(search.size()), realm.groups().count("group11").get("count"));
}
Also used : Response(javax.ws.rs.core.Response) GroupRepresentation(org.keycloak.representations.idm.GroupRepresentation) RealmResource(org.keycloak.admin.client.resource.RealmResource) GroupResource(org.keycloak.admin.client.resource.GroupResource) Test(org.junit.Test)

Example 4 with GroupResource

use of org.keycloak.admin.client.resource.GroupResource in project keycloak by keycloak.

the class GroupTest method orderGroupsByName.

@Test
public void orderGroupsByName() throws Exception {
    RealmResource realm = this.adminClient.realms().realm("test");
    // Clean up all test groups
    for (GroupRepresentation group : realm.groups().groups()) {
        GroupResource resource = realm.groups().group(group.getId());
        resource.remove();
        assertAdminEvents.assertEvent("test", OperationType.DELETE, AdminEventPaths.groupPath(group.getId()), ResourceType.GROUP);
    }
    // Create two pages worth of groups in a random order
    List<GroupRepresentation> testGroups = new ArrayList<>();
    for (int i = 0; i < 40; i++) {
        GroupRepresentation group = new GroupRepresentation();
        group.setName("group" + i);
        testGroups.add(group);
    }
    Collections.shuffle(testGroups);
    for (GroupRepresentation group : testGroups) {
        group = createGroup(realm, group);
    }
    // Groups should be ordered by name
    Comparator<GroupRepresentation> compareByName = Comparator.comparing(GroupRepresentation::getName);
    // Assert that all groups are returned in order
    List<GroupRepresentation> allGroups = realm.groups().groups();
    assertEquals(40, allGroups.size());
    assertTrue(Comparators.isInStrictOrder(allGroups, compareByName));
    // Assert that pagination results are returned in order
    List<GroupRepresentation> firstPage = realm.groups().groups(0, 20);
    assertEquals(20, firstPage.size());
    assertTrue(Comparators.isInStrictOrder(firstPage, compareByName));
    List<GroupRepresentation> secondPage = realm.groups().groups(20, 20);
    assertEquals(20, secondPage.size());
    assertTrue(Comparators.isInStrictOrder(secondPage, compareByName));
    // Check that the ordering of groups across multiple pages is correct
    // Since the individual pages are ordered it is sufficient to compare
    // every group from the first page to the first group of the second page
    GroupRepresentation firstGroupOnSecondPage = secondPage.get(0);
    for (GroupRepresentation firstPageGroup : firstPage) {
        int comparisonResult = compareByName.compare(firstPageGroup, firstGroupOnSecondPage);
        assertTrue(comparisonResult < 0);
    }
}
Also used : GroupRepresentation(org.keycloak.representations.idm.GroupRepresentation) RealmResource(org.keycloak.admin.client.resource.RealmResource) ArrayList(java.util.ArrayList) GroupResource(org.keycloak.admin.client.resource.GroupResource) Test(org.junit.Test)

Example 5 with GroupResource

use of org.keycloak.admin.client.resource.GroupResource in project keycloak by keycloak.

the class UserTest method testGetGroupsForUserFullRepresentation.

@Test
public void testGetGroupsForUserFullRepresentation() {
    RealmResource realm = adminClient.realms().realm("test");
    String userName = "averagejoe";
    String groupName = "groupWithAttribute";
    Map<String, List<String>> attributes = new HashMap<String, List<String>>();
    attributes.put("attribute1", Arrays.asList("attribute1", "attribute2"));
    UserRepresentation userRepresentation = UserBuilder.edit(createUserRepresentation(userName, "joe@average.com", "average", "joe", true)).addPassword("password").build();
    try (Creator<UserResource> u = Creator.create(realm, userRepresentation);
        Creator<GroupResource> g = Creator.create(realm, GroupBuilder.create().name(groupName).attributes(attributes).build())) {
        String groupId = g.id();
        UserResource user = u.resource();
        user.joinGroup(groupId);
        List<GroupRepresentation> userGroups = user.groups(0, 100, false);
        assertFalse(userGroups.isEmpty());
        assertTrue(userGroups.get(0).getAttributes().containsKey("attribute1"));
    }
}
Also used : GroupRepresentation(org.keycloak.representations.idm.GroupRepresentation) HashMap(java.util.HashMap) RealmResource(org.keycloak.admin.client.resource.RealmResource) UserResource(org.keycloak.admin.client.resource.UserResource) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) GroupResource(org.keycloak.admin.client.resource.GroupResource) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) Test(org.junit.Test)

Aggregations

GroupResource (org.keycloak.admin.client.resource.GroupResource)7 Test (org.junit.Test)6 Response (javax.ws.rs.core.Response)5 RealmResource (org.keycloak.admin.client.resource.RealmResource)5 GroupsResource (org.keycloak.admin.client.resource.GroupsResource)3 GroupRepresentation (org.keycloak.representations.idm.GroupRepresentation)3 ArrayList (java.util.ArrayList)2 UserResource (org.keycloak.admin.client.resource.UserResource)2 UsersResource (org.keycloak.admin.client.resource.UsersResource)2 UserRepresentation (org.keycloak.representations.idm.UserRepresentation)2 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Logger (org.jboss.logging.Logger)1 AbstractTestRealmKeycloakTest (org.keycloak.testsuite.AbstractTestRealmKeycloakTest)1