/// <summary> /// Update user /// </summary> /// <param name="email">Email</param> /// <param name="userAccount">User account [Account]</param> /// <param name="additionalinformation">New password</param> /// <returns>Return true on successful update, else a string with an error message</returns> public static UserManagerResult EditUser(string email, Account userAccount, string additionalinformation) { UserManagerResult userManagerResult = new UserManagerResult(); try { using (AlenMotorsDbEntities alenMotorsDbEntities = new AlenMotorsDbEntities()) { // Allternative 1 if (email != userAccount.Email) { foreach (Account account0 in alenMotorsDbEntities.Accounts.ToList()) { if (account0.Email.Replace(" ", string.Empty) == email && account0.Password.Replace(" ", string.Empty) == Generic.EncodePassword(additionalinformation, email)) { foreach (Account account1 in alenMotorsDbEntities.Accounts.ToList()) { if (userAccount.Email == account1.Email.Replace(" ", String.Empty)) { if (userAccount.RegistrationDate != null) { account1.Email = userAccount.RegistrationDate; } account1.LastName = userAccount.LastName; account1.FirstName = userAccount.FirstName; account1.BirthDate = userAccount.BirthDate; account1.Gender = userAccount.Gender; account1.PhoneNumber = userAccount.PhoneNumber; if (userAccount.Password != null) { account1.Password = Generic.EncodePassword(userAccount.Password, userAccount.Email); } userManagerResult.Success = true; alenMotorsDbEntities.SaveChanges(); return userManagerResult; } } } } userManagerResult.Success = false; return userManagerResult; } // Allternative 2 if (userAccount.Password != null) { foreach (Account account in alenMotorsDbEntities.Accounts.ToList()) { if (account.Email.Replace(" ", string.Empty) == email && account.Password.Replace(" ", string.Empty) == Generic.EncodePassword(userAccount.Password, email)) { account.LastName = userAccount.LastName; account.FirstName = userAccount.FirstName; account.BirthDate = userAccount.BirthDate; account.Gender = userAccount.Gender; account.PhoneNumber = userAccount.PhoneNumber; if (additionalinformation != null) { account.Password = Generic.EncodePassword(additionalinformation, email); } if (email != account.Email.Replace(" ", String.Empty)) { account.Email = email; } userManagerResult.Success = true; alenMotorsDbEntities.SaveChanges(); return userManagerResult; } } } // Allternative 3 foreach (Account account in alenMotorsDbEntities.Accounts.ToList()) { if (account.Email.Replace(" ", string.Empty) == email && account.Password.Replace(" ", string.Empty) == Generic.EncodePassword(additionalinformation, email)) { account.LastName = userAccount.LastName; account.FirstName = userAccount.FirstName; account.BirthDate = userAccount.BirthDate; account.Gender = userAccount.Gender; account.PhoneNumber = userAccount.PhoneNumber; if (additionalinformation != null) { account.Password = Generic.EncodePassword(additionalinformation, email); } if (email != account.Email.Replace(" ", String.Empty)) { account.Email = email; } userManagerResult.Success = true; alenMotorsDbEntities.SaveChanges(); return userManagerResult; } } } userManagerResult.Success = false; return userManagerResult; } catch (Exception ex) { userManagerResult.ErrorMessage = ex.Message; return userManagerResult; } }
public ActionResult EditUser(ManagementEditUserViewModel model) { if (!ModelState.IsValid) { ModelState.AddModelError("", "Model validation Error"); return View(model); } if (Roles.IsUserInRole(User.Identity.Name, "Developer")) { Account userAccount = new Account { Email = model.Email, //we are gonna use RegistrationDate to deliver the new email RegistrationDate = model.NewEmail, LastName = model.LastName, FirstName = model.FirstName, BirthDate = model.BirthDate, Gender = model.Gender, PhoneNumber = model.PhoneNumber, Password = model.Password, }; UserManagerResult updateUserResult = UserManager.EditUser(User.Identity.Name, userAccount, model.ManagementPassword); if (updateUserResult.ErrorMessage != null) { ModelState.AddModelError("", updateUserResult.ErrorMessage); model.Password = ""; model.ConfirmPassword = ""; model.ManagementPassword = ""; return View(model); } if (updateUserResult.Success) { if (User.Identity.Name == model.Email) { return RedirectToAction("LogOut", "Account"); } TempData["UserUpdated"] = "User updated successfully"; return RedirectToAction("Index", "Management"); } ModelState.AddModelError("", "Wrong Management password"); return View(model); } return RedirectToAction("index", "Home"); }
/// <summary> /// Returns the infrmation that corresponds to the provided email /// </summary> /// <param name="email">Email</param> /// <returns>Returns all the corresponds information (Account object), else a string with an error message</returns> public static UserManagerResult GetUser(string email) { UserManagerResult userManagerResult = new UserManagerResult(); Account user = new Account(); try { using (AlenMotorsDbEntities alenMotorsDbEntities = new AlenMotorsDbEntities()) { foreach (Account account in alenMotorsDbEntities.Accounts) { if (account.Email.Replace(" ", string.Empty) != email) { continue; } user.Email = account.Email; user.LastName = account.LastName; user.FirstName = account.FirstName; user.Gender = account.Gender; user.PhoneNumber = account.PhoneNumber; user.BirthDate = account.BirthDate; userManagerResult.User = user; userManagerResult.Success = true; return userManagerResult; } } } catch (Exception ex) { userManagerResult.ErrorMessage = ex.Message; return userManagerResult; } return userManagerResult; }
public ActionResult Account(AccountViewModel model) { if (!ModelState.IsValid) { ModelState.AddModelError("", "Model validation Error"); return View(model); } Account userAccount = new Account { Email = User.Identity.Name, LastName = model.LastName, FirstName = model.FirstName, BirthDate = model.BirthDate, Gender = model.Gender, PhoneNumber = model.PhoneNumber, Password = model.Password, }; UserManagerResult updateUserResult = UserManager.EditUser(User.Identity.Name, userAccount, model.NewPassword); if (updateUserResult.ErrorMessage != null) { ModelState.AddModelError("", updateUserResult.ErrorMessage); model.Password = ""; model.NewPassword = ""; model.ConfirmNewPassword = ""; return View(model); } if (updateUserResult.Success) { ModelState.AddModelError("", "Changes Saved"); model.Password = ""; model.NewPassword = ""; model.ConfirmNewPassword = ""; return View(model); } ModelState.AddModelError("", "The old password you entered is wrong"); model.Password = ""; model.NewPassword = ""; model.ConfirmNewPassword = ""; return View(model); }