Search in sources :

Example 1 with UserPolicyResource

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

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

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

UserPoliciesResource (org.keycloak.admin.client.resource.UserPoliciesResource)3 UserPolicyResource (org.keycloak.admin.client.resource.UserPolicyResource)3 UserPolicyRepresentation (org.keycloak.representations.idm.authorization.UserPolicyRepresentation)3 Response (javax.ws.rs.core.Response)2 Test (org.junit.Test)2 AuthorizationResource (org.keycloak.admin.client.resource.AuthorizationResource)2 NotFoundException (javax.ws.rs.NotFoundException)1