예제 #1
0
        /// <summary>
        /// Attempt to Login a user account.
        /// </summary>
        /// <param name="request">Request object to login.</param>
        /// <param name="response">Response object to populate with details.</param>
        /// <returns>Response object with details about login.</returns>
        public DCO.Response.UserManagementResponse login(
            UserManagementRequest request,
            UserManagementResponse response)
        {
            IUserManagementDataServices userManagementDataServices = getUserManagementDataServices();

            DDO.UserAccount retrievedDataUserAccount =
                userManagementDataServices.GetUserByUsername(request.UserAccountToProcess.Username);

            DCO.UserAccount retrievedCoreUserAccount =
                UserAccountConverter.ConvertToCoreUserAccount(retrievedDataUserAccount);

            // Check if a user is found.
            if (retrievedCoreUserAccount != null)
            {
                // Check if the passwords match.
                if (retrievedCoreUserAccount.Password == request.UserAccountToProcess.Password)
                {
                    if (retrievedDataUserAccount.FailedAttempts >= 3)
                    {
                        response.IsRequestSuccessful  = false;
                        response.ResponseErrorMessage = "User is currently locked out. Please contact an administrator.";

                        Logger.Log("[Locked Account] " + retrievedCoreUserAccount.ToString(),
                                   LogType.Warning);
                    }
                    else
                    {
                        retrievedDataUserAccount.FailedAttempts = 0;
                        userManagementDataServices.EditUser(retrievedDataUserAccount);

                        response.UserAccountsRetrieved.Add(retrievedCoreUserAccount);
                        response.IsRequestSuccessful = true;

                        Logger.Log("[Login Successful] " + retrievedCoreUserAccount.ToString(),
                                   LogType.Informational);
                    }
                }
                // User found but password given was incorrect.
                // Increase failed attempts.
                else
                {
                    // If failed attempts is less than 3,
                    // add counters to failed attempts.
                    if (retrievedDataUserAccount.FailedAttempts < 3)
                    {
                        retrievedDataUserAccount.FailedAttempts += 1;
                        userManagementDataServices.EditUser(retrievedDataUserAccount);

                        if (retrievedDataUserAccount.FailedAttempts == 3)
                        {
                            Logger.Log("[Locking Account] " + retrievedCoreUserAccount.ToString(),
                                       LogType.Warning);
                        }
                    }
                }
            }

            return(response);
        }
예제 #2
0
        /// <summary>
        /// Edits a User.
        /// </summary>
        /// <param name="request">Request object.</param>
        /// <param name="response">Response object.</param>
        /// <returns>Populated Response object.</returns>
        public UserManagementResponse editUser(
            UserManagementRequest request,
            UserManagementResponse response)
        {
            IUserManagementDataServices userManagementDataServices = getUserManagementDataServices();

            if (request.RequestorUserAccount.UserID == request.UserAccountToProcess.UserID)
            {
                response.IsRequestSuccessful  = false;
                response.ResponseErrorMessage = "The currently logged in user cannot edit itself.";
                return(response);
            }

            // Check Username given.
            if (string.IsNullOrEmpty(request.UserAccountToProcess.Username))
            {
                Logger.Log("[Edit User Failed] Empty Username",
                           LogType.Warning);

                response.IsRequestSuccessful  = false;
                response.ResponseErrorMessage = "Username should not be empty. ";

                return(response);
            }

            // Check if Username already exists
            DDO.UserAccount dataUserAccount =
                userManagementDataServices.GetUserByUsername(request.UserAccountToProcess.Username);

            if (dataUserAccount != null)
            {
                var coreUserAccount = UserAccountConverter.ConvertToCoreUserAccount(dataUserAccount);

                if (coreUserAccount.Username == request.UserAccountToProcess.Username &&
                    coreUserAccount.UserID != request.UserAccountToProcess.UserID)
                {
                    Logger.Log("[Edit User Failed] Username already exists.",
                               LogType.Warning);

                    response.IsRequestSuccessful  = false;
                    response.ResponseErrorMessage = "Username already taken. Username should be unique. ";

                    return(response);
                }
            }


            // Check Name given.
            if (!ValidationHelper.CheckIfNameIsValid(request.UserAccountToProcess.UserInfo.FirstName) ||
                !ValidationHelper.CheckIfNameIsValid(request.UserAccountToProcess.UserInfo.LastName))
            {
                Logger.Log("[Edit User Failed] Invalid Names",
                           LogType.Warning);

                response.IsRequestSuccessful  = false;
                response.ResponseErrorMessage = "Names should only be composed of letter, spaces, and should not be empty. ";

                return(response);
            }

            request.UserAccountToProcess.UserInfo.Age =
                UserManagementHelper.CalculateAge(request.UserAccountToProcess.UserInfo.BirthDate);

            dataUserAccount = UserAccountConverter.ConvertToDataUserAccount(request.UserAccountToProcess);

            response.IsRequestSuccessful = userManagementDataServices.EditUser(dataUserAccount);

            if (response.IsRequestSuccessful)
            {
                Logger.Log("[Edit User Successful] " + request.UserAccountToProcess.ToString(),
                           LogType.Informational);
            }
            else
            {
                Logger.Log("[Edit User Failed]",
                           LogType.Warning);
                response.ResponseErrorMessage = "Problem encountered while editing a user.";
            }

            return(response);
        }