Reduced code duplication
This commit is contained in:
parent
c15361c129
commit
ee28be982f
1 changed files with 21 additions and 39 deletions
|
@ -21,11 +21,16 @@ import org.keycloak.OAuthErrorException;
|
||||||
import org.keycloak.authorization.AuthorizationProvider;
|
import org.keycloak.authorization.AuthorizationProvider;
|
||||||
import org.keycloak.authorization.common.KeycloakIdentity;
|
import org.keycloak.authorization.common.KeycloakIdentity;
|
||||||
import org.keycloak.authorization.model.PermissionTicket;
|
import org.keycloak.authorization.model.PermissionTicket;
|
||||||
|
import org.keycloak.authorization.model.Resource;
|
||||||
import org.keycloak.authorization.model.ResourceServer;
|
import org.keycloak.authorization.model.ResourceServer;
|
||||||
|
import org.keycloak.authorization.model.Scope;
|
||||||
import org.keycloak.authorization.store.PermissionTicketStore;
|
import org.keycloak.authorization.store.PermissionTicketStore;
|
||||||
|
import org.keycloak.authorization.store.ResourceStore;
|
||||||
|
import org.keycloak.authorization.store.ScopeStore;
|
||||||
import org.keycloak.authorization.store.StoreFactory;
|
import org.keycloak.authorization.store.StoreFactory;
|
||||||
import org.keycloak.models.Constants;
|
import org.keycloak.models.Constants;
|
||||||
import org.keycloak.models.RealmModel;
|
import org.keycloak.models.RealmModel;
|
||||||
|
import org.keycloak.models.UserModel;
|
||||||
import org.keycloak.models.UserProvider;
|
import org.keycloak.models.UserProvider;
|
||||||
import org.keycloak.models.utils.ModelToRepresentation;
|
import org.keycloak.models.utils.ModelToRepresentation;
|
||||||
import org.keycloak.models.utils.RepresentationToModel;
|
import org.keycloak.models.utils.RepresentationToModel;
|
||||||
|
@ -37,19 +42,14 @@ import javax.ws.rs.DELETE;
|
||||||
import javax.ws.rs.GET;
|
import javax.ws.rs.GET;
|
||||||
import javax.ws.rs.POST;
|
import javax.ws.rs.POST;
|
||||||
import javax.ws.rs.PUT;
|
import javax.ws.rs.PUT;
|
||||||
|
import javax.ws.rs.Path;
|
||||||
|
import javax.ws.rs.PathParam;
|
||||||
import javax.ws.rs.Produces;
|
import javax.ws.rs.Produces;
|
||||||
import javax.ws.rs.QueryParam;
|
import javax.ws.rs.QueryParam;
|
||||||
import javax.ws.rs.core.Response;
|
import javax.ws.rs.core.Response;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import javax.ws.rs.Path;
|
|
||||||
import javax.ws.rs.PathParam;
|
|
||||||
import org.keycloak.authorization.model.Resource;
|
|
||||||
import org.keycloak.authorization.model.Scope;
|
|
||||||
import org.keycloak.authorization.store.ResourceStore;
|
|
||||||
import org.keycloak.authorization.store.ScopeStore;
|
|
||||||
import org.keycloak.models.UserModel;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author <a href="mailto:psilva@redhat.com">Pedro Igor</a>
|
* @author <a href="mailto:psilva@redhat.com">Pedro Igor</a>
|
||||||
|
@ -190,34 +190,7 @@ public class PermissionTicketService {
|
||||||
StoreFactory storeFactory = authorization.getStoreFactory();
|
StoreFactory storeFactory = authorization.getStoreFactory();
|
||||||
PermissionTicketStore permissionTicketStore = storeFactory.getPermissionTicketStore();
|
PermissionTicketStore permissionTicketStore = storeFactory.getPermissionTicketStore();
|
||||||
|
|
||||||
Map<String, String> filters = new HashMap<>();
|
Map<String, String> filters = getFilters(storeFactory, resourceId, scopeId, owner, requester, granted);
|
||||||
|
|
||||||
if (resourceId != null) {
|
|
||||||
filters.put(PermissionTicket.RESOURCE, resourceId);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (scopeId != null) {
|
|
||||||
ScopeStore scopeStore = storeFactory.getScopeStore();
|
|
||||||
Scope scope = scopeStore.findById(scopeId, resourceServer.getId());
|
|
||||||
|
|
||||||
if (scope == null) {
|
|
||||||
scope = scopeStore.findByName(scopeId, resourceServer.getId());
|
|
||||||
}
|
|
||||||
|
|
||||||
filters.put(PermissionTicket.SCOPE, scope != null ? scope.getId() : scopeId);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (owner != null) {
|
|
||||||
filters.put(PermissionTicket.OWNER, getUserId(owner));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (requester != null) {
|
|
||||||
filters.put(PermissionTicket.REQUESTER, getUserId(requester));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (granted != null) {
|
|
||||||
filters.put(PermissionTicket.GRANTED, granted.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
return Response.ok().entity(permissionTicketStore.find(filters, resourceServer.getId(), firstResult != null ? firstResult : -1, maxResult != null ? maxResult : Constants.DEFAULT_MAX_RESULTS)
|
return Response.ok().entity(permissionTicketStore.find(filters, resourceServer.getId(), firstResult != null ? firstResult : -1, maxResult != null ? maxResult : Constants.DEFAULT_MAX_RESULTS)
|
||||||
.stream()
|
.stream()
|
||||||
|
@ -237,6 +210,18 @@ public class PermissionTicketService {
|
||||||
@QueryParam("returnNames") Boolean returnNames) {
|
@QueryParam("returnNames") Boolean returnNames) {
|
||||||
StoreFactory storeFactory = authorization.getStoreFactory();
|
StoreFactory storeFactory = authorization.getStoreFactory();
|
||||||
PermissionTicketStore permissionTicketStore = storeFactory.getPermissionTicketStore();
|
PermissionTicketStore permissionTicketStore = storeFactory.getPermissionTicketStore();
|
||||||
|
Map<String, String> filters = getFilters(storeFactory, resourceId, scopeId, owner, requester, granted);
|
||||||
|
long count = permissionTicketStore.count(filters, resourceServer.getId());
|
||||||
|
|
||||||
|
return Response.ok().entity(count).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, String> getFilters(StoreFactory storeFactory,
|
||||||
|
String resourceId,
|
||||||
|
String scopeId,
|
||||||
|
String owner,
|
||||||
|
String requester,
|
||||||
|
Boolean granted) {
|
||||||
Map<String, String> filters = new HashMap<>();
|
Map<String, String> filters = new HashMap<>();
|
||||||
|
|
||||||
if (resourceId != null) {
|
if (resourceId != null) {
|
||||||
|
@ -265,11 +250,8 @@ public class PermissionTicketService {
|
||||||
if (granted != null) {
|
if (granted != null) {
|
||||||
filters.put(PermissionTicket.GRANTED, granted.toString());
|
filters.put(PermissionTicket.GRANTED, granted.toString());
|
||||||
}
|
}
|
||||||
long count = permissionTicketStore.count(filters, resourceServer.getId());
|
|
||||||
|
|
||||||
Map<String, Long> map = new HashMap<>();
|
return filters;
|
||||||
map.put("count", count);
|
|
||||||
return Response.ok().entity(map).build();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getUserId(String userIdOrName) {
|
private String getUserId(String userIdOrName) {
|
||||||
|
|
Loading…
Reference in a new issue