use of eu.bcvsolutions.idm.core.api.config.cache.domain.ValueWrapper in project CzechIdMng by bcvsolutions.
the class DefaultEntityEventManager method addEventCache.
/**
* Include event in transaction processing.
*
* @param eventId
* @param transactionId
*/
@SuppressWarnings("unchecked")
private void addEventCache(UUID eventId, UUID transactionId) {
Assert.notNull(eventId, "Event has to be asynchronous (~persisted).");
LOG.trace("Add event [{}] into cache under transaction [{}].", eventId, transactionId);
//
lock.lock();
try {
if (transactionId == null) {
return;
}
//
ValueWrapper value = cacheManager.getValue(TRANSACTION_EVENT_CACHE_NAME, transactionId);
//
Set<UUID> events = null;
if (value == null) {
events = new HashSet<>();
} else {
events = (Set<UUID>) value.get();
}
events.add(eventId);
cacheManager.cacheValue(TRANSACTION_EVENT_CACHE_NAME, transactionId, events);
} finally {
lock.unlock();
}
}
use of eu.bcvsolutions.idm.core.api.config.cache.domain.ValueWrapper in project CzechIdMng by bcvsolutions.
the class DefaultConfigurationService method getValue.
@Override
@Transactional(readOnly = true)
public String getValue(String key, String defaultValue) {
ValueWrapper cachedValue = getCachedValue(key);
if (cachedValue != null) {
return (String) cachedValue.get();
}
//
LOG.debug("Reading configuration for key [{}]", key);
String value = null;
boolean confidential = true;
// IdM configuration has higher priority than property file.
IdmConfigurationDto config = getByCode(key);
if (config != null) {
if (config.isConfidential()) {
value = confidentialStorage.get(config.getId(), getEntityClass(), CONFIDENTIAL_PROPERTY_VALUE, String.class);
LOG.debug("Configuration value for key [{}] was found in confidential storage", config.getName());
} else {
value = config.getValue();
confidential = false;
LOG.trace("Configuration value for key [{}] was found in database.", key);
}
} else if (env != null) {
// try to find value in property configuration
value = env.getProperty(key);
confidential = GuardedString.shouldBeGuarded(key);
}
// fill default value
if (value == null) {
// TODO: null vs. isEmpty?
value = defaultValue;
}
LOG.debug("Resolved configuration value for key [{}] is [{}].", key, confidential ? GuardedString.SECRED_PROXY_STRING : value);
setCachedValue(key, value);
return value;
}
use of eu.bcvsolutions.idm.core.api.config.cache.domain.ValueWrapper in project CzechIdMng by bcvsolutions.
the class DefaultIdmRoleCompositionService method findAllSubRoles.
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public List<IdmRoleCompositionDto> findAllSubRoles(UUID superiorId, BasePermission... permission) {
Assert.notNull(superiorId, "Superior role identifier is required.");
//
ValueWrapper value = cacheManager.getValue(ALL_SUB_ROLES_CACHE_NAME, superiorId);
if (value != null) {
// never null
return (List) value.get();
}
//
List<IdmRoleCompositionDto> results = new ArrayList<>();
findAllSubRoles(results, new ArrayList<>(), superiorId, permission);
cacheManager.cacheValue(ALL_SUB_ROLES_CACHE_NAME, superiorId, results);
//
return results;
}
use of eu.bcvsolutions.idm.core.api.config.cache.domain.ValueWrapper in project CzechIdMng by bcvsolutions.
the class DefaultAuthorizationManagerIntegrationTest method testCache.
@Test
@Transactional
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testCache() {
// create and login identity
IdmIdentityDto identity = getHelper().createIdentity();
UUID mockIdentity = UUID.randomUUID();
// prepare role
IdmRoleDto role = getHelper().createRole();
IdmAuthorizationPolicyDto policy = getHelper().createBasePolicy(role.getId(), IdmBasePermission.AUTOCOMPLETE, IdmBasePermission.READ);
getHelper().createIdentityRole(identity, role);
//
Assert.assertNull(cacheManager.getValue(AuthorizationManager.AUTHORIZATION_POLICY_DEFINITION_CACHE_NAME, identity.getId()));
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_DEFINITION_CACHE_NAME, policy.getId()));
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<IdmAuthorizationPolicyDto> 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 policy => evict whole cache
policy.setPermissions(IdmBasePermission.AUTOCOMPLETE, IdmBasePermission.READ, IdmBasePermission.UPDATE);
authorizationPolicyService.save(policy);
Assert.assertNull(cacheManager.getValue(AuthorizationManager.AUTHORIZATION_POLICY_DEFINITION_CACHE_NAME, policy.getId()));
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<>());
permissions = manager.getPermissions(role);
Assert.assertEquals(3, 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.assertTrue(permissions.stream().anyMatch(p -> p.equals(IdmBasePermission.UPDATE.getName())));
Assert.assertNotNull(cacheManager.getValue(AuthorizationManager.AUTHORIZATION_POLICY_CACHE_NAME, identity.getId()));
Assert.assertNotNull(cacheManager.getValue(AuthorizationManager.PERMISSION_CACHE_NAME, identity.getId()));
} finally {
// evict logged identity cache only
logout();
}
// check cache is evicted only for logged identity
Assert.assertNull(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));
}
use of eu.bcvsolutions.idm.core.api.config.cache.domain.ValueWrapper in project CzechIdMng by bcvsolutions.
the class DefaultFormService method getCachedDefinition.
private IdmFormDefinitionDto getCachedDefinition(UUID definitionId) {
String cacheKey = getCacheKey(definitionId);
ValueWrapper value = cacheManager.getValue(FORM_DEFINITION_CACHE_NAME, cacheKey);
if (value != null) {
// never null
return ((FormDefinitionCache) value.get()).getById(definitionId);
}
//
IdmFormDefinitionDto definition = formDefinitionService.get(definitionId);
if (definition == null) {
// definition not found => not cached
return null;
}
FormDefinitionCache cachedDefinitions = new FormDefinitionCache();
cachedDefinitions.putDefinition(definition);
cacheManager.cacheValue(FORM_DEFINITION_CACHE_NAME, cacheKey, cachedDefinitions);
//
return cachedDefinitions.getById(definitionId);
}
Aggregations