Search in sources :

Example 6 with RoleContainerModel

use of org.keycloak.models.RoleContainerModel in project keycloak by keycloak.

the class MapClientProviderFactory method onEvent.

@Override
public void onEvent(ProviderEvent event) {
    if (event instanceof RoleContainerModel.RoleRemovedEvent) {
        RoleRemovedEvent e = (RoleContainerModel.RoleRemovedEvent) event;
        RoleModel role = e.getRole();
        RoleContainerModel container = role.getContainer();
        RealmModel realm;
        if (container instanceof RealmModel) {
            realm = (RealmModel) container;
        } else if (container instanceof ClientModel) {
            realm = ((ClientModel) container).getRealm();
        } else {
            return;
        }
        ((MapClientProvider) e.getKeycloakSession().getProvider(ClientProvider.class)).preRemove(realm, role);
    }
}
Also used : RealmModel(org.keycloak.models.RealmModel) ClientModel(org.keycloak.models.ClientModel) ClientProvider(org.keycloak.models.ClientProvider) RoleRemovedEvent(org.keycloak.models.RoleContainerModel.RoleRemovedEvent) RoleModel(org.keycloak.models.RoleModel) RoleContainerModel(org.keycloak.models.RoleContainerModel)

Example 7 with RoleContainerModel

use of org.keycloak.models.RoleContainerModel in project keycloak by keycloak.

the class RoleNameMapper method mapName.

@Override
public String mapName(ProtocolMapperModel model, RoleModel roleModel) {
    RoleContainerModel container = roleModel.getContainer();
    ClientModel app = null;
    if (container instanceof ClientModel) {
        app = (ClientModel) container;
    }
    String role = model.getConfig().get(ROLE_CONFIG);
    String newName = model.getConfig().get(NEW_ROLE_NAME);
    int scopeIndex = role.indexOf('.');
    if (scopeIndex > -1 && app != null) {
        final String clientId = app.getClientId();
        if (!role.startsWith(clientId + "."))
            return null;
        role = role.substring(clientId.length() + 1);
    } else {
        if (app != null)
            return null;
    }
    if (roleModel.getName().equals(role))
        return newName;
    return null;
}
Also used : ClientModel(org.keycloak.models.ClientModel) RoleContainerModel(org.keycloak.models.RoleContainerModel)

Example 8 with RoleContainerModel

use of org.keycloak.models.RoleContainerModel in project keycloak by keycloak.

the class ExportUtils method exportRole.

/**
 * Full export of role including composite roles
 * @param role
 * @return RoleRepresentation with all stuff filled (including composite roles)
 */
public static RoleRepresentation exportRole(RoleModel role) {
    RoleRepresentation roleRep = ModelToRepresentation.toRepresentation(role);
    Set<RoleModel> composites = role.getCompositesStream().collect(Collectors.toSet());
    if (composites != null && composites.size() > 0) {
        Set<String> compositeRealmRoles = null;
        Map<String, List<String>> compositeClientRoles = null;
        for (RoleModel composite : composites) {
            RoleContainerModel crContainer = composite.getContainer();
            if (crContainer instanceof RealmModel) {
                if (compositeRealmRoles == null) {
                    compositeRealmRoles = new HashSet<>();
                }
                compositeRealmRoles.add(composite.getName());
            } else {
                if (compositeClientRoles == null) {
                    compositeClientRoles = new HashMap<>();
                }
                ClientModel app = (ClientModel) crContainer;
                String appName = app.getClientId();
                List<String> currentAppComposites = compositeClientRoles.get(appName);
                if (currentAppComposites == null) {
                    currentAppComposites = new ArrayList<>();
                    compositeClientRoles.put(appName, currentAppComposites);
                }
                currentAppComposites.add(composite.getName());
            }
        }
        RoleRepresentation.Composites compRep = new RoleRepresentation.Composites();
        if (compositeRealmRoles != null) {
            compRep.setRealm(compositeRealmRoles);
        }
        if (compositeClientRoles != null) {
            compRep.setClient(compositeClientRoles);
        }
        roleRep.setComposites(compRep);
    }
    return roleRep;
}
Also used : RoleRepresentation(org.keycloak.representations.idm.RoleRepresentation) RoleModel(org.keycloak.models.RoleModel) RealmModel(org.keycloak.models.RealmModel) ClientModel(org.keycloak.models.ClientModel) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) RoleContainerModel(org.keycloak.models.RoleContainerModel)

Example 9 with RoleContainerModel

