public GetJoggingRoutesQueryValidator(IHttpContextAccessor httpContextAccessor) { ArgumentChecker.CheckNotNull(new { httpContextAccessor }); var userIdentity = UserIdentity.FromPrincipal(httpContextAccessor.HttpContext.User); this.Custom(query => { if (userIdentity.IsAdmin || userIdentity.Id == query.UserId) { return(null); } throw new UnauthorizedAccessException(); }); this.Custom(query => { if (!query.FromDate.HasValue || !query.UntilDate.HasValue || (query.FromDate <= query.UntilDate)) { return(null); } return(new ValidationFailure( propertyName: string.Empty, error: "'From date' must be less than or equal to 'Until date'")); }); }
public GetSingleUserQueryValidator(JoggingTrackerDbContext dbContext, IHttpContextAccessor httpContextAccessor) { ArgumentChecker.CheckNotNull(new { dbContext, httpContextAccessor }); var userIdentity = UserIdentity.FromPrincipal(httpContextAccessor.HttpContext.User); this.Custom(command => { if (userIdentity.IsAdmin || userIdentity.IsUserManager || userIdentity.Id == command.Id) { return(null); } throw new UnauthorizedAccessException(); }); this.CustomAsync(async query => { if (await dbContext.Users.AnyAsync(u => u.Id == query.Id)) { return(null); } throw new ObjectNotFoundException($"{nameof(User)} not found."); }); }
public UpdateJoggingRouteCommandValidator(IHttpContextAccessor httpContextAccessor) { ArgumentChecker.CheckNotNull(new { httpContextAccessor }); var userIdentity = UserIdentity.FromPrincipal(httpContextAccessor.HttpContext.User); this.Custom(command => { if (userIdentity.IsAdmin || userIdentity.Id == command.UserId) { return(null); } throw new UnauthorizedAccessException(); }); this.RuleFor(r => r.DistanceKilometers) .NotNull() .GreaterThan(0); this.RuleFor(r => r.StartTime) .NotNull() .LessThan(r => r.EndTime) .WithMessage("'Start time' must be less than 'End time'"); this.RuleFor(r => r.EndTime) .NotNull() .GreaterThan(r => r.StartTime) .WithMessage("'End time' must be greater than 'Start time'"); }
public DeleteJoggingRouteCommandValidator(JoggingTrackerDbContext dbContext, IHttpContextAccessor httpContextAccessor) { ArgumentChecker.CheckNotNull(new { dbContext, httpContextAccessor }); var userIdentity = UserIdentity.FromPrincipal(httpContextAccessor.HttpContext.User); this.Custom(command => { if (userIdentity.IsAdmin || userIdentity.Id == command.UserId) { return(null); } throw new UnauthorizedAccessException(); }); this.CustomAsync(async command => { if (await dbContext.JoggingRoutes.AnyAsync(r => r.Id == command.Id)) { return(null); } throw new ObjectNotFoundException($"{nameof(JoggingRoute)} not found."); }); }
public GetWeeklyRecordsQueryValidator(IHttpContextAccessor httpContextAccessor) { ArgumentChecker.CheckNotNull(new { httpContextAccessor }); var userIdentity = UserIdentity.FromPrincipal(httpContextAccessor.HttpContext.User); this.Custom(query => { if (userIdentity.Id == query.UserId) { return(null); } throw new UnauthorizedAccessException(); }); }
public UpdateUserCommandValidator(JoggingTrackerDbContext dbContext, IServerTime serverTime, IHttpContextAccessor httpContextAccessor) { ArgumentChecker.CheckNotNull(new { dbContext, serverTime, httpContextAccessor }); var userIdentity = UserIdentity.FromPrincipal(httpContextAccessor.HttpContext.User); this.Custom(command => { if (userIdentity.IsAdmin || userIdentity.IsUserManager || userIdentity.Id == command.Id) { return(null); } throw new UnauthorizedAccessException(); }); this.RuleFor(u => u.Name) .Must(name => !string.IsNullOrWhiteSpace(name)) .WithMessage($"'{nameof(RegisterUserCommand.Name)}' is required.") .Length(User.NameLengthMin, User.NameLengthMax); this.RuleFor(u => u.Surname) .Must(surname => !string.IsNullOrWhiteSpace(surname)) .WithMessage($"'{nameof(RegisterUserCommand.Surname)}' is required.") .Length(User.SurnameLengthMin, User.SurnameLengthMax); this.RuleFor(u => u.BirthDate) .NotNull() .LessThanOrEqualTo(serverTime.UtcNow) .WithMessage("Invalid date of birth."); this.RuleFor(u => u.Gender) .NotNull(); this.CustomAsync(async command => { if (await dbContext.Users.AnyAsync(u => u.Id == command.Id)) { return(null); } throw new ObjectNotFoundException($"{nameof(User)} not found."); }); }
public IActionResult GetUserIdentity() { return(this.Ok(UserIdentity.FromPrincipal(this.User))); }