Search in sources :

Example 6 with BasePermission

use of eu.bcvsolutions.idm.core.security.api.domain.BasePermission in project CzechIdMng by bcvsolutions.

the class PermissionUtils method toPermissions.

/**
 * Method resolve given list of permission constants (e.g. 'IDENTITY_READ').
 * From the list will be parsed (by separator from BasePermission) permission. For IDENTITY_READ
 * will be result READ.
 *
 * BEWARE: if given list contains constant from different group result set will be united.
 * For input list IDENTITY_READ, ROLE_UPDATE, ROLE_READ will be result: READ, UPDATE!
 *
 * @param authorities group (authorities) or base permissions.
 * @return BasePermission list of base permissions
 */
public static Collection<BasePermission> toPermissions(Collection<String> authorities) {
    if (CollectionUtils.isEmpty(authorities)) {
        return Collections.<BasePermission>emptySet();
    }
    Set<String> resolvedPermissions = new HashSet<>(authorities.size());
    Set<BasePermission> result = new HashSet<>(authorities.size());
    for (String authority : authorities) {
        BasePermission permission = toPermission(authority);
        if (permission == null) {
            continue;
        }
        String permissionName = permission.getName();
        if (resolvedPermissions.contains(permissionName)) {
            continue;
        }
        // 
        result.add(permission);
        resolvedPermissions.add(permissionName);
    }
    return result;
}
Also used : IdmBasePermission(eu.bcvsolutions.idm.core.security.api.domain.IdmBasePermission) BasePermission(eu.bcvsolutions.idm.core.security.api.domain.BasePermission) HashSet(java.util.HashSet)

Example 7 with BasePermission

use of eu.bcvsolutions.idm.core.security.api.domain.BasePermission in project CzechIdMng by bcvsolutions.

the class DefaultIdmRequestIdentityRoleService method compileIdentityRolesWithConcepts.

/**
 * Find concepts for given identity-roles. If some exists (in given request),
 * then will be altered for concept metadata (operation, EAVs)
 *
 * @param requestIdentityRoles
 * @param identityRoles
 * @param filter
 * @param permission
 */
