基于springboot+redis+bootstrap+mysql开发一套属于自己的分布式springcloud云权限架构(十三)【权限架构生产者(改造角色管理)】

秒速五厘米 2022-05-31 11:11 181阅读 0赞
  1. 在第十二章我们完成了对菜单管理的集成,在第九章我们完成了角色了管理,但是我们并没有将菜单和角色两者关联上,因此本章将在这两章的基础上进行一次改造,使得我们的菜单和角色产生关联。
  2. 打开我们的UserRoleService.java改造成以下的内容:
  3. package com.produce.sys.service;
  4. import com.base.entity.QueryUserRole;
  5. import com.base.entity.RoleAssociateTree;
  6. import com.base.entity.Tree;
  7. import com.base.entity.UserRole;
  8. import com.produce.common.base.dao.GenericDao;
  9. import com.produce.common.base.service.GenericService;
  10. import com.produce.sys.dao.RoleAssociateTreeDao;
  11. import com.produce.sys.dao.UserRoleDao;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.stereotype.Service;
  14. import org.springframework.transaction.annotation.Transactional;
  15. import java.util.List;
  16. /**
  17. *@author linzf
  18. **/
  19. @Service("userRoleService")
  20. @Transactional(rollbackFor={IllegalArgumentException.class})
  21. public class UserRoleService extends GenericService<UserRole, QueryUserRole> {
  22. @Autowired
  23. @SuppressWarnings("SpringJavaAutowiringInspection")
  24. private UserRoleDao userRoleDao;
  25. @Autowired
  26. @SuppressWarnings("SpringJavaAutowiringInspection")
  27. private RoleAssociateTreeDao roleAssociateTreeDao;
  28. @Override
  29. protected GenericDao<UserRole, QueryUserRole> getDao() {
  30. return userRoleDao;
  31. }
  32. /**
  33. * 功能描述:获取权限菜单数据
  34. * @param entity
  35. * @return
  36. */
  37. public UserRole getUserRoleAssociate(UserRole entity){
  38. return userRoleDao.getUserRoleAssociate(entity);
  39. }
  40. /**
  41. * 功能描述:删除角色数据
  42. * @param entityList
  43. * @return
  44. * @throws Exception
  45. */
  46. @Override
  47. public boolean removeBath(List<UserRole> entityList) throws Exception {
  48. for(UserRole userRole:entityList){
  49. roleAssociateTreeDao.removeTreeByRoleId(userRole);
  50. }
  51. return super.removeBath(entityList);
  52. }
  53. /**
  54. * 增加角色数据
  55. * @param entity 保存对象
  56. * @return
  57. * @throws Exception
  58. */
  59. @Override
  60. public boolean save(UserRole entity) throws Exception {
  61. entity.packagingTrees(entity.getTreeArray());
  62. List<Tree> treeList = entity.getTreeList();
  63. boolean success = super.save(entity);
  64. for(Tree tree:treeList){
  65. roleAssociateTreeDao.save(new RoleAssociateTree(entity.getId(),tree.getId()));
  66. }
  67. return success;
  68. }
  69. @Override
  70. public boolean update(UserRole entity) throws Exception {
  71. entity.packagingTrees(entity.getTreeArray());
  72. List<Tree> treeList = entity.getTreeList();
  73. roleAssociateTreeDao.removeTreeByRoleId(entity);
  74. for(Tree tree:treeList){
  75. roleAssociateTreeDao.save(new RoleAssociateTree(entity.getId(),tree.getId()));
  76. }
  77. return super.update(entity);
  78. }
  79. }
  80. 接着打开我们的RoleController.java改造成以下的内容:
  81. package com.produce.sys.controller;
  82. import com.base.entity.QueryUserRole;
  83. import com.base.entity.Tree;
  84. import com.base.entity.UserRole;
  85. import com.produce.common.base.constant.SystemStaticConst;
  86. import com.produce.common.base.controller.GenericController;
  87. import com.produce.common.base.service.GenericService;
  88. import com.produce.sys.service.TreeService;
  89. import com.produce.sys.service.UserRoleService;
  90. import org.springframework.beans.factory.annotation.Autowired;
  91. import org.springframework.http.MediaType;
  92. import org.springframework.web.bind.annotation.*;
  93. import java.util.HashMap;
  94. import java.util.List;
  95. import java.util.Map;
  96. /*
  97. * 类描述:
  98. * @auther linzf
  99. * @create 2018/2/6 0006
  100. */
  101. @RestController
  102. @RequestMapping("/role")
  103. public class RoleController extends GenericController<UserRole, QueryUserRole> {
  104. @Autowired
  105. private UserRoleService userRoleService;
  106. @Autowired
  107. private TreeService treeService;
  108. @Override
  109. protected GenericService<UserRole, QueryUserRole> getService() {
  110. return userRoleService;
  111. }
  112. @RequestMapping(value = "/loadRoleTree",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
  113. public Map<String,Object> loadRoleTree(@RequestBody UserRole entity){
  114. entity = userRoleService.getUserRoleAssociate(entity);
  115. List<Tree> treeList = treeService.query(null);
  116. if(entity!=null){
  117. for(Tree tree:entity.getTreeList()){
  118. treeList.stream().forEach(t->{
  119. if(t.getId()==tree.getId()){
  120. t.setChecked(true);
  121. }
  122. });
  123. }
  124. }
  125. Map<String,Object> result = new HashMap<String, Object>();
  126. result.put(SystemStaticConst.RESULT, SystemStaticConst.SUCCESS);
  127. result.put("data",treeList);
  128. return result;
  129. }
  130. }
  131. 到此处我们已经完成了对权限架构生产者的集成工作,在下一章节我们将阐述如何实现权限架构消费者的实现。
  132. 到此为止的GitHub项目地址:[https://github.com/185594-5-27/spring-cloud-rbac/tree/master-base-produce-complete][https_github.com_185594-5-27_spring-cloud-rbac_tree_master-base-produce-complete]

上一篇文章地址:基于springboot+redis+bootstrap+mysql开发一套属于自己的分布式springcloud云权限架构(十二)【权限架构生产者(菜单管理)】

下一篇文章地址:基于springboot+redis+bootstrap+mysql开发一套属于自己的分布式springcloud云权限架构(十四)【权限架构消费者(通用类编写)】

QQ交流群:578746866

发表评论

表情:
评论列表 (有 0 条评论,181人围观)

还没有评论,来说两句吧...

相关阅读