示例#1
0
 private void ValidateGender(int genderId)
 {
     if (!EnumLibrary.GetIntValuesFromEnumType(typeof(Genders)).Contains(genderId))
     {
         AddValidationError("Gender", $"Id {genderId} not found!");
     }
 }
示例#2
0
 private void ValidateType(int typeId)
 {
     if (!EnumLibrary.GetIntValuesFromEnumType(typeof(FilmTypes)).Contains(typeId))
     {
         AddValidationError("Type", $"Id {typeId} not found!");
     }
 }
示例#3
0
        public async Task ValidateForUpdate(int id, UserForUpdateDto userForUpdate)
        {
            Validate(userForUpdate);

            var user = await _uow.Repository <Domain.Entities.User>().FindOneAsync(new UserWithRolesSpecification(id));

            AddValidationErrorIfValueIsNull(user, "User", $"Id {id} not found");

            AddValidationErrorIfIdDoesntExist(userForUpdate.RoleIds.ToList(), EnumLibrary.GetIntValuesFromEnumType(typeof(Roles)), "Role", "Id __id__ not found");

            ThrowValidationErrorsIfNotEmpty();

            var loggedUser = await _uow.Repository <Domain.Entities.User>().FindOneAsync(new UserWithRolesSpecification((int)_currentUserService.UserId));

            var isLoggedUserAdmin = loggedUser.Roles.Any(x => x.RoleId == (int)Roles.Admin);

            var roles = user.Roles.Select(x => x.Role);

            /* if the logged user is not admin and is trying to update someones roles
             * or the user is trying to update his roles, throw 403 forbidden */
            if ((!isLoggedUserAdmin || _currentUserService.UserId == id) &&
                (!roles.Select(x => x.Id).All(userForUpdate.RoleIds.Contains) ||
                 roles.Count() != userForUpdate.RoleIds.Count()))
            {
                throw new ForbiddenException();
            }
        }
示例#4
0
 private void ValidateEntityType(int entityType)
 {
     if (!EnumLibrary.GetIntValuesFromEnumType(typeof(EntityTypes)).Contains(entityType))
     {
         AddValidationError("Photo", $"EntityTypeId {entityType} is not valid!");
     }
 }
示例#5
0
        public void GetIntValuesFromEnumType_PassEnum_ReturnListOfIntegers()
        {
            var enumType = typeof(TestEnums);

            var result = EnumLibrary.GetIntValuesFromEnumType(enumType);

            result.Should().Contain(1);
            result.Should().Contain(2);
            result.Should().HaveCount(2);
        }
示例#6
0
 public void GetIntValuesFromEnumType_TypeIsNull_ThrowException()
 {
     Assert.That(() => EnumLibrary.GetIntValuesFromEnumType(null), Throws.Exception.TypeOf <Exception>());
 }