Handler.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using Application.Abstractions.Messaging;
  2. using Application.Abstractions.Identity;
  3. using SharedKernel.Constants;
  4. using System.Data;
  5. namespace Application.Features.Director.Role.Permissions.Get;
  6. public sealed class Handler(IIdentityRoleReader roleReader) : IQueryHandler<Query, Response>
  7. {
  8. public async Task<Response> Handle(Query request, CancellationToken ct)
  9. {
  10. var roleAndClaims = await roleReader.GetRoleAsync(request.RoleID, ct);
  11. if (roleAndClaims is null)
  12. {
  13. throw new DataException($"Role with ID '{request.RoleID}' not found.");
  14. }
  15. string[] DefaultActions = { "Create", "View", "Edit", "Delete" };
  16. var groups = new List<Response.PermissionGroup>();
  17. var menus = Menus.GetMenus();
  18. void Traverse(Menu menu, string? parentName = null)
  19. {
  20. var fullGroupName = string.IsNullOrWhiteSpace(parentName) ? menu.Name : $"{parentName} - {menu.Name}";
  21. var fullName = string.IsNullOrWhiteSpace(parentName) ? $"{menu.Name}.{menu.Id}" : $"{parentName} - {menu.Name}.{menu.Id}";
  22. // 권한 그룹 생성
  23. if (!string.IsNullOrWhiteSpace(menu.Path) || (menu.Children != null && menu.Children.Count > 0))
  24. {
  25. groups.Add(new Response.PermissionGroup
  26. {
  27. GroupName = fullGroupName,
  28. Permissions = [..DefaultActions.Select(action =>
  29. {
  30. var permission = $"Permissions.{fullName}.{action}";
  31. return new Response.PermissionGroup.Checkbox {
  32. DisplayValue = permission,
  33. IsSelected = roleAndClaims.Claims.Contains(permission)
  34. };
  35. })]
  36. });
  37. }
  38. if (menu.Children != null)
  39. {
  40. foreach (var child in menu.Children)
  41. {
  42. Traverse(child, menu.Name);
  43. }
  44. }
  45. }
  46. foreach (var menu in menus)
  47. {
  48. Traverse(menu);
  49. }
  50. return new Response {
  51. RoleID = roleAndClaims.ID,
  52. RoleName = roleAndClaims.Name,
  53. RoleClaims = groups
  54. };
  55. }
  56. }