RoleService.cs 4.0 KB

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