| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- using Application.Abstractions.Identity.Models;
- using Application.Features.Director.GetRolePermissions;
- using Application.Features.Director.UpdateRolePermissions;
- using MediatR;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.AspNetCore.Mvc.RazorPages;
- namespace Admin.Pages.Director.Role
- {
- public class PermissionModel(IMediator mediator) : PageModel
- {
- [BindProperty]
- public Response Role { get; set; } = new() { RoleID = string.Empty };
- public async Task<IActionResult> OnGetAsync(string id, CancellationToken ct)
- {
- if (string.IsNullOrEmpty(id))
- {
- TempData["ErrorMessages"] = "유효하지 않은 접근입니다.";
- return RedirectToPage("/Director/Role/Index");
- }
- Role = await mediator.Send(new GetRoleClaimsQuery(id), ct);
- return Page();
- }
- public async Task<IActionResult> OnPostAsync(CancellationToken ct)
- {
- try
- {
- if (!ModelState.IsValid)
- {
- throw new Exception("유효성 검사에 실패하였습니다.");
- }
- var permissions = new PermissionDto
- {
- RoleClaims = [.. Role.RoleClaims.Select(g => new PermissionDto.PermissionGroup
- {
- Permissions = [.. g.Permissions.Select(p => new PermissionDto.PermissionGroup.Checkbox
- {
- DisplayValue = p.DisplayValue,
- IsSelected = p.IsSelected
- })]
- })]
- };
- var command = new UpdateRolePermissionsCommand(
- Role.RoleID,
- permissions
- );
- await mediator.Send(command, ct);
- TempData["SuccessMessage"] = $"{Role.RoleName} 권한 정보가 수정되었습니다.";
- return RedirectToPage("/Director/Role/Index", new { id = Role.RoleID });
- }
- catch (Exception e)
- {
- TempData["ErrorMessages"] = e.Message;
- return Page();
- }
- }
- }
- }
|