Esempio n. 1
0
 /// <summary>
 /// Initializes an instance of the <see cref="UserLoginResult"/> class
 /// </summary>
 /// <param name="resultStatus"></param>
 /// <param name="message"></param>
 /// <param name="userAccountDetails"></param>
 public UserLoginResult(ServiceResultStatusCode resultStatus, string message, UserAccountDetailsDto userAccountDetails)
 {
     ResultStatus       = resultStatus;
     Message            = message;
     UserAccountDetails = userAccountDetails;
 }
        /// <summary>
        /// Method to process login functionality
        /// </summary>
        /// <param name = "username" > String of the username</param>
        /// <param name = "password" > String of the submitted password</param>
        /// <returns>User object if authentication successful</returns>
        public async Task <UserLoginResult> Login(string username, string password)
        {
            if (string.IsNullOrEmpty(username))
            {
                throw new ArgumentNullException(nameof(username));
            }

            if (string.IsNullOrEmpty(password))
            {
                throw new ArgumentNullException(nameof(password));
            }

            // attempt to retrieve a matching user from the db
            var user = await _userDal.GetUserAsync(username);

            string message;

            // if not found return null
            if (user == null)
            {
                message = $"User not found. \"Username\"={username}";
                _log.LogInformation(message);
                return(new UserLoginResult(ServiceResultStatusCode.NotFound, message, null));
            }

            if (user.State != AccountState.Active)
            {
                message = $"Account login failed. Account state is not active \"Username\"={username}, \"AccountState\"={user.State}";
                return(new UserLoginResult(ServiceResultStatusCode.Failed, message, null));
            }

            if (user.JobDescription.Role == UserRole.Unknown)
            {
                message = $"Account login failed. Account role is unknown \"Username\"={username}, \"AccountRole\"={user.JobDescription.Role}";
                return(new UserLoginResult(ServiceResultStatusCode.Failed, message, null));
            }

            // user found, check the password is correct and build the ClaimsPrincipal, if not, return null
            if (!_cryptographyService.VerifyPasswordHash(user.PasswordHash, user.PasswordSalt, password))
            {
                message = $"Account login failed. Username or password not recognised. \"Username\"={username}";
                _log.LogInformation(message);
                return(new UserLoginResult(ServiceResultStatusCode.Failed, message, null));
            }

            var userAccountDetails = new UserAccountDetailsDto(user);

            message = "Account login Success.";
            _log.LogInformation(message);
            var results = new UserLoginResult(ServiceResultStatusCode.Success, message, userAccountDetails);

            if (user.JobDescription.Role == UserRole.Patient)
            {
                var patient = _patientUserDal.GetAssociatedPatient(user.Id);

                if (patient != null)
                {
                    results.PatientAccountPatientId = patient.Id;
                }
            }

            return(results);
        }