private void compileIdentityRolesWithConcepts(List<IdmRequestIdentityRoleDto> requestIdentityRoles, List<IdmIdentityRoleDto> identityRoles, IdmRequestIdentityRoleFilter filter, BasePermission... permission) {
    // Convert identity-roles to Set of IDs.
    Set<UUID> identityRoleIds = identityRoles.stream().map(IdmIdentityRoleDto::getId).collect(Collectors.toSet());
    // Find concepts by identity-roles IDs.
    IdmConceptRoleRequestFilter conceptFilter = new IdmConceptRoleRequestFilter();
    conceptFilter.setIdentityRoleIds(identityRoleIds);
    conceptFilter.setRoleRequestId(filter.getRoleRequestId());
    List<IdmConceptRoleRequestDto> conceptsForThisPage = conceptRoleService.find(conceptFilter, null, permission).getContent();
    // 
    conceptsForThisPage.stream().filter(// 
    concept -> ConceptRoleRequestOperation.ADD != concept.getOperation()).forEach(concept -> {
        // 
        IdmRequestIdentityRoleDto requestIdentityRoleWithConcept = // 
        requestIdentityRoles.stream().filter(requestIdentityRole -> requestIdentityRole.getIdentityRole() != null && requestIdentityRole.getIdentityRole().equals(concept.getIdentityRole()) && requestIdentityRole.getId().equals(requestIdentityRole.getIdentityRole())).findFirst().orElse(// 
        null);
        if (requestIdentityRoleWithConcept != null) {
            requestIdentityRoleWithConcept.setOperation(concept.getOperation());
            requestIdentityRoleWithConcept.setId(concept.getId());
            requestIdentityRoleWithConcept.setValidFrom(concept.getValidFrom());
            requestIdentityRoleWithConcept.setValidTill(concept.getValidTill());
            requestIdentityRoleWithConcept.setRoleRequest(concept.getRoleRequest());
            IdmFormInstanceDto formInstanceDto;
            // For updated identity-role replace EAVs from the concept
            if (ConceptRoleRequestOperation.UPDATE == concept.getOperation()) {
                // Check on change of values is made only on ended request! 'Original' value is current value and in audit it was confusing (only 'new' value is show now).
                formInstanceDto = conceptRoleService.getRoleAttributeValues(concept, !concept.getState().isTerminatedState());
                this.addEav(requestIdentityRoleWithConcept, formInstanceDto);
            }
        }
    });
}
Also used : IdmConceptRoleRequestFilter(eu.bcvsolutions.idm.core.api.dto.filter.IdmConceptRoleRequestFilter) IdmConceptRoleRequestService(eu.bcvsolutions.idm.core.api.service.IdmConceptRoleRequestService) DtoUtils(eu.bcvsolutions.idm.core.api.utils.DtoUtils) IdmRequestIdentityRoleDto(eu.bcvsolutions.idm.core.api.dto.IdmRequestIdentityRoleDto) IdmIdentityRoleDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityRoleDto) AbstractReadDtoService(eu.bcvsolutions.idm.core.api.service.AbstractReadDtoService) Autowired(org.springframework.beans.factory.annotation.Autowired) IdmRoleRequestService(eu.bcvsolutions.idm.core.api.service.IdmRoleRequestService) RoleBasePermission(eu.bcvsolutions.idm.core.security.api.domain.RoleBasePermission) IdmRequestIdentityRoleService(eu.bcvsolutions.idm.core.api.service.IdmRequestIdentityRoleService) IdmConceptRoleRequest_(eu.bcvsolutions.idm.core.model.entity.IdmConceptRoleRequest_) AbstractBaseDtoService(eu.bcvsolutions.idm.core.rest.AbstractBaseDtoService) BasePermission(eu.bcvsolutions.idm.core.security.api.domain.BasePermission) Pageable(org.springframework.data.domain.Pageable) IdmIdentityContractService(eu.bcvsolutions.idm.core.api.service.IdmIdentityContractService) IdmRoleRequestDto(eu.bcvsolutions.idm.core.api.dto.IdmRoleRequestDto) Set(java.util.Set) PageRequest(org.springframework.data.domain.PageRequest) UUID(java.util.UUID) Page(org.springframework.data.domain.Page) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) List(java.util.List) ContractBasePermission(eu.bcvsolutions.idm.core.security.api.domain.ContractBasePermission) PermissionUtils(eu.bcvsolutions.idm.core.security.api.utils.PermissionUtils) LocalDate(java.time.LocalDate) PageImpl(org.springframework.data.domain.PageImpl) IdmIdentityRole_(eu.bcvsolutions.idm.core.model.entity.IdmIdentityRole_) RoleRequestedByType(eu.bcvsolutions.idm.core.api.domain.RoleRequestedByType) IdmRoleSystemService(eu.bcvsolutions.idm.core.api.service.IdmRoleSystemService) MessageFormat(java.text.MessageFormat) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ModelMapper(org.modelmapper.ModelMapper) Lists(com.google.common.collect.Lists) LookupService(eu.bcvsolutions.idm.core.api.service.LookupService) Service(org.springframework.stereotype.Service) IdmConceptRoleRequestDto(eu.bcvsolutions.idm.core.api.dto.IdmConceptRoleRequestDto) WorkflowProcessInstanceService(eu.bcvsolutions.idm.core.workflow.service.WorkflowProcessInstanceService) IdmFormInstanceDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormInstanceDto) IdmIdentityContractDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityContractDto) IdmIdentityRoleService(eu.bcvsolutions.idm.core.api.service.IdmIdentityRoleService) IdmIdentityRoleFilter(eu.bcvsolutions.idm.core.api.dto.filter.IdmIdentityRoleFilter) IdmRoleService(eu.bcvsolutions.idm.core.api.service.IdmRoleService) IdmRoleSystemFilter(eu.bcvsolutions.idm.core.api.dto.filter.IdmRoleSystemFilter) BaseFilter(eu.bcvsolutions.idm.core.api.dto.filter.BaseFilter) IdmRoleDto(eu.bcvsolutions.idm.core.api.dto.IdmRoleDto) IdmRequestIdentityRoleFilter(eu.bcvsolutions.idm.core.api.dto.filter.IdmRequestIdentityRoleFilter) ConceptRoleRequestOperation(eu.bcvsolutions.idm.core.api.domain.ConceptRoleRequestOperation) IdmConceptRoleRequestFilter(eu.bcvsolutions.idm.core.api.dto.filter.IdmConceptRoleRequestFilter) Transactional(org.springframework.transaction.annotation.Transactional) Assert(org.springframework.util.Assert) InvalidFormAttributeDto(eu.bcvsolutions.idm.core.eav.api.dto.InvalidFormAttributeDto) IdmFormInstanceDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormInstanceDto) IdmRequestIdentityRoleDto(eu.bcvsolutions.idm.core.api.dto.IdmRequestIdentityRoleDto) IdmConceptRoleRequestDto(eu.bcvsolutions.idm.core.api.dto.IdmConceptRoleRequestDto) UUID(java.util.UUID)

