public static AppUser UpdateAppUserFromAppUserEditView(ApplicationDbContext db, AppUserEditView view) { //Update AppUser AppUser appUser = GetAppUser(db, view.AppUserId); appUser.FirstName = view.FirstName; appUser.LastName = view.LastName; appUser.EntityStatus = view.EntityStatus; appUser.PrivacyLevel = view.PrivacyLevel; if (view.SelectedBranchId != null) { appUser.CurrentBranchId = view.SelectedBranchId.Value; } db.Entry(appUser).State = EntityState.Modified; db.SaveChanges(); //Update BranchUser (Role) BranchUser branchUser = BranchUserHelpers.GetBranchUser(db, appUser.AppUserId, appUser.CurrentBranchId); BranchUserHelpers.UpdateBranchUserRole(db, branchUser.BranchUserId, view.UserRole); //Update UserSettings AppUserSettingsHelpers.UpdateUserSettingsFromAppUserEditView(db, view); return(appUser); }
public static bool UpdateUsersFromUserAdminView(ApplicationDbContext db, List <UserAdminView> userAdminViewForUser, IPrincipal user) { //Get logged in user details for Task creation (if required) AppUser loggedInUser = AppUserHelpers.GetAppUser(db, user); try { List <Guid> userChangedFromAdminList = new List <Guid>(); foreach (UserAdminView userAdminView in userAdminViewForUser) { //Get original appUser record so that we can compare previous and current entity status' AppUser appUser = AppUserHelpers.GetAppUser(db, userAdminView.AppUserId); EntityStatusEnum previousEntityStatus = appUser.EntityStatus; //Update the AppUser record (except Current Branch as that was done in real time) appUser = AppUserHelpers.UpdateAppUserExcludingCurrentBranchField(db, userAdminView.AppUserId, userAdminView.FirstName, userAdminView.LastName, userAdminView.AppUserEntityStatus, userAdminView.PrivacyLevel); //if change of status from on-hold - anything then look for outstanding task and set to closed if (userAdminView.AppUserEntityStatus != EntityStatusEnum.OnHold && previousEntityStatus == EntityStatusEnum.OnHold) { UserTaskHelpers.CloseAllTasksForUserChangingStatusFromOnHold(db, appUser.AppUserId); } //If change of status to on-hold then create a Task if (userAdminView.AppUserEntityStatus == EntityStatusEnum.OnHold && previousEntityStatus != EntityStatusEnum.OnHold) { UserTaskHelpers.CreateUserTask(TaskTypeEnum.UserOnHold, "User on hold, awaiting administrator/manager activation", appUser.AppUserId, loggedInUser.AppUserId, EntityStatusEnum.Active); } //If change of status from Active then check outstanding actions/tasks and reassign/create new tasks/actions??? //CURRENTLY - don't do anything for ACTIONS as these are specific and TASKS will be assigned at ADMIN level also so don't do anything here at present //If change of role from ADMIN to something else then tasks will need to be re-assigned - THIS IS DONE IN /Data/SetUserToNewRole List <Guid> userSetToAdminList = new List <Guid>(); //Update the User Role on each Branch foreach (UserAdminRelatedBranchesView relatedBranch in userAdminView.RelatedBranches) { //just keep a list of the user's we have set to Admin as within the UpdateBranchUserRole, if a role is set to Admin on 1 user, then that user on every branch is set to Admin, so we don't want to set it back Guid toAdminResult = userSetToAdminList.Find(x => x == relatedBranch.AppUserId); if (toAdminResult == Guid.Empty) { BranchUserHelpers.UpdateBranchUserRole(db, relatedBranch.BranchUserId, relatedBranch.UserRole); if (relatedBranch.UserRole == UserRoleEnum.Admin) { userSetToAdminList.Add(relatedBranch.AppUserId); } } } } return(true); } catch (Exception e) { Console.WriteLine("{0} Exception caught.", e); return(false); } }