public async Task <ActionResult> AddUser([Bind(Include = "FirstName,LastName,LoginEmail,LoginPassword,ConfirmPassword,PrivacyLevel,UserRole")] UserAdminAddUserView model) { if (Request.Form["resetbutton"] != null) { return(RedirectToAction("AddUser")); } if (ModelState.IsValid) { //Create a new AppUser AppUser appUser = AppUserHelpers.CreateAppUser(db, model, User); var user = new ApplicationUser { UserName = model.LoginEmail, Email = model.LoginEmail, AppUserId = appUser.AppUserId, CurrentUserRole = appUser.UserRole }; var result = await UserManager.CreateAsync(user, model.LoginPassword); if (result.Succeeded) { if (Request.Form["adduserbutton"] != null) { return(RedirectToAction("AddUser")); } return(RedirectToAction("UserAdmin")); } //Delete the appUser account as this has not gone through AppUserHelpers.DeleteAppUser(db, appUser.AppUserId); AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }
public async Task <ActionResult> Create(AppUserView model) { if (ModelState.IsValid) { //initialise the task creation flags bool createUserOnHoldTask = false; //Retrieve Branch Branch branch = BranchHelpers.GetBranch(db, model.SelectedBranchId.Value); //Create a new AppUser then write here AppUser appUser = AppUserHelpers.CreateAppUser(model.FirstName, model.LastName, branch.BranchId, model.EntityStatus, model.Email, model.PrivacyLevel, model.UserRole); BranchUser branchUser = null; var user = new ApplicationUser { UserName = model.Email, Email = model.Email, AppUserId = appUser.AppUserId, FullName = model.FirstName + " " + model.LastName, CurrentUserRole = model.UserRole }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { //set on-hold task flag if (model.EntityStatus == EntityStatusEnum.OnHold) { createUserOnHoldTask = true; } //Now Update related entities //BranchUser - set the status as ACTIVE as the link is active even though the entities linked are not. branchUser = BranchUserHelpers.CreateBranchUser(appUser.AppUserId, branch.BranchId, branch.CompanyId, model.UserRole, EntityStatusEnum.Active); //Task creation if (createUserOnHoldTask) { UserTaskHelpers.CreateUserTask(TaskTypeEnum.UserOnHold, "New user on hold, awaiting administrator/manager activation", appUser.AppUserId, appUser.AppUserId, EntityStatusEnum.Active); } return(RedirectToAction("UserAdmin", "Admin")); } //Delete the appUser account as this has not gone through AppUserHelpers.DeleteAppUser(appUser.AppUserId); AddErrors(result); } // If we got this far, something failed, redisplay form - set up the drop downs dependant on what was there originally from the model Branch userBranch = BranchHelpers.GetCurrentBranchForUser(AppUserHelpers.GetGuidFromUserGetAppUserId(User.Identity.GetAppUserId())); //DropDown ViewBag.BranchList = ControlHelpers.AllBranchesForCompanyListDropDown(userBranch.CompanyId, userBranch.BranchId); ViewBag.UserRoleList = ControlHelpers.UserRoleEnumListDropDown(); ViewBag.EntityStatusList = ControlHelpers.EntityStatusEnumListDropDown(); return(View(model)); }
public async Task <ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { //Create a new AppUser AppUser appUser = AppUserHelpers.CreateAppUser(model, User, UserRoleEnum.User); var user = new ApplicationUser { UserName = model.Email, Email = model.Email, AppUserId = appUser.AppUserId, CurrentUserRole = appUser.UserRole }; 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 https://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")); } else { //Delete the appUser account as this has not gone through AppUserHelpers.DeleteAppUser(appUser.AppUserId); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }
public async Task <ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { //If this is a new user and company then set to ACTIVE and an ADMIN role, else set to ON-HOLD and USER role and await activating by admin for branch/company of user and/or new branch details. EntityStatusEnum statusForUser = EntityStatusEnum.Active; UserRoleEnum userRoleForUser = UserRoleEnum.Admin; //initialise the task creation flags bool createUserOnHoldTask = false; bool createBranchOnHoldTask = false; if (model.SelectedCompanyId.HasValue) { statusForUser = EntityStatusEnum.OnHold; userRoleForUser = UserRoleEnum.User; } //Create a new AppUser then write here AppUser appUser = AppUserHelpers.CreateAppUser(model.FirstName, model.LastName, Guid.Empty, statusForUser, model.Email, PrivacyLevelEnum.None, userRoleForUser); Company company = null; Branch branch = null; BranchUser branchUser = null; var user = new ApplicationUser { UserName = model.Email, Email = model.Email, AppUserId = appUser.AppUserId, FullName = model.FirstName + " " + model.LastName, CurrentUserRole = userRoleForUser }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { //only log in if this user is not set to on-hold if (!model.SelectedCompanyId.HasValue) { await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); } else //we will need to create a task for the branch { createUserOnHoldTask = true; } //Now Update related entities //Company bool createCompany = true; if (model.SelectedCompanyId.HasValue) { if (model.SelectedCompanyId.Value != Guid.Empty) { createCompany = false; } } if (createCompany) { company = CompanyHelpers.CreateCompany(Guid.Empty, model.CompanyName, model.CompanyRegistrationDetails, model.CharityRegistrationDetails, model.VATRegistrationDetails, model.AllowBranchTrading, PrivacyLevelEnum.None, statusForUser); } else { company = CompanyHelpers.GetCompany(model.SelectedCompanyId.Value); } //Branch bool createBranch = true; if (model.SelectedBranchId.HasValue) { if (model.SelectedBranchId.Value != Guid.Empty) { createBranch = false; } } if (createBranch) { string branchName = model.BranchName; if (!model.SelectedCompanyId.HasValue) { branchName = "Head Office"; } if (createCompany) //use details stored against company part of model { branch = BranchHelpers.CreateBranch(company.CompanyId, model.CompanyBusinessType.Value, branchName, model.CompanyAddressLine1, model.CompanyAddressLine2, model.CompanyAddressLine3, model.CompanyAddressTownCity, model.CompanyAddressCounty, model.CompanyAddressPostcode, model.CompanyTelephoneNumber, model.CompanyEmail, model.CompanyContactName, company.PrivacyLevel, statusForUser); } else { //set last addAdminUsers flag to true as this is a new branch on an existing company so all Admin users need to be associated with this branch branch = BranchHelpers.CreateBranch(company.CompanyId, model.BranchBusinessType.Value, branchName, model.BranchAddressLine1, model.BranchAddressLine2, model.BranchAddressLine3, model.BranchAddressTownCity, model.BranchAddressCounty, model.BranchAddressPostcode, model.BranchTelephoneNumber, model.BranchEmail, model.BranchContactName, company.PrivacyLevel, statusForUser); createBranchOnHoldTask = true; } //Company - set head office branch as the newly created branch for this new company (defaults to 'Head Office') if (!model.SelectedCompanyId.HasValue) { company = CompanyHelpers.UpdateCompanyHeadOffice(company.CompanyId, branch.BranchId); } } else { branch = BranchHelpers.GetBranch(model.SelectedBranchId.Value); } //BranchUser - set the status as ACTIVE as the link is active even though the entities linked are not. branchUser = BranchUserHelpers.CreateBranchUser(appUser.AppUserId, branch.BranchId, company.CompanyId, userRoleForUser, EntityStatusEnum.Active); //if addAdminUsersToThisBranch is true then add all admin users for the company to this branch BranchUserHelpers.CreateBranchAdminUsersForNewBranch(branch, userRoleForUser); //Update AppUser with the branch we are adding/using to set as current branch for new user appUser = AppUserHelpers.UpdateCurrentBranchId(appUser.AppUserId, branch.BranchId); // For more information on how to enable account confirmation and password reset please visit https://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>"); //Task creation if (createUserOnHoldTask) { UserTaskHelpers.CreateUserTask(TaskTypeEnum.UserOnHold, "New user on hold, awaiting administrator/manager activation", appUser.AppUserId, appUser.AppUserId, EntityStatusEnum.Active); } if (createBranchOnHoldTask) { UserTaskHelpers.CreateUserTask(TaskTypeEnum.BranchOnHold, "New branch on hold, awaiting administrator activation", branch.BranchId, appUser.AppUserId, EntityStatusEnum.Active); } if (model.SelectedCompanyId.HasValue) { return(RedirectToAction("Confirmation")); } else { return(RedirectToAction("Index", "Home")); } } //Delete the appUser account as this has not gone through AppUserHelpers.DeleteAppUser(appUser.AppUserId); AddErrors(result); } // If we got this far, something failed, redisplay form - set up the drop downs dependant on what was there originally from the model if (model.SelectedCompanyId.HasValue) { ViewBag.CompanyList = ControlHelpers.AllCompaniesListDropDown(model.SelectedCompanyId.Value); if (model.SelectedBranchId.HasValue) { ViewBag.BranchList = ControlHelpers.AllBranchesForCompanyListDropDown(model.SelectedCompanyId.Value, model.SelectedBranchId.Value); } else { ViewBag.BranchList = new SelectList(Enumerable.Empty <SelectListItem>(), "BranchId", "BranchName"); } } else { ViewBag.CompanyList = ControlHelpers.AllCompaniesListDropDown(); ViewBag.BranchList = new SelectList(Enumerable.Empty <SelectListItem>(), "BranchId", "BranchName"); } ViewBag.BusinessTypeList = ControlHelpers.BusinessTypeEnumListDropDown(); return(View(model)); }