public void Put([FromBody] RegisterViewModel registerViewModel)
        {
            var enrollmentRequest = new EnrollmentRequest
            {
                LastName = registerViewModel.LastName,
                AccountNumber = registerViewModel.AccountNumber,
                Username = registerViewModel.Username,
                Password = registerViewModel.Password                
            };

            var oppStatus = service.Enroll(enrollmentRequest);

            // Return success or error state
            if (!oppStatus.Success)
            {
                throw new Exception(oppStatus.Messages[0]);
            }
        }
        public OperationStatus Enroll(EnrollmentRequest enrollmentRequest)
        {
            try
            {
                using (var unitOfWork = unitOfWorkFactory.CreateUnitOfWork())
                {
                    // Verify that the provided enrollment data matches a person in the system
                    var personQueryable = unitOfWork.Persons.GetQueryable()
                        .Where(p => 
                            p.LastName == enrollmentRequest.LastName && 
                            p.Accounts.Any(a => a.Account.AccountNumber == enrollmentRequest.AccountNumber));

                    if (personQueryable.Any())
                    {
                        var person = personQueryable.First();

                        // Verify that the person does not already have an account
                        var personLoginQueryable = unitOfWork.PersonLogins.GetQueryable()
                            .Where(p => p.PersonID == person.PersonID);

                        if (personLoginQueryable.Any())                        
                        {
                            return new OperationStatus { Success = false, Messages = new List<string> { "The holder of this account is already registered in the system." } };
                        }
                        else
                        {
                            // Verify that the username is not already used
                            personLoginQueryable = unitOfWork.PersonLogins.GetQueryable()
                                .Where(p => p.LoginID.ToLower() == enrollmentRequest.Username.ToLower());

                            if (personLoginQueryable.Any())
                            {
                                return new OperationStatus { Success = false, Messages = new List<string> { "The username is already in use." } };
                            }
                            else
                            {
                                var passwordValidationStatus = PasswordUtils.ValidatePassword(enrollmentRequest.Password);

                                if (passwordValidationStatus.Success)
                                {
                                    var personLogin = new PersonLogin();

                                    personLogin.PersonID = person.PersonID;
                                    personLogin.LoginID = enrollmentRequest.Username;

                                    // The stored password will be a hash based on a salt and the password provided
                                    var salt = PasswordUtils.CreateSalt(PASSWORD_SALT_SIZE);
                                    personLogin.Salt = salt;
                                    personLogin.HashedPassword = PasswordUtils.GenerateHashedPassword(enrollmentRequest.Password, salt);

                                    unitOfWork.PersonLogins.Add(personLogin);
                                    unitOfWork.Commit();

                                    return new OperationStatus { Success = true };
                                }
                                else
                                {
                                    return passwordValidationStatus;
                                }
                            }
                        }                                        
                    }
                    else
                    {
                        return new OperationStatus { Success = false, Messages = new List<string> { "There is no one in the system that matches the information provided" } };
                    }
                }
            }
            catch (Exception e)
            {
                return OperationStatus.CreateFromException("Error deleting person.", e);
            }
        }
예제 #3
0
        public OperationStatus Enroll(EnrollmentRequest enrollmentRequest)
        {
            try
            {
                using (var unitOfWork = unitOfWorkFactory.CreateUnitOfWork())
                {
                    // Verify that the provided enrollment data matches a person in the system
                    var personQueryable = unitOfWork.Persons.GetQueryable()
                                          .Where(p =>
                                                 p.LastName == enrollmentRequest.LastName &&
                                                 p.Accounts.Any(a => a.Account.AccountNumber == enrollmentRequest.AccountNumber));

                    if (personQueryable.Any())
                    {
                        var person = personQueryable.First();

                        // Verify that the person does not already have an account
                        var personLoginQueryable = unitOfWork.PersonLogins.GetQueryable()
                                                   .Where(p => p.PersonID == person.PersonID);

                        if (personLoginQueryable.Any())
                        {
                            return(new OperationStatus {
                                Success = false, Messages = new List <string> {
                                    "The holder of this account is already registered in the system."
                                }
                            });
                        }
                        else
                        {
                            // Verify that the username is not already used
                            personLoginQueryable = unitOfWork.PersonLogins.GetQueryable()
                                                   .Where(p => p.LoginID.ToLower() == enrollmentRequest.Username.ToLower());

                            if (personLoginQueryable.Any())
                            {
                                return(new OperationStatus {
                                    Success = false, Messages = new List <string> {
                                        "The username is already in use."
                                    }
                                });
                            }
                            else
                            {
                                var passwordValidationStatus = PasswordUtils.ValidatePassword(enrollmentRequest.Password);

                                if (passwordValidationStatus.Success)
                                {
                                    var personLogin = new PersonLogin();

                                    personLogin.PersonID = person.PersonID;
                                    personLogin.LoginID  = enrollmentRequest.Username;

                                    // The stored password will be a hash based on a salt and the password provided
                                    var salt = PasswordUtils.CreateSalt(PASSWORD_SALT_SIZE);
                                    personLogin.Salt           = salt;
                                    personLogin.HashedPassword = PasswordUtils.GenerateHashedPassword(enrollmentRequest.Password, salt);

                                    unitOfWork.PersonLogins.Add(personLogin);
                                    unitOfWork.Commit();

                                    return(new OperationStatus {
                                        Success = true
                                    });
                                }
                                else
                                {
                                    return(passwordValidationStatus);
                                }
                            }
                        }
                    }
                    else
                    {
                        return(new OperationStatus {
                            Success = false, Messages = new List <string> {
                                "There is no one in the system that matches the information provided"
                            }
                        });
                    }
                }
            }
            catch (Exception e)
            {
                return(OperationStatus.CreateFromException("Error deleting person.", e));
            }
        }