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 ActionResult SetUserToNewRole(Guid appUserId, Guid branchId, string newRoleId) { int roleId = 0; int.TryParse(newRoleId, out roleId); BranchUserHelpers.UpdateBranchUserRoleForAllBranches(appUserId, (UserRoleEnum)roleId); //This will re-assign admin tasks to the other Admins (if not already assigned) UserTaskAssignmentHelpers.ReassignAllTasksForUserChangingRoleFromAdmin(appUserId); return(Json(new { success = true })); }
public ActionResult OtherAdminUsersExistForCompany(Guid appUserId, Guid branchId) { List <BranchUser> branchUsers = BranchUserHelpers.GetAdminBranchUsersForBranchExcludingUser(branchId, appUserId); BranchUser branchUserForCallingUser = BranchUserHelpers.GetBranchUser(appUserId, branchId, BranchHelpers.GetBranch(branchId).CompanyId); //Add 1 to the selected item as there is a blank option at the start string originalSelectedItem = ((int)branchUserForCallingUser.UserRole + 1).ToString(); if (branchUsers == null || branchUsers.Count == 0) { return(Json(new { success = false, originalRole = originalSelectedItem })); } else { return(Json(new { success = true, originalRole = originalSelectedItem })); } }
// GET: Task public ActionResult Details(Guid?userTaskId) { if (userTaskId == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } ApplicationDbContext db = new ApplicationDbContext(); //Get current user for building the Usertaskview of selected record AppUser appUser = AppUserHelpers.GetAppUser(db, User); //Get UserTaskView of selected UserTask record UserTaskView userTaskView = null; UserTaskFullView userTaskFullView = new UserTaskFullView() { UserTaskView = userTaskView }; try //try helps with issues, if no records for example then we are left with null userTaskView instead of error { userTaskView = UserTaskViewHelpers.GetUserTaskForUserView(db, appUser.AppUserId, userTaskId.Value); //Get the requestor appuser details and branch details AppUser createdByAppUser = AppUserHelpers.GetAppUser(userTaskView.CreatedBy.AppUserId); Branch createdByAppUserCurrentBranch = BranchHelpers.GetCurrentBranchForUser(db, userTaskView.CreatedBy.AppUserId); //If this is a on-hold user then get the current user role BranchUser branchUser = null; if (userTaskView.AppUserReference != null) { branchUser = BranchUserHelpers.GetBranchUser(db, userTaskView.AppUserReference.AppUserId, userTaskView.AppUserReference.CurrentBranchId); } //Build the view model userTaskFullView = new UserTaskFullView(); userTaskFullView.UserTaskView = userTaskView; if (branchUser != null) { userTaskFullView.BranchUserUserRole = branchUser.UserRole; } userTaskFullView.CreatedByAppUser = createdByAppUser; userTaskFullView.CreatedByAppUserCurrentBranch = createdByAppUserCurrentBranch; if (userTaskView.AppUserReference != null) { ViewBag.EntityStatusUserRole = EnumHelpers.GetDescription((EntityStatusEnum)userTaskFullView.UserTaskView.AppUserReference.EntityStatus); } else { ViewBag.EntityStatusUserRole = ""; } if (userTaskView.BranchReference != null) { ViewBag.EntityStatusBranchStatus = EnumHelpers.GetDescription((EntityStatusEnum)userTaskFullView.UserTaskView.BranchReference.EntityStatus); } else { ViewBag.EntityStatusBranchStatus = ""; } ViewBag.EntityStatusCreatedByUserRole = EnumHelpers.GetDescription((EntityStatusEnum)userTaskFullView.CreatedByAppUser.EntityStatus); ViewBag.EntityStatusCreatedByUserBranchStatus = EnumHelpers.GetDescription((EntityStatusEnum)userTaskFullView.CreatedByAppUserCurrentBranch.EntityStatus); ViewBag.UserTaskUserRole = EnumHelpers.GetDescription((UserRoleEnum)branchUser.UserRole); } catch { } return(View(userTaskFullView)); }
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)); }