Search in sources :

Example 1 with GroupsResource

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

the class GroupPolicyManagementTest method testDeleteGroupAndPolicy.

@Test
public void testDeleteGroupAndPolicy() {
    AuthorizationResource authorization = getClient().authorization();
    GroupPolicyRepresentation representation = new GroupPolicyRepresentation();
    representation.setName(UUID.randomUUID().toString());
    representation.setDescription("description");
    representation.setDecisionStrategy(DecisionStrategy.CONSENSUS);
    representation.setLogic(Logic.NEGATIVE);
    representation.setGroupsClaim("groups");
    representation.addGroupPath("/Group G", true);
    assertCreated(authorization, representation);
    GroupsResource groups = getRealm().groups();
    GroupRepresentation group = groups.groups("Group G", null, null).get(0);
    groups.group(group.getId()).remove();
    try {
        getClient().authorization().policies().group().findByName(representation.getName());
    } catch (NotFoundException e) {
    }
    representation.getGroups().clear();
    representation.addGroupPath("/Group H/Group I/Group K");
    representation.addGroupPath("/Group F");
    assertCreated(authorization, representation);
    group = groups.groups("Group K", null, null).get(0);
    groups.group(group.getId()).remove();
    GroupPolicyRepresentation policy = getClient().authorization().policies().group().findByName(representation.getName());
    assertNotNull(policy);
    assertEquals(1, policy.getGroups().size());
}
Also used : GroupRepresentation(org.keycloak.representations.idm.GroupRepresentation) NotFoundException(javax.ws.rs.NotFoundException) GroupPolicyRepresentation(org.keycloak.representations.idm.authorization.GroupPolicyRepresentation) AuthorizationResource(org.keycloak.admin.client.resource.AuthorizationResource) GroupsResource(org.keycloak.admin.client.resource.GroupsResource) Test(org.junit.Test)

Example 2 with GroupsResource

use of org.keycloak.admin.client.resource.GroupsResource 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 GroupsResource

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

the class GroupTest method getGroupsWithBriefRepresentation.

@Test
public void getGroupsWithBriefRepresentation() {
    RealmResource realm = adminClient.realms().realm("test");
    GroupsResource groupsResource = adminClient.realms().realm("test").groups();
    GroupRepresentation group = new GroupRepresentation();
    group.setName("groupWithAttribute");
    Map<String, List<String>> attributes = new HashMap<String, List<String>>();
    attributes.put("attribute1", Arrays.asList("attribute1", "attribute2"));
    group.setAttributes(attributes);
    group = createGroup(realm, group);
    List<GroupRepresentation> groups = groupsResource.groups("groupWithAttribute", 0, 20);
    assertFalse(groups.isEmpty());
    assertNull(groups.get(0).getAttributes());
}
Also used : GroupRepresentation(org.keycloak.representations.idm.GroupRepresentation) HashMap(java.util.HashMap) RealmResource(org.keycloak.admin.client.resource.RealmResource) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) GroupsResource(org.keycloak.admin.client.resource.GroupsResource) Test(org.junit.Test)

Example 4 with GroupsResource

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

the class GroupTest method defaultMaxResults.

@Test
public void defaultMaxResults() {
    GroupsResource groups = adminClient.realms().realm("test").groups();
    try (Response response = groups.add(GroupBuilder.create().name("test").build())) {
        String groupId = ApiUtil.getCreatedId(response);
        GroupResource group = groups.group(groupId);
        UsersResource users = adminClient.realms().realm("test").users();
        for (int i = 0; i < 110; i++) {
            try (Response r = users.create(UserBuilder.create().username("test-" + i).build())) {
                users.get(ApiUtil.getCreatedId(r)).joinGroup(groupId);
            }
        }
        assertEquals(100, group.members(null, null).size());
        assertEquals(100, group.members().size());
        assertEquals(105, group.members(0, 105).size());
        assertEquals(110, group.members(0, 1000).size());
        assertEquals(110, group.members(-1, -2).size());
    }
}
Also used : Response(javax.ws.rs.core.Response) UsersResource(org.keycloak.admin.client.resource.UsersResource) GroupResource(org.keycloak.admin.client.resource.GroupResource) GroupsResource(org.keycloak.admin.client.resource.GroupsResource) Test(org.junit.Test)

Example 5 with GroupsResource

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

the class GroupTest method testBriefRepresentationOnGroupMembers.

@Test
public void testBriefRepresentationOnGroupMembers() {
    RealmResource realm = adminClient.realms().realm("test");
    String groupName = "brief-grouptest-group";
    String userName = "brief-grouptest-user";
    GroupsResource groups = realm.groups();
    try (Response response = groups.add(GroupBuilder.create().name(groupName).build())) {
        String groupId = ApiUtil.getCreatedId(response);
        GroupResource group = groups.group(groupId);
        UsersResource users = realm.users();
        UserRepresentation userRepresentation = UserBuilder.create().username(userName).addAttribute("myattribute", "myvalue").build();
        Response r = users.create(userRepresentation);
        UserResource user = users.get(ApiUtil.getCreatedId(r));
        user.joinGroup(groupId);
        UserRepresentation defaultRepresentation = group.members(null, null).get(0);
        UserRepresentation fullRepresentation = group.members(null, null, false).get(0);
        UserRepresentation briefRepresentation = group.members(null, null, true).get(0);
        assertEquals("full group member representation includes attributes", fullRepresentation.getAttributes(), userRepresentation.getAttributes());
        assertEquals("default group member representation is full", defaultRepresentation.getAttributes(), userRepresentation.getAttributes());
        assertNull("brief group member representation omits attributes", briefRepresentation.getAttributes());
        group.remove();
        user.remove();
    }
}
Also used : Response(javax.ws.rs.core.Response) RealmResource(org.keycloak.admin.client.resource.RealmResource) UsersResource(org.keycloak.admin.client.resource.UsersResource) UserResource(org.keycloak.admin.client.resource.UserResource) GroupResource(org.keycloak.admin.client.resource.GroupResource) GroupsResource(org.keycloak.admin.client.resource.GroupsResource) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) Test(org.junit.Test)

Aggregations

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