Search in sources :

Example 16 with UserPolicyRepresentation

use of org.keycloak.representations.idm.authorization.UserPolicyRepresentation in project keycloak by keycloak.

the class UserPolicyManagementTest method testGenericConfig.

@Test
public void testGenericConfig() {
    AuthorizationResource authorization = getClient().authorization();
    UserPolicyRepresentation representation = new UserPolicyRepresentation();
    representation.setName("Test Generic Config Permission");
    representation.addUser("User A");
    UserPoliciesResource policies = authorization.policies().user();
    try (Response response = policies.create(representation)) {
        UserPolicyRepresentation created = response.readEntity(UserPolicyRepresentation.class);
        PolicyResource policy = authorization.policies().policy(created.getId());
        PolicyRepresentation genericConfig = policy.toRepresentation();
        assertNotNull(genericConfig.getConfig());
        assertNotNull(genericConfig.getConfig().get("users"));
        UserRepresentation user = getRealm().users().search("User A").get(0);
        assertTrue(genericConfig.getConfig().get("users").contains(user.getId()));
    }
}
Also used : Response(javax.ws.rs.core.Response) PolicyRepresentation(org.keycloak.representations.idm.authorization.PolicyRepresentation) UserPolicyRepresentation(org.keycloak.representations.idm.authorization.UserPolicyRepresentation) PolicyResource(org.keycloak.admin.client.resource.PolicyResource) UserPolicyResource(org.keycloak.admin.client.resource.UserPolicyResource) UserPolicyRepresentation(org.keycloak.representations.idm.authorization.UserPolicyRepresentation) UserPoliciesResource(org.keycloak.admin.client.resource.UserPoliciesResource) AuthorizationResource(org.keycloak.admin.client.resource.AuthorizationResource) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) Test(org.junit.Test)

Example 17 with UserPolicyRepresentation

use of org.keycloak.representations.idm.authorization.UserPolicyRepresentation in project keycloak by keycloak.

the class UserPolicyManagementTest method testDelete.

@Test
public void testDelete() {
    AuthorizationResource authorization = getClient().authorization();
    UserPolicyRepresentation representation = new UserPolicyRepresentation();
    representation.setName("Test Delete Permission");
    representation.addUser("User A");
    UserPoliciesResource policies = authorization.policies().user();
    try (Response response = policies.create(representation)) {
        UserPolicyRepresentation created = response.readEntity(UserPolicyRepresentation.class);
        policies.findById(created.getId()).remove();
        UserPolicyResource removed = policies.findById(created.getId());
        try {
            removed.toRepresentation();
            fail("Permission not removed");
        } catch (NotFoundException ignore) {
        }
    }
}
Also used : Response(javax.ws.rs.core.Response) UserPolicyRepresentation(org.keycloak.representations.idm.authorization.UserPolicyRepresentation) UserPoliciesResource(org.keycloak.admin.client.resource.UserPoliciesResource) UserPolicyResource(org.keycloak.admin.client.resource.UserPolicyResource) NotFoundException(javax.ws.rs.NotFoundException) AuthorizationResource(org.keycloak.admin.client.resource.AuthorizationResource) Test(org.junit.Test)

Example 18 with UserPolicyRepresentation

use of org.keycloak.representations.idm.authorization.UserPolicyRepresentation in project keycloak by keycloak.

the class UserPolicyManagementTest method testDeleteUser.

@Test
public void testDeleteUser() {
    AuthorizationResource authorization = getClient().authorization();
    UserPolicyRepresentation representation = new UserPolicyRepresentation();
    representation.setName("Realm User Policy");
    representation.setDescription("description");
    representation.setDecisionStrategy(DecisionStrategy.CONSENSUS);
    representation.setLogic(Logic.NEGATIVE);
    representation.addUser("User D");
    representation.addUser("User E");
    representation.addUser("User F");
    assertCreated(authorization, representation);
    UsersResource users = getRealm().users();
    UserRepresentation user = users.search("User D").get(0);
    users.get(user.getId()).remove();
    representation = authorization.policies().user().findById(representation.getId()).toRepresentation();
    Assert.assertEquals(2, representation.getUsers().size());
    Assert.assertFalse(representation.getUsers().contains(user.getId()));
    user = users.search("User E").get(0);
    users.get(user.getId()).remove();
    representation = authorization.policies().user().findById(representation.getId()).toRepresentation();
    Assert.assertEquals(1, representation.getUsers().size());
    Assert.assertFalse(representation.getUsers().contains(user.getId()));
    user = users.search("User F").get(0);
    users.get(user.getId()).remove();
    try {
        authorization.policies().user().findById(representation.getId()).toRepresentation();
        fail("User policy should be removed");
    } catch (NotFoundException nfe) {
    // ignore
    }
}
Also used : UserPolicyRepresentation(org.keycloak.representations.idm.authorization.UserPolicyRepresentation) UsersResource(org.keycloak.admin.client.resource.UsersResource) NotFoundException(javax.ws.rs.NotFoundException) AuthorizationResource(org.keycloak.admin.client.resource.AuthorizationResource) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) Test(org.junit.Test)

