Search in sources :

Example 71 with Resource

use of org.keycloak.authorization.model.Resource in project keycloak by keycloak.

the class GroupPermissions method initialize.

private void initialize(GroupModel group) {
    root.initializeRealmResourceServer();
    root.initializeRealmDefaultScopes();
    ResourceServer server = root.realmResourceServer();
    Scope manageScope = root.realmManageScope();
    Scope viewScope = root.realmViewScope();
    Scope manageMembersScope = root.initializeRealmScope(MANAGE_MEMBERS_SCOPE);
    Scope viewMembersScope = root.initializeRealmScope(VIEW_MEMBERS_SCOPE);
    Scope manageMembershipScope = root.initializeRealmScope(MANAGE_MEMBERSHIP_SCOPE);
    String groupResourceName = getGroupResourceName(group);
    Resource groupResource = resourceStore.findByName(groupResourceName, server.getId());
    if (groupResource == null) {
        groupResource = resourceStore.create(groupResourceName, server, server.getId());
        Set<Scope> scopeset = new HashSet<>();
        scopeset.add(manageScope);
        scopeset.add(viewScope);
        scopeset.add(viewMembersScope);
        scopeset.add(manageMembershipScope);
        scopeset.add(manageMembersScope);
        groupResource.updateScopes(scopeset);
        groupResource.setType("Group");
    }
    String managePermissionName = getManagePermissionGroup(group);
    Policy managePermission = policyStore.findByName(managePermissionName, server.getId());
    if (managePermission == null) {
        Helper.addEmptyScopePermission(authz, server, managePermissionName, groupResource, manageScope);
    }
    String viewPermissionName = getViewPermissionGroup(group);
    Policy viewPermission = policyStore.findByName(viewPermissionName, server.getId());
    if (viewPermission == null) {
        Helper.addEmptyScopePermission(authz, server, viewPermissionName, groupResource, viewScope);
    }
    String manageMembersPermissionName = getManageMembersPermissionGroup(group);
    Policy manageMembersPermission = policyStore.findByName(manageMembersPermissionName, server.getId());
    if (manageMembersPermission == null) {
        Helper.addEmptyScopePermission(authz, server, manageMembersPermissionName, groupResource, manageMembersScope);
    }
    String viewMembersPermissionName = getViewMembersPermissionGroup(group);
    Policy viewMembersPermission = policyStore.findByName(viewMembersPermissionName, server.getId());
    if (viewMembersPermission == null) {
        Helper.addEmptyScopePermission(authz, server, viewMembersPermissionName, groupResource, viewMembersScope);
    }
    String manageMembershipPermissionName = getManageMembershipPermissionGroup(group);
    Policy manageMembershipPermission = policyStore.findByName(manageMembershipPermissionName, server.getId());
    if (manageMembershipPermission == null) {
        Helper.addEmptyScopePermission(authz, server, manageMembershipPermissionName, groupResource, manageMembershipScope);
    }
}
Also used : Policy(org.keycloak.authorization.model.Policy) Scope(org.keycloak.authorization.model.Scope) Resource(org.keycloak.authorization.model.Resource) ResourceServer(org.keycloak.authorization.model.ResourceServer) HashSet(java.util.HashSet)

Example 72 with Resource

use of org.keycloak.authorization.model.Resource in project keycloak by keycloak.

the class GroupPermissions method deletePermissions.

private void deletePermissions(GroupModel group) {
    ResourceServer server = root.realmResourceServer();
    if (server == null)
        return;
    Policy managePermission = managePermission(group);
    if (managePermission != null) {
        policyStore.delete(managePermission.getId());
    }
    Policy viewPermission = viewPermission(group);
    if (viewPermission != null) {
        policyStore.delete(viewPermission.getId());
    }
    Policy manageMembersPermission = manageMembersPermission(group);
    if (manageMembersPermission != null) {
        policyStore.delete(manageMembersPermission.getId());
    }
    Policy viewMembersPermission = viewMembersPermission(group);
    if (viewMembersPermission != null) {
        policyStore.delete(viewMembersPermission.getId());
    }
    Policy manageMembershipPermission = manageMembershipPermission(group);
    if (manageMembershipPermission != null) {
        policyStore.delete(manageMembershipPermission.getId());
    }
    Resource resource = groupResource(group);
    if (resource != null)
        resourceStore.delete(resource.getId());
}
Also used : Policy(org.keycloak.authorization.model.Policy) Resource(org.keycloak.authorization.model.Resource) ResourceServer(org.keycloak.authorization.model.ResourceServer)

Example 73 with Resource