Example 8 with BasePermission

use of eu.bcvsolutions.idm.core.security.api.domain.BasePermission in project CzechIdMng by bcvsolutions.

the class DefaultIdmConceptRoleRequestService method checkAccess.

@Override
public IdmConceptRoleRequest checkAccess(IdmConceptRoleRequest entity, BasePermission... permission) {
    if (entity == null) {
        // nothing to check
        return null;
    }
    if (ObjectUtils.isEmpty(permission)) {
        return entity;
    }
    // We can delete the concept if we have UPDATE permission on request
    Set<BasePermission> permissionsForRequest = Sets.newHashSet();
    for (BasePermission p : permission) {
        if (p.equals(IdmBasePermission.DELETE)) {
            permissionsForRequest.add(IdmBasePermission.UPDATE);
        } else {
            permissionsForRequest.add(p);
        }
    }
    // We have rights on the concept, when we have rights on whole request
    if (getAuthorizationManager().evaluate(entity.getRoleRequest(), permissionsForRequest.toArray(new BasePermission[0]))) {
        return entity;
    }
    // We have rights on the concept, when we have rights on workflow process using in the concept.
    // Beware, concet can use different WF process than whole request. So we need to check directly process on concept!
    String processId = entity.getWfProcessId();
    if (!Strings.isNullOrEmpty(processId)) {
        WorkflowProcessInstanceDto processInstance = workflowProcessInstanceService.get(processId, true);
        if (processInstance != null) {
            return entity;
        }
        if (processInstance == null) {
            // Ok process was not returned, but we need to check historic process (on involved user) too.
            WorkflowHistoricProcessInstanceDto historicProcess = historicProcessService.get(processId);
            if (historicProcess != null) {
                return entity;
            }
        }
    }
    throw new ForbiddenEntityException((BaseEntity) entity, permission);
}
Also used : BasePermission(eu.bcvsolutions.idm.core.security.api.domain.BasePermission) IdmBasePermission(eu.bcvsolutions.idm.core.security.api.domain.IdmBasePermission) WorkflowProcessInstanceDto(eu.bcvsolutions.idm.core.workflow.model.dto.WorkflowProcessInstanceDto) WorkflowHistoricProcessInstanceDto(eu.bcvsolutions.idm.core.workflow.model.dto.WorkflowHistoricProcessInstanceDto) ForbiddenEntityException(eu.bcvsolutions.idm.core.api.exception.ForbiddenEntityException)

Example 9 with BasePermission

use of eu.bcvsolutions.idm.core.security.api.domain.BasePermission in project CzechIdMng by bcvsolutions.

the class AbstractReadDtoService method findEntities.

