use of eu.bcvsolutions.idm.core.api.config.cache.domain.ValueWrapper in project CzechIdMng by bcvsolutions.
the class DefaultAuthorizationManager method getEnabledDistinctPolicies.
/**
* Cache decorator - get or load current identity authorization policies.
* Distinct policies are returned only
*
* @param identityId
* @param entityType
* @return
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
protected List<IdmAuthorizationPolicyDto> getEnabledDistinctPolicies(UUID identityId, Class<? extends Identifiable> entityType) {
if (identityId == null) {
// TODO: support setting policies to not logged user - e.g. public endpoints.
return Lists.newArrayList();
}
Assert.notNull(entityType, "Entity type is required.");
//
// try to get cached policies
Map<Class<? extends Identifiable>, List<UUID>> cachedPolicies;
ValueWrapper value = cacheManager.getValue(AUTHORIZATION_POLICY_CACHE_NAME, identityId);
if (value != null) {
// cache value is never null - create copy
cachedPolicies = new HashMap<>((Map) value.get());
} else {
cachedPolicies = new HashMap<>();
}
if (cachedPolicies.containsKey(entityType)) {
// cache contains policy identifiers only -> get policy dto
return cachedPolicies.get(entityType).stream().map(policyId -> getAuthorizationPolicy(policyId)).collect(Collectors.toList());
}
// distinct policies
List<IdmAuthorizationPolicyDto> enabledDistinctPolicies = new ArrayList<>();
// load policies
service.getEnabledPolicies(identityId, entityType).stream().filter(// TODO: compatibility issues - agendas without authorization support
p -> supportsEntityType(p, entityType)).forEach(policy -> {
boolean contains = false;
for (IdmAuthorizationPolicyDto registeredPolicy : enabledDistinctPolicies) {
if (isDuplicate(policy, registeredPolicy)) {
// policy with the same configuration is already registered
contains = true;
break;
}
}
// register policy
if (!contains) {
enabledDistinctPolicies.add(policy);
}
// cache all policies (even duplicates to prevent select)
cacheManager.cacheValue(AUTHORIZATION_POLICY_DEFINITION_CACHE_NAME, policy.getId(), policy);
});
// cache policies as uuid
cachedPolicies.put(entityType, // dto => uuid
enabledDistinctPolicies.stream().map(AbstractDto::getId).collect(Collectors.toList()));
cacheManager.cacheValue(AUTHORIZATION_POLICY_CACHE_NAME, identityId, cachedPolicies);
//
return enabledDistinctPolicies;
}
use of eu.bcvsolutions.idm.core.api.config.cache.domain.ValueWrapper in project CzechIdMng by bcvsolutions.
the class DefaultAuthorizationManager method getAuthorizationPolicy.
/**
* Get autorization policy with cache usage.
*
* @param policyId policy identifier
* @return policy dto
* @since 10.7.0
*/
private IdmAuthorizationPolicyDto getAuthorizationPolicy(UUID policyId) {
ValueWrapper value = cacheManager.getValue(AUTHORIZATION_POLICY_DEFINITION_CACHE_NAME, policyId);
if (value != null) {
// cache value can be null
return (IdmAuthorizationPolicyDto) value.get();
}
// load + cache (not exist ~ null policy can be cached to to prevent future select)
IdmAuthorizationPolicyDto policy = service.get(policyId);
cacheManager.cacheValue(AUTHORIZATION_POLICY_DEFINITION_CACHE_NAME, policyId, policy);
//
return policy;
}
use of eu.bcvsolutions.idm.core.api.config.cache.domain.ValueWrapper in project CzechIdMng by bcvsolutions.
the class DefaultTokenManager method getToken.
@Override
public IdmTokenDto getToken(UUID tokenId, BasePermission... permission) {
ValueWrapper value = cacheManager.getValue(TOKEN_CACHE_NAME, tokenId);
if (value != null) {
return (IdmTokenDto) value.get();
}
//
IdmTokenDto token = tokenService.get(tokenId, permission);
cacheManager.cacheValue(TOKEN_CACHE_NAME, tokenId, token);
//
return token;
}
use of eu.bcvsolutions.idm.core.api.config.cache.domain.ValueWrapper in project CzechIdMng by bcvsolutions.
the class DefaultAuthorizationManagerIntegrationTest method testCacheAfterContractIsChanged.
@Test
@Transactional
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testCacheAfterContractIsChanged() {
// create and login identity
IdmIdentityDto identity = getHelper().createIdentity();
UUID mockIdentity = UUID.randomUUID();
// prepare role
IdmRoleDto role = getHelper().createRole();
getHelper().createBasePolicy(role.getId(), IdmBasePermission.AUTOCOMPLETE, IdmBasePermission.READ);
getHelper().createIdentityRole(identity, role);
//
Assert.assertNull(cacheManager.getValue(AuthorizationManager.AUTHORIZATION_POLICY_CACHE_NAME, identity.getId()));
Assert.assertNull(cacheManager.getValue(AuthorizationManager.PERMISSION_CACHE_NAME, identity.getId()));
Assert.assertNull(cacheManager.getValue(AuthorizationManager.AUTHORIZATION_POLICY_CACHE_NAME, mockIdentity));
Assert.assertNull(cacheManager.getValue(AuthorizationManager.PERMISSION_CACHE_NAME, mockIdentity));
//
cacheManager.cacheValue(AuthorizationManager.AUTHORIZATION_POLICY_CACHE_NAME, mockIdentity, new HashMap<>());
cacheManager.cacheValue(AuthorizationManager.PERMISSION_CACHE_NAME, mockIdentity, new HashMap<>());
Assert.assertNotNull(cacheManager.getValue(AuthorizationManager.AUTHORIZATION_POLICY_CACHE_NAME, mockIdentity));
Assert.assertNotNull(cacheManager.getValue(AuthorizationManager.PERMISSION_CACHE_NAME, mockIdentity));
//
// without login
Set<String> permissions = manager.getPermissions(role);
Assert.assertTrue(permissions.isEmpty());
//
try {
getHelper().login(identity);
//
// new entity is not supported with cache, but permissions are evaluated
permissions = manager.getPermissions(new IdmRoleDto());
Assert.assertEquals(2, permissions.size());
Assert.assertTrue(permissions.stream().anyMatch(p -> p.equals(IdmBasePermission.AUTOCOMPLETE.getName())));
Assert.assertTrue(permissions.stream().anyMatch(p -> p.equals(IdmBasePermission.READ.getName())));
Assert.assertNull(cacheManager.getValue(AuthorizationManager.PERMISSION_CACHE_NAME, identity.getId()));
//
// load from db
permissions = manager.getPermissions(role);
Assert.assertEquals(2, permissions.size());
Assert.assertTrue(permissions.stream().anyMatch(p -> p.equals(IdmBasePermission.AUTOCOMPLETE.getName())));
Assert.assertTrue(permissions.stream().anyMatch(p -> p.equals(IdmBasePermission.READ.getName())));
Assert.assertNotNull(cacheManager.getValue(AuthorizationManager.AUTHORIZATION_POLICY_CACHE_NAME, identity.getId()));
Assert.assertNotNull(cacheManager.getValue(AuthorizationManager.PERMISSION_CACHE_NAME, identity.getId()));
// load from cache
permissions = manager.getPermissions(role);
Assert.assertEquals(2, permissions.size());
Assert.assertTrue(permissions.stream().anyMatch(p -> p.equals(IdmBasePermission.AUTOCOMPLETE.getName())));
Assert.assertTrue(permissions.stream().anyMatch(p -> p.equals(IdmBasePermission.READ.getName())));
Assert.assertNotNull(cacheManager.getValue(AuthorizationManager.AUTHORIZATION_POLICY_CACHE_NAME, identity.getId()));
Assert.assertNotNull(cacheManager.getValue(AuthorizationManager.PERMISSION_CACHE_NAME, identity.getId()));
// check cache content - one
ValueWrapper cacheValue = cacheManager.getValue(AuthorizationManager.AUTHORIZATION_POLICY_CACHE_NAME, identity.getId());
List<UUID> cachedPolicies = (List) ((Map) cacheValue.get()).get(role.getClass());
Assert.assertEquals(1, cachedPolicies.size());
Assert.assertEquals(BasePermissionEvaluator.class.getCanonicalName(), ((IdmAuthorizationPolicyDto) cacheManager.getValue(AuthorizationManager.AUTHORIZATION_POLICY_DEFINITION_CACHE_NAME, cachedPolicies.get(0)).get()).getEvaluatorType());
cacheValue = cacheManager.getValue(AuthorizationManager.PERMISSION_CACHE_NAME, identity.getId());
permissions = (Set) ((Map) cacheValue.get()).get(role.getId());
Assert.assertEquals(2, permissions.size());
Assert.assertTrue(permissions.stream().anyMatch(p -> p.equals(IdmBasePermission.AUTOCOMPLETE.getName())));
Assert.assertTrue(permissions.stream().anyMatch(p -> p.equals(IdmBasePermission.READ.getName())));
//
// change contract => evict cache of logged identity
getHelper().createContract(identity);
//
// check cache is evicted only for logged identity
Assert.assertNotNull(cacheManager.getValue(AuthorizationManager.AUTHORIZATION_POLICY_CACHE_NAME, identity.getId()));
Assert.assertNull(cacheManager.getValue(AuthorizationManager.PERMISSION_CACHE_NAME, identity.getId()));
Assert.assertNotNull(cacheManager.getValue(AuthorizationManager.AUTHORIZATION_POLICY_CACHE_NAME, mockIdentity));
Assert.assertNotNull(cacheManager.getValue(AuthorizationManager.PERMISSION_CACHE_NAME, mockIdentity));
} finally {
logout();
}
}
use of eu.bcvsolutions.idm.core.api.config.cache.domain.ValueWrapper in project CzechIdMng by bcvsolutions.
the class DefaultEntityEventManager method deregisterAsynchronousTask.
@Override
public boolean deregisterAsynchronousTask(LongRunningTaskExecutor<?> executor) {
if (!isAsynchronous()) {
return true;
}
//
if (notifiedLrts.containsKey(executor.getLongRunningTaskId())) {
notifiedLrts.remove(executor.getLongRunningTaskId());
return false;
}
//
//
UUID transactionId = TransactionContextHolder.getContext().getTransactionId();
ValueWrapper value = cacheManager.getValue(TRANSACTION_EVENT_CACHE_NAME, transactionId);
if (value == null) {
LOG.debug("Transaction id [{}] was processed already (synchronously or complete).", transactionId);
lrts.remove(transactionId);
return true;
}
//
return false;
}
Aggregations