/// <summary> /// Set a user to OrganizationMaintainer. /// The user will add to DepartmentApplicationUser as maintainer by all department in current organization. /// /// </summary> /// <param name="departmentApplicationUserViewModel"></param> public async Task UpdateOrganizationMaintainer(DepartmentApplicationUserViewModel departmentApplicationUserViewModel) { try { //Update IsOrganizationMaintainer in AspNetUser table var currentUser = _userManager.FindByIdAsync(departmentApplicationUserViewModel.ApplicationUserId).Result; currentUser.IsOrganizationMaintainer = departmentApplicationUserViewModel.ApplicationUser.IsOranizationMaintainer; var currentDepartment = _db.Departments.FirstOrDefaultAsync(x => x.DepartmentId == departmentApplicationUserViewModel.DepartmentId).Result; //TODO: Remove or fix. Når man fjerner brukeren fra DepartmentApplicaction, så har bruekrne ingen department og forsvinner fra nettsiden. /** * var departmens = * _db.Departments.Where(x => x.OrganizationId == currentDepartment.OrganizationId).ToList(); * foreach (var jaja in departmens) * { * DepartmentApplicationUser departmentApplicationUser = new DepartmentApplicationUser * { * ApplicationUserId = currentUser.Id, * DepartmentId = jaja.DepartmentId, * IsMaintainer = departmentApplicationUserViewModel.ApplicationUser.IsOranizationMaintainer * }; * if (departmentApplicationUserViewModel.ApplicationUser.IsOranizationMaintainer) * { * _db.DepartmentApplicationUsers.Update(departmentApplicationUser); * await _db.SaveChangesAsync(); * } * else * { * _db.DepartmentApplicationUsers.Remove(departmentApplicationUser); * await _db.SaveChangesAsync(); * } * } **/ _db.ApplicationUsers.Update(currentUser); await _db.SaveChangesAsync(); //Update AspNetUserRoles var isMaintainer = departmentApplicationUserViewModel.ApplicationUser.IsOranizationMaintainer; var roleName = Roles.OrganizationMaintainer; if (isMaintainer) { await _userManager.AddToRoleAsync(currentUser, roleName); } else { await _userManager.RemoveFromRoleAsync(currentUser, roleName); } } catch (Exception e) { Console.WriteLine(e); throw; } }
public async Task <IActionResult> DMaintainerUpdate( [FromBody] DepartmentApplicationUserViewModel departmentApplicationUserViewModel) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } await _repository.UpdateMaintainDepartment(departmentApplicationUserViewModel); return(Ok(departmentApplicationUserViewModel)); }
/// <summary> /// When user is granted to be a maintainer to current Department /// Update isMaintainer in DepartmentApplicationUsers to current Department /// Add DepartmentRoles to the Current user. /// /// When remove Department Maintainer roles, will remove isMaintainer and Role. /// </summary> /// <param name="departmentApplicationUserView">Vale with isMaintainer, Department and username</param> public async Task UpdateMaintainDepartment(DepartmentApplicationUserViewModel departmentApplicationUserView) { //Update the DepartmentApplicationUser to isMaintainer to current user DepartmentApplicationUser departmentApplicationUser = new DepartmentApplicationUser { ApplicationUserId = departmentApplicationUserView.ApplicationUserId, DepartmentId = departmentApplicationUserView.DepartmentId, IsMaintainer = departmentApplicationUserView.IsMaintainer }; _db.DepartmentApplicationUsers.Update(departmentApplicationUser); await _db.SaveChangesAsync(); var currentUser = _userManager.FindByIdAsync(departmentApplicationUser.ApplicationUserId).Result; var roleName = Roles.DepartmentMaintainer; //If user is not in DepartmentRoles so add to role or removce. if (departmentApplicationUser.IsMaintainer) { var isInRole = await _userManager.IsInRoleAsync(currentUser, "Department Maintainer"); if (!isInRole) { await _userManager.AddToRoleAsync(currentUser, roleName); } } else { var isUserMaintainer = _db.DepartmentApplicationUsers.Any(x => x.IsMaintainer && x.ApplicationUserId == currentUser.Id); if (!isUserMaintainer) { await _userManager.RemoveFromRoleAsync(currentUser, roleName); } } }