RoleService.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using System.Collections.ObjectModel;
  3. using System.Linq;
  4. using VCommon.DataModel;
  5. using VCommon.VApplication.Authorization;
  6. using VCommon.VApplication.Dto;
  7. using VCommon.VApplication.Linq.Expressions;
  8. using VCommon.VAutoMapper;
  9. using VCommon.VOpenApi.VAspNetCore;
  10. using VCommonCoreExample.AppServices.Basic;
  11. using VCommonCoreExample.AppServices.System.Dto;
  12. using VCommonCoreExample.AppServices.System.Roles.Dto;
  13. using VCommonCoreExample.Authorization;
  14. using VCommonCoreExample.Caching;
  15. using VCommonCoreExample.Entity;
  16. namespace VCommonCoreExample.AppServices.System.Roles
  17. {
  18. [VServiceAuthorize(PermissionCodes.System.Role.EntryRole)]
  19. public interface IRoleService
  20. {
  21. [VServiceAuthorize(PermissionCodes.System.Role.Query)]
  22. IPagedResult<RoleDto> List(RoleListRequest input);
  23. [VServiceAuthorize(
  24. PermissionCodes.System.Role.Create,
  25. PermissionCodes.System.Role.Update)]
  26. RoleFormPrepOutput FormPrep(Guid? input);
  27. [VServiceAuthorize(PermissionCodes.System.Role.Create)]
  28. Guid Create(RoleCreateInput input);
  29. [VServiceAuthorize(PermissionCodes.System.Role.Update)]
  30. void Update(RoleUpdateInput input);
  31. [VServiceAuthorize(PermissionCodes.System.Role.Delete)]
  32. void Delete(DeleteInput input);
  33. }
  34. public class RoleService : DbTableAppServiceBase<Role>, IRoleService
  35. {
  36. private readonly IPermissionManager _permissionManager;
  37. private readonly AuthCache _authCache;
  38. public RoleService(IPermissionManager permissionManager, AuthCache authCache)
  39. {
  40. _permissionManager = permissionManager;
  41. _authCache = authCache;
  42. }
  43. public IPagedResult<RoleDto> List(RoleListRequest input)
  44. {
  45. using var repo = GetRepository();
  46. var exp = PredicateBuilder.SearchByName(input);
  47. var count = repo.Count(exp);
  48. var items = repo.QueryNoTracking(exp)
  49. .OrderByCreateTimeDescAndPageBy(input)
  50. .ProjectionToArray<Role, RoleDto>();
  51. return new RoleListOutput(count, items);
  52. }
  53. public RoleFormPrepOutput FormPrep(Guid? input)
  54. {
  55. var tree = _permissionManager.GetPermissionTreeOutput(VSession.Side);
  56. var treeOutput = TreeCopy.CreateFrom<PermissionNodeOutput, ElTreeNode, string, string>(tree,
  57. (k, v, c) => new ElTreeNode { Key = k, Label = v, Children = c.ToArray() },
  58. p => p.Code,
  59. p => p.Name,
  60. p => p.Children);
  61. var result = new RoleFormPrepOutput(treeOutput.ToArray());
  62. if (input.HasValue)
  63. {
  64. using var repo = GetRepository();
  65. result.Output = repo.QueryProjection<RoleDto>(input.Value);
  66. }
  67. return result;
  68. }
  69. public Guid Create(RoleCreateInput input)
  70. {
  71. foreach (var s in input.PermissionSet)
  72. {
  73. if (false == _permissionManager.ValidPermission(s)) throw new VFriendlyException($"无效的权限代码:{s}");
  74. }
  75. var repo = GetRepository();
  76. if (repo.CheckExist(p => p.Name == input.Name)) throw new VFriendlyException("角色名称已经存在");
  77. var entity = input.MapTo<Role>();
  78. repo.Add(entity);
  79. repo.SaveChanges();
  80. return entity.Id;
  81. }
  82. public void Update(RoleUpdateInput input)
  83. {
  84. foreach (var s in input.PermissionSet)
  85. {
  86. if (false == _permissionManager.ValidPermission(s)) throw new VFriendlyException($"无效的权限代码:{s}");
  87. }
  88. var repo = GetRepository();
  89. if (repo.CheckExist(p => p.Id != input.Id && p.Name == input.Name)) throw new VFriendlyException("角色名称已经存在");
  90. var entity = repo.GetEntityOrDefault(input.Id) ?? throw new VFriendlyException("找不到角色");
  91. input.MapTo(entity);
  92. repo.SaveChanges();
  93. _authCache.ClearRole(VSession.TenantId, entity.Id);
  94. }
  95. public void Delete(DeleteInput input)
  96. {
  97. var repo = GetRepository();
  98. var entity = repo.GetEntityOrDefault(input.Id) ?? throw new VFriendlyException("找不到角色");
  99. repo.Delete(entity);
  100. repo.SaveChanges();
  101. _authCache.ClearRole(VSession.TenantId, input.Id);
  102. }
  103. }
  104. }