public new async Task <IActionResult> User(string userId) { var user = await _UserManager.FindByIdAsync(userId); if (user == null) { return(NotFound()); } var roles = await _UserManager.GetRolesAsync(user); var userVM = new UsersViewModel.UserViewModel { Id = user.Id, Email = user.Email, Verified = user.EmailConfirmed || !user.RequiresEmailConfirmation, IsAdmin = _userService.IsRoleAdmin(roles) }; return(View(userVM)); }
public new async Task <IActionResult> User(string userId, UsersViewModel.UserViewModel viewModel) { var user = await _UserManager.FindByIdAsync(userId); if (user == null) { return(NotFound()); } var admins = await _UserManager.GetUsersInRoleAsync(Roles.ServerAdmin); var roles = await _UserManager.GetRolesAsync(user); var wasAdmin = _userService.IsRoleAdmin(roles); if (!viewModel.IsAdmin && admins.Count == 1 && wasAdmin) { TempData[WellKnownTempData.ErrorMessage] = "This is the only Admin, so their role can't be removed until another Admin is added."; return(View(viewModel)); // return } if (viewModel.IsAdmin != wasAdmin) { if (viewModel.IsAdmin) { await _UserManager.AddToRoleAsync(user, Roles.ServerAdmin); } else { await _UserManager.RemoveFromRoleAsync(user, Roles.ServerAdmin); } TempData[WellKnownTempData.SuccessMessage] = "User successfully updated"; } return(RedirectToAction(nameof(User), new { userId = userId })); }