use of org.keycloak.authorization.model.Resource in project keycloak by keycloak.
the class UserManagedPermissionServiceTest method testRemovePoliciesOnResourceDelete.
private static void testRemovePoliciesOnResourceDelete(KeycloakSession session) {
RealmModel realm = session.realms().getRealmByName("authz-test");
ClientModel client = realm.getClientByClientId("resource-server-test");
AuthorizationProvider provider = session.getProvider(AuthorizationProvider.class);
UserModel user = session.users().getUserByUsername(realm, "marta");
Map<Policy.FilterOption, String[]> filters = new HashMap<>();
filters.put(Policy.FilterOption.TYPE, new String[] { "uma" });
filters.put(OWNER, new String[] { user.getId() });
List<Policy> policies = provider.getStoreFactory().getPolicyStore().findByResourceServer(filters, client.getId(), -1, -1);
assertEquals(1, policies.size());
Policy policy = policies.get(0);
assertFalse(policy.getResources().isEmpty());
Resource resource = policy.getResources().iterator().next();
assertEquals("Resource A", resource.getName());
provider.getStoreFactory().getResourceStore().delete(resource.getId());
filters = new HashMap<>();
filters.put(OWNER, new String[] { user.getId() });
policies = provider.getStoreFactory().getPolicyStore().findByResourceServer(filters, client.getId(), -1, -1);
assertTrue(policies.isEmpty());
}
use of org.keycloak.authorization.model.Resource in project keycloak by keycloak.
the class UserManagedPermissionServiceTest method testRemovePoliciesOnClientDelete.
private static void testRemovePoliciesOnClientDelete(KeycloakSession session) {
RealmModel realm = session.realms().getRealmByName("authz-test");
ClientModel client = realm.getClientByClientId("resource-server-test");
AuthorizationProvider provider = session.getProvider(AuthorizationProvider.class);
UserModel user = session.users().getUserByUsername(realm, "marta");
Map<Policy.FilterOption, String[]> filters = new HashMap<>();
filters.put(Policy.FilterOption.TYPE, new String[] { "uma" });
filters.put(OWNER, new String[] { user.getId() });
List<Policy> policies = provider.getStoreFactory().getPolicyStore().findByResourceServer(filters, client.getId(), -1, -1);
assertEquals(1, policies.size());
Policy policy = policies.get(0);
assertFalse(policy.getResources().isEmpty());
Resource resource = policy.getResources().iterator().next();
assertEquals("Resource A", resource.getName());
realm.removeClient(realm.getClientByClientId("client-remove").getId());
filters = new HashMap<>();
filters.put(OWNER, new String[] { user.getId() });
policies = provider.getStoreFactory().getPolicyStore().findByResourceServer(filters, client.getId(), -1, -1);
assertTrue(policies.isEmpty());
}
use of org.keycloak.authorization.model.Resource in project keycloak by keycloak.
the class UserManagedPermissionServiceTest method testRemovePoliciesOnGroupDelete.
private static void testRemovePoliciesOnGroupDelete(KeycloakSession session) {
RealmModel realm = session.realms().getRealmByName("authz-test");
ClientModel client = realm.getClientByClientId("resource-server-test");
AuthorizationProvider provider = session.getProvider(AuthorizationProvider.class);
UserModel user = session.users().getUserByUsername(realm, "marta");
Map<Policy.FilterOption, String[]> filters = new HashMap<>();
filters.put(Policy.FilterOption.TYPE, new String[] { "uma" });
filters.put(OWNER, new String[] { user.getId() });
List<Policy> policies = provider.getStoreFactory().getPolicyStore().findByResourceServer(filters, client.getId(), -1, -1);
assertEquals(1, policies.size());
Policy policy = policies.get(0);
assertFalse(policy.getResources().isEmpty());
Resource resource = policy.getResources().iterator().next();
assertEquals("Resource A", resource.getName());
realm.removeGroup(realm.searchForGroupByNameStream("group_remove", -1, -1).findAny().get());
filters = new HashMap<>();
filters.put(OWNER, new String[] { user.getId() });
policies = provider.getStoreFactory().getPolicyStore().findByResourceServer(filters, client.getId(), -1, -1);
assertTrue(policies.isEmpty());
}
use of org.keycloak.authorization.model.Resource in project keycloak by keycloak.
the class MapPermissionTicketStore method findGrantedResources.
@Override
public List<Resource> findGrantedResources(String requester, String name, int first, int max) {
DefaultModelCriteria<PermissionTicket> mcb = criteria();
mcb = mcb.compare(SearchableFields.REQUESTER, Operator.EQ, requester).compare(SearchableFields.GRANTED_TIMESTAMP, Operator.EXISTS);
Function<MapPermissionTicketEntity, Resource> ticketResourceMapper;
ResourceStore resourceStore = authorizationProvider.getStoreFactory().getResourceStore();
if (name != null) {
ticketResourceMapper = ticket -> {
Map<Resource.FilterOption, String[]> filterOptionMap = new EnumMap<>(Resource.FilterOption.class);
filterOptionMap.put(Resource.FilterOption.ID, new String[] { ticket.getResourceId() });
filterOptionMap.put(Resource.FilterOption.NAME, new String[] { name });
List<Resource> resource = resourceStore.findByResourceServer(filterOptionMap, ticket.getResourceServerId(), -1, 1);
return resource.isEmpty() ? null : resource.get(0);
};
} else {
ticketResourceMapper = ticket -> resourceStore.findById(ticket.getResourceId(), ticket.getResourceServerId());
}
return paginatedStream(tx.read(withCriteria(mcb).orderBy(SearchableFields.RESOURCE_ID, ASCENDING)).filter(distinctByKey(MapPermissionTicketEntity::getResourceId)).map(ticketResourceMapper).filter(Objects::nonNull), first, max).collect(Collectors.toList());
}
use of org.keycloak.authorization.model.Resource in project keycloak by keycloak.
the class GroupPermissions method resource.
@Override
public Resource resource(GroupModel group) {
ResourceServer server = root.realmResourceServer();
if (server == null)
return null;
Resource resource = resourceStore.findByName(getGroupResourceName(group), server.getId());
if (resource == null)
return null;
return resource;
}
Aggregations