RoleMgrController.java 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package com.lightinit.hsdatashow.controller.admin;
  2. import com.lightinit.hsdatashow.common.RoleModulesUtilsPro;
  3. import com.lightinit.hsdatashow.entity.BaseExample;
  4. import com.lightinit.hsdatashow.entity.RoleModule;
  5. import com.lightinit.hsdatashow.entity.SentryRole;
  6. import com.lightinit.hsdatashow.entity.SentryRoleAction;
  7. import com.lightinit.hsdatashow.model.ResultState;
  8. import com.lightinit.hsdatashow.model.ResultStateCode;
  9. import com.lightinit.hsdatashow.model.admin.*;
  10. import com.lightinit.hsdatashow.service.IRoleService;
  11. import com.lightinit.hsdatashow.service.IShiroPermissionsService;
  12. import org.slf4j.Logger;
  13. import org.slf4j.LoggerFactory;
  14. import org.springframework.beans.BeanUtils;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.stereotype.Controller;
  17. import org.springframework.validation.BindingResult;
  18. import org.springframework.web.bind.annotation.*;
  19. import org.springframework.web.servlet.ModelAndView;
  20. import javax.servlet.http.HttpSession;
  21. import javax.validation.Valid;
  22. import java.util.ArrayList;
  23. import java.util.Date;
  24. import java.util.List;
  25. /**
  26. * @author WangYao
  27. * @date 2018/6/21 13:30
  28. * @description 角色管理控制器
  29. */
  30. @Controller
  31. @RequestMapping("/admin/role_mgr")
  32. public class RoleMgrController extends BaseController {
  33. private static final Logger logger = LoggerFactory.getLogger(RoleMgrController.class);
  34. @Autowired
  35. private IRoleService roleService;
  36. @Autowired
  37. private RoleModulesUtilsPro roleModulesUtilsPro;
  38. @Autowired
  39. private IShiroPermissionsService shiroPermissionsService;
  40. @RequestMapping(value = "index.htm")
  41. public ModelAndView index(){
  42. ModelAndView modelAndView = new ModelAndView("admin/role_mgr/index");
  43. return modelAndView;
  44. }
  45. @RequestMapping(value = "list.action")
  46. public ModelAndView list(@RequestParam(defaultValue = "1") int pageNo) {
  47. ModelAndView modelAndView = new ModelAndView("admin/role_mgr/list");
  48. BaseExample.Page pager = new BaseExample.Page(pageNo, 10);
  49. List<SentryRole> outputModel = roleService.QueryList(pager.getPageNo(), pager.getPageSize(),null);
  50. pager.setTotal(roleService.QueryCount(null));
  51. modelAndView.addObject("list", outputModel);
  52. modelAndView.addObject("pager", pager);
  53. return modelAndView;
  54. }
  55. @RequestMapping(value = "add.htm")
  56. public ModelAndView add(){
  57. ModelAndView modelAndView = new ModelAndView("admin/role_mgr/add");
  58. return modelAndView;
  59. }
  60. @RequestMapping(value = "add.action")
  61. @ResponseBody
  62. public ResultState doAdd(HttpSession session, @Valid RoleMgrDataModel inputModel, BindingResult bindingResult){
  63. if (bindingResult.hasErrors()){
  64. return getAllFieldInvalidResultState(inputModel, bindingResult);
  65. }
  66. if (roleService.IsExitsRole(inputModel.getRolecode())) {
  67. return buildInvalidResultState(ResultStateCode.INVALID_DATA, "角色代码已存在");
  68. }
  69. SentryRole sentryRole = new SentryRole();
  70. BeanUtils.copyProperties(inputModel, sentryRole);
  71. sentryRole.setAddtiime(new Date());
  72. boolean success = roleService.InsertRole(sentryRole);
  73. ResultState resultState = new ResultState();
  74. if (!success){
  75. resultState.setStateCode(ResultStateCode.INVALID_DATA);
  76. resultState.setMsg("添加角色失败");
  77. }
  78. return resultState;
  79. }
  80. @RequestMapping(value = "edit.htm/{id}")
  81. public ModelAndView edit(@PathVariable long id){
  82. SentryRole busiModel = roleService.QueryOne(id);
  83. if(busiModel == null){
  84. return null;
  85. }
  86. ModelAndView modelAndView = new ModelAndView("admin/role_mgr/edit");
  87. modelAndView.addObject("busiModel", busiModel);
  88. return modelAndView;
  89. }
  90. @RequestMapping(value = "edit.action")
  91. @ResponseBody
  92. public ResultState doEdit(@Valid RoleMgrDataModel inputModel, BindingResult bindingResult){
  93. if (bindingResult.hasErrors()){
  94. return getAllFieldInvalidResultState(inputModel, bindingResult);
  95. }
  96. SentryRole role = roleService.QueryOne(inputModel.getId());
  97. if(role == null){
  98. return buildInvalidResultState(ResultStateCode.INVALID_DATA, "当前角色不存在");
  99. }
  100. role.setParentid(inputModel.getParentid());
  101. role.setRolecode(inputModel.getRolecode().toLowerCase());
  102. role.setRolename(inputModel.getRolename());
  103. boolean success = roleService.UpdateRole(role);
  104. ResultState resultState = new ResultState();
  105. if (!success){
  106. resultState.setStateCode(ResultStateCode.INVALID_DATA);
  107. resultState.setMsg("编辑角色失败");
  108. }
  109. return resultState;
  110. }
  111. @RequestMapping(value = "auth.htm/{id}")
  112. public ModelAndView auth(@PathVariable long id){
  113. SentryRole busiModel = roleService.QueryOne(id);
  114. if(busiModel == null){
  115. return null;
  116. }
  117. ModelAndView modelAndView = new ModelAndView("admin/role_mgr/auth");
  118. modelAndView.addObject("busiModel", busiModel);
  119. List<RoleModule> roleModuleList = roleService.QueryModuleList(id);
  120. modelAndView.addObject("selectedModuleList", roleModuleList);
  121. SentryRole role = roleService.QueryOne(id);
  122. List<String> roleCodeList = new ArrayList<>();
  123. roleCodeList.add(role.getRolecode());
  124. List<SentryRoleAction> selectedActionList = shiroPermissionsService.selectSentryRoleAction(roleCodeList);
  125. modelAndView.addObject("selectedActionList", selectedActionList);
  126. return modelAndView;
  127. }
  128. @RequestMapping(value = "auth.action")
  129. @ResponseBody
  130. public ResultState doAuth(@RequestBody RoleMgrModuleDataModel inputModel){
  131. if (inputModel == null || inputModel.getRoleId() == null || inputModel.getRoleId() <= 0){
  132. return buildInvalidResultState(ResultStateCode.INVALID_DATA, "参数错误");
  133. }
  134. SentryRole role = roleService.QueryOne(inputModel.getRoleId());
  135. if(role == null){
  136. return buildInvalidResultState(ResultStateCode.INVALID_DATA, "当前角色不存在");
  137. }
  138. boolean success = roleService.SaveRoleModule(inputModel.getRoleId(), inputModel.getSelectedModuleIds(), inputModel.getSelectedActionIds());
  139. ResultState resultState = new ResultState();
  140. if (!success){
  141. resultState.setStateCode(ResultStateCode.INVALID_DATA);
  142. resultState.setMsg("授权失败");
  143. }
  144. return resultState;
  145. }
  146. @RequestMapping(value = "delete.action/{id}")
  147. @ResponseBody
  148. public ResultState delete(@PathVariable long id){
  149. ResultState<Long> resultState = new ResultState<>();
  150. boolean success=false;
  151. if (id > 0) {
  152. SentryRole item = roleService.QueryOne(id);
  153. if(item == null){
  154. resultState.setStateCode(ResultStateCode.INVALID_DATA);
  155. resultState.setMsg("当前角色已不存在");
  156. }else if(item.getChildren()!=null && item.getChildren().size()>0){
  157. resultState.setStateCode(ResultStateCode.INVALID_DATA);
  158. resultState.setMsg("角色【"+item.getRolename()+"】还有子角色,当前角色不能删除");
  159. }else {
  160. List<Long> roleIds=roleModulesUtilsPro.RoleIds(item);
  161. if(roleIds.size()==0){
  162. resultState.setStateCode(ResultStateCode.INVALID_DATA);
  163. resultState.setMsg("参数错误");
  164. }else {
  165. if (roleService.CheckRoleHasModules(roleIds)) {
  166. resultState.setStateCode(ResultStateCode.INVALID_DATA);
  167. resultState.setMsg("角色【"+item.getRolename()+"】已经分配权限,当前角色不能删除");
  168. }else if (roleService.CheckRoleHasUser(roleIds)) {
  169. resultState.setStateCode(ResultStateCode.INVALID_DATA);
  170. resultState.setMsg("角色【"+item.getRolename()+"】已经分配用户,当前角色不能删除");
  171. }else{
  172. if(roleService.DeleteRole(id)){
  173. /*resultState.setStateCode(ResultStateCode.SUCCESS);
  174. resultState.setMsg("角色【"+item.getRolename()+"】删除成功");*/
  175. }else{
  176. resultState.setStateCode(ResultStateCode.INVALID_DATA);
  177. resultState.setMsg("角色【"+item.getRolename()+"】删除失败");
  178. }
  179. }
  180. }
  181. }
  182. } else {
  183. resultState.setStateCode(ResultStateCode.INVALID_DATA);
  184. resultState.setMsg("参数错误");
  185. }
  186. return resultState;
  187. }
  188. }