예제 #1
0
        public static IEnumerable <IEntitySubdivisionForUserPermissionValidationResult> Validate(IUnitOfWork uow, int userId, Type[] entityTypes)
        {
            var result = new List <EntitySubdivisionForUserPermissionValidationResult>();

            string[]    entityNames     = entityTypes.Select(x => x.Name).ToArray();
            var         employee        = _employeeRepository.GetEmployeesForUser(uow, userId).FirstOrDefault();
            Subdivision mainSubdivision = employee?.Subdivision;

            if (mainSubdivision != null)
            {
                var mainTypesName      = mainSubdivision.DocumentTypes.Select(x => x.Type);
                var mainAvailableTypes = entityTypes.Where(x => mainTypesName.Contains(x.Name));
                if (mainAvailableTypes.Any())
                {
                    EntitySubdivisionForUserPermissionValidationResult mainResultItem = new EntitySubdivisionForUserPermissionValidationResult(mainSubdivision, true);
                    foreach (var mainAvailableType in mainAvailableTypes)
                    {
                        var mainPermission = ServicesConfig.CommonServices.PermissionService.ValidateUserPermission(mainAvailableType, userId);
                        mainResultItem.AddPermission(
                            mainAvailableType,
                            new EntityPermission(
                                mainPermission.CanCreate,
                                mainPermission.CanRead,
                                mainPermission.CanUpdate,
                                mainPermission.CanDelete
                                )
                            );
                    }
                    result.Add(mainResultItem);
                }
            }

            var subdivisionsForEntities = _subdivisionRepository.GetSubdivisionsForDocumentTypes(uow, entityTypes);
            var specialPermissions      = _permissionRepository.GetAllSubdivisionForUserEntityPermissionForSomeEntities(uow, userId, entityNames)
                                          .Where(x => subdivisionsForEntities.Contains(x.Subdivision) || Subdivision.ReferenceEquals(x.Subdivision, mainSubdivision));

            foreach (var entityType in entityTypes)
            {
                var mainPermission = ServicesConfig.CommonServices.PermissionService.ValidateUserPermission(entityType, userId);
                foreach (var permissionitem in specialPermissions.Where(x => x.TypeOfEntity.Type == entityType.Name))
                {
                    EntitySubdivisionForUserPermissionValidationResult resultItem = result.FirstOrDefault(x => x.Subdivision == permissionitem.Subdivision);
                    if (resultItem == null)
                    {
                        var isMainSubdivision = permissionitem.Subdivision != null && mainSubdivision != null && permissionitem.Subdivision.Id == mainSubdivision.Id;
                        resultItem = new EntitySubdivisionForUserPermissionValidationResult(permissionitem.Subdivision, isMainSubdivision);
                        result.Add(resultItem);
                    }
                    resultItem.AddPermission(
                        entityType,
                        new EntityPermission(
                            mainPermission.CanCreate && permissionitem.CanCreate,
                            mainPermission.CanRead && permissionitem.CanRead,
                            mainPermission.CanUpdate && permissionitem.CanUpdate,
                            mainPermission.CanDelete && permissionitem.CanDelete
                            )
                        );
                }
            }

            return(result);
        }