use of org.keycloak.models.RoleContainerModel in project keycloak by keycloak.

the class RoleMapperResource method getRoleMappings.

/**
 * Get role mappings
 *
 * @return
 */
@GET
@Produces(MediaType.APPLICATION_JSON)
@NoCache
public MappingsRepresentation getRoleMappings() {
    viewPermission.require();
    List<RoleRepresentation> realmRolesRepresentation = new ArrayList<>();
    Map<String, ClientMappingsRepresentation> appMappings = new HashMap<>();
    final AtomicReference<ClientMappingsRepresentation> mappings = new AtomicReference<>();
    roleMapper.getRoleMappingsStream().forEach(roleMapping -> {
        RoleContainerModel container = roleMapping.getContainer();
        if (container instanceof RealmModel) {
            realmRolesRepresentation.add(ModelToRepresentation.toBriefRepresentation(roleMapping));
        } else if (container instanceof ClientModel) {
            ClientModel clientModel = (ClientModel) container;
            mappings.set(appMappings.get(clientModel.getClientId()));
            if (mappings.get() == null) {
                mappings.set(new ClientMappingsRepresentation());
                mappings.get().setId(clientModel.getId());
                mappings.get().setClient(clientModel.getClientId());
                mappings.get().setMappings(new ArrayList<>());
                appMappings.put(clientModel.getClientId(), mappings.get());
            }
            mappings.get().getMappings().add(ModelToRepresentation.toBriefRepresentation(roleMapping));
        }
    });
    MappingsRepresentation all = new MappingsRepresentation();
    if (!realmRolesRepresentation.isEmpty())
        all.setRealmMappings(realmRolesRepresentation);
    if (!appMappings.isEmpty())
        all.setClientMappings(appMappings);
    return all;
}
Also used : RoleRepresentation(org.keycloak.representations.idm.RoleRepresentation) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) RealmModel(org.keycloak.models.RealmModel) ClientModel(org.keycloak.models.ClientModel) MappingsRepresentation(org.keycloak.representations.idm.MappingsRepresentation) ClientMappingsRepresentation(org.keycloak.representations.idm.ClientMappingsRepresentation) ClientMappingsRepresentation(org.keycloak.representations.idm.ClientMappingsRepresentation) RoleContainerModel(org.keycloak.models.RoleContainerModel) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) NoCache(org.jboss.resteasy.annotations.cache.NoCache)

Example 10 with RoleContainerModel

use of org.keycloak.models.RoleContainerModel in project keycloak by keycloak.

the class ClientScopeEvaluateResource method scopeMappings.

/**
 * @param scopeParam
 * @param roleContainerId either realm name OR client UUID
 * @return
 */
@Path("scope-mappings/{roleContainerId}")
public ClientScopeEvaluateScopeMappingsResource scopeMappings(@QueryParam("scope") String scopeParam, @PathParam("roleContainerId") String roleContainerId) {
    auth.clients().requireView(client);
    if (roleContainerId == null) {
        throw new NotFoundException("No roleContainerId provided");
    }
    RoleContainerModel roleContainer = roleContainerId.equals(realm.getName()) ? realm : realm.getClientById(roleContainerId);
    if (roleContainer == null) {
        throw new NotFoundException("Role Container not found");
    }
    return new ClientScopeEvaluateScopeMappingsResource(roleContainer, auth, client, scopeParam, session);
}
Also used : NotFoundException(javax.ws.rs.NotFoundException) RoleContainerModel(org.keycloak.models.RoleContainerModel) Path(javax.ws.rs.Path)

Aggregations

RoleContainerModel (org.keycloak.models.RoleContainerModel)11 ClientModel (org.keycloak.models.ClientModel)8 RealmModel (org.keycloak.models.RealmModel)7 RoleModel (org.keycloak.models.RoleModel)7 RoleRemovedEvent (org.keycloak.models.RoleContainerModel.RoleRemovedEvent)4 LDAPObject (org.keycloak.storage.ldap.idm.model.LDAPObject)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 RoleRepresentation (org.keycloak.representations.idm.RoleRepresentation)2 LDAPQuery (org.keycloak.storage.ldap.idm.query.internal.LDAPQuery)2 LDAPGroupMapperMode (org.keycloak.storage.ldap.mappers.membership.LDAPGroupMapperMode)2 SynchronizationResult (org.keycloak.storage.user.SynchronizationResult)2 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 Objects (java.util.Objects)1 Set (java.util.Set)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1