Search in sources :

Example 1 with BaseEntity

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;
}
Also used : BaseEntity(eu.bcvsolutions.idm.core.api.entity.BaseEntity)

Example 2 with BaseEntity

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;
}
Also used : BaseEntity(eu.bcvsolutions.idm.core.api.entity.BaseEntity) BaseDto(eu.bcvsolutions.idm.core.api.dto.BaseDto) Field(java.lang.reflect.Field) CoreException(eu.bcvsolutions.idm.core.api.exception.CoreException) AbstractDto(eu.bcvsolutions.idm.core.api.dto.AbstractDto) PropertyMapping(org.modelmapper.spi.PropertyMapping) Embedded(eu.bcvsolutions.idm.core.api.domain.Embedded) UUID(java.util.UUID)

Example 3 with BaseEntity

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;
}
Also used : AccIdentityAccountFilter(eu.bcvsolutions.idm.acc.dto.filter.AccIdentityAccountFilter) BaseEntity(eu.bcvsolutions.idm.core.api.entity.BaseEntity) AccIdentityAccountDto(eu.bcvsolutions.idm.acc.dto.AccIdentityAccountDto)

Example 4 with BaseEntity

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;
}
Also used : BaseEntity(eu.bcvsolutions.idm.core.api.entity.BaseEntity) Identifiable(eu.bcvsolutions.idm.core.api.domain.Identifiable)

Example 5 with BaseEntity

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;
}
Also used : BaseEntity(eu.bcvsolutions.idm.core.api.entity.BaseEntity) Field(java.lang.reflect.Field) CoreException(eu.bcvsolutions.idm.core.api.exception.CoreException) PropertyMapping(org.modelmapper.spi.PropertyMapping) Embedded(eu.bcvsolutions.idm.core.api.domain.Embedded) UUID(java.util.UUID)

Aggregations

BaseEntity (eu.bcvsolutions.idm.core.api.entity.BaseEntity)6 Embedded (eu.bcvsolutions.idm.core.api.domain.Embedded)2 CoreException (eu.bcvsolutions.idm.core.api.exception.CoreException)2 Field (java.lang.reflect.Field)2 UUID (java.util.UUID)2 PropertyMapping (org.modelmapper.spi.PropertyMapping)2 AccIdentityAccountDto (eu.bcvsolutions.idm.acc.dto.AccIdentityAccountDto)1 AccIdentityAccountFilter (eu.bcvsolutions.idm.acc.dto.filter.AccIdentityAccountFilter)1 IdmAuditDto (eu.bcvsolutions.idm.core.api.audit.dto.IdmAuditDto)1 Identifiable (eu.bcvsolutions.idm.core.api.domain.Identifiable)1 AbstractDto (eu.bcvsolutions.idm.core.api.dto.AbstractDto)1 BaseDto (eu.bcvsolutions.idm.core.api.dto.BaseDto)1 IdmRole (eu.bcvsolutions.idm.core.model.entity.IdmRole)1 AbstractIntegrationTest (eu.bcvsolutions.idm.test.api.AbstractIntegrationTest)1 Test (org.junit.Test)1 Transactional (org.springframework.transaction.annotation.Transactional)1