Permission.cshtml.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using Application.Abstractions.Identity.Models;
  2. using MediatR;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.AspNetCore.Mvc.RazorPages;
  5. namespace Admin.Pages.Director.Role
  6. {
  7. public class PermissionModel(IMediator mediator) : PageModel
  8. {
  9. [BindProperty]
  10. public GetRolePermissions.Response Role { get; set; } = new() { RoleID = string.Empty };
  11. public async Task<IActionResult> OnGetAsync(string id, CancellationToken ct)
  12. {
  13. if (string.IsNullOrEmpty(id))
  14. {
  15. TempData["ErrorMessages"] = "유효하지 않은 접근입니다.";
  16. return RedirectToPage("/Director/Role/Index");
  17. }
  18. Role = await mediator.Send(new GetRolePermissions.Query(id), ct);
  19. return Page();
  20. }
  21. public async Task<IActionResult> OnPostAsync(CancellationToken ct)
  22. {
  23. try
  24. {
  25. if (!ModelState.IsValid)
  26. {
  27. throw new Exception("유효성 검사에 실패하였습니다.");
  28. }
  29. var permissions = new PermissionDto
  30. {
  31. RoleClaims = [.. Role.RoleClaims.Select(g => new PermissionDto.PermissionGroup
  32. {
  33. Permissions = [.. g.Permissions.Select(p => new PermissionDto.PermissionGroup.Checkbox
  34. {
  35. DisplayValue = p.DisplayValue,
  36. IsSelected = p.IsSelected
  37. })]
  38. })]
  39. };
  40. var command = new UpdateRolePermissions.Command(
  41. Role.RoleID,
  42. permissions
  43. );
  44. await mediator.Send(command, ct);
  45. TempData["SuccessMessage"] = $"{Role.RoleName} 권한 정보가 수정되었습니다.";
  46. return RedirectToPage("/Director/Role/Index", new { id = Role.RoleID });
  47. }
  48. catch (Exception e)
  49. {
  50. TempData["ErrorMessages"] = e.Message;
  51. return Page();
  52. }
  53. }
  54. }
  55. }