Search in sources :

Example 1 with RolePolicyResource

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

the class RolePolicyManagementTest method testUpdate.

@Test
public void testUpdate() {
    AuthorizationResource authorization = getClient().authorization();
    RolePolicyRepresentation representation = new RolePolicyRepresentation();
    representation.setName("Update Test Role Policy");
    representation.setDescription("description");
    representation.setDecisionStrategy(DecisionStrategy.CONSENSUS);
    representation.setLogic(Logic.NEGATIVE);
    representation.addRole("Role A", false);
    representation.addRole("Role B", true);
    representation.addRole("Role C", false);
    assertCreated(authorization, representation);
    representation.setName("changed");
    representation.setDescription("changed");
    representation.setDecisionStrategy(DecisionStrategy.AFFIRMATIVE);
    representation.setLogic(Logic.POSITIVE);
    representation.setRoles(representation.getRoles().stream().filter(roleDefinition -> !roleDefinition.getId().equals("Resource A")).collect(Collectors.toSet()));
    RolePoliciesResource policies = authorization.policies().role();
    RolePolicyResource permission = policies.findById(representation.getId());
    permission.update(representation);
    assertRepresentation(representation, permission);
    for (RolePolicyRepresentation.RoleDefinition roleDefinition : representation.getRoles()) {
        if (roleDefinition.getId().equals("Role B")) {
            roleDefinition.setRequired(false);
        }
        if (roleDefinition.getId().equals("Role C")) {
            roleDefinition.setRequired(true);
        }
    }
    permission.update(representation);
    assertRepresentation(representation, permission);
}
Also used : RolePolicyRepresentation(org.keycloak.representations.idm.authorization.RolePolicyRepresentation) RolePoliciesResource(org.keycloak.admin.client.resource.RolePoliciesResource) RolePolicyResource(org.keycloak.admin.client.resource.RolePolicyResource) AuthorizationResource(org.keycloak.admin.client.resource.AuthorizationResource) Test(org.junit.Test)

Example 2 with RolePolicyResource

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

the class RolePolicyManagementTest method assertCreated.

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

Example 3 with RolePolicyResource

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

the class RolePolicyManagementTest method testDelete.

@Test
public void testDelete() {
    AuthorizationResource authorization = getClient().authorization();
    RolePolicyRepresentation representation = new RolePolicyRepresentation();
    representation.setName("Test Delete Permission");
    representation.addRole("Role A", false);
    RolePoliciesResource policies = authorization.policies().role();
    try (Response response = policies.create(representation)) {
        RolePolicyRepresentation created = response.readEntity(RolePolicyRepresentation.class);
        policies.findById(created.getId()).remove();
        RolePolicyResource removed = policies.findById(created.getId());
        try {
            removed.toRepresentation();
            fail("Permission not removed");
        } catch (NotFoundException ignore) {
        }
    }
}
Also used : Response(javax.ws.rs.core.Response) RolePolicyRepresentation(org.keycloak.representations.idm.authorization.RolePolicyRepresentation) RolePoliciesResource(org.keycloak.admin.client.resource.RolePoliciesResource) RolePolicyResource(org.keycloak.admin.client.resource.RolePolicyResource) NotFoundException(javax.ws.rs.NotFoundException) AuthorizationResource(org.keycloak.admin.client.resource.AuthorizationResource) Test(org.junit.Test)

Aggregations

RolePoliciesResource (org.keycloak.admin.client.resource.RolePoliciesResource)3 RolePolicyResource (org.keycloak.admin.client.resource.RolePolicyResource)3 RolePolicyRepresentation (org.keycloak.representations.idm.authorization.RolePolicyRepresentation)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