Search in sources :

Example 21 with UserPolicyRepresentation

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

the class UserPolicyManagementTest method assertRepresentation.

private void assertRepresentation(UserPolicyRepresentation representation, UserPolicyResource permission) {
    UserPolicyRepresentation actual = permission.toRepresentation();
    assertRepresentation(representation, actual, () -> permission.resources(), () -> Collections.emptyList(), () -> permission.associatedPolicies());
    assertEquals(representation.getUsers().size(), actual.getUsers().size());
    assertEquals(0, actual.getUsers().stream().filter(userId -> !representation.getUsers().stream().filter(userName -> getUserName(userId).equalsIgnoreCase(userName)).findFirst().isPresent()).count());
}
Also used : UserPolicyRepresentation(org.keycloak.representations.idm.authorization.UserPolicyRepresentation)

Example 22 with UserPolicyRepresentation

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

the class AggregatePolicyManagementTest method configureTest.

@Before
public void configureTest() {
    super.configureTest();
    RealmResource realmResource = testRealmResource();
    RolesResource realmRoles = realmResource.roles();
    realmRoles.create(new RoleRepresentation("Role A", "", false));
    realmRoles.create(new RoleRepresentation("Role B", "", false));
    UsersResource users = realmResource.users();
    users.create(UserBuilder.create().username("user a").build());
    ClientsResource clients = realmResource.clients();
    clients.create(ClientBuilder.create().clientId("client a").build());
    realmResource.groups().add(GroupBuilder.create().name("Group A").build());
    RolePolicyRepresentation policyA = new RolePolicyRepresentation();
    policyA.setName("Policy A");
    policyA.addRole("Role A");
    AuthorizationResource authorization = realmResource.clients().get(newClient.getId()).authorization();
    PoliciesResource policies = authorization.policies();
    RolePoliciesResource roles = policies.role();
    roles.create(policyA);
    RolePolicyRepresentation policyB = new RolePolicyRepresentation();
    policyB.setName("Policy B");
    policyB.addRole("Role B");
    roles.create(policyB);
    UserPolicyRepresentation policyC = new UserPolicyRepresentation();
    policyC.setName("Policy C");
    policyC.addUser("test");
    policies.user().create(policyC).close();
}
Also used : RoleRepresentation(org.keycloak.representations.idm.RoleRepresentation) RolePolicyRepresentation(org.keycloak.representations.idm.authorization.RolePolicyRepresentation) RealmResource(org.keycloak.admin.client.resource.RealmResource) UserPolicyRepresentation(org.keycloak.representations.idm.authorization.UserPolicyRepresentation) UsersResource(org.keycloak.admin.client.resource.UsersResource) ClientsResource(org.keycloak.admin.client.resource.ClientsResource) RolePoliciesResource(org.keycloak.admin.client.resource.RolePoliciesResource) PoliciesResource(org.keycloak.admin.client.resource.PoliciesResource) RolePoliciesResource(org.keycloak.admin.client.resource.RolePoliciesResource) RolesResource(org.keycloak.admin.client.resource.RolesResource) AuthorizationResource(org.keycloak.admin.client.resource.AuthorizationResource) Before(org.junit.Before)

Example 23 with UserPolicyRepresentation

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

the class ResourcePermissionManagementTest method configureTest.

@Before
public void configureTest() {
    super.configureTest();
    RolesResource realmRoles = testRealmResource().roles();
    realmRoles.create(new RoleRepresentation("Role A", "", false));
    realmRoles.create(new RoleRepresentation("Role B", "", false));
    RolePolicyRepresentation policyA = new RolePolicyRepresentation();
    policyA.setName("Policy A");
    policyA.addRole("Role A");
    AuthorizationResource authorization = testRealmResource().clients().get(newClient.getId()).authorization();
    PoliciesResource policies = authorization.policies();
    RolePoliciesResource roles = policies.role();
    roles.create(policyA);
    RolePolicyRepresentation policyB = new RolePolicyRepresentation();
    policyB.setName("Policy B");
    policyB.addRole("Role B");
    roles.create(policyB);
    UserPolicyRepresentation policyC = new UserPolicyRepresentation();
    policyC.setName("Policy C");
    policyC.addUser("test");
    policies.user().create(policyC).close();
    ResourcesResource resources = authorization.resources();
    resources.create(new ResourceRepresentation("Resource A"));
    resources.create(new ResourceRepresentation("Resource B"));
}
Also used : RoleRepresentation(org.keycloak.representations.idm.RoleRepresentation) RolePolicyRepresentation(org.keycloak.representations.idm.authorization.RolePolicyRepresentation) UserPolicyRepresentation(org.keycloak.representations.idm.authorization.UserPolicyRepresentation) RolePoliciesResource(org.keycloak.admin.client.resource.RolePoliciesResource) PoliciesResource(org.keycloak.admin.client.resource.PoliciesResource) RolePoliciesResource(org.keycloak.admin.client.resource.RolePoliciesResource) RolesResource(org.keycloak.admin.client.resource.RolesResource) AuthorizationResource(org.keycloak.admin.client.resource.AuthorizationResource) ResourcesResource(org.keycloak.admin.client.resource.ResourcesResource) ResourceRepresentation(org.keycloak.representations.idm.authorization.ResourceRepresentation) Before(org.junit.Before)

Example 24 with UserPolicyRepresentation

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

the class UserPolicyForm method toRepresentation.

public UserPolicyRepresentation toRepresentation() {
    UserPolicyRepresentation representation = new UserPolicyRepresentation();
    representation.setName(UIUtils.getTextInputValue(name));
    representation.setDescription(UIUtils.getTextInputValue(description));
    representation.setLogic(Logic.valueOf(UIUtils.getTextFromElement(logic.getFirstSelectedOption()).toUpperCase()));
    representation.setUsers(usersInput.getSelected());
    return representation;
}
Also used : UserPolicyRepresentation(org.keycloak.representations.idm.authorization.UserPolicyRepresentation)

Example 25 with UserPolicyRepresentation

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

the class AbstractPolicyManagementTest method createUserPolicy.

private void createUserPolicy(String name, RealmResource realm, ClientResource client, String username) throws IOException {
    String userId = realm.users().search(username).stream().map(representation -> representation.getId()).findFirst().orElseThrow(() -> new RuntimeException("Expected user [userId]"));
    UserPolicyRepresentation representation = new UserPolicyRepresentation();
    representation.setName(name);
    representation.addUser(userId);
    Response response = client.authorization().policies().user().create(representation);
    response.close();
}
Also used : Response(javax.ws.rs.core.Response) UserPolicyRepresentation(org.keycloak.representations.idm.authorization.UserPolicyRepresentation)

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