use of eu.bcvsolutions.idm.core.api.entity.BaseEntity in project CzechIdMng by bcvsolutions.
the class CodeableEvaluator method getPermissions.
@Override
public Set<String> getPermissions(Identifiable entity, AuthorizationPolicy policy) {
Set<String> permissions = super.getPermissions(entity, policy);
if (entity == null || entity.getId() == null) {
return permissions;
}
// check type
if (StringUtils.isEmpty(policy.getAuthorizableType()) || !entity.getClass().getCanonicalName().equals(policy.getAuthorizableType())) {
return permissions;
}
// load entity
BaseEntity lookupEntity = lookupEntity(policy);
if (lookupEntity == null) {
return permissions;
}
//
if (Objects.equal(lookupEntity, entity)) {
// equals by id internally
permissions.addAll(policy.getPermissions());
}
return permissions;
}
use of eu.bcvsolutions.idm.core.api.entity.BaseEntity in project CzechIdMng by bcvsolutions.
the class EntityToUuidConverter method convert.
@Override
public UUID convert(MappingContext<BaseEntity, UUID> context) {
if (context != null && context.getSource() != null && context.getSource().getId() instanceof UUID) {
MappingContext<?, ?> parentContext = context.getParent();
if (parentContext != null && parentContext.getDestination() != null && AbstractDto.class.isAssignableFrom(parentContext.getDestinationType()) && parentContext.getSource() != null && BaseEntity.class.isAssignableFrom(parentContext.getSourceType())) {
try {
AbstractDto parentDto = (AbstractDto) parentContext.getDestination();
BaseEntity entity = (BaseEntity) context.getSource();
Map<String, BaseDto> embedded = parentDto.getEmbedded();
PropertyMapping propertyMapping = (PropertyMapping) context.getMapping();
// Find name of field by property mapping
String field = propertyMapping.getLastDestinationProperty().getName();
// Find field in DTO class
Field fieldTyp = getFirstFieldInClassHierarchy(parentContext.getDestinationType(), field);
if (fieldTyp.isAnnotationPresent(Embedded.class)) {
Embedded embeddedAnnotation = fieldTyp.getAnnotation(Embedded.class);
if (embeddedAnnotation.enabled()) {
// If has field Embedded (enabled) annotation, then
// we will create new
// instance of DTO
//
AbstractDto dto = null;
// If dto class is abstract get dto from lookup
if (Modifier.isAbstract(embeddedAnnotation.dtoClass().getModifiers())) {
dto = (AbstractDto) getLookupService().lookupDto(entity.getClass(), entity.getId());
} else {
dto = embeddedAnnotation.dtoClass().newInstance();
}
dto.setTrimmed(true);
// Separate map entity to new embedded DTO
modeler.map(entity, dto);
embedded.put(field, dto);
// Add filled DTO to embedded map to parent DTO
parentDto.setEmbedded(embedded);
}
}
} catch (InstantiationException | IllegalAccessException | NoSuchFieldException | SecurityException e) {
throw new CoreException(e);
}
}
return (UUID) context.getSource().getId();
}
return null;
}
use of eu.bcvsolutions.idm.core.api.entity.BaseEntity in project CzechIdMng by bcvsolutions.
the class ReadAccountByIdentityEvaluator method getPermissions.
@Override
public Set<String> getPermissions(AccAccount authorizable, AuthorizationPolicy policy) {
Set<String> permissions = super.getPermissions(authorizable, policy);
if (authorizable == null || !securityService.isAuthenticated()) {
return permissions;
}
AccIdentityAccountFilter identityAccountsFilter = new AccIdentityAccountFilter();
identityAccountsFilter.setAccountId(authorizable.getId());
List<AccIdentityAccountDto> identityAccounts = identityAccountService.find(identityAccountsFilter, null).getContent();
identityAccounts.forEach(identityAccount -> {
BaseEntity identity = lookupService.lookupEntity(IdmIdentity.class, identityAccount.getIdentity());
Set<String> identityPermissions = authorizationManager.getPermissions(identity);
if (PermissionUtils.hasPermission(identityPermissions, IdmBasePermission.READ)) {
permissions.add(IdmBasePermission.READ.name());
}
if (PermissionUtils.hasPermission(identityPermissions, IdmBasePermission.AUTOCOMPLETE)) {
permissions.add(IdmBasePermission.AUTOCOMPLETE.name());
}
});
return permissions;
}
use of eu.bcvsolutions.idm.core.api.entity.BaseEntity in project CzechIdMng by bcvsolutions.
the class CodeableEvaluator method lookupEntity.
/**
* Find entity by identifiable object ... this is little strange (we find entity only for adding it to other search)
*
* @param policy
* @return
*/
@SuppressWarnings("unchecked")
private BaseEntity lookupEntity(AuthorizationPolicy policy) {
Object identifier = policy.getEvaluatorProperties().get(PARAMETER_IDENTIFIER);
if (identifier == null || StringUtils.isEmpty(policy.getAuthorizableType())) {
return null;
}
// find entity by identifiable object ... this is little strange (we find entity only for adding it to other search)
BaseEntity entity;
try {
entity = lookupService.lookupEntity((Class<? extends Identifiable>) Class.forName(policy.getAuthorizableType()), identifier.toString());
} catch (ClassNotFoundException ex) {
LOG.warn("Class for name [{}] not found - skipping", policy.getAuthorizableType());
return null;
} catch (IllegalArgumentException ex) {
LOG.warn("Authorizable type [{}] does not support entity lookup - skipping", policy.getAuthorizableType(), ex);
return null;
}
if (entity == null) {
LOG.debug("Entity for type [{}] and code [{}] wasn't found - skipping", policy.getAuthorizableType(), identifier);
return null;
}
return entity;
}
use of eu.bcvsolutions.idm.core.api.entity.BaseEntity in project CzechIdMng by bcvsolutions.
the class UuidToEntityConverter method convert.
@Override
public BaseEntity convert(MappingContext<UUID, BaseEntity> context) {
if (context != null && context.getSource() != null) {
UUID sourceUUID = context.getSource();
Class<BaseEntity> entityClass = context.getDestinationType();
MappingContext<?, ?> parentContext = context.getParent();
PropertyMapping propertyMapping = (PropertyMapping) context.getMapping();
// Find name of field by property mapping
String field = propertyMapping.getLastDestinationProperty().getName();
try {
// Find field in DTO class
Field fieldTyp = getFirstFieldInClassHierarchy(parentContext.getSourceType(), field);
if (fieldTyp.isAnnotationPresent(Embedded.class)) {
Embedded embeddedAnnotation = fieldTyp.getAnnotation(Embedded.class);
if (embeddedAnnotation.enabled()) {
EntityLookup<?> lookup = getLookupService().getEntityLookup(embeddedAnnotation.dtoClass());
if (lookup != null) {
return lookup.lookup(sourceUUID);
}
}
}
} catch (NoSuchFieldException | SecurityException e) {
throw new CoreException(e);
}
// We do not have lookup by embedded annotation. We try load service for entity
EntityLookup<?> lookup = getLookupService().getEntityLookup(entityClass);
if (lookup != null) {
return lookup.lookup(sourceUUID);
}
}
return null;
}
Aggregations