Beispiel #1
0
        public async Task <bool> ValidateName(Role Role)
        {
            if (string.IsNullOrWhiteSpace(Role.Name))
            {
                Role.AddError(nameof(RoleValidator), nameof(Role.Name), ErrorCode.NameEmpty);
            }
            else
            {
                if (Role.Name.Length > 255)
                {
                    Role.AddError(nameof(RoleValidator), nameof(Role.Name), ErrorCode.NameOverLength);
                }
                RoleFilter RoleFilter = new RoleFilter
                {
                    Skip = 0,
                    Take = 10,
                    Id   = new IdFilter {
                        NotEqual = Role.Id
                    },
                    Name = new StringFilter {
                        Equal = Role.Name
                    },
                    Selects = RoleSelect.Name
                };

                int count = await UOW.RoleRepository.Count(RoleFilter);

                if (count != 0)
                {
                    Role.AddError(nameof(RoleValidator), nameof(Role.Name), ErrorCode.NameExisted);
                }
            }
            return(Role.IsValidated);
        }
Beispiel #2
0
        public async Task <bool> BulkDelete(List <Role> Roles)
        {
            RoleFilter RoleFilter = new RoleFilter
            {
                Skip = 0,
                Take = int.MaxValue,
                Id   = new IdFilter {
                    In = Roles.Select(a => a.Id).ToList()
                },
                Selects = RoleSelect.Id
            };

            var listInDB = await UOW.RoleRepository.List(RoleFilter);

            var listExcept = Roles.Except(listInDB);

            if (listExcept == null || listExcept.Count() == 0)
            {
                return(true);
            }
            foreach (var Role in listExcept)
            {
                Role.AddError(nameof(RoleValidator), nameof(Role.Id), ErrorCode.IdNotExisted);
            }
            return(false);
        }
Beispiel #3
0
        private async Task <bool> ValidateAssignAppUser(Role Role)
        {
            List <long>   ids           = Role.AppUserRoleMappings.Select(a => a.AppUserId).ToList();
            AppUserFilter AppUserFilter = new AppUserFilter
            {
                Skip = 0,
                Take = int.MaxValue,
                Id   = new IdFilter {
                    In = ids
                },
                OrganizationId = new IdFilter(),
                Selects        = AppUserSelect.Id,
                OrderBy        = AppUserOrder.Id,
            };

            var listInDB = await UOW.AppUserRepository.List(AppUserFilter);

            var listExcept = Role.AppUserRoleMappings.Select(a => a.AppUserId).Except(listInDB.Select(a => a.Id));

            if (listExcept.Any())
            {
                foreach (var AppUserID in listExcept)
                {
                    Role.AddError(nameof(RoleValidator), AppUserID.ToString(), ErrorCode.AppUserNotExisted);
                }
            }

            return(Role.IsValidated);
        }
Beispiel #4
0
        public async Task <bool> ValidateCode(Role Role)
        {
            if (string.IsNullOrWhiteSpace(Role.Code))
            {
                Role.AddError(nameof(RoleValidator), nameof(Role.Code), ErrorCode.CodeEmpty);
            }
            else
            {
                var Code = Role.Code;
                if (Role.Code.Contains(" ") || !FilterExtension.ChangeToEnglishChar(Code).Equals(Role.Code))
                {
                    Role.AddError(nameof(RoleValidator), nameof(Role.Code), ErrorCode.CodeHasSpecialCharacter);
                }
                else if (Role.Code.Length > 255)
                {
                    Role.AddError(nameof(RoleValidator), nameof(Role.Code), ErrorCode.CodeOverLength);
                }
                else
                {
                    RoleFilter RoleFilter = new RoleFilter
                    {
                        Skip = 0,
                        Take = 10,
                        Id   = new IdFilter {
                            NotEqual = Role.Id
                        },
                        Code = new StringFilter {
                            Equal = Role.Code
                        },
                        Selects = RoleSelect.Code
                    };
                    int count = await UOW.RoleRepository.Count(RoleFilter);

                    if (count != 0)
                    {
                        Role.AddError(nameof(RoleValidator), nameof(Role.Code), ErrorCode.CodeExisted);
                    }
                }
            }
            return(Role.IsValidated);
        }
Beispiel #5
0
        public async Task <bool> ValidateStatus(Role Role)
        {
            StatusFilter StatusFilter = new StatusFilter
            {
                Skip = 0,
                Take = 10,
                Id   = new IdFilter {
                    Equal = Role.StatusId
                },
                Selects = StatusSelect.Id
            };
            int count = await UOW.StatusRepository.Count(StatusFilter);

            if (count == 0)
            {
                Role.AddError(nameof(RoleValidator), nameof(Role.Status), ErrorCode.StatusNotExisted);
            }
            return(count != 0);
        }
Beispiel #6
0
        public async Task <bool> ValidateId(Role Role)
        {
            RoleFilter RoleFilter = new RoleFilter
            {
                Skip = 0,
                Take = 10,
                Id   = new IdFilter {
                    Equal = Role.Id
                },
                Selects = RoleSelect.Id
            };

            int count = await UOW.RoleRepository.Count(RoleFilter);

            if (count == 0)
            {
                Role.AddError(nameof(RoleValidator), nameof(Role.Id), ErrorCode.IdNotExisted);
            }
            return(count == 1);
        }
Beispiel #7
0
        public async Task <bool> Import(List <Role> Roles)
        {
            var listCodeInDB = (await UOW.RoleRepository.List(new RoleFilter
            {
                Skip = 0,
                Take = int.MaxValue,
                Selects = RoleSelect.Code
            })).Select(e => e.Code);

            foreach (var Role in Roles)
            {
                if (listCodeInDB.Contains(Role.Code))
                {
                    Role.AddError(nameof(RoleValidator), nameof(Role.Code), ErrorCode.CodeExisted);
                    return(false);
                }
                await ValidatePermission(Role);
            }
            return(Roles.Any(s => !s.IsValidated) ? false : true);
        }