private void ValidateGender(int genderId) { if (!EnumLibrary.GetIntValuesFromEnumType(typeof(Genders)).Contains(genderId)) { AddValidationError("Gender", $"Id {genderId} not found!"); } }
private void ValidateType(int typeId) { if (!EnumLibrary.GetIntValuesFromEnumType(typeof(FilmTypes)).Contains(typeId)) { AddValidationError("Type", $"Id {typeId} not found!"); } }
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(); } }
private void ValidateEntityType(int entityType) { if (!EnumLibrary.GetIntValuesFromEnumType(typeof(EntityTypes)).Contains(entityType)) { AddValidationError("Photo", $"EntityTypeId {entityType} is not valid!"); } }
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); }
public void GetIntValuesFromEnumType_TypeIsNull_ThrowException() { Assert.That(() => EnumLibrary.GetIntValuesFromEnumType(null), Throws.Exception.TypeOf <Exception>()); }