@Override public ProtocolMapperModel getProtocolMapperById(String id) { ProtocolMapperEntity entity = getProtocolMapperEntity(id); if (entity == null) return null; return entityToModel(entity); }
@Override public String getAttribute(String name) { return getAttributes().get(name); }
protected TypedQuery<ClientScopeRoleMappingEntity> getRealmScopeMappingQuery(RoleModel role) { TypedQuery<ClientScopeRoleMappingEntity> query = em.createNamedQuery("clientScopeHasRole", ClientScopeRoleMappingEntity.class); query.setParameter("clientScope", getEntity()); RoleEntity roleEntity = RoleAdapter.toRoleEntity(role, em); query.setParameter("role", roleEntity); return query; }
@Override public ProtocolMapperModel getProtocolMapperByName(String protocol, String name) { ProtocolMapperEntity entity = getProtocolMapperEntityByName(protocol, name); if (entity == null) return null; return entityToModel(entity); }
@Override public void addScopeMapping(RoleModel role) { if (hasScope(role)) return; ClientScopeRoleMappingEntity entity = new ClientScopeRoleMappingEntity(); entity.setClientScope(getEntity()); RoleEntity roleEntity = RoleAdapter.toRoleEntity(role, em); entity.setRole(roleEntity); em.persist(entity); em.flush(); em.detach(entity); }
@Override public ProtocolMapperModel addProtocolMapper(ProtocolMapperModel model) { if (getProtocolMapperByName(model.getProtocol(), model.getName()) != null) { throw new ModelDuplicateException("Protocol mapper name must be unique per protocol"); } String id = model.getId() != null ? model.getId() : KeycloakModelUtils.generateId(); ProtocolMapperEntity entity = new ProtocolMapperEntity(); entity.setId(id); entity.setName(model.getName()); entity.setProtocol(model.getProtocol()); entity.setProtocolMapper(model.getProtocolMapper()); entity.setClientScope(this.entity); entity.setConfig(model.getConfig()); em.persist(entity); this.entity.getProtocolMappers().add(entity); return entityToModel(entity); }
@Override public void removeProtocolMapper(ProtocolMapperModel mapping) { ProtocolMapperEntity toDelete = getProtocolMapperEntity(mapping.getId()); if (toDelete != null) { session.users().preRemove(mapping); this.entity.getProtocolMappers().remove(toDelete); em.remove(toDelete); } }
@Override public boolean hasScope(RoleModel role) { Set<RoleModel> roles = getScopeMappings(); if (roles.contains(role)) return true; for (RoleModel mapping : roles) { if (mapping.hasRole(role)) return true; } return false; }
@Override public ClientScopeModel getClientScopeById(String id, RealmModel realm) { ClientScopeEntity app = em.find(ClientScopeEntity.class, id); // Check if application belongs to this realm if (app == null || !realm.getId().equals(app.getRealm().getId())) return null; ClientScopeAdapter adapter = new ClientScopeAdapter(realm, em, session, app); return adapter; }
@Override public int hashCode() { return getId().hashCode(); }
@Override public void deleteScopeMapping(RoleModel role) { TypedQuery<ClientScopeRoleMappingEntity> query = getRealmScopeMappingQuery(role); List<ClientScopeRoleMappingEntity> results = query.getResultList(); if (results.size() == 0) return; for (ClientScopeRoleMappingEntity entity : results) { em.remove(entity); } }
@Override public void updateProtocolMapper(ProtocolMapperModel mapping) { ProtocolMapperEntity entity = getProtocolMapperEntity(mapping.getId()); entity.setProtocolMapper(mapping.getProtocolMapper()); if (entity.getConfig() == null) { entity.setConfig(mapping.getConfig()); } else { entity.getConfig().clear(); entity.getConfig().putAll(mapping.getConfig()); } em.flush(); }
@Override public Set<RoleModel> getRealmScopeMappings() { Set<RoleModel> roleMappings = getScopeMappings(); Set<RoleModel> appRoles = new HashSet<>(); for (RoleModel role : roleMappings) { RoleContainerModel container = role.getContainer(); if (container instanceof RealmModel) { if (container.getId().equals(realm.getId())) { appRoles.add(role); } } } return appRoles; }
@Override public ClientScopeModel addClientScope(String id, String name) { ClientScopeEntity entity = new ClientScopeEntity(); entity.setId(id); name = KeycloakModelUtils.convertClientScopeName(name); entity.setName(name); entity.setRealm(realm); realm.getClientScopes().add(entity); em.persist(entity); em.flush(); final ClientScopeModel resource = new ClientScopeAdapter(this, em, session, entity); em.flush(); return resource; }
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || !(o instanceof ClientScopeModel)) return false; ClientScopeModel that = (ClientScopeModel) o; return that.getId().equals(getId()); }
@Override public Set<RoleModel> getScopeMappings() { TypedQuery<String> query = em.createNamedQuery("clientScopeRoleMappingIds", String.class); query.setParameter("clientScope", getEntity()); List<String> ids = query.getResultList(); Set<RoleModel> roles = new HashSet<RoleModel>(); for (String roleId : ids) { RoleModel role = realm.getRoleById(roleId); if (role == null) continue; roles.add(role); } return roles; }
public static ClientScopeEntity toClientScopeEntity(ClientScopeModel model, EntityManager em) { if (model instanceof ClientScopeAdapter) { return ((ClientScopeAdapter)model).getEntity(); } return em.getReference(ClientScopeEntity.class, model.getId()); }