From ee28be982fefab57e163f822a45d8f2182e98f58 Mon Sep 17 00:00:00 2001 From: stefvdwel Date: Tue, 16 Feb 2021 10:42:05 +0100 Subject: [PATCH] Reduced code duplication --- .../permission/PermissionTicketService.java | 60 +++++++------------ 1 file changed, 21 insertions(+), 39 deletions(-) diff --git a/services/src/main/java/org/keycloak/authorization/protection/permission/PermissionTicketService.java b/services/src/main/java/org/keycloak/authorization/protection/permission/PermissionTicketService.java index 030a105130..4d4fd62ce6 100644 --- a/services/src/main/java/org/keycloak/authorization/protection/permission/PermissionTicketService.java +++ b/services/src/main/java/org/keycloak/authorization/protection/permission/PermissionTicketService.java @@ -21,11 +21,16 @@ import org.keycloak.OAuthErrorException; import org.keycloak.authorization.AuthorizationProvider; import org.keycloak.authorization.common.KeycloakIdentity; import org.keycloak.authorization.model.PermissionTicket; +import org.keycloak.authorization.model.Resource; import org.keycloak.authorization.model.ResourceServer; +import org.keycloak.authorization.model.Scope; 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.models.Constants; import org.keycloak.models.RealmModel; +import org.keycloak.models.UserModel; import org.keycloak.models.UserProvider; import org.keycloak.models.utils.ModelToRepresentation; import org.keycloak.models.utils.RepresentationToModel; @@ -37,19 +42,14 @@ import javax.ws.rs.DELETE; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.Response; import java.util.HashMap; import java.util.Map; 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 Pedro Igor @@ -190,34 +190,7 @@ public class PermissionTicketService { StoreFactory storeFactory = authorization.getStoreFactory(); PermissionTicketStore permissionTicketStore = storeFactory.getPermissionTicketStore(); - Map filters = new HashMap<>(); - - 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()); - } + Map filters = getFilters(storeFactory, resourceId, scopeId, owner, requester, granted); return Response.ok().entity(permissionTicketStore.find(filters, resourceServer.getId(), firstResult != null ? firstResult : -1, maxResult != null ? maxResult : Constants.DEFAULT_MAX_RESULTS) .stream() @@ -237,6 +210,18 @@ public class PermissionTicketService { @QueryParam("returnNames") Boolean returnNames) { StoreFactory storeFactory = authorization.getStoreFactory(); PermissionTicketStore permissionTicketStore = storeFactory.getPermissionTicketStore(); + Map filters = getFilters(storeFactory, resourceId, scopeId, owner, requester, granted); + long count = permissionTicketStore.count(filters, resourceServer.getId()); + + return Response.ok().entity(count).build(); + } + + private Map getFilters(StoreFactory storeFactory, + String resourceId, + String scopeId, + String owner, + String requester, + Boolean granted) { Map filters = new HashMap<>(); if (resourceId != null) { @@ -265,11 +250,8 @@ public class PermissionTicketService { if (granted != null) { filters.put(PermissionTicket.GRANTED, granted.toString()); } - long count = permissionTicketStore.count(filters, resourceServer.getId()); - Map map = new HashMap<>(); - map.put("count", count); - return Response.ok().entity(map).build(); + return filters; } private String getUserId(String userIdOrName) {