/*************************************************************************************************/ public void DisplayModifyResult(UserInformationResult result) { switch (result) { case UserInformationResult.Failed: UIHelper.UpdateStatusLabel("Failed to modify information!", statusLabel, ErrorLevel.Error); break; case UserInformationResult.InvalidEmail: UIHelper.UpdateStatusLabel("Invalid email!", statusLabel, ErrorLevel.Error); break; case UserInformationResult.InvalidFirstName: UIHelper.UpdateStatusLabel("Invalid first name!", statusLabel, ErrorLevel.Error); break; case UserInformationResult.InvalidLastName: UIHelper.UpdateStatusLabel("Invalid last name!", statusLabel, ErrorLevel.Error); break; case UserInformationResult.InvalidPhoneNumber: UIHelper.UpdateStatusLabel("Invalid phone number!", statusLabel, ErrorLevel.Error); break; case UserInformationResult.Success: UIHelper.UpdateStatusLabel("Success", statusLabel, ErrorLevel.Ok); ClearChangePasswordView(); this.Close(); break; } }
/*************************************************************************************************/ public UserInformationResult EditUser(User user) { UserInformationResult result = UserInformationResult.Failed; if (user != null) { if (IsLoggedIn()) { var validation = VerifyUserInformation(user); if (validation == UserInformationResult.Success) { User dbUser = _dbcontext.GetUserByGUID(_currentUser.GUID); User newCurrentUser = new User( _currentUser.GUID, _currentUser.Username, _currentUser.PlainTextRandomKey, user.FirstName, user.LastName, user.PhoneNumber, user.Email, true); _currentUser = newCurrentUser; User modifiedUser = new User ( dbUser.GUID, dbUser.EncryptedKey, dbUser.Username, dbUser.Iterations, dbUser.Salt, dbUser.Hash, _encryptDecrypt.Encrypt(user.FirstName, _currentUser.PlainTextRandomKey), _encryptDecrypt.Encrypt(user.LastName, _currentUser.PlainTextRandomKey), _encryptDecrypt.Encrypt(user.PhoneNumber, _currentUser.PlainTextRandomKey), _encryptDecrypt.Encrypt(user.Email, _currentUser.PlainTextRandomKey) ); bool success = _dbcontext.ModifyUser(dbUser, modifiedUser); if (success) { result = UserInformationResult.Success; // TODO - return other results } else { result = UserInformationResult.Failed; } } else { result = validation; } } } return(result); }
/*================================================================================================= * PUBLIC METHODS *================================================================================================*/ /*************************************************************************************************/ /*================================================================================================= * PRIVATE METHODS *================================================================================================*/ /*************************************************************************************************/ private void ModifyUserInformation(string firstName, string lastName, string email, string phoneNumber) { User user = new User(null, null, firstName, lastName, phoneNumber, email); UserInformationResult result = _passwordService.EditUser(user); _editUserView.DisplayModifyResult(result); }
/*************************************************************************************************/ private UserInformationResult VerifyUserInformation(User user) { UserInformationResult result = UserInformationResult.Success; if (user != null) { if (string.IsNullOrEmpty(user.FirstName)) { result = UserInformationResult.InvalidFirstName; } if (string.IsNullOrEmpty(user.LastName)) { result = UserInformationResult.InvalidLastName; } if (string.IsNullOrEmpty(user.Email) || user.Email == "*****@*****.**") { result = UserInformationResult.InvalidEmail; } else { var regex = @"^[\w!#$%&'*+\-/=?\^_`{|}~]+(\.[\w!#$%&'*+\-/=?\^_`{|}~]+)*" + "@" + @"((([\-\w]+\.)+[a-zA-Z]{2,4})|(([0-9]{1,3}\.){3}[0-9]{1,3}))$"; Match match = Regex.Match(user.Email, regex, RegexOptions.IgnoreCase); if (!match.Success) { result = UserInformationResult.InvalidEmail; } } if (string.IsNullOrEmpty(user.PhoneNumber) || user.PhoneNumber == "xxx-xxx-xxxx") { result = UserInformationResult.InvalidPhoneNumber; } else { if (!IsValidUSPhoneNumber(user.PhoneNumber)) { result = UserInformationResult.InvalidPhoneNumber; } } } return(result); }
public UserInformationResult GetListClassById(string userId) { var user = _users.Find(u => u.Id == userId).FirstOrDefault(); var groups = _groups.Find(g => true).ToList(); List <Groups> Listgroups = new List <Groups>(); foreach (Groups item in groups) { for (int i = 0; i < item.ListUser.Count; i++) { if (item.ListUser[i].Id == userId) { Listgroups.Add(item); } } } var groupResults = _mapper.Map <List <Groups>, List <GroupResult> >(Listgroups); List <string> classId = new List <string>(); foreach (Groups item in Listgroups) { classId.Add(item.ClassId); } List <ClassResult> classResults = new List <ClassResult>(); foreach (string id in classId) { Class c = _classes.Find(c => c.Id == id).FirstOrDefault(); var temp = _mapper.Map <ClassResult>(c); classResults.Add(temp); } UserInformationResult userInformation = new UserInformationResult(); userInformation.Id = user.Id; userInformation.UserName = user.Username; userInformation.FirstName = user.FirstName; userInformation.LastName = user.LastName; userInformation.ClassResults = classResults; userInformation.GroupResults = groupResults; return(userInformation); }
/*************************************************************************************************/ public CreateUserResult CreateNewUser(User user) { CreateUserResult createUserResult = CreateUserResult.Failed; if (user != null) { User queryResult = _dbcontext.GetUserByUsername(user.Username); if (queryResult != null) { createUserResult = CreateUserResult.UsernameTaken; } else { UserInformationResult verifyUser = VerifyUserInformation(user); ChangeUserPasswordResult verifyPassword = VerifyPasswordRequirements(user.PlainTextPassword); // Verify that username and password pass requirements if (!VerifyUsernameRequirements(user.Username)) { createUserResult = CreateUserResult.UsernameNotValid; } else if (verifyPassword != ChangeUserPasswordResult.Success) { switch (verifyPassword) { case ChangeUserPasswordResult.Failed: createUserResult = CreateUserResult.PasswordNotValid; break; case ChangeUserPasswordResult.LengthRequirementNotMet: createUserResult = CreateUserResult.LengthRequirementNotMet; break; case ChangeUserPasswordResult.NoLowerCaseCharacter: createUserResult = CreateUserResult.NoLowerCaseCharacter; break; case ChangeUserPasswordResult.NoNumber: createUserResult = CreateUserResult.NoNumber; break; case ChangeUserPasswordResult.NoSpecialCharacter: createUserResult = CreateUserResult.NoSpecialCharacter; break; case ChangeUserPasswordResult.NoUpperCaseCharacter: createUserResult = CreateUserResult.NoUpperCaseCharacter; break; case ChangeUserPasswordResult.PasswordsDoNotMatch: createUserResult = CreateUserResult.PasswordNotValid; break; default: createUserResult = CreateUserResult.PasswordNotValid; break; } } else if (verifyUser != UserInformationResult.Success) { switch (verifyUser) { case UserInformationResult.InvalidEmail: createUserResult = CreateUserResult.EmailNotValid; break; case UserInformationResult.InvalidFirstName: createUserResult = CreateUserResult.FirstNameNotValid; break; case UserInformationResult.InvalidLastName: createUserResult = CreateUserResult.LastNameNotValid; break; case UserInformationResult.InvalidPhoneNumber: createUserResult = CreateUserResult.PhoneNumberNotValid; break; case UserInformationResult.Failed: createUserResult = CreateUserResult.Failed; break; } } else { createUserResult = CreateUserResult.Successful; UserEncrypedData newEncryptedData = _masterPassword.GenerateNewUserEncryptedDataFromPassword(user.PlainTextPassword); User newUser = new User( newEncryptedData.UniqueGUID, // Leave unique guid in plaintext _encryptDecrypt.Encrypt(newEncryptedData.RandomGeneratedKey, user.PlainTextPassword), // Encrypt the random key with the users password user.Username, // Leave username in plaintext newEncryptedData.Iterations.ToString(CultureInfo.CurrentCulture), // Leave iterations in plaintext newEncryptedData.Salt, newEncryptedData.Hash, _encryptDecrypt.Encrypt(user.FirstName, newEncryptedData.RandomGeneratedKey), // Encrypt with decrypted random key _encryptDecrypt.Encrypt(user.LastName, newEncryptedData.RandomGeneratedKey), // Encrypt with decrypted random key _encryptDecrypt.Encrypt(user.PhoneNumber, newEncryptedData.RandomGeneratedKey), // Encrypt with decrypted random key _encryptDecrypt.Encrypt(user.Email, newEncryptedData.RandomGeneratedKey) // Encrypt with decrypted random key ); _dbcontext.AddUser(newUser); } } } return(createUserResult); }