use of org.keycloak.authorization.jpa.entities.ScopeEntity in project keycloak by keycloak.
the class JPAScopeStore method create.
@Override
public Scope create(String id, final String name, final ResourceServer resourceServer) {
ScopeEntity entity = new ScopeEntity();
if (id == null) {
entity.setId(KeycloakModelUtils.generateId());
} else {
entity.setId(id);
}
entity.setName(name);
entity.setResourceServer(ResourceServerAdapter.toEntity(entityManager, resourceServer));
this.entityManager.persist(entity);
this.entityManager.flush();
return new ScopeAdapter(entity, entityManager, provider.getStoreFactory());
}
use of org.keycloak.authorization.jpa.entities.ScopeEntity in project keycloak by keycloak.
the class JPAScopeStore method findByResourceServer.
@Override
public List<Scope> findByResourceServer(Map<Scope.FilterOption, String[]> attributes, String resourceServerId, int firstResult, int maxResult) {
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<ScopeEntity> querybuilder = builder.createQuery(ScopeEntity.class);
Root<ScopeEntity> root = querybuilder.from(ScopeEntity.class);
querybuilder.select(root.get("id"));
List<Predicate> predicates = new ArrayList();
predicates.add(builder.equal(root.get("resourceServer").get("id"), resourceServerId));
attributes.forEach((filterOption, value) -> {
switch(filterOption) {
case ID:
predicates.add(root.get(filterOption.getName()).in(value));
break;
case NAME:
predicates.add(builder.like(builder.lower(root.get(filterOption.getName())), "%" + value[0].toLowerCase() + "%"));
break;
default:
throw new IllegalArgumentException("Unsupported filter [" + filterOption + "]");
}
});
querybuilder.where(predicates.toArray(new Predicate[predicates.size()])).orderBy(builder.asc(root.get("name")));
TypedQuery query = entityManager.createQuery(querybuilder);
List result = paginateQuery(query, firstResult, maxResult).getResultList();
List<Scope> list = new LinkedList<>();
for (Object id : result) {
list.add(provider.getStoreFactory().getScopeStore().findById((String) id, resourceServerId));
}
return list;
}
use of org.keycloak.authorization.jpa.entities.ScopeEntity in project keycloak by keycloak.
the class ResourceAdapter method updateScopes.
@Override
public void updateScopes(Set<Scope> toUpdate) {
throwExceptionIfReadonly();
Set<String> ids = new HashSet<>();
for (Scope scope : toUpdate) {
ids.add(scope.getId());
}
Iterator<ScopeEntity> it = entity.getScopes().iterator();
while (it.hasNext()) {
ScopeEntity next = it.next();
if (!ids.contains(next.getId()))
it.remove();
else
ids.remove(next.getId());
}
for (String addId : ids) {
entity.getScopes().add(em.getReference(ScopeEntity.class, addId));
}
}
Aggregations