Exemple #1
0
        public async Task <UserResponse> LoginByUserName(LoginUser loginuser)
        {
            var _userResponse = new UserResponse();
            var _errorTitle   = "UserManagementApi:LoginByUserName";

            if (loginuser != null && (!string.IsNullOrWhiteSpace(loginuser.UserName) &&
                                      !string.IsNullOrWhiteSpace(loginuser.Password)))
            {
                _userResponse = await LoginByUserName(loginuser.UserName, loginuser.Password);
            }
            else
            {
                _userResponse = ResponseError_ModelView(_errorTitle, "No username/passoword params found");
            }

            return(_userResponse);
        }
Exemple #2
0
        /// <summary>
        /// Validate Account via Email
        /// </summary>
        /// <param name="loginemail">LoginEmail model</param>
        /// <returns>UserResponse</returns>
        public async Task <UserResponse> LoginByEmail(LoginEmail loginemail)
        {
            var _userResponse = new UserResponse();
            var _errorTitle   = "UserManagementApi:LoginByEmail";

            if (loginemail != null && (!string.IsNullOrWhiteSpace(loginemail.Email) &&
                                       !string.IsNullOrWhiteSpace(loginemail.Password)))
            {
                _userResponse = await LoginByEmail(loginemail.Email, loginemail.Password);
            }
            else
            {
                _userResponse = ResponseError_ModelView(_errorTitle, "No email/passoword params found");
            }

            return(_userResponse);
        }
Exemple #3
0
        /// <summary>
        /// Sets lock state to true or false
        /// </summary>
        /// <param name="id">userid</param>
        /// <param name="description">details of setting the lock state</param>
        /// <param name="isLocked">true or false</param>
        /// <returns></returns>
        public async Task <UserResponse> SetLockState(Guid id, string description, bool isLocked)
        {
            var _userResponse = new UserResponse();
            var _desc         = new System.Text.StringBuilder();
            var _user         = new User();
            int _rowsAffected = 0;
            var _errorTitle   = "UserManagementApi:LockUserAccount";

            //Verify description parameter
            if (string.IsNullOrWhiteSpace(description))
            {
                return(ResponseError_ModelView(_errorTitle, "No description was found"));
            }

            //Verify id parameter
            if (id == Guid.Empty)
            {
                return(ResponseError_ModelView(_errorTitle, "User Id is was not found"));
            }

            //Connection String
            if (string.IsNullOrWhiteSpace(ConnectionString))
            {
                return(ResponseError_ModelView(_errorTitle, "No db connection found"));
            }

            return(await Task.Run(() =>
            {
                _user = GetUser(SqlQueries.GetUserById_Sql, new { Id = id });
                if (_user != null && !string.IsNullOrWhiteSpace(_user.Email))
                {
                    _user.LockEnabled = isLocked;
                    _user.LockoutDescription = description;
                    _user.DateCreated = DateTime.Now;
                    _rowsAffected = UserInsertOrUpdate(_user);

                    if (_rowsAffected <= 0)
                    {
                        return ResponseError_ModelView(_errorTitle, $"No records were inserted. Row Affected were {_rowsAffected} User Id: {_user.Id.ToString()}");
                    }
                }
                _userResponse = ResponseOk_ModelView(_user);
                return _userResponse;
            }));
        }
Exemple #4
0
        /// <summary>
        /// Sets IsActive field.
        /// helper function to facilites (delete) disable/enable behaviors
        /// </summary>
        /// <param name="id">UserId (System.Guid)</param>
        /// <param name="description">description why the account is being disabled/enabled</param>
        /// <param name="isDisabled">true/false state</param>
        /// <returns>UserResponse</returns>
        public async Task <UserResponse> SetActiveState(Guid id, string description, bool isActive)
        {
            var _userResponse = new UserResponse();
            var _user         = new User();
            var _errorTitle   = "UserManagementApi:DisableUserAccount";
            var _rowsAffected = 0;

            //Verify description parameter
            if (string.IsNullOrWhiteSpace(description))
            {
                return(ResponseError_ModelView(_errorTitle, "No description was found"));
            }

            //Verify id parameter
            if (id == Guid.Empty)
            {
                return(ResponseError_ModelView(_errorTitle, "User Id is was not found"));
            }

            //ConnectionString check
            if (string.IsNullOrWhiteSpace(ConnectionString))
            {
                return(ResponseError_ModelView(_errorTitle, "No Connection to db was found"));
            }


            return(await Task.Run(() =>
            {
                _user = GetUser(SqlQueries.GetUserById_Sql, new { Id = id });
                if (_user != null && !string.IsNullOrWhiteSpace(_user.Email))
                {
                    _user.IsActive = isActive;
                    _user.AccountNotes = description;
                    _user.DateCreated = DateTime.Now;
                    _rowsAffected = UserInsertOrUpdate(_user);

                    if (_rowsAffected <= 0)
                    {
                        return ResponseError_ModelView(_errorTitle, $"No records were inserted. Row Affected were {_rowsAffected} User Id: {_user.Id.ToString()}");
                    }
                }
                _userResponse = ResponseOk_ModelView(_user);
                return _userResponse;
            }));
        }
