平安校园
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

RoleService.cs 11 KiB

há 3 meses
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. 
  2. //
  3. using Microsoft.Extensions.Logging;
  4. using NewLife.Serialization;
  5. namespace SafeCampus.Application;
  6. /// <inheritdoc cref="IRoleService"/>
  7. public class RoleService : IRoleService
  8. {
  9. private readonly ILogger<RoleService> _logger;
  10. private readonly ISysUserService _sysUserService;
  11. private readonly ISysRoleService _sysRoleService;
  12. private readonly IResourceService _resourceService;
  13. private readonly IRelationService _relationService;
  14. public RoleService(ILogger<RoleService> logger, ISysUserService sysUserService, ISysRoleService sysRoleService,
  15. IResourceService resourceService, IRelationService relationService)
  16. {
  17. _logger = logger;
  18. _sysUserService = sysUserService;
  19. _sysRoleService = sysRoleService;
  20. _resourceService = resourceService;
  21. _relationService = relationService;
  22. }
  23. #region 查询
  24. /// <inheritdoc/>
  25. public async Task<RoleOwnResourceOutput> OwnResource(BaseIdInput input, string category)
  26. {
  27. return await _sysRoleService.OwnResource(input, category);
  28. }
  29. /// <inheritdoc/>
  30. public async Task<List<UserSelectorOutPut>> OwnUser(BaseIdInput input)
  31. {
  32. return await _sysRoleService.OwnUser(input);
  33. }
  34. /// <inheritdoc/>
  35. public async Task<SqlSugarPagedList<SysRole>> Page(RolePageInput input)
  36. {
  37. //获取数据范围
  38. var dataScope = await _sysUserService.GetLoginUserApiDataScope();
  39. input.OrgIds = dataScope;
  40. input.Category = CateGoryConst.ROLE_ORG;//只查询机构角色
  41. //分页查询
  42. var pageInfo = await _sysRoleService.Page(input);
  43. return pageInfo;
  44. }
  45. /// <inheritdoc/>
  46. public async Task<SqlSugarPagedList<RoleSelectorOutPut>> RoleSelector(RoleSelectorInput input)
  47. {
  48. //获取数据范围
  49. var dataScope = await _sysUserService.GetLoginUserApiDataScope();
  50. input.OrgIds = dataScope;
  51. return await _sysRoleService.RoleSelector(input);
  52. }
  53. /// <inheritdoc/>
  54. public async Task<List<RoleTreeOutput>> Tree(RoleTreeInput input)
  55. {
  56. var dataScope = await _sysUserService.GetLoginUserApiDataScope();
  57. input.OrgIds = dataScope;
  58. return await _sysRoleService.Tree(input);
  59. }
  60. /// <inheritdoc/>
  61. public async Task<SysRole> Detail(BaseIdInput input)
  62. {
  63. var role = await _sysRoleService.Detail(input);
  64. var errorMessage = "您没有权限查看该角色";
  65. await _sysUserService.CheckApiDataScope(role.OrgId, role.CreateUserId, errorMessage);
  66. return role;
  67. }
  68. /// <inheritdoc/>
  69. public async Task<List<ResTreeSelector>> ResourceTreeSelector()
  70. {
  71. var resourceTreeSelectors = new List<ResTreeSelector>();//定义结果
  72. var user = await _sysUserService.GetUserById(UserManager.UserId);//获取用户
  73. //获取角色资源关系
  74. var relations =
  75. await _relationService.GetRelationListByObjectIdListAndCategory(user.RoleIdList, CateGoryConst.RELATION_SYS_ROLE_HAS_RESOURCE);
  76. var menuIds = new HashSet<long>();//菜单ID集合
  77. var buttonIds = new HashSet<long>();//按钮ID集合
  78. relations.ForEach(it =>
  79. {
  80. var resources = it.ExtJson.ToJsonEntity<RelationRoleResource>();//转换为对象获取资源
  81. menuIds.Add(resources.MenuId);//添加菜单ID
  82. resources.ButtonInfo.ForEach(b => buttonIds.Add(b));//添加按钮ID
  83. });
  84. var menuList = await _resourceService.GetListByCategory(CateGoryConst.RESOURCE_MENU);//获取所有菜单
  85. var buttonList = await _resourceService.GetListByCategory(CateGoryConst.RESOURCE_BUTTON);//获取所有按钮
  86. var moduleList = await _resourceService.GetListByCategory(CateGoryConst.RESOURCE_MODULE);//获取所有模块
  87. var ownMenuList = menuList.Where(it => menuIds.Contains(it.Id)).ToList();//已拥有的菜单
  88. var parentMenuList = menuList.Where(it => ownMenuList.Select(m => m.ParentId).Contains(it.Id)).ToList();//获取所有父级菜单
  89. ownMenuList.AddRange(parentMenuList);//将父级菜单加入到拥有菜单列表
  90. var ownButtonList = buttonList.Where(it => buttonIds.Contains(it.Id)).ToList();//已拥有的按钮
  91. var moduleIds = ownMenuList.Select(it => it.Module.GetValueOrDefault()).Distinct().ToList();//模块ID集合
  92. var ownModuleList = moduleList.Where(it => moduleIds.Contains(it.Id)).ToList();//已拥有的模块
  93. //遍历模块
  94. foreach (var module in ownModuleList)
  95. {
  96. //将实体转换为ResourceTreeSelectorOutPut获取基本信息
  97. var resourceTreeSelector = module.Adapt<ResTreeSelector>();
  98. resourceTreeSelector.Menu = await GetRoleGrantResourceMenus(module.Id, ownMenuList, ownButtonList);//获取授权菜单
  99. resourceTreeSelectors.Add(resourceTreeSelector);
  100. }
  101. return resourceTreeSelectors;
  102. }
  103. #endregion
  104. #region 增加
  105. /// <inheritdoc/>
  106. public async Task Add(RoleAddInput input)
  107. {
  108. await CheckInput(input);//检查参数
  109. await _sysRoleService.Add(input);
  110. }
  111. #endregion
  112. #region 编辑
  113. /// <inheritdoc/>
  114. public async Task Edit(RoleEditInput input)
  115. {
  116. await CheckInput(input);//检查参数
  117. await _sysRoleService.Edit(input);
  118. }
  119. /// <inheritdoc/>
  120. public async Task GrantResource(GrantResourceInput input)
  121. {
  122. await _sysRoleService.GrantResource(input);
  123. }
  124. /// <inheritdoc/>
  125. public async Task GrantUser(GrantUserInput input)
  126. {
  127. await _sysRoleService.GrantUser(input);
  128. }
  129. #endregion
  130. #region 删除
  131. /// <inheritdoc/>
  132. public async Task Delete(BaseIdListInput input)
  133. {
  134. //获取所有ID
  135. var ids = input.Ids;
  136. //获取要删除的机构列表
  137. var roleList = (await _sysRoleService.GetListAsync()).Where(it => ids.Contains(it.Id)).ToList();
  138. if (roleList.Any(it => it.Category == CateGoryConst.ROLE_GLOBAL))//如果有全局角色
  139. throw Oops.Bah("不能删除全局角色");
  140. //检查数据范围
  141. var orgIds = roleList.Select(it => it.OrgId.GetValueOrDefault()).ToList();
  142. var createUserIds = roleList.Select(it => it.CreateUserId.GetValueOrDefault()).ToList();
  143. await _sysUserService.CheckApiDataScope(orgIds, createUserIds, "您没有权限删除这些角色");
  144. await _sysRoleService.Delete(input);
  145. }
  146. #endregion
  147. #region 方法
  148. /// <summary>
  149. /// 检查输入参数
  150. /// </summary>
  151. /// <param name="sysRole"></param>
  152. private async Task CheckInput(SysRole sysRole)
  153. {
  154. sysRole.Category = CateGoryConst.ROLE_ORG;
  155. if (sysRole.DefaultDataScope.ScopeCategory == CateGoryConst.SCOPE_ALL)
  156. throw Oops.Bah("不能添加全局数据范围的角色");
  157. await _sysUserService.CheckApiDataScope(sysRole.OrgId, sysRole.CreateUserId.GetValueOrDefault(), "您没有权限添加该角色");
  158. }
  159. /// <summary>
  160. /// 获取授权菜单
  161. /// </summary>
  162. /// <param name="moduleId">模块Id</param>
  163. /// <param name="ownMenuList">拥有菜单列表</param>
  164. /// <param name="ownButtonList">拥有权限按钮列表</param>
  165. /// <returns></returns>
  166. public async Task<List<ResTreeSelector.RoleGrantResourceMenu>> GetRoleGrantResourceMenus(long moduleId, List<SysResource> ownMenuList,
  167. List<SysResource> ownButtonList)
  168. {
  169. var roleGrantResourceMenus = new List<ResTreeSelector.RoleGrantResourceMenu>();//定义结果
  170. var parentMenuList = ownMenuList.Where(it => it.ParentId == SafeCampusConst.ZERO).ToList();//获取一级目录
  171. //遍历一级目录
  172. foreach (var parent in parentMenuList)
  173. {
  174. //如果是目录则去遍历下级
  175. if (parent.MenuType == SysResourceConst.CATALOG)
  176. {
  177. //获取所有下级菜单
  178. var menuList = _resourceService.GetChildListById(ownMenuList, parent.Id, false);
  179. if (menuList.Count > 0)//如果有菜单
  180. {
  181. //遍历下级菜单
  182. foreach (var menu in menuList)
  183. {
  184. //如果菜单类型是菜单
  185. if (menu.MenuType is SysResourceConst.MENU or SysResourceConst.SUBSET)
  186. {
  187. //获取菜单下按钮集合并转换成对应实体
  188. var buttonList = ownButtonList.Where(it => it.ParentId == menu.Id).ToList();
  189. var buttons = buttonList.Adapt<List<ResTreeSelector.RoleGrantResourceButton>>();
  190. roleGrantResourceMenus.Add(new ResTreeSelector.RoleGrantResourceMenu
  191. {
  192. Id = menu.Id,
  193. ParentId = parent.Id,
  194. ParentName = parent.Title,
  195. Module = moduleId,
  196. Title = _resourceService.GetRoleGrantResourceMenuTitle(menuList, menu),//菜单名称需要特殊处理因为有二级菜单
  197. Button = buttons
  198. });
  199. }
  200. else if (menu.MenuType == SysResourceConst.LINK || menu.MenuType == SysResourceConst.IFRAME)//如果是内链或者外链
  201. {
  202. //直接加到资源列表
  203. roleGrantResourceMenus.Add(new ResTreeSelector.RoleGrantResourceMenu
  204. {
  205. Id = menu.Id,
  206. ParentId = parent.Id,
  207. ParentName = parent.Title,
  208. Module = moduleId,
  209. Title = menu.Title
  210. });
  211. }
  212. }
  213. }
  214. else
  215. {
  216. //否则就将自己加到一级目录里面
  217. roleGrantResourceMenus.Add(new ResTreeSelector.RoleGrantResourceMenu
  218. {
  219. Id = parent.Id,
  220. ParentId = parent.Id,
  221. ParentName = parent.Title,
  222. Module = moduleId
  223. });
  224. }
  225. }
  226. else
  227. {
  228. //就将自己加到一级目录里面
  229. var roleGrantResourcesButtons = new ResTreeSelector.RoleGrantResourceMenu
  230. {
  231. Id = parent.Id,
  232. ParentId = parent.Id,
  233. ParentName = parent.Title,
  234. Module = moduleId,
  235. Title = parent.Title
  236. };
  237. //如果菜单类型是菜单
  238. if (parent.MenuType == SysResourceConst.MENU)
  239. {
  240. //获取菜单下按钮集合并转换成对应实体
  241. var buttonList = ownButtonList.Where(it => it.ParentId == parent.Id).ToList();
  242. roleGrantResourcesButtons.Button = buttonList.Adapt<List<ResTreeSelector.RoleGrantResourceButton>>();
  243. }
  244. roleGrantResourceMenus.Add(roleGrantResourcesButtons);
  245. }
  246. }
  247. return roleGrantResourceMenus;
  248. }
  249. #endregion
  250. }