use of org.keycloak.authorization.model.Resource in project keycloak by keycloak.

the class ClientPermissions method canMapCompositeRoles.

@Override
public boolean canMapCompositeRoles(ClientModel client) {
    ResourceServer server = resourceServer(client);
    if (server == null)
        return false;
    Resource resource = authz.getStoreFactory().getResourceStore().findByName(getResourceName(client), server.getId());
    if (resource == null)
        return false;
    Policy policy = authz.getStoreFactory().getPolicyStore().findByName(getMapRolesCompositePermissionName(client), server.getId());
    if (policy == null) {
        return false;
    }
    Set<Policy> associatedPolicies = policy.getAssociatedPolicies();
    // if no policies attached to permission then just do default behavior
    if (associatedPolicies == null || associatedPolicies.isEmpty()) {
        return false;
    }
    Scope scope = authz.getStoreFactory().getScopeStore().findByName(MAP_ROLES_COMPOSITE_SCOPE, server.getId());
    return root.evaluatePermission(resource, server, scope);
}
Also used : Policy(org.keycloak.authorization.model.Policy) Scope(org.keycloak.authorization.model.Scope) Resource(org.keycloak.authorization.model.Resource) ResourceServer(org.keycloak.authorization.model.ResourceServer)

Example 74 with Resource

use of org.keycloak.authorization.model.Resource in project keycloak by keycloak.

the class ClientPermissions method deletePermissions.

private void deletePermissions(ClientModel client) {
    ResourceServer server = resourceServer(client);
    if (server == null)
        return;
    deletePolicy(getManagePermissionName(client), server);
    deletePolicy(getViewPermissionName(client), server);
    deletePolicy(getMapRolesPermissionName(client), server);
    deletePolicy(getMapRolesClientScopePermissionName(client), server);
    deletePolicy(getMapRolesCompositePermissionName(client), server);
    deletePolicy(getConfigurePermissionName(client), server);
    deletePolicy(getExchangeToPermissionName(client), server);
    Resource resource = authz.getStoreFactory().getResourceStore().findByName(getResourceName(client), server.getId());
    ;
    if (resource != null)
        authz.getStoreFactory().getResourceStore().delete(resource.getId());
}
Also used : Resource(org.keycloak.authorization.model.Resource) ResourceServer(org.keycloak.authorization.model.ResourceServer)

Example 75 with Resource

use of org.keycloak.authorization.model.Resource in project keycloak by keycloak.

the class ClientPermissions method canMapRoles.

@Override
public boolean canMapRoles(ClientModel client) {
    ResourceServer server = resourceServer(client);
    if (server == null)
        return false;
    Resource resource = authz.getStoreFactory().getResourceStore().findByName(getResourceName(client), server.getId());
    if (resource == null)
        return false;
    Policy policy = authz.getStoreFactory().getPolicyStore().findByName(getMapRolesPermissionName(client), server.getId());
    if (policy == null) {
        return false;
    }
    Set<Policy> associatedPolicies = policy.getAssociatedPolicies();
    // if no policies attached to permission then just do default behavior
    if (associatedPolicies == null || associatedPolicies.isEmpty()) {
        return false;
    }
    Scope scope = mapRolesScope(server);
    return root.evaluatePermission(resource, server, scope);
}
Also used : Policy(org.keycloak.authorization.model.Policy) Scope(org.keycloak.authorization.model.Scope) Resource(org.keycloak.authorization.model.Resource) ResourceServer(org.keycloak.authorization.model.ResourceServer)

Aggregations

Resource (org.keycloak.authorization.model.Resource)87 ResourceServer (org.keycloak.authorization.model.ResourceServer)51 Policy (org.keycloak.authorization.model.Policy)45 Scope (org.keycloak.authorization.model.Scope)44 AuthorizationProvider (org.keycloak.authorization.AuthorizationProvider)27 ResourceStore (org.keycloak.authorization.store.ResourceStore)27 StoreFactory (org.keycloak.authorization.store.StoreFactory)26 ArrayList (java.util.ArrayList)22 ClientModel (org.keycloak.models.ClientModel)22 List (java.util.List)20 HashSet (java.util.HashSet)19 Map (java.util.Map)19 UserModel (org.keycloak.models.UserModel)18 RealmModel (org.keycloak.models.RealmModel)16 HashMap (java.util.HashMap)15 Set (java.util.Set)15 EnumMap (java.util.EnumMap)14 Collectors (java.util.stream.Collectors)14 Path (javax.ws.rs.Path)13 PolicyStore (org.keycloak.authorization.store.PolicyStore)13