Exemple #5
0
        /// <summary>
        /// Resets Password Account
        /// </summary>
        /// <param name="resetpassword">ResetPassword model</param>
        /// <returns>UserResponse</returns>
        public async Task <UserResponse> ResetPassword(ResetPassword resetpassword)
        {
            var _userResponse = new UserResponse();
            var _errorTitle   = "UserManagementApi:ResetPassword";

            if (resetpassword != null && (!string.IsNullOrWhiteSpace(resetpassword.Email) &&
                                          !string.IsNullOrWhiteSpace(resetpassword.ConfirmedPassword) &&
                                          !string.IsNullOrWhiteSpace(resetpassword.Password)))
            {
                _userResponse = await ResetPassword(resetpassword.Email, resetpassword.Password, resetpassword.ConfirmedPassword);
            }
            else
            {
                _userResponse = ResponseError_ModelView(_errorTitle, "No Encrypted Key found. see documentation for additional details");
            }

            return(_userResponse);
        }
Exemple #6
0
        /// <summary>
        /// retrieves UserResponse by email
        /// </summary>
        /// <param name="email"></param>
        /// <returns>UserResponse (ModelView)</returns>
        public async Task <UserResponse> GetUserByEmail(string email)
        {
            var _userResponse = new UserResponse();
            var _user         = new User();
            var _errorTitle   = "UserManagementApi:GetUserByEmail";

            //email parameter check
            if (string.IsNullOrWhiteSpace(email))
            {
                return(ResponseError_ModelView(_errorTitle, "email can not be empty"));
            }

            if (!email.IsValidEmail())
            {
                return(ResponseError_ModelView(_errorTitle, "email is not valid"));
            }


            //ConnectionString check
            if (string.IsNullOrWhiteSpace(ConnectionString))
            {
                return(ResponseError_ModelView(_errorTitle, "No Connection to db was found"));
            }

            return(await Task.Run(() =>
            {
                _user = GetUser(SqlQueries.GetUserByEmail_Sql, new { Email = email });
                if (_user != null)
                {
                    _userResponse = ResponseOk_ModelView(_user);
                }
                else
                {
                    _userResponse = ResponseError_ModelView(_errorTitle, "User was not found");
                }
                return _userResponse;
            }));
        }
Exemple #7
0
        /// <summary>
        /// Login common method to verify login using email or username
        /// </summary>
        /// <param name="emailorusername">username or email</param>
        /// <param name="password">password</param>
        /// <returns></returns>
        public async Task <UserResponse> Login(string emailorusername, string password)
        {
            var _userResponse = new UserResponse();
            var _errorTitle   = "UserManagementApi:Login";

            if (string.IsNullOrWhiteSpace(emailorusername) || string.IsNullOrWhiteSpace(password))
            {
                return(ResponseError_ModelView(_errorTitle, "No email/passoword params found"));
            }

            return(await Task.Run(async() =>
            {
                if (emailorusername.IsValidEmail())
                {
                    _userResponse = await LoginByEmail(emailorusername, password);
                }
                else
                {
                    _userResponse = await LoginByUserName(emailorusername, password);
                }
                return _userResponse;
            }));
        }
Exemple #8
0
        /// <summary>
        /// Validate Account via Email
        /// </summary>
        /// <param name="email">user email</param>
        /// <param name="password">user password</param>
        /// <returns></returns>
        public async Task <UserResponse> LoginByEmail(string email, string password)
        {
            var _userResponse = new UserResponse();
            var _errorTitle   = "UserManagementApi:LoginByEmail";

            if (string.IsNullOrWhiteSpace(email) || string.IsNullOrWhiteSpace(password))
            {
                return(ResponseError_ModelView(_errorTitle, "No email/passoword params found"));
            }

            //check email validation
            if (!email.IsValidEmail())
            {
                return(ResponseError_ModelView(_errorTitle, "Invalid Email"));
            }


            return(await Task.Run(() =>
            {
                var _user = GetUser(SqlQueries.GetUserByEmail_Sql, new { Email = email });
                if (_user != null)
                {
                    if (!_user.PasswordHash.DecryptString(EncryptKey).Equals(password))
                    {
                        return ResponseError_ModelView(_errorTitle, "Invalid Password");
                    }

                    _userResponse = ResponseOk_ModelView(_user);
                }
                else
                {
                    _userResponse = ResponseError_ModelView(_errorTitle, "User was not found");
                }

                return _userResponse;
            }));
        }
