void remove(Policy policy) { DroolsPolicy holder = this.containers.remove(policy.getId()); if (holder != null) { holder.dispose(); } }
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || !(o instanceof Policy)) return false; Policy that = (Policy) o; return that.getId().equals(getId()); }
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || !(o instanceof Policy)) return false; Policy that = (Policy) o; return that.getId().equals(getId()); }
private void verifyCircularReference(Policy policy, List<String> ids) { if (!policy.getType().equals("aggregate")) { return; } if (ids.contains(policy.getId())) { throw new RuntimeException("Circular reference found [" + policy.getName() + "]."); } ids.add(policy.getId()); for (Policy associated : policy.getAssociatedPolicies()) { verifyCircularReference(associated, ids); } }
@Override public List<Policy> findByScopeIds(List<String> scopeIds, String resourceServerId) { if (scopeIds == null) return null; Set<Policy> result = new HashSet<>(); for (String id : scopeIds) { String cacheKey = getPolicyByScope(id, resourceServerId); result.addAll(cacheQuery(cacheKey, PolicyScopeListQuery.class, () -> getPolicyStoreDelegate().findByScopeIds(Arrays.asList(id), resourceServerId), (revision, resources) -> new PolicyScopeListQuery(revision, cacheKey, id, resources.stream().map(resource -> resource.getId()).collect(Collectors.toSet()), resourceServerId), resourceServerId, null)); } return new ArrayList<>(result); }
@Override public List<Policy> findByScopeIds(List<String> scopeIds, String resourceId, String resourceServerId) { if (scopeIds == null) return null; Set<Policy> result = new HashSet<>(); for (String id : scopeIds) { String cacheKey = getPolicyByResourceScope(id, resourceId, resourceServerId); result.addAll(cacheQuery(cacheKey, PolicyScopeListQuery.class, () -> getPolicyStoreDelegate().findByScopeIds(Arrays.asList(id), resourceId, resourceServerId), (revision, resources) -> new PolicyScopeListQuery(revision, cacheKey, id, resources.stream().map(resource -> resource.getId()).collect(Collectors.toSet()), resourceServerId), resourceServerId, null)); } return new ArrayList<>(result); }
private void updatePolicy(Policy policy, String code) { scriptCache.remove(policy.getId()); policy.putConfig("code", code); } }
@Override public void setPolicy(Policy policy) { if (policy != null) { entity.setPolicy(entityManager.getReference(PolicyEntity.class, policy.getId())); } }
public static PolicyEntity toEntity(EntityManager em, Policy policy) { if (policy instanceof PolicyAdapter) { return ((PolicyAdapter)policy).getEntity(); } else { return em.getReference(PolicyEntity.class, policy.getId()); } }
@Override public void findByScopeIds(List<String> scopeIds, String resourceId, String resourceServerId, Consumer<Policy> consumer) { for (String id : scopeIds) { String cacheKey = getPolicyByResourceScope(id, resourceId, resourceServerId); cacheQuery(cacheKey, PolicyScopeListQuery.class, () -> getPolicyStoreDelegate().findByScopeIds(Arrays.asList(id), resourceId, resourceServerId), (revision, resources) -> new PolicyScopeListQuery(revision, cacheKey, id, resources.stream().map(resource -> resource.getId()).collect(Collectors.toSet()), resourceServerId), resourceServerId, consumer); } }
@Override public List<Policy> findByResource(String resourceId, String resourceServerId) { String cacheKey = getPolicyByResource(resourceId, resourceServerId); return cacheQuery(cacheKey, PolicyResourceListQuery.class, () -> getPolicyStoreDelegate().findByResource(resourceId, resourceServerId), (revision, policies) -> new PolicyResourceListQuery(revision, cacheKey, resourceId, policies.stream().map(policy -> policy.getId()).collect(Collectors.toSet()), resourceServerId), resourceServerId, null); }
@Override public void findByResourceType(String resourceType, String resourceServerId, Consumer<Policy> consumer) { String cacheKey = getPolicyByResourceType(resourceType, resourceServerId); cacheQuery(cacheKey, PolicyResourceListQuery.class, () -> getPolicyStoreDelegate().findByResourceType(resourceType, resourceServerId), (revision, policies) -> new PolicyResourceListQuery(revision, cacheKey, resourceType, policies.stream().map(policy -> policy.getId()).collect(Collectors.toSet()), resourceServerId), resourceServerId, consumer); }
@Override public void findByResource(String resourceId, String resourceServerId, Consumer<Policy> consumer) { String cacheKey = getPolicyByResource(resourceId, resourceServerId); cacheQuery(cacheKey, PolicyResourceListQuery.class, () -> getPolicyStoreDelegate().findByResource(resourceId, resourceServerId), (revision, policies) -> new PolicyResourceListQuery(revision, cacheKey, resourceId, policies.stream().map(policy -> policy.getId()).collect(Collectors.toSet()), resourceServerId), resourceServerId, consumer); }
@Override public List<Policy> findByResourceType(String resourceType, String resourceServerId) { String cacheKey = getPolicyByResourceType(resourceType, resourceServerId); return cacheQuery(cacheKey, PolicyResourceListQuery.class, () -> getPolicyStoreDelegate().findByResourceType(resourceType, resourceServerId), (revision, policies) -> new PolicyResourceListQuery(revision, cacheKey, resourceType, policies.stream().map(policy -> policy.getId()).collect(Collectors.toSet()), resourceServerId), resourceServerId, null); }
@Override public void addAssociatedPolicy(Policy associatedPolicy) { getMongoEntity().addAssociatedPolicy(associatedPolicy.getId()); updateMongoEntity(); }
@Override public void removeAssociatedPolicy(Policy associatedPolicy) { getMongoEntity().removeAssociatedPolicy(associatedPolicy.getId()); updateMongoEntity(); }
@Override public Policy create(AbstractPolicyRepresentation representation, ResourceServer resourceServer) { Policy policy = getPolicyStoreDelegate().create(representation, resourceServer); Policy cached = findById(policy.getId(), resourceServer.getId()); registerPolicyInvalidation(policy.getId(), representation.getName(), representation.getResources(), representation.getScopes(), null, resourceServer.getId()); if (cached == null) { cached = findById(policy.getId(), resourceServer.getId()); } return cached; }
void cachePolicy(Policy model) { String id = model.getId(); if (cache.getCache().containsKey(id)) { return; } if (!modelMightExist(id)) { return; } if (invalidations.contains(id)) return; cache.addRevisioned(createCachedPolicy(model, id), startupRevision); }
@Override public void onRemove(Policy policy, AuthorizationProvider authorization) { PolicyStore policyStore = authorization.getStoreFactory().getPolicyStore(); for (Policy associatedPolicy : policy.getAssociatedPolicies()) { policyStore.delete(associatedPolicy.getId()); } }
private EvaluatableScriptAdapter getEvaluatableScript(final AuthorizationProvider authz, final Policy policy) { return scriptCache.computeIfAbsent(policy.getId(), id -> { final ScriptingProvider scripting = authz.getKeycloakSession().getProvider(ScriptingProvider.class); ScriptModel script = getScriptModel(policy, authz.getRealm(), scripting); return scripting.prepareEvaluatableScript(script); }); }