平安校园
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

287 lines
11 KiB

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