Exemple #9
0
        /// <summary>
        /// Resets Password Account
        /// </summary>
        /// <param name="email">user email</param>
        /// <param name="currentpassword">current active password</param>
        /// <param name="confirmedpassword">confirmed active account</param>
        /// <returns>UserResponse</returns>
        public async Task <UserResponse> ResetPassword(string email, string currentpassword, string confirmedpassword)
        {
            var _userResponse = new UserResponse();
            var _errorTitle   = "UserManagementApi:ResetPassword";
            var _rowsAffected = 0;

            //Verify email parameter
            if (string.IsNullOrWhiteSpace(email))
            {
                return(ResponseError_ModelView(_errorTitle, "No email was found"));
            }

            //Verify current password parameter
            if (string.IsNullOrWhiteSpace(currentpassword))
            {
                return(ResponseError_ModelView(_errorTitle, "No current password parameter found"));
            }

            //Verify confirmedpassword parameter
            if (string.IsNullOrWhiteSpace(confirmedpassword))
            {
                return(ResponseError_ModelView(_errorTitle, "No confirmed password  found"));
            }

            //Verify db password against the current password
            var _user = GetUser(SqlQueries.GetUserByEmail_Sql, new { Email = email });

            if (_user == null || string.IsNullOrWhiteSpace(email))
            {
                return(ResponseError_ModelView(_errorTitle, "User Account not Found"));
            }

            //Verify Encrypted Key parameter
            if (string.IsNullOrWhiteSpace(EncryptKey))
            {
                return(ResponseError_ModelView(_errorTitle, "No Encrypted Key found. see documentation for additional details"));
            }

            //Verify current passwords match
            if (!_user.PasswordHash.DecryptString(EncryptKey).Equals(currentpassword))
            {
                return(ResponseError_ModelView(_errorTitle, "current password does not match our system"));
            }

            return(await Task.Run(() =>
            {
                _user.PasswordHash = confirmedpassword.EncryptString(EncryptKey);
                _user.AccountNotes = "password reset";
                _user.AccessFailedCount = 0;
                _user.LockEnabled = false;
                _user.LockoutDescription = string.Empty;
                _user.IsActive = true;
                _user.DateCreated = DateTime.Now;
                _rowsAffected = UserInsertOrUpdate(_user);

                if (_rowsAffected <= 0)
                {
                    return ResponseError_ModelView(_errorTitle, $"reset password failed. Row Affected were {_rowsAffected} User Id: {_user.Id.ToString()}");
                }

                _userResponse = ResponseOk_ModelView(_user);
                return _userResponse;
            }));
        }
Exemple #10
0
        /// <summary>
        /// Adds User. Email is a requirement (no username)
        /// </summary>
        /// <param name="user"></param>
        /// <returns>UserResponse</returns>
        public async Task <UserResponse> AddOrUpdateUserWithEmail(User user)
        {
            var _userResponse = new UserResponse();
            var _userTemp     = new UserResponse();

            var _errorTitle = "UserManagementApi:AddUserByUserName";

            //User parameter
            if (user == null)
            {
                return(ResponseError_ModelView(_errorTitle, "user parameter can not be empty/null"));
            }

            //Email parameter check
            if (string.IsNullOrWhiteSpace(user.Email) || !user.Email.IsValidEmail())
            {
                return(ResponseError_ModelView(_errorTitle, "Invalid Email"));
            }

            _userTemp = await GetUserByEmail(user.Email);

            if (_userTemp.Status.Equals("ok") && !string.IsNullOrWhiteSpace(_userTemp.Email))
            {
                return(ResponseError_ModelView(_errorTitle, "Email already exist"));
            }


            //Password parameter check
            var passwValidationPhrase = GetPasswordValidationPhrase(user.PasswordHash);

            if (passwValidationPhrase != "VALID")
            {
                return(ResponseError_ModelView(_errorTitle, passwValidationPhrase));
            }

            //Connection String
            if (string.IsNullOrWhiteSpace(ConnectionString))
            {
                return(ResponseError_ModelView(_errorTitle, "No Connection to db was found"));
            }

            return(await Task.Run(() =>
            {
                user.Id = (user.Id == Guid.Empty) ? Guid.NewGuid() : user.Id;
                user.PasswordHash = user.PasswordHash.EncryptString(EncryptKey);
                user.IsActive = true;
                user.AccessFailedCount = 0;
                user.LockEnabled = false;
                user.LockoutDescription = string.Empty;
                user.ReportsToId = null;
                user.DateCreated = DateTime.Now;
                int _rowsAffected = UserInsertOrUpdate(user);

                //Make sure execute fuction suceeded
                if (_rowsAffected <= 0)
                {
                    return ResponseError_ModelView(_errorTitle, $"No records were inserted. Row Affected were {_rowsAffected}");
                }

                //Only show ModelView response
                _userResponse = ResponseOk_ModelView(user);
                return _userResponse;
            }));
        }