javademo

RoleServiceImpl.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package com.example.service.system.impl;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.example.entity.database.system.Role;
  4. import com.example.entity.database.system.SysRoleDept;
  5. import com.example.entity.database.system.SysUserRole;
  6. import com.example.mapper.system.RoleMapper;
  7. import com.example.mapper.system.SysRoleDeptMapper;
  8. import com.example.mapper.system.SysRoleMenuMapper;
  9. import com.example.mapper.system.SysUserRoleMapper;
  10. import com.example.service.BaseServiceImpl;
  11. import com.example.service.system.IRoleService;
  12. import com.example.util.helper.StringHelper;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.stereotype.Service;
  15. import org.springframework.transaction.annotation.Transactional;
  16. import org.springframework.util.StringUtils;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. @Transactional
  20. @Service
  21. public class RoleServiceImpl extends BaseServiceImpl<RoleMapper, Role> implements IRoleService{
  22. public RoleServiceImpl(){ super(false); }
  23. @Autowired
  24. private SysUserRoleMapper sysUserRoleMapper;
  25. @Autowired
  26. private SysRoleDeptMapper sysRoleDeptMapper;
  27. @Autowired
  28. private IRoleService roleService;
  29. @Autowired
  30. private SysRoleMenuMapper roleMenuMapper;
  31. @Autowired
  32. private SysRoleDeptMapper roleDeptMapper;
  33. @Autowired
  34. private SysUserRoleMapper userRoleMapper;
  35. /**
  36. * 修改数据权限信息
  37. * @param role 角色信息
  38. * @return 结果
  39. */
  40. @Override
  41. @Transactional
  42. public int authDataScope(Role role)
  43. {
  44. // 修改角色信息
  45. roleService.update(role);
  46. // 删除角色与部门关联
  47. sysRoleDeptMapper.deleteRoleDeptByRoleId(role.getRole_id());
  48. // 新增角色和部门信息(数据权限)
  49. return insertRoleDept(role);
  50. }
  51. /**
  52. * 新增角色部门信息(数据权限)
  53. * @param role 角色对象
  54. */
  55. public int insertRoleDept(Role role)
  56. {
  57. int rows = 1;
  58. // 新增角色与部门(数据权限)管理
  59. List<SysRoleDept> list = new ArrayList<SysRoleDept>();
  60. for (Long deptId : role.getDeptIds())
  61. {
  62. SysRoleDept rd = new SysRoleDept();
  63. rd.setRole_id(role.getRole_id());
  64. rd.setDept_id(deptId);
  65. list.add(rd);
  66. }
  67. if (list.size() > 0)
  68. {
  69. rows = sysRoleDeptMapper.batchRoleDept(list);
  70. }
  71. return rows;
  72. }
  73. /**
  74. * 取消授权用户角色
  75. *
  76. * @param userRole 用户和角色关联信息
  77. * @return 结果
  78. */
  79. @Override
  80. public int deleteAuthUser(SysUserRole userRole)
  81. {
  82. return sysUserRoleMapper.deleteUserRoleInfo(userRole);
  83. }
  84. /**
  85. * 批量取消授权用户角色
  86. *
  87. * @param roleId 角色ID
  88. * @param userIds 需要取消授权的用户数据ID
  89. * @return 结果
  90. */
  91. @Override
  92. public int deleteAuthUsers(Long roleId, Long[] userIds)
  93. {
  94. return sysUserRoleMapper.deleteUserRoleInfos(roleId, userIds);
  95. }
  96. /**
  97. * 批量选择授权用户角色
  98. *
  99. * @param roleId 角色ID
  100. * @param userIds 需要授权的用户数据ID
  101. * @return 结果
  102. */
  103. @Override
  104. public int insertAuthUsers(Long roleId, Long[] userIds)
  105. {
  106. // 新增用户与角色管理
  107. List<SysUserRole> list = new ArrayList<SysUserRole>();
  108. for (Long userId : userIds)
  109. {
  110. SysUserRole ur = new SysUserRole();
  111. ur.setUser_id(userId);
  112. ur.setRole_id(roleId);
  113. list.add(ur);
  114. }
  115. return sysUserRoleMapper.batchUserRole(list);
  116. }
  117. /**
  118. * 批量删除角色信息
  119. *
  120. * @param roleIds 需要删除的角色ID
  121. * @return 结果
  122. */
  123. @Override
  124. public void deleteRoleByIds(Long[] roleIds)
  125. {
  126. for (Long role_id : roleIds)
  127. {
  128. checkRoleAllowed(new Role(role_id));
  129. checkRoleDataScope(role_id);
  130. //Role role = selectRoleById(roleId);
  131. LambdaQueryWrapper<Role> qw = new LambdaQueryWrapper<>();
  132. qw.eq(Role::getRole_id,role_id);
  133. Role role = this.getEntity(qw);
  134. if (countUserRoleByRoleId(role_id) > 0)
  135. {
  136. throw new RuntimeException(String.format("%1$s已分配,不能删除", role.getRole_name()));
  137. }
  138. }
  139. // 删除角色与菜单关联
  140. roleMenuMapper.deleteRoleMenu(roleIds);
  141. // 删除角色与部门关联
  142. roleDeptMapper.deleteRoleDept(roleIds);
  143. }
  144. /**
  145. * 校验角色是否允许操作
  146. *
  147. * @param role 角色信息
  148. */
  149. @Override
  150. public void checkRoleAllowed(Role role)
  151. {
  152. if (!StringHelper.isNull(role.getRole_id()) && role.isAdmin())
  153. {
  154. // throw new ServiceException("不允许操作超级管理员角色");
  155. throw new RuntimeException("不允许操作超级管理员角色");
  156. }
  157. }
  158. /**
  159. * 校验角色是否有数据权限
  160. *
  161. * @param roleId 角色id
  162. */
  163. @Override
  164. public void checkRoleDataScope(Long roleId)
  165. {
  166. //if (!User.isAdmin(SecurityUtils.getUserId()))
  167. // {
  168. Role role = new Role();
  169. role.setRole_id(roleId);
  170. //List<Role> roles = SpringUtils.getAopProxy(this).selectRoleList(role);
  171. //List<Role> roles = this.selectRoleList(role);
  172. List<Role> roles = this.getList();
  173. if (StringUtils.isEmpty(roles))
  174. {
  175. throw new RuntimeException("没有权限访问角色数据!");
  176. }
  177. //}
  178. }
  179. /**
  180. * 通过角色ID查询角色使用数量
  181. *
  182. * @param roleId 角色ID
  183. * @return 结果
  184. */
  185. @Override
  186. public int countUserRoleByRoleId(Long roleId)
  187. {
  188. return userRoleMapper.countUserRoleByRoleId(roleId);
  189. }
  190. }