RoleInfoController.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. package com.dragoninfo.dcuc.authweb.restcontroller.auth;
  2. import com.alibaba.fastjson.JSON;
  3. import com.dragoninfo.dcuc.auth.auth.entity.RoleInfo;
  4. import com.dragoninfo.dcuc.auth.auth.entity.StaffAssignAuthInfo;
  5. import com.dragoninfo.dcuc.auth.auth.facade.IRoleInfoFacade;
  6. import com.dragoninfo.dcuc.auth.auth.facade.IStaffAssignAuthInfoFacade;
  7. import com.dragoninfo.dcuc.auth.auth.vo.RoleInfoVO;
  8. import com.dragoninfo.dcuc.auth.auth.vo.RsGridCheckedVO;
  9. import com.dragoninfo.dcuc.auth.auth.vo.zerotrust.rolemanage.RoleOperateApplyVo;
  10. import com.dragoninfo.dcuc.authweb.restcontroller.auth.vo.RoleFunRsRlVo;
  11. import com.dragoninfo.dcuc.authweb.restcontroller.auth.vo.RoleInfoVo;
  12. import com.dragoninfo.dcuc.authweb.restcontroller.auth.vo.RoleQuotaVo;
  13. import com.dragoninfo.dcuc.authweb.restcontroller.auth.vo.StaffAssignAuthInfoVo;
  14. import com.dragoninfo.dcuc.common.utils.ResponseUtil;
  15. import com.dragoninfo.duceap.core.response.Result;
  16. import com.dragonsoft.duceap.base.entity.http.ResponseDTO;
  17. import com.dragonsoft.duceap.base.entity.search.SearchDTO;
  18. import com.dragonsoft.duceap.base.entity.security.SecurityUser;
  19. import com.dragonsoft.duceap.base.enums.BooleanEnum;
  20. import com.dragonsoft.duceap.base.utils.UserContextUtils;
  21. import com.dragonsoft.duceap.commons.util.string.StringUtils;
  22. import com.dragonsoft.duceap.core.context.ContextUtils;
  23. import com.dragonsoft.duceap.core.search.Searchable;
  24. import com.dragonsoft.duceap.core.search.enums.SearchOperator;
  25. import com.dragonsoft.duceap.core.search.filter.Condition;
  26. import com.dragonsoft.duceap.web.annotation.Permission;
  27. import io.swagger.annotations.Api;
  28. import io.swagger.annotations.ApiImplicitParam;
  29. import io.swagger.annotations.ApiImplicitParams;
  30. import io.swagger.annotations.ApiOperation;
  31. import org.slf4j.Logger;
  32. import org.slf4j.LoggerFactory;
  33. import org.springframework.beans.BeanUtils;
  34. import org.springframework.beans.factory.annotation.Autowired;
  35. import org.springframework.data.domain.Page;
  36. import org.springframework.http.MediaType;
  37. import org.springframework.web.bind.annotation.*;
  38. import javax.validation.Valid;
  39. import java.util.ArrayList;
  40. import java.util.Date;
  41. import java.util.List;
  42. @Api(tags = {"授权模块-角色管理接口"})
  43. @RestController
  44. @RequestMapping(value = "authsvr/v2/roleinfo")
  45. public class RoleInfoController {
  46. private static Logger logger = LoggerFactory.getLogger(RoleInfoController.class);
  47. @Autowired
  48. private IRoleInfoFacade roleInfoFacade;
  49. @Autowired
  50. private IStaffAssignAuthInfoFacade iStaffAssignAuthInfoFacade;
  51. @ApiOperation(value = "角色新增、修改、删除操作申请")
  52. @PostMapping("role-operate-apply")
  53. public Result<Object> roleOperateApply(@Valid @RequestBody RoleOperateApplyVo roleOperateApplyVo) {
  54. ResponseDTO<Object> responseDTO = roleInfoFacade.roleOperateApply(roleOperateApplyVo);
  55. if (ResponseUtil.isSuccess(responseDTO)) {
  56. return Result.success();
  57. } else {
  58. return Result.failMessage(responseDTO.getMessage());
  59. }
  60. }
  61. /**
  62. * 角色列表
  63. *
  64. * @param searchDTO
  65. * @return
  66. */
  67. @ApiOperation(value = "角色列表")
  68. @ApiImplicitParam(name = "searchable", value = "查询条件,app_id_eq不能为空")
  69. @PostMapping(value = "roleList/_search")
  70. public Result<List<RoleInfoVO>> roleList(SearchDTO searchDTO) {
  71. Page<RoleInfoVO> roleInfoVOPage = roleInfoFacade.page(searchDTO);
  72. return Result.success(roleInfoVOPage.getTotalElements(), roleInfoVOPage.getContent());
  73. }
  74. /**
  75. * 保存菜单,功能
  76. *
  77. * @return
  78. */
  79. @ApiOperation(value = "保存菜单")
  80. @Permission(value = "power_config")
  81. @PostMapping(value = "/rsGrid", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  82. public Result saveRsGrid(@RequestBody RoleFunRsRlVo roleFunRsRlVo) {
  83. Result result;
  84. try {
  85. //TODO
  86. //DTO暂未设计,沿用原来的接口参数,后期修改
  87. String roleId = roleFunRsRlVo.getRoleId();
  88. String rsGridList = JSON.toJSONString(roleFunRsRlVo.getRsGridLists());
  89. roleInfoFacade.saveRsGrid(rsGridList, roleId);
  90. result = Result.success("保存成功");
  91. } catch (Exception e) {
  92. logger.error("saveRsGrid error:{}", e);
  93. result = Result.fail("保存异常");
  94. }
  95. return result;
  96. }
  97. /**
  98. * 查看角色信息
  99. *
  100. * @param id
  101. * @return
  102. */
  103. @ApiOperation(value = "查看角色信息")
  104. @ApiImplicitParams({@ApiImplicitParam(paramType = "path", name = "id", value = "角色ID", required = true
  105. , example = "40288a8b699fc2500169a33b20540000")})
  106. @GetMapping(value = "/role/{id}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  107. public Result<RoleInfoVo> roleInfo(@PathVariable("id") String id) {
  108. RoleInfo roleInfo = roleInfoFacade.get(id);
  109. if (null == roleInfo) {
  110. return Result.failMessage("角色不存在");
  111. }
  112. RoleInfoVo vo = new RoleInfoVo();
  113. BeanUtils.copyProperties(roleInfo, vo);
  114. return Result.success(vo);
  115. }
  116. /**
  117. * 保存角色
  118. *
  119. * @param roleInfoVo
  120. * @return
  121. */
  122. @ApiOperation(value = "保存角色信息")
  123. @Permission(value = "power_config")
  124. @PostMapping(value = "/role", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  125. public Result saveRole(@RequestBody RoleInfoVo roleInfoVo) {
  126. Result result;
  127. try {
  128. if (!StringUtils.isEmpty(roleInfoVo.getDetail()) && (roleInfoVo.getDetail().trim().length() > 40 || roleInfoVo.getDetail().trim().getBytes().length > 120)) {
  129. result = Result.fail("角色描述不能超过40个字");
  130. return result;
  131. }
  132. Searchable searchable = Searchable.newSearchable();
  133. searchable.addSearchFilter("code", SearchOperator.eq, roleInfoVo.getCode());
  134. if (StringUtils.isNotEmpty(roleInfoVo.getId())) {
  135. searchable.addSearchFilter("id", SearchOperator.ne, roleInfoVo.getId());
  136. }
  137. List<RoleInfo> list = roleInfoFacade.roleList(searchable.toSearchDTO());
  138. if (list != null && list.size() > 0) {
  139. return Result.failMessage("角色代码已被使用");
  140. }
  141. SecurityUser user = UserContextUtils.getCurrentUser();
  142. roleInfoVo.setInitNumber(0);
  143. roleInfoVo.setCreator(user.getId());
  144. roleInfoVo.setIsActive(BooleanEnum.TRUE.getValue());
  145. roleInfoVo.setPoliceCategory(StringUtils.isEmpty(roleInfoVo.getPoliceCategory()) ? null : roleInfoVo.getPoliceCategory());
  146. roleInfoVo.setCreateTime(new Date());
  147. //TODO
  148. //DTO暂未设计,沿用原来的接口参数,后期修改
  149. RoleInfo roleInfo = new RoleInfo();
  150. BeanUtils.copyProperties(roleInfoVo, roleInfo);
  151. roleInfoFacade.save(roleInfo);
  152. result = Result.success("保存成功");
  153. } catch (Exception e) {
  154. logger.error("", e);
  155. result = Result.fail("保存失败");
  156. }
  157. return result;
  158. }
  159. /**
  160. * 修改角色
  161. *
  162. * @param roleInfoVo
  163. * @return
  164. */
  165. @ApiOperation(value = "修改角色信息")
  166. @Permission(value = "power_config")
  167. @PutMapping(value = "/role", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  168. public Result updateRole(@RequestBody RoleInfoVo roleInfoVo) {
  169. Result result;
  170. try {
  171. if (!StringUtils.isEmpty(roleInfoVo.getDetail()) && (roleInfoVo.getDetail().trim().length() > 40 || roleInfoVo.getDetail().trim().getBytes().length > 120)) {
  172. result = Result.fail("角色描述不能超过40个字");
  173. return result;
  174. }
  175. Searchable searchable = Searchable.newSearchable();
  176. searchable.addSearchFilter("code", SearchOperator.eq, roleInfoVo.getCode());
  177. if (StringUtils.isNotEmpty(roleInfoVo.getId())) {
  178. searchable.addSearchFilter("id", SearchOperator.ne, roleInfoVo.getId());
  179. }
  180. List<RoleInfo> list = roleInfoFacade.roleList(searchable.toSearchDTO());
  181. if (list != null && list.size() > 0) {
  182. return Result.fail("角色代码已被使用");
  183. }
  184. RoleInfo role_info = roleInfoFacade.get(roleInfoVo.getId());
  185. role_info.setCode(roleInfoVo.getCode());
  186. role_info.setName(roleInfoVo.getName());
  187. role_info.setRoleLevel(roleInfoVo.getRoleLevel());
  188. role_info.setDetail(roleInfoVo.getDetail());
  189. role_info.setModifier(ContextUtils.getUserInfo().getName());
  190. role_info.setModifiedTime(new Date());
  191. role_info.setPoliceCategory(StringUtils.isEmpty(roleInfoVo.getPoliceCategory()) ? null : roleInfoVo.getPoliceCategory());
  192. role_info.setRoleBusiness(roleInfoVo.getRoleBusiness());
  193. role_info.setIsNotLimitCount(roleInfoVo.getIsNotLimitCount());
  194. role_info.setRoleCategory(roleInfoVo.getRoleCategory());
  195. roleInfoFacade.update(role_info);
  196. result = Result.success("保存成功");
  197. } catch (Exception e) {
  198. logger.error("", e);
  199. result = Result.fail("保存失败");
  200. }
  201. return result;
  202. }
  203. /**
  204. * 删除角色
  205. *
  206. * @param id 角色id
  207. * @return
  208. */
  209. @ApiOperation(value = "删除角色信息")
  210. @ApiImplicitParams({@ApiImplicitParam(paramType = "path", name = "id", value = "角色ID", required = true
  211. , example = "40288a8b699fc2500169a33b20540000")})
  212. @Permission(value = "power_config")
  213. @DeleteMapping(value = "/role/{id}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  214. public Result deleteRole(@PathVariable("id") String id) {
  215. Result result;
  216. try {
  217. //删除角色信息
  218. roleInfoFacade.delete(id);
  219. //删除角色相关表的信息
  220. roleInfoFacade.delAllRolesInfo(id);
  221. result = Result.success("删除成功");
  222. } catch (Exception e) {
  223. logger.error("delete role error:{}", e);
  224. result = Result.fail("删除异常");
  225. }
  226. return result;
  227. }
  228. /**
  229. * 根据角色获取选中菜单、功能
  230. *
  231. * @param roleId 角色id
  232. * @return
  233. */
  234. @ApiOperation(value = "根据角色获取选中菜单、功能")
  235. @ApiImplicitParam(name = "roleId", value = "角色id", required = true)
  236. @GetMapping(value = "/checked/{roleId}")
  237. public Result<List<RsGridCheckedVO>> checked(@PathVariable("roleId") String roleId) {
  238. List<RsGridCheckedVO> rsGridCheckedVOList = roleInfoFacade.getRsGridChecked(roleId);
  239. return Result.success(rsGridCheckedVOList);
  240. }
  241. /**
  242. * 获取角色配额授权情况列表
  243. *
  244. * @param searchDTO
  245. * @return
  246. */
  247. @ApiOperation(value = "获取角色配额授权情况列表")
  248. @ApiImplicitParam(name = "searchDTO", value = "查询条件,applicationId不能为空", required = true)
  249. @PostMapping(value = "/roleQuotaList/_search")
  250. public Result<List<RoleInfoVO>> getRoleQuotaList(SearchDTO searchDTO) {
  251. Searchable searchable = Searchable.toSearchable(searchDTO);
  252. Condition condition = searchable.getSearchFilterByKey("applicationId_eq");
  253. if (null == condition) return Result.fail("400", "applicationId 不能为空");
  254. String applicationId = (String) condition.getValue();
  255. if (StringUtils.isBlank(applicationId)){
  256. return Result.fail("400", "applicationId 不能为空");
  257. }
  258. Page<RoleInfoVO> page = roleInfoFacade.getRoleQuotaList(searchDTO);
  259. return Result.success(page.getTotalElements(), page.getContent());
  260. }
  261. /**
  262. * 获取应用有限配额的角色列表
  263. *
  264. * @param searchDTO
  265. * @return
  266. */
  267. @ApiOperation(value = "获取应用有限配额的角色列表")
  268. @ApiImplicitParam(name = "searchDTO", value = "查询条件,applicationId不能为空", required = true)
  269. @PostMapping(value = "/orgQuotoAuthInfo/_search")
  270. public Result<List<RoleInfoVO>> getOrgQuotoAuthInfo(SearchDTO searchDTO) {
  271. Searchable searchable = Searchable.toSearchable(searchDTO);
  272. Condition condition = searchable.getSearchFilterByKey("applicationId_eq");
  273. if (null == condition) {
  274. return Result.fail("400", "applicationId 不能为空");
  275. }
  276. String applicationId = (String) condition.getValue();
  277. if (StringUtils.isBlank(applicationId)) {
  278. return Result.fail("400", "applicationId 不能为空");
  279. }
  280. Condition roleLevel_eq = searchable.getSearchFilterByKey("roleLevel_eq");
  281. if (roleLevel_eq != null) {
  282. searchable.removeSearchFilter("roleLevel_eq");
  283. searchable.addSearchFilter("role_level", SearchOperator.eq, roleLevel_eq.getValue());
  284. }
  285. searchable.addSearchFilter("is_not_limit_count", SearchOperator.eq, BooleanEnum.TRUE.value);
  286. Page<RoleInfoVO> page = roleInfoFacade.getQuotoRoles(searchable.toSearchDTO());
  287. return Result.success(page.getTotalElements(), page.getContent());
  288. }
  289. /**
  290. * 角色初始配额信息保存
  291. *
  292. * @return
  293. */
  294. @ApiOperation(value = "角色初始配额信息保存")
  295. @Permission(value = "quota_init")
  296. @PostMapping(value = "/roleQuota", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  297. public Result roleQuotaSave(@RequestBody List<RoleQuotaVo> roleQuotaList) {
  298. Result result;
  299. try {
  300. String listStr = JSON.toJSONString(roleQuotaList);
  301. roleInfoFacade.roleQuotaSave(listStr);
  302. result = Result.success("保存成功");
  303. } catch (Exception e) {
  304. logger.error("role quota save error:{}", e);
  305. result = Result.fail(e.getMessage());
  306. }
  307. return result;
  308. }
  309. /**
  310. * 用户角色列表
  311. *
  312. * @param searchDTO
  313. * @return
  314. */
  315. @ApiOperation(value = "用户角色列表")
  316. @ApiImplicitParam(name = "searchDTO", value = "查询条件,staffId不能为空 ", required = true)
  317. @PostMapping(value = "/userRole/_search")
  318. public Result<List<StaffAssignAuthInfoVo>> userRole(SearchDTO searchDTO) {
  319. Searchable searchable = Searchable.toSearchable(searchDTO);
  320. String staffId = (String) searchable.getSearchFilterByKey("staffId_eq").getValue();
  321. if (StringUtils.isBlank(staffId)) {
  322. return Result.fail("300", "staffId 不能为空");
  323. }
  324. Page<StaffAssignAuthInfo> page = iStaffAssignAuthInfoFacade.findForPage(searchable.toSearchDTO());
  325. List<StaffAssignAuthInfoVo> vos = new ArrayList<>();
  326. for (StaffAssignAuthInfo source : page.getContent()) {
  327. StaffAssignAuthInfoVo vo = new StaffAssignAuthInfoVo();
  328. BeanUtils.copyProperties(source, vo);
  329. }
  330. return Result.success(page.getTotalElements(), vos);
  331. }
  332. }