Example 19 with UserPolicyRepresentation

use of org.keycloak.representations.idm.authorization.UserPolicyRepresentation in project keycloak by keycloak.

the class UserPolicyManagementTest method assertCreated.

private void assertCreated(AuthorizationResource authorization, UserPolicyRepresentation representation) {
    UserPoliciesResource permissions = authorization.policies().user();
    try (Response response = permissions.create(representation)) {
        UserPolicyRepresentation created = response.readEntity(UserPolicyRepresentation.class);
        UserPolicyResource permission = permissions.findById(created.getId());
        assertRepresentation(representation, permission);
    }
}
Also used : Response(javax.ws.rs.core.Response) UserPolicyRepresentation(org.keycloak.representations.idm.authorization.UserPolicyRepresentation) UserPoliciesResource(org.keycloak.admin.client.resource.UserPoliciesResource) UserPolicyResource(org.keycloak.admin.client.resource.UserPolicyResource)

Example 20 with UserPolicyRepresentation

use of org.keycloak.representations.idm.authorization.UserPolicyRepresentation in project keycloak by keycloak.

the class UserPolicyManagementTest method testUpdate.

@Test
public void testUpdate() {
    AuthorizationResource authorization = getClient().authorization();
    UserPolicyRepresentation representation = new UserPolicyRepresentation();
    representation.setName("Update Test User Policy");
    representation.setDescription("description");
    representation.setDecisionStrategy(DecisionStrategy.CONSENSUS);
    representation.setLogic(Logic.NEGATIVE);
    representation.addUser("User A");
    representation.addUser("User B");
    representation.addUser("User C");
    assertCreated(authorization, representation);
    representation.setName("changed");
    representation.setDescription("changed");
    representation.setDecisionStrategy(DecisionStrategy.AFFIRMATIVE);
    representation.setLogic(Logic.POSITIVE);
    representation.setUsers(representation.getUsers().stream().filter(userName -> !userName.equals("User A")).collect(Collectors.toSet()));
    UserPoliciesResource policies = authorization.policies().user();
    UserPolicyResource permission = policies.findById(representation.getId());
    permission.update(representation);
    assertRepresentation(representation, permission);
    representation.setUsers(representation.getUsers().stream().filter(userName -> !userName.equals("User C")).collect(Collectors.toSet()));
    permission.update(representation);
    assertRepresentation(representation, permission);
}
Also used : UserPolicyRepresentation(org.keycloak.representations.idm.authorization.UserPolicyRepresentation) UserPoliciesResource(org.keycloak.admin.client.resource.UserPoliciesResource) UserPolicyResource(org.keycloak.admin.client.resource.UserPolicyResource) AuthorizationResource(org.keycloak.admin.client.resource.AuthorizationResource) Test(org.junit.Test)

Aggregations

UserPolicyRepresentation (org.keycloak.representations.idm.authorization.UserPolicyRepresentation)33 Test (org.junit.Test)15 AuthorizationResource (org.keycloak.admin.client.resource.AuthorizationResource)11 Policy (org.keycloak.authorization.model.Policy)10 Response (javax.ws.rs.core.Response)7 RolePolicyRepresentation (org.keycloak.representations.idm.authorization.RolePolicyRepresentation)7 RealmModel (org.keycloak.models.RealmModel)6 UserModel (org.keycloak.models.UserModel)6 JSPolicyRepresentation (org.keycloak.representations.idm.authorization.JSPolicyRepresentation)6 Before (org.junit.Before)4 Keycloak (org.keycloak.admin.client.Keycloak)4 UserPoliciesResource (org.keycloak.admin.client.resource.UserPoliciesResource)4 UserPolicyResource (org.keycloak.admin.client.resource.UserPolicyResource)4 ClientModel (org.keycloak.models.ClientModel)4 RoleRepresentation (org.keycloak.representations.idm.RoleRepresentation)4 ClientPolicyRepresentation (org.keycloak.representations.idm.authorization.ClientPolicyRepresentation)4 GroupPolicyRepresentation (org.keycloak.representations.idm.authorization.GroupPolicyRepresentation)4 PolicyRepresentation (org.keycloak.representations.idm.authorization.PolicyRepresentation)4 ResourceRepresentation (org.keycloak.representations.idm.authorization.ResourceRepresentation)4 AdminPermissionManagement (org.keycloak.services.resources.admin.permissions.AdminPermissionManagement)4