Permission.cshtml.cs 2.0 KB

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