Ejemplo n.º 1
0
        public bool Execute(IUseCaseOutput <AuthorizeUserResponse> output, AuthorizeUserRequest request)
        {
            try
            {
                if (!request.Validate(out var errors))
                {
                    return(output.Accept(new ValidationErrorResponse(errors, "Invalid request.")));
                }

                var passwordHash = UserValidationHelper.HashPassword(request.Password);
                var user         = _userRepository.FindUserInfo(request.UserName, passwordHash);

                return(output.Accept(new AuthorizeUserResponse
                {
                    UserId = user.Id,
                    DisplayName = user.DisplayName,
                }));
            }
            catch (EntityNotFoundException)
            {
                return(output.Accept(new FailureResponse($"Username or password is invalid.")));
            }
            catch (Exception e)
            {
                return(output.Accept(new FailureResponse($"Tehnical error happend. {e.Message}")));
            }
        }
Ejemplo n.º 2
0
 private static User CreateDefaultUser() => new User(
     "id",
     "username",
     "displayname",
     UserValidationHelper.HashPassword("passwordhash"),
     new UserProjects(new[]
 {
     new BasicProject("id1", "name1"),
     new BasicProject("id2", "name2")
 }));
Ejemplo n.º 3
0
        public void UserConstructorAndPropertyGettersWork()
        {
            var validUserName    = "******";
            var validDisplayName = "display1";
            var validPassword    = UserValidationHelper.HashPassword("abc123");
            var validProjects    = new UserProjects(new[]
            {
                new BasicProject("id2", "name1"),
                new BasicProject("id3", "name2"),
            });

            var user = new User("id1", validUserName, validDisplayName, validPassword, validProjects);

            Assert.Equal(validUserName, user.UserName);
            Assert.Equal(validDisplayName, user.DisplayName);
            Assert.Equal(validPassword, user.PasswordHash);
        }
Ejemplo n.º 4
0
        public void UserConstructorArgumentValidationWorks()
        {
            var validUserName   = "******";
            var invalidUserName = new string('u', 51);

            var validDisplayName   = "display1";
            var invalidDisplayName = new string('d', 101);

            var validPassword    = UserValidationHelper.HashPassword("abc123");
            var invalidPassword1 = "abc123";
            var invalidPassword2 = new string('p', 33);

            var validProjects = new UserProjects(new[]
            {
                new BasicProject("id2", "name1"),
                new BasicProject("id3", "name2"),
            });
            var invalidProjects = new UserProjects(new[]
            {
                new BasicProject("id4", "name3"),
                new BasicProject("id5", "name3"),
            });

            Assert.Throws <ArgumentException>(() =>
                                              new User("id1", invalidUserName, validDisplayName, validPassword, validProjects));

            Assert.Throws <ArgumentException>(() =>
                                              new User("id1", validUserName, invalidDisplayName, validPassword, validProjects));

            Assert.Throws <ArgumentException>(() =>
                                              new User("id1", validUserName, validDisplayName, invalidPassword1, validProjects));

            Assert.Throws <ArgumentException>(() =>
                                              new User("id1", validUserName, validDisplayName, invalidPassword2, validProjects));

            Assert.Throws <ArgumentException>(() =>
                                              new User("id1", validUserName, validDisplayName, validPassword, invalidProjects));
        }
Ejemplo n.º 5
0
        public bool Execute(IUseCaseOutput <RegisterUserResponse> output, RegisterUserRequest request)
        {
            try
            {
                if (!request.Validate(out var errors))
                {
                    return(output.Accept(new ValidationErrorResponse(errors, "Invalid request.")));
                }

                var passwordHash = UserValidationHelper.HashPassword(request.Password);
                var user         = new User(Identity.BlankIdentity, request.UserName, request.DisplayName, passwordHash);

                var id = _userRepository.CreateUser(user);
                if (id == null)
                {
                    return(output.Accept(new FailureResponse("User couldn't be registered.")));
                }

                return(output.Accept(new RegisterUserResponse
                {
                    GivenId = user.Id,
                    Message = $"User {user.UserName} successfuly created",
                }));
            }
            catch (ValidationException e)
            {
                var errors = new Dictionary <string, string> {
                    { e.PropertyKey, e.Message }
                };
                return(output.Accept(new ValidationErrorResponse(errors, $"Invalid data for {e.PropertyKey}.")));
            }
            catch (Exception e)
            {
                return(output.Accept(new FailureResponse($"Tehnical error happend. {e.Message}")));
            }
        }