Advertisement
LenCristoff

Untitled

Dec 5th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. namespace Eventures.Web.Areas.Admin.Pages.Users
  2. {
  3. using Data.Models;
  4. using Microsoft.AspNetCore.Authorization;
  5. using Microsoft.AspNetCore.Identity;
  6. using Microsoft.AspNetCore.Mvc;
  7. using Microsoft.AspNetCore.Mvc.RazorPages;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Threading.Tasks;
  11.  
  12. [Authorize(Roles = "Admin")]
  13. public class ManageRoleModel : PageModel
  14. {
  15. private readonly UserManager<User> userManager;
  16. private readonly RoleManager<IdentityRole> roleManager;
  17.  
  18. public ManageRoleModel(
  19. UserManager<User> userManager,
  20. RoleManager<IdentityRole> roleManager)
  21. {
  22. this.userManager = userManager;
  23. this.roleManager = roleManager;
  24. }
  25.  
  26. public UsersListWrapperViewModel UsersListWrapper { get; set; }
  27.  
  28. [TempData]
  29. public string ErrorMessage { get; set; }
  30.  
  31. public class UsersListWrapperViewModel
  32. {
  33. public IList<UsersListViewModel> Users { get; set; }
  34. }
  35.  
  36. public class UsersListViewModel
  37. {
  38. public string Id { get; set; }
  39.  
  40. public string Username { get; set; }
  41.  
  42. public string Role { get; set; }
  43. }
  44.  
  45. public void OnGet()
  46. {
  47. if (!string.IsNullOrEmpty(ErrorMessage))
  48. {
  49. ModelState.AddModelError(string.Empty, ErrorMessage);
  50. }
  51.  
  52. var users = new List<UsersListViewModel>();
  53. foreach (var user in this.userManager.Users)
  54. {
  55. if (user.UserName == this.User.Identity.Name)
  56. {
  57. continue;
  58. }
  59.  
  60. var role = this.userManager.GetRolesAsync(user).GetAwaiter().GetResult();
  61.  
  62. var userModel = new UsersListViewModel
  63. {
  64. Id = user.Id,
  65. Username = user.UserName,
  66. Role = role.FirstOrDefault()
  67. };
  68.  
  69. users.Add(userModel);
  70. }
  71.  
  72. this.UsersListWrapper = new UsersListWrapperViewModel { Users = users };
  73. this.ViewData["Roles"] = this.roleManager.Roles.Select(r => r.Name);
  74. }
  75.  
  76. public async Task<IActionResult> OnPost(string userId, string currentRole, string newRole)
  77. {
  78. var user = this.userManager.Users.FirstOrDefault(u => u.Id == userId);
  79. var currentRoleExists = await this.roleManager.RoleExistsAsync(currentRole);
  80. var newRoleExists = await this.roleManager.RoleExistsAsync(currentRole);
  81.  
  82. if (user != null && currentRoleExists && newRoleExists)
  83. {
  84. await this.userManager.RemoveFromRoleAsync(user, currentRole);
  85. await this.userManager.AddToRoleAsync(user, newRole);
  86. }
  87. else
  88. {
  89. this.TempData["Error"] = "Invalid data.";
  90. }
  91.  
  92. return RedirectToPage("./ManageRole");
  93. }
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement