Permission.cshtml.cs 2.2 KB

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