Esempio n. 1
0
        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 CreateJoggingRouteCommandHandler(IMapper mapper, JoggingTrackerDbContext dbContext)
        {
            ArgumentChecker.CheckNotNull(new { mapper, dbContext });

            this._mapper    = mapper;
            this._dbContext = dbContext;
        }
Esempio n. 3
0
        public async Task RegisterUserCommand_UserRegisters_NewUserCreated()
        {
            var command = new RegisterUserCommand()
            {
                Username        = "******",
                Password        = "******",
                ConfirmPassword = "******",
                Name            = "Testname",
                Surname         = "Testsurname",
                BirthDate       = DateTime.Now,
                Email           = "*****@*****.**",
                Gender          = Gender.Male
            };

            UpdateUserCommand user;

            using (var dbContext = new JoggingTrackerDbContext(this._options))
            {
                dbContext.Roles.Add(new Role()
                {
                    Id = Guid.NewGuid(), Name = Role.UserRole
                });
                await dbContext.SaveChangesAsync();

                var commandHandler = new RegisterUserCommandHandler(this._securityProvider, this._serverTime, this._mapper, dbContext);
                user = await commandHandler.Handle(command);
            }

            using (var dbContext = new JoggingTrackerDbContext(this._options))
            {
                Assert.NotNull(await dbContext.Users.SingleAsync(u => u.Id == user.Id));
            }
        }
Esempio n. 4
0
        public GetJoggingRoutesQueryHandler(IMapper mapper, JoggingTrackerDbContext dbContext)
        {
            ArgumentChecker.CheckNotNull(new { mapper, dbContext });

            this._mapper    = mapper;
            this._dbContext = dbContext;
        }
Esempio n. 5
0
        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 DeleteUserCommandValidator(JoggingTrackerDbContext dbContext)
        {
            ArgumentChecker.CheckNotNull(new { dbContext });

            this.CustomAsync(async command =>
            {
                if (await dbContext.Users.AnyAsync(u => u.Id == command.Id))
                {
                    return(null);
                }

                throw new ObjectNotFoundException($"{nameof(User)} not found.");
            });
        }
        public LoginUserCommandValidator(JoggingTrackerDbContext dbContext, ISecurityProvider securityProvider)
        {
            ArgumentChecker.CheckNotNull(new { dbContext, securityProvider });

            this.CustomAsync(async command =>
            {
                var user = await dbContext.Users
                           .Where(u => u.Username == command.Username)
                           .SingleOrDefaultAsync();

                if (user == null || !securityProvider.IsValidPassword(command.Password.Trim(), user.Password))
                {
                    return(new ValidationFailure(string.Empty, "Invalid credentials."));
                }

                return(null);
            });
        }
        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 RegisterUserCommandHandler(
            ISecurityProvider securityProvider,
            IServerTime serverTime,
            IMapper mapper,
            JoggingTrackerDbContext dbContext)
        {
            ArgumentChecker.CheckNotNull(new
            {
                securityProvider,
                serverTime,
                mapper,
                dbContext
            });

            this._securityProvider = securityProvider;
            this._serverTime       = serverTime;
            this._mapper           = mapper;
            this._dbContext        = dbContext;
        }
Esempio n. 10
0
        public LoginUserCommandHandler(
            ISecurityProvider securityProvider,
            IServerTime serverTime,
            IOptions <TokenValidationParameters> tokenValidationParameterOptions,
            IConfigurationRoot configuration,
            JoggingTrackerDbContext dbContext)
        {
            ArgumentChecker.CheckNotNull(new
            {
                serverTime,
                tokenValidationParameterOptions,
                configuration,
                dbContext
            });

            this._serverTime = serverTime;
            this._tokenValidationParameters = tokenValidationParameterOptions.Value;
            this._configuration             = configuration;
            this._dbContext = dbContext;
        }
        public DeleteUserCommandHandler(JoggingTrackerDbContext dbContext)
        {
            ArgumentChecker.CheckNotNull(new { dbContext });

            this._dbContext = dbContext;
        }
Esempio n. 12
0
        public UpdateJoggingRouteCommandHandler(JoggingTrackerDbContext dbContext)
        {
            ArgumentChecker.CheckNotNull(new { dbContext });

            this._dbContext = dbContext;
        }
Esempio n. 13
0
        public GetWeeklyRecordsQueryHandler(JoggingTrackerDbContext dbContext)
        {
            ArgumentChecker.CheckNotNull(new { dbContext });

            this._dbContext = dbContext;
        }
Esempio n. 14
0
        public GetSingleJoggingRouteQueryHandler(JoggingTrackerDbContext dbContext)
        {
            ArgumentChecker.CheckNotNull(new { dbContext });

            this._dbContext = dbContext;
        }
Esempio n. 15
0
        public OptionalTransactionBehavior(JoggingTrackerDbContext dbContext)
        {
            ArgumentChecker.CheckNotNull(new { dbContext });

            this._dbContext = dbContext;
        }
Esempio n. 16
0
        public RegisterUserCommandValidator(JoggingTrackerDbContext dbContext, IServerTime serverTime)
        {
            ArgumentChecker.CheckNotNull(new { dbContext, serverTime });

            this.RuleFor(u => u.Username)
            .Must(username => !string.IsNullOrWhiteSpace(username))
            .WithMessage($"'{nameof(RegisterUserCommand.Username)}' is required.")
            .Length(User.UsernameLengthMin, User.UsernameLengthMax);

            this.RuleFor(u => u.Email)
            .EmailAddress()
            .Must(email => !string.IsNullOrWhiteSpace(email))
            .WithMessage($"'{nameof(RegisterUserCommand.Email)}' is required.")
            .Length(User.EmailLengthMin, User.EmailLengthMax);

            this.RuleFor(u => u.Password)
            .Must(password => !string.IsNullOrWhiteSpace(password))
            .WithMessage($"'{nameof(RegisterUserCommand.Password)}' is required.")
            .Equal(u => u.ConfirmPassword)
            .WithMessage($"'{nameof(RegisterUserCommand.Password)}' and '{nameof(RegisterUserCommand.ConfirmPassword)}' must be equal.")
            .Length(User.PasswordLengthMin, User.PasswordLengthMax)
            .WithMessage($"'{nameof(RegisterUserCommand.Password)}' length must be between {User.PasswordLengthMin} and {User.PasswordLengthMax} characters.");

            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.Username == command.Username))
                {
                    return(null);
                }

                return(new ValidationFailure(
                           propertyName: nameof(User.Username),
                           error: $"{nameof(User.Username)} is already in use."));
            });

            this.CustomAsync(async command =>
            {
                if (!await dbContext.Users.AnyAsync(u => u.Email == command.Email))
                {
                    return(null);
                }

                return(new ValidationFailure(
                           propertyName: nameof(User.Email),
                           error: $"{nameof(User.Email)} is already in use."));
            });
        }