///<summary> /// Gets a user record by email address ///</summary> public User GetUser(string emailAddress) { try { DataTable users = ApplicationData.GetInstance().DataSet.Tables["everbank_users"]; DataRow[] results = users.Select($"email_address = '{UserUtilities.ConformString(emailAddress)}'"); if (results.Length > 0) { DataRow row = results[0]; User user = new User() { EmailAddress = row.Field <string>("email_address"), FirstName = row.Field <string>("first_name"), Id = row.Field <int>("uid"), Password = row.Field <string>("password"), }; return(user); } else { return(null); } } catch (Exception ex) { throw ex; } }
public void AuthenticateUser() { string emailAddress = " [email protected] "; string password = "******"; string hashedPassword = UserUtilities.HashString("password123"); UserRepository userRepository = new UserRepository(); User newUser = userRepository.AddUser(emailAddress, hashedPassword, "AuthTester1"); UserService UserService = new UserService(); ServiceResponse response = UserService.AuthenticateUser(emailAddress, password); User authenticatedUser = response.ResponseObject as User; Assert.IsNotNull(authenticatedUser, "User was not successfully authenticated."); Assert.AreEqual(UserUtilities.ConformString(emailAddress), authenticatedUser.EmailAddress, "User's email address is not properly conformed for storage."); Assert.IsNull(response.Messages, "Error messages were returned from authentication method."); }
///<summary> /// Attempts to Authenticate the user and if successful, returns the user's information ///</summary> public ServiceResponse AuthenticateUser(string emailAddress, string password) { try { User user = userRepository.GetUser(emailAddress); if (user != null) { string conformedPassword = UserUtilities.ConformString(password); string hashedPassword = UserUtilities.HashString(conformedPassword); if (hashedPassword == user.Password) { return(new ServiceResponse() { ResponseObject = user, }); } } Message errorMessage = new Message() { Text = "The email address or password that you provided were incorrect. Please try again.", Type = MessageType.ERROR, }; return(new ServiceResponse() { Messages = new List <Message>() { errorMessage }, }); } catch { Message errorMessage = new Message() { Text = "There was an error accessing your user profile. Please try again. If the error continues then please contact us at 123-456-7890.", Type = MessageType.ERROR, }; return(new ServiceResponse() { Messages = new List <Message>() { errorMessage }, }); } }
///<summary> /// Adds a new user to the DataSet ///</summary> public User AddUser(string emailAddress, string hashedPassword, string firstName) { try { string conformedEmail = UserUtilities.ConformString(emailAddress); DataTable users = ApplicationData.GetInstance().DataSet.Tables["everbank_users"]; DataRow newRow = users.NewRow(); newRow["uid"] = users.Rows.Count + 1; newRow["email_address"] = conformedEmail; newRow["first_name"] = firstName.Trim(); newRow["password"] = hashedPassword; users.Rows.Add(newRow); User newUser = GetUser(conformedEmail); return(newUser); } catch (Exception ex) { throw ex; } }
///<summary> /// Attempts to create a user and if successful, returns the new user ///</summary> public ServiceResponse CreateUser(string emailAddress, string password, string firstName) { bool isPasswordComplex = UserUtilities.CheckPasswordComplexity(password); if (!isPasswordComplex) { Message errorMessage = new Message() { Text = "Please choose a password that is 8 of more characters and contains at least one letter and one number.", Type = MessageType.WARN, }; return(new ServiceResponse() { Messages = new List <Message> { errorMessage }, }); } string conformedEmail = UserUtilities.ConformString(emailAddress); bool isEmailValid = UserUtilities.CheckEmailValidity(conformedEmail); if (!isEmailValid) { Message errorMessage = new Message() { Text = "Please supply a valid email address.", Type = MessageType.WARN, }; return(new ServiceResponse() { Messages = new List <Message> { errorMessage }, }); } try { UserRepository userRepository = new UserRepository(); User existingUser = userRepository.GetUser(emailAddress); if (existingUser == null) { string conformedPassword = UserUtilities.ConformString(password); string hashedPassword = UserUtilities.HashString(conformedPassword); User newUser = userRepository.AddUser(emailAddress, hashedPassword, firstName); return(new ServiceResponse() { ResponseObject = newUser, }); } else { Message errorMessage = new Message() { Text = "There is an existing account at this email address. Please try logging in with these credentials.", Type = MessageType.WARN, }; return(new ServiceResponse() { Messages = new List <Message>() { errorMessage }, }); } } catch { Message errorMessage = new Message() { Text = "There was an error creating your profile. Please try again. If the error continues then please contact us at 123-456-7890.", Type = MessageType.ERROR, }; return(new ServiceResponse() { Messages = new List <Message>() { errorMessage }, }); } }