public async Task<ActionResult> RequestAccount(RequestAccountViewModel model) { if (ModelState.IsValid) { string accountType = Request["AccountType"]; if (accountType == "Physician") { Physician physician = new Physician() { FirstName = model.FirstName, LastName = model.LastName, Email = model.Email, Address = model.Address, PhoneNumber = model.PhoneNumber }; // Write to ASP user database var user = new ApplicationUser { UserName = model.Username, Email = model.Email }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { // Successful account creation; add user to Physician database. _physicianService.CreatePhysician(physician); _physicianService.SaveChanges(); AccountRequest newUser = new AccountRequest() { ReasonForAccount = model.ReasonForAccount }; _accountRequestService.CreateAccountRequest(newUser); _accountRequestService.SaveChanges(); user.PhysicianId = physician.Id; user.AccountRequestId = newUser.Id; result = await UserManager.UpdateAsync(user); //Role must match what is found in the database AspNetRoles table. result = await UserManager.AddToRoleAsync(user.Id, "Physician"); } else { // Create Physician failed. AddErrors(result); return View(model); } return RedirectToAction("RequestPhysicianAccountConfirm", new System.Web.Routing.RouteValueDictionary( new { email = physician.Email, address = physician.Address, phoneNumber = physician.PhoneNumber, firstName = physician.FirstName, lastName = physician.LastName, reasonForAccount = model.ReasonForAccount })); } else if (accountType == "ExperimentAdministrator") { ExperimentAdministrator experimentAdministrator = new ExperimentAdministrator() { FirstName = model.FirstName, LastName = model.LastName, Email = model.Email, Address = model.Address, PhoneNumber = model.PhoneNumber }; // Write to ASP user database var user = new ApplicationUser { UserName = model.Username, Email = model.Email }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { // Successful account creation; add user to Experiment Administrator database. _experimentAdminService.CreateExperimentAdministrator(experimentAdministrator); _experimentAdminService.SaveChanges(); AccountRequest newUser = new AccountRequest() { ReasonForAccount = model.ReasonForAccount }; _accountRequestService.CreateAccountRequest(newUser); _accountRequestService.SaveChanges(); user.ExperimentAdministratorId = experimentAdministrator.Id; user.AccountRequestId = newUser.Id; result = await UserManager.UpdateAsync(user); //Role must match what is found in the database AspNetRoles table. result = await UserManager.AddToRoleAsync(user.Id, "Experiment Administrator"); } else { // Create Physician failed. AddErrors(result); return View(model); } return RedirectToAction("RequestExperimentAdministratorAccountConfirm", new System.Web.Routing.RouteValueDictionary( new { email = experimentAdministrator.Email, address = experimentAdministrator.Address, phoneNumber = experimentAdministrator.PhoneNumber, firstName = experimentAdministrator.FirstName, lastName = experimentAdministrator.LastName, reasonForAccount = model.ReasonForAccount })); } else { // ERROR: Shouldn't be here. return View("Error"); } } /* if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false); // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 // Send an email with this link // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); return RedirectToAction("Index", "Home"); } AddErrors(result); } */ // If we got this far, something failed, redisplay form return View(model); }
public ActionResult ConfirmUpdateUser (UpdateUserViewModel model) { var user = new ApplicationUser(); user = UserManager.FindById(User.Identity.GetUserId()); if (User.IsInRole("Patient")) { Patient patient = new Patient(); patient = _patientService.GetPatient(user.PatientId); patient.Birthdate = model.Birthdate; patient.Height = model.Height; patient.Weight = model.Weight; patient.Ethnicity = (int)Enum.Parse(typeof(PatientEthnicity), model.Ethnicity); patient.Gender = (int)Enum.Parse(typeof(PatientGender), model.Gender); patient.Location = (int)Enum.Parse(typeof(Location), model.Location); patient.Race = (int)Enum.Parse(typeof(PatientRace), model.Race); _patientService.SaveChanges(); } else if (User.IsInRole("Physician")) { Physician physician = new Physician(); physician = _physicianService.GetPhysician(user.PhysicianId); physician.Email = model.Email; user.Email = model.Email; physician.Address = model.Address; physician.FirstName = model.FirstName; physician.LastName = model.LastName; physician.PhoneNumber = model.PhoneNumber; _physicianService.SaveChanges(); } else if (User.IsInRole("Experiment Administrator")) { ExperimentAdministrator experimentAdministrator = new ExperimentAdministrator(); experimentAdministrator = _experimentAdminService.GetExperimentAdministrator(user.ExperimentAdministratorId); experimentAdministrator.Email = model.Email; user.Email = model.Email; experimentAdministrator.Address = model.Address; experimentAdministrator.FirstName = model.FirstName; experimentAdministrator.LastName = model.LastName; experimentAdministrator.PhoneNumber = model.PhoneNumber; _experimentAdminService.SaveChanges(); } else if (User.IsInRole("System Administrator")) { // Not yet implemented. user.Email = model.Email; } else { // Error path. ModelState.AddModelError("", "ERROR: User role not specified."); return View(); } return (Redirect ("/Account/LoginRedirect")); }
// // GET: /Manage/Index public async Task<ActionResult> Index(ManageMessageId? message) { ApplicationUser user = UserManager.FindById(User.Identity.GetUserId()); string accountRole = ""; //user = UserManager.FindById(userId); UserRole userRole = UserRole.Patient; if (User.IsInRole(UserRole.Patient.ToString())) { userRole = UserRole.Patient; accountRole = UserRole.Patient.ToString(); } else if (User.IsInRole(UserRole.Physician.ToString())) { userRole = UserRole.Physician; accountRole = UserRole.Physician.ToString(); } else if (User.IsInRole(UserRole.Experiment_Administrator.ToString().Replace("_", " "))) { userRole = UserRole.Experiment_Administrator; accountRole = UserRole.Experiment_Administrator.ToString().Replace("_", " "); } else { userRole = UserRole.System_Administrator; accountRole = UserRole.System_Administrator.ToString().Replace("_", " "); } ViewBag.StatusMessage = message == ManageMessageId.ChangePasswordSuccess ? "Your password has been changed." : message == ManageMessageId.SetPasswordSuccess ? "Your password has been set." : message == ManageMessageId.SetTwoFactorSuccess ? "Your two-factor authentication provider has been set." : message == ManageMessageId.Error ? "An error has occurred." : message == ManageMessageId.AddPhoneSuccess ? "Your phone number was added." : message == ManageMessageId.RemovePhoneSuccess ? "Your phone number was removed." : ""; IndexViewModel model = new IndexViewModel { HasPassword = (user.PasswordHash != null) ? true : false, PhoneNumber = user.PhoneNumber, TwoFactor = user.TwoFactorEnabled, //Logins = user.Logins, //BrowserRemembered = user.brows AccountRole = accountRole }; switch (userRole) { case UserRole.Patient: // Patient Patient patient = new Patient(); patient = _patientService.GetPatient(user.PatientId); model.Username = user.UserName; model.Weight = patient.Weight; model.Height = patient.Height; model.Race = patient.Race.ToString(); model.Ethnicity = patient.Ethnicity.ToString(); model.Location = patient.Location.ToString(); model.Birthdate = patient.Birthdate; model.Gender = patient.Gender.ToString(); break; case UserRole.Physician: // Physician Physician physician = new Physician(); physician = _physicianService.GetPhysician(user.PhysicianId); model.Email = physician.Email; model.Username = user.UserName; model.Address = physician.Address; model.PhoneNumber = physician.PhoneNumber; model.FirstName = physician.FirstName; model.LastName = physician.LastName; break; case UserRole.Experiment_Administrator: // Experiment Administrator ExperimentAdministrator experimentAdministrator = new ExperimentAdministrator(); experimentAdministrator = _experimentAdminService.GetExperimentAdministrator(user.ExperimentAdministratorId); model.Email = experimentAdministrator.Email; model.Username = user.UserName; model.Address = experimentAdministrator.Address; model.PhoneNumber = experimentAdministrator.PhoneNumber; model.FirstName = experimentAdministrator.FirstName; model.LastName = experimentAdministrator.LastName; break; case UserRole.System_Administrator: // System Admin model.Username = user.UserName; model.Email = user.Email; break; default: // Display error break; } return View(model); }
/// <summary> /// Add a new ExperimentAdministrator to the database /// </summary> /// <param name="experimentAdmin">Experiment Administrator object to add to the database</param> public void CreateExperimentAdministrator(ExperimentAdministrator experimentAdmin) { if(experimentAdmin != null) { _experimentAdminRepository.Add(experimentAdmin); } }