Exemple #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);
        }
Exemple #2
0
        /// <summary>
        /// Search users.
        /// </summary>
        /// <param name="request">Request object.</param>
        /// <param name="response">Response object.</param>
        /// <returns>Populated Response object.</returns>
        public UserManagementResponse searchUsersByName(
            UserManagementRequest request,
            UserManagementResponse response)
        {
            IUserManagementDataServices userManagementDataServices = getUserManagementDataServices();

            List <DDO.UserAccount> dataUserAccounts = new List <DDO.UserAccount>();

            List <DCO.UserAccount> coreUserAccounts = new List <UserAccount>();

            switch (request.SearchCriteriaEnum)
            {
            case UserInfoSearchCriteriaEnum.FirstName:
                dataUserAccounts = userManagementDataServices.GetUsersByFirstName(request.SearchKeyWord);
                break;

            case UserInfoSearchCriteriaEnum.LastName:
                dataUserAccounts = userManagementDataServices.GetUsersByLastName(request.SearchKeyWord);
                break;
            }

            foreach (DDO.UserAccount dataUserAccount in dataUserAccounts)
            {
                coreUserAccounts.Add(UserAccountConverter.ConvertToCoreUserAccount(dataUserAccount));
            }

            response.UserAccountsRetrieved = coreUserAccounts;
            response.IsRequestSuccessful   = true;

            StringBuilder sbuilder = new StringBuilder();

            sbuilder.Append("[Search Users Success] [Keyword:").Append(request.SearchKeyWord).Append("]")
            .Append("[Criteria:").Append(request.SearchCriteriaEnum.ToString("g")).Append("]")
            .Append("[ResultCount:").Append(coreUserAccounts.Count).Append("]");

            Logger.Log(sbuilder.ToString(),
                       LogType.Informational);

            return(response);
        }
Exemple #3
0
        /// <summary>
        /// Gets all the Users.
        /// </summary>
        /// <param name="request">Request object.</param>
        /// <param name="response">Response object.</param>
        /// <returns>Populated response object.</returns>
        public UserManagementResponse getUsers(
            UserManagementRequest request,
            UserManagementResponse response)
        {
            IUserManagementDataServices userManagementDataServices = getUserManagementDataServices();

            List <DDO.UserAccount> dataUserAccounts = userManagementDataServices.GetUsers();

            List <DCO.UserAccount> coreUserAccounts = new List <UserAccount>();

            foreach (DDO.UserAccount dataUserAccount in dataUserAccounts)
            {
                coreUserAccounts.Add(UserAccountConverter.ConvertToCoreUserAccount(dataUserAccount));
            }

            response.UserAccountsRetrieved = coreUserAccounts;

            Logger.Log("[Get Users Success] Total User Accounts Retrieved: " + coreUserAccounts.Count,
                       LogType.Informational);

            return(response);
        }
Exemple #4
0
        /// <summary>
        /// Verifies and update UserAccounts retrieved from Data Provider.
        /// </summary>
        private void verifyAndUpdateUserAccounts()
        {
            // 1.) Get Services
            IUserManagementDataServices userManagementDataServices = getUserManagementDataServices();

            // 2.) Get Users
            List <DDO.UserAccount> dataUserAccounts = userManagementDataServices.GetUsers();

            // 3.) Validate and Verify Data
            List <DCO.UserAccount> coreUserAccounts = new List <UserAccount>();

            foreach (DDO.UserAccount dataUserAccount in dataUserAccounts)
            {
                coreUserAccounts.Add(UserAccountConverter.ConvertToCoreUserAccount(dataUserAccount));
            }

            foreach (DCO.UserAccount coreUserAccount in coreUserAccounts)
            {
                if (coreUserAccount.UserID == null)
                {
                    Logger.Log("[Data Error] A UserID is invalid. Terminating Applciation.",
                               LogType.Error);

                    Environment.Exit(1);
                }

                if (string.IsNullOrEmpty(coreUserAccount.Username))
                {
                    Logger.Log("[Data Error] A Username is invalid. Terminating Applciation.",
                               LogType.Error);

                    Environment.Exit(1);
                }
                else
                {
                    if (coreUserAccounts.FindAll(u => u.Username == coreUserAccount.Username).Count > 1)
                    {
                        Logger.Log("[Data Error] A duplicate UserID is found. Terminating Applciation.",
                                   LogType.Error);

                        Environment.Exit(1);
                    }
                }

                if (string.IsNullOrEmpty(coreUserAccount.Password))
                {
                    Logger.Log("[Data Error] A Password is invalid. Terminating Applciation.",
                               LogType.Error);

                    Environment.Exit(1);
                }

                if (coreUserAccount.UserInfo == null)
                {
                    Logger.Log("[Data Error] A UserInfo is invalid. Terminating Applciation.",
                               LogType.Error);

                    Environment.Exit(1);
                }

                if (!ValidationHelper.CheckIfNameIsValid(coreUserAccount.UserInfo.FirstName))
                {
                    Logger.Log("[Data Error] A First Name is invalid. Terminating Applciation.",
                               LogType.Error);

                    Environment.Exit(1);
                }

                if (!ValidationHelper.CheckIfNameIsValid(coreUserAccount.UserInfo.LastName))
                {
                    Logger.Log("[Data Error] A Last Name is invalid. Terminating Applciation.",
                               LogType.Error);

                    Environment.Exit(1);
                }

                if (coreUserAccount.UserInfo.BirthDate > DateTime.Now)
                {
                    Logger.Log("[Data Error] A BirthDate is invalid. Terminating Applciation.",
                               LogType.Error);

                    Environment.Exit(1);
                }

                coreUserAccount.UserInfo.Age =
                    UserManagementHelper.CalculateAge(coreUserAccount.UserInfo.BirthDate);
            }

            // 4.) Update Data
            dataUserAccounts = new List <DDO.UserAccount>();

            foreach (DCO.UserAccount coreUserAccount in coreUserAccounts)
            {
                userManagementDataServices.EditUser(UserAccountConverter.ConvertToDataUserAccount(coreUserAccount));
            }
        }
Exemple #5
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);
        }