public static List<String> getRoleNames(Collection<RoleModel> roles) { List<String> roleNames = new ArrayList<String>(); for (RoleModel role : roles) { roleNames.add(role.getName()); } return roleNames; }
@Override public String getName() { if (updated != null) return updated.getName(); return cached.getName(); }
@Override public String getName() { if (isUpdated()) return updated.getName(); return cached.getName(); }
private static List<String> userRoles(String userName, KeycloakSession keycloakSession) { List<String> userRoles = new ArrayList<>(); for (RoleModel roleMapping : keycloakSession.users() .getUserByUsername(userName, keycloakSession.getContext().getRealm()) .getRoleMappings()) userRoles.add(roleMapping.getName()); return userRoles; }
private static List<String> userRoles(String userName, KeycloakSession keycloakSession) { List<String> userRoles = new ArrayList<>(); for (RoleModel roleMapping : keycloakSession.users() .getUserByUsername(userName, keycloakSession.getContext().getRealm()) .getRoleMappings()) userRoles.add(roleMapping.getName()); return userRoles; }
@Override public boolean removeRole(RealmModel realm, RoleModel role) { session.users().preRemove(realm, role); RoleContainerModel container = role.getContainer(); if (container.getDefaultRoles().contains(role.getName())) { container.removeDefaultRoles(role.getName()); } return getMongoStore().removeEntity(MongoRoleEntity.class, role.getId(), invocationContext); }
@Override public boolean removeRole(RealmModel realm, RoleModel role) { session.users().preRemove(realm, role); RoleContainerModel container = role.getContainer(); if (container.getDefaultRoles().contains(role.getName())) { container.removeDefaultRoles(role.getName()); } RoleEntity roleEntity = em.getReference(RoleEntity.class, role.getId()); String compositeRoleTable = JpaUtils.getTableNameForNativeQuery("COMPOSITE_ROLE", em); em.createNativeQuery("delete from " + compositeRoleTable + " where CHILD_ROLE = :role").setParameter("role", roleEntity).executeUpdate(); realm.getClients().forEach(c -> c.deleteScopeMapping(role)); em.createNamedQuery("deleteClientScopeRoleMappingByRole").setParameter("role", roleEntity).executeUpdate(); int val = em.createNamedQuery("deleteGroupRoleMappingsByRole").setParameter("roleId", roleEntity.getId()).executeUpdate(); em.flush(); em.remove(roleEntity); session.getKeycloakSessionFactory().publish(new RoleContainerModel.RoleRemovedEvent() { @Override public RoleModel getRole() { return role; } @Override public KeycloakSession getKeycloakSession() { return session; } }); em.flush(); return true; }
public static ClientRemovedEvent create(ClientModel client) { ClientRemovedEvent event = new ClientRemovedEvent(); event.realmId = client.getRealm().getId(); event.clientUuid = client.getId(); event.clientId = client.getClientId(); event.clientRoles = new HashMap<>(); for (RoleModel clientRole : client.getRoles()) { event.clientRoles.put(clientRole.getId(), clientRole.getName()); } return event; }
@Override public void onExport(Policy policy, PolicyRepresentation representation, AuthorizationProvider authorizationProvider) { Map<String, String> config = new HashMap<>(); Set<RolePolicyRepresentation.RoleDefinition> roles = toRepresentation(policy, authorizationProvider).getRoles(); for (RolePolicyRepresentation.RoleDefinition roleDefinition : roles) { RoleModel role = authorizationProvider.getRealm().getRoleById(roleDefinition.getId()); if (role.isClientRole()) { roleDefinition.setId(ClientModel.class.cast(role.getContainer()).getClientId() + "/" + role.getName()); } else { roleDefinition.setId(role.getName()); } } try { config.put("roles", JsonSerialization.writeValueAsString(roles)); } catch (IOException cause) { throw new RuntimeException("Failed to export role policy [" + policy.getName() + "]", cause); } representation.setConfig(config); }
@Override public boolean removeRole(RealmModel realm, RoleModel role) { listInvalidations.add(role.getContainer().getId()); invalidateRole(role.getId()); invalidationEvents.add(RoleRemovedEvent.create(role.getId(), role.getName(), role.getContainer().getId())); roleRemovalInvalidations(role.getId(), role.getName(), role.getContainer().getId()); return getRealmDelegate().removeRole(realm, role); }
public CachedRole(RoleModel model, RealmModel realm) { composite = model.isComposite(); description = model.getDescription(); id = model.getId(); name = model.getName(); scopeParamRequired = model.isScopeParamRequired(); this.realm = realm.getId(); if (composite) { for (RoleModel child : model.getComposites()) { composites.add(child.getId()); } } }
public static GroupRepresentation toRepresentation(GroupModel group, boolean full) { GroupRepresentation rep = new GroupRepresentation(); rep.setId(group.getId()); rep.setName(group.getName()); rep.setPath(buildGroupPath(group)); if (!full) return rep; // Role mappings Set<RoleModel> roles = group.getRoleMappings(); List<String> realmRoleNames = new ArrayList<>(); Map<String, List<String>> clientRoleNames = new HashMap<>(); for (RoleModel role : roles) { if (role.getContainer() instanceof RealmModel) { realmRoleNames.add(role.getName()); } else { ClientModel client = (ClientModel)role.getContainer(); String clientId = client.getClientId(); List<String> currentClientRoles = clientRoleNames.get(clientId); if (currentClientRoles == null) { currentClientRoles = new ArrayList<>(); clientRoleNames.put(clientId, currentClientRoles); } currentClientRoles.add(role.getName()); } } rep.setRealmRoles(realmRoleNames); rep.setClientRoles(clientRoleNames); Map<String, List<String>> attributes = group.getAttributes(); rep.setAttributes(attributes); return rep; }
private boolean hasRole(Identity identity, RoleModel role, RealmModel realm) { String roleName = role.getName(); if (role.isClientRole()) { ClientModel clientModel = realm.getClientById(role.getContainerId()); return identity.hasClientRole(clientModel.getClientId(), roleName); } return identity.hasRealmRole(roleName); }
private void processRoles(Set<RoleModel> inputRoles, List<RoleModel> realmRoles, MultivaluedHashMap<String, ClientRoleEntry> clientRoles) { for (RoleModel role : inputRoles) { if (role.getContainer() instanceof RealmModel) { realmRoles.add(role); } else { ClientModel currentClient = (ClientModel) role.getContainer(); ClientRoleEntry clientRole = new ClientRoleEntry(currentClient.getClientId(), currentClient.getName(), role.getName(), role.getDescription()); clientRoles.add(currentClient.getClientId(), clientRole); } } }
public CachedRole(Long revision, RoleModel model, RealmModel realm) { super(revision, model.getId()); composite = model.isComposite(); description = model.getDescription(); name = model.getName(); this.realm = realm.getId(); if (composite) { for (RoleModel child : model.getComposites()) { composites.add(child.getId()); } } attributes = new DefaultLazyLoader<>(roleModel -> new MultivaluedHashMap<>(roleModel.getAttributes()), MultivaluedHashMap::new); }
@Override public boolean removeClient(String id, RealmModel realm) { ClientModel client = getClientById(id, realm); if (client == null) return false; invalidateClient(client.getId()); // this is needed so that a client that hasn't been committed isn't cached in a query listInvalidations.add(realm.getId()); invalidationEvents.add(ClientRemovedEvent.create(client)); cache.clientRemoval(realm.getId(), id, client.getClientId(), invalidations); for (RoleModel role : client.getRoles()) { roleRemovalInvalidations(role.getId(), role.getName(), client.getId()); } return getRealmDelegate().removeClient(id, realm); }
result.add(roleEntity); roleEntity.setName(roleModel.getName()); roleEntity.setDescription(roleModel.getDescription());
public static RoleRepresentation toRepresentation(RoleModel role) { RoleRepresentation rep = new RoleRepresentation(); rep.setId(role.getId()); rep.setName(role.getName()); rep.setDescription(role.getDescription()); rep.setScopeParamRequired(role.isScopeParamRequired()); rep.setComposite(role.isComposite()); return rep; }