protected Page<E> findEntities(F filter, Pageable pageable, BasePermission... permission) {
    // transform filter to criteria
    Specification<E> criteria = new Specification<E>() {

        public Predicate toPredicate(Root<E> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
            List<Predicate> predicates = new ArrayList<>();
            // if filter is null, no filter predicates will be built
            if (filter != null) {
                predicates.addAll(AbstractReadDtoService.this.toPredicates(root, query, builder, filter));
            }
            // 
            // permisions are not evaluated, if no permission was given or authorizable type is null (=> authorization policies are not supported)
            BasePermission[] permissions = PermissionUtils.trimNull(permission);
            if (!ObjectUtils.isEmpty(permissions) && (AbstractReadDtoService.this instanceof AuthorizableService)) {
                AuthorizableType authorizableType = ((AuthorizableService<?>) AbstractReadDtoService.this).getAuthorizableType();
                if (authorizableType != null && authorizableType.getType() != null) {
                    predicates.add(getAuthorizationManager().getPredicate(root, query, builder, permissions));
                }
            }
            // 
            return query.where(predicates.toArray(new Predicate[predicates.size()])).getRestriction();
        }
    };
    return getRepository().findAll(criteria, pageable);
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Root(javax.persistence.criteria.Root) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) ArrayList(java.util.ArrayList) Specification(org.springframework.data.jpa.domain.Specification) AuthorizableType(eu.bcvsolutions.idm.core.security.api.dto.AuthorizableType) Predicate(javax.persistence.criteria.Predicate) AuthorizableService(eu.bcvsolutions.idm.core.security.api.service.AuthorizableService) BasePermission(eu.bcvsolutions.idm.core.security.api.domain.BasePermission)

Example 10 with BasePermission

use of eu.bcvsolutions.idm.core.security.api.domain.BasePermission in project CzechIdMng by bcvsolutions.

the class PermissionUtils method toPermission.

/**
 * Method resolve given permission constants (e.g. 'IDENTITY_READ').
 * Value will be parsed (by separator from BasePermission) permission. For 'IDENTITY_READ'
 * will be result 'READ'.
 *
 * @param authority group (authority) or base permission.
 * @return base permission
 * @since 10.3.0
 */
public static BasePermission toPermission(String authority) {
    if (StringUtils.isEmpty(authority)) {
        return null;
    }
    // 
    if (authority.contains(BasePermission.SEPARATOR)) {
        String[] split = authority.split(BasePermission.SEPARATOR);
        // permission is on last place
        authority = split[split.length - 1];
    }
    // 
    final String rawPermission = authority;
    // Base permission may be child from IdmBasePermission
    BasePermission permission = EnumUtils.getEnum(IdmBasePermission.class, rawPermission);
    // but can be registered dynamically in custom module => new BasePermission is created
    if (permission == null) {
        permission = (BasePermission) () -> rawPermission;
    }
    // 
    return permission;
}
Also used : IdmBasePermission(eu.bcvsolutions.idm.core.security.api.domain.IdmBasePermission) BasePermission(eu.bcvsolutions.idm.core.security.api.domain.BasePermission)

Aggregations

BasePermission (eu.bcvsolutions.idm.core.security.api.domain.BasePermission)18 IdmBasePermission (eu.bcvsolutions.idm.core.security.api.domain.IdmBasePermission)10 ArrayList (java.util.ArrayList)10 UUID (java.util.UUID)10 List (java.util.List)9 Autowired (org.springframework.beans.factory.annotation.Autowired)8 Lists (com.google.common.collect.Lists)7 HashSet (java.util.HashSet)7 Transactional (org.springframework.transaction.annotation.Transactional)7 DtoUtils (eu.bcvsolutions.idm.core.api.utils.DtoUtils)6 Serializable (java.io.Serializable)6 Collectors (java.util.stream.Collectors)6 IdmIdentityRoleDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityRoleDto)5 IdmIdentityRoleFilter (eu.bcvsolutions.idm.core.api.dto.filter.IdmIdentityRoleFilter)5 IdmIdentityRoleService (eu.bcvsolutions.idm.core.api.service.IdmIdentityRoleService)5 AuthorizableType (eu.bcvsolutions.idm.core.security.api.dto.AuthorizableType)5 HashMap (java.util.HashMap)5 Set (java.util.Set)5 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)5 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)5