@Override public List<Scope> getScopes() { if (isUpdated()) return updated.getScopes(); if (scopes != null) return scopes; scopes = new LinkedList<>(); for (String scopeId : cached.getScopesIds(modelSupplier)) { scopes.add(cacheSession.getScopeStore().findById(scopeId, cached.getResourceServerId())); } return scopes = Collections.unmodifiableList(scopes); }
@Override public void delete(String id) { if (id == null) return; Resource resource = findById(id, null); if (resource == null) return; cache.invalidateObject(id); invalidationEvents.add(ResourceRemovedEvent.create(id, resource.getName(), resource.getType(), resource.getUris(), resource.getOwner(), resource.getScopes().stream().map(scope -> scope.getId()).collect(Collectors.toSet()), resource.getResourceServer().getId())); cache.resourceRemoval(id, resource.getName(), resource.getType(), resource.getUris(), resource.getOwner(), resource.getScopes().stream().map(scope -> scope.getId()).collect(Collectors.toSet()), resource.getResourceServer().getId(), invalidations); getResourceStoreDelegate().delete(id); }
public CachedResource(Long revision, Resource resource) { super(revision, resource.getId()); this.name = resource.getName(); this.displayName = resource.getDisplayName(); this.type = resource.getType(); this.owner = resource.getOwner(); this.iconUri = resource.getIconUri(); this.resourceServerId = resource.getResourceServer().getId(); ownerManagedAccess = resource.isOwnerManagedAccess(); if (resource.isFetched("uris")) { Set<String> data = new HashSet<>(resource.getUris()); this.uris = source -> data; } else { this.uris = new DefaultLazyLoader<>(source -> new HashSet<>(source.getUris()), Collections::emptySet); } if (resource.isFetched("scopes")) { Set<String> data = resource.getScopes().stream().map(Scope::getId).collect(Collectors.toSet()); this.scopesIds = source -> data; } else { this.scopesIds = new DefaultLazyLoader<>(source -> source.getScopes().stream().map(Scope::getId).collect(Collectors.toSet()), Collections::emptySet); } if (resource.isFetched("attributes")) { MultivaluedHashMap<String, String> data = new MultivaluedHashMap<>(resource.getAttributes()); this.attributes = source -> data; } else { this.attributes = new DefaultLazyLoader<>(source -> new MultivaluedHashMap<>(source.getAttributes()), MultivaluedHashMap::new); } }
@Override public Resource create(String id, String name, ResourceServer resourceServer, String owner) { Resource resource = getResourceStoreDelegate().create(id, name, resourceServer, owner); Resource cached = findById(resource.getId(), resourceServer.getId()); registerResourceInvalidation(resource.getId(), resource.getName(), resource.getType(), resource.getUris(), resource.getScopes().stream().map(scope -> scope.getId()).collect(Collectors.toSet()), resourceServer.getId(), resource.getOwner()); if (cached == null) { cached = findById(resource.getId(), resourceServer.getId()); } return cached; }
@Override public void updateScopes(Set<Scope> scopes) { Resource updated = getDelegateForUpdate(); for (Scope scope : updated.getScopes()) { if (!scopes.contains(scope)) { PermissionTicketStore permissionStore = cacheSession.getPermissionTicketStore(); List<PermissionTicket> permissions = permissionStore.findByScope(scope.getId(), getResourceServer().getId()); for (PermissionTicket permission : permissions) { permissionStore.delete(permission.getId()); } } } PolicyStore policyStore = cacheSession.getPolicyStore(); for (Scope scope : updated.getScopes()) { if (!scopes.contains(scope)) { policyStore.findByResource(getId(), getResourceServer().getId(), policy -> policy.removeScope(scope)); } } cacheSession.registerResourceInvalidation(cached.getId(), cached.getName(), cached.getType(), cached.getUris(modelSupplier), scopes.stream().map(scope1 -> scope1.getId()).collect(Collectors.toSet()), cached.getResourceServerId(), cached.getOwner()); updated.updateScopes(scopes); }