SaasAuthHandler.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package com.persagy.person.manage;
  2. import java.util.HashMap;
  3. import java.util.List;
  4. import java.util.Map;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Component;
  7. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  8. import com.google.common.collect.Lists;
  9. import com.persagy.common.constant.SaasCommonConstant;
  10. import com.persagy.common.exception.BusinessException;
  11. import com.persagy.common.utils.StringUtil;
  12. import com.persagy.person.pojo.dto.SaasAccount;
  13. import com.persagy.person.pojo.dto.SaasArea;
  14. import com.persagy.person.pojo.dto.SaasGroup;
  15. import com.persagy.person.service.ISaasAccountService;
  16. import com.persagy.person.service.ISaasGroupService;
  17. /**
  18. * @version
  19. * @description
  20. * @company persagy
  21. * @author zhangqiankun
  22. * @since 2021年3月18日: 上午10:20:22
  23. */
  24. @Component
  25. public class SaasAuthHandler {
  26. @Autowired
  27. private ISaasGroupService saasGroupService;
  28. @Autowired
  29. private ISaasAccountService saasAccountService;
  30. @Autowired
  31. private SaasAreaProjectHandler saasAreaProjectHandler;
  32. /**
  33. * 验证此账号的集团项目权限
  34. *
  35. * @param accountId
  36. * @param groupCode
  37. * @return
  38. * 权限鉴定通过时:
  39. * 若有返回集团编码,则此返回值必须作为代码向下执行的sql条件;
  40. * 若有返回accountType,需进行 SaasCommonConstant.STR_STATUS_2.equals(accountType) 账号类型的判断
  41. */
  42. public Map<String, String> validGroupProjectAuth(String accountId, String groupCode) {
  43. // 获取此账号对应的集团权限
  44. SaasAccount account = this.saasAccountService.getOne(accountId, null, null);
  45. if (account == null) {
  46. throw new BusinessException("账号信息为空");
  47. }
  48. // 添加数据访问控制,账号类型,0-所有集团项目,1-单集团所有项目,2-其他
  49. String accountType = account.getAccountType();
  50. // 1时,判断是否有权限
  51. Map<String, String> result = new HashMap<String, String>(2);
  52. if (SaasCommonConstant.STR_STATUS_1.equals(accountType)) {
  53. String allowGroupCode = account.getGroupCode();
  54. if (StringUtil.isNotBlank(groupCode) && !groupCode.equals(allowGroupCode)) {
  55. throw new BusinessException("账号无该集团的访问权限");
  56. }
  57. result.put(SaasCommonConstant.GROUP_CODE, allowGroupCode);
  58. }
  59. result.put(SaasCommonConstant.ACCOUNT_TYPE, accountType);
  60. return result;
  61. }
  62. /**
  63. * 返回允许此账号访问的集团信息
  64. *
  65. * @param accountId
  66. * @param groupCode
  67. * @return 允许此账号访问的集团信息
  68. */
  69. public List<SaasGroup> getAllowGroupList(String accountId, String groupCode) {
  70. // 获取此账号对应的集团权限
  71. SaasAccount account = this.saasAccountService.getOne(accountId, null, null);
  72. if (account == null) {
  73. throw new BusinessException("账号信息为空");
  74. }
  75. // 添加数据访问控制,账号类型,0-所有集团项目,1-单集团所有项目,2-其他
  76. String accountType = account.getAccountType();
  77. // 1时,判断是否有权限
  78. if (SaasCommonConstant.STR_STATUS_1.equals(accountType)) {
  79. String allowGroupCode = account.getGroupCode();
  80. if (StringUtil.isNotBlank(groupCode) && !groupCode.equals(allowGroupCode)) {
  81. throw new BusinessException("账号无该集团的访问权限");
  82. }
  83. groupCode = allowGroupCode;
  84. }
  85. return this.getAllowGroupList(accountId, groupCode, accountType);
  86. }
  87. /**
  88. * 返回允许此账号访问的集团信息
  89. *
  90. * @param accountId
  91. * @param groupCode
  92. * @param accountType
  93. * @return 允许此账号访问的集团信息
  94. */
  95. public List<SaasGroup> getAllowGroupList(String accountId, String groupCode, String accountType) {
  96. List<SaasGroup> groups = null;
  97. if (SaasCommonConstant.STR_STATUS_2.equals(accountType)) {
  98. SaasGroup saasGroup = new SaasGroup();
  99. saasGroup.setGroupCode(groupCode);
  100. saasGroup.setValid(SaasCommonConstant.STATUS_1);
  101. groups = this.saasGroupService.queryAllowGroupInfo(saasGroup, accountId);
  102. } else {
  103. LambdaQueryWrapper<SaasGroup> queryWrapper = new SaasGroup.Builder().createQueryWrapper().validEq(SaasCommonConstant.STATUS_1).builderQueryWrapper();
  104. groups = this.saasGroupService.list(queryWrapper);
  105. }
  106. return groups == null ? Lists.newArrayList() : groups;
  107. }
  108. /**
  109. * 返回被允许访问的区域树
  110. *
  111. * @param accountId
  112. * @param groupCode
  113. * @return 允许此账号访问的集团信息
  114. */
  115. public List<SaasArea> getAllowAreaList(String accountId, String groupCode) {
  116. // 获取此账号对应的集团权限
  117. SaasAccount account = this.saasAccountService.getOne(accountId, null, null);
  118. if (account == null) {
  119. throw new BusinessException("账号信息为空");
  120. }
  121. // 添加数据访问控制,账号类型,0-所有集团项目,1-单集团所有项目,2-其他
  122. String accountType = account.getAccountType();
  123. // 1时,判断是否有权限
  124. if (SaasCommonConstant.STR_STATUS_1.equals(accountType)) {
  125. String allowGroupCode = account.getGroupCode();
  126. if (StringUtil.isNotBlank(groupCode) && !groupCode.equals(allowGroupCode)) {
  127. throw new BusinessException("账号无该集团的访问权限");
  128. }
  129. groupCode = allowGroupCode;
  130. }
  131. return this.getAllowAreaList(accountId, groupCode, accountType);
  132. }
  133. /**
  134. * 返回被允许访问的区域树
  135. *
  136. * @param accountId
  137. * @param groupCode
  138. * @param accountType
  139. * @return
  140. */
  141. public List<SaasArea> getAllowAreaList(String accountId, String groupCode, String accountType) {
  142. List<SaasArea> topList = this.saasAreaProjectHandler.getTopAreaList(groupCode);
  143. if (SaasCommonConstant.STR_STATUS_2.equals(accountType)) {
  144. // 其他类型时,需要根据权限表数据,去匹配区域树
  145. this.saasAreaProjectHandler.queryAllowAreaTree(topList, accountId, groupCode);
  146. } else {
  147. this.saasAreaProjectHandler.querySaasAreaTree(topList, false);
  148. }
  149. return topList;
  150. }
  151. /**
  152. * 验证此账号的菜单权限
  153. *
  154. * @param accountId
  155. * @param groupCode
  156. * @return
  157. * 权限鉴定通过时:
  158. * 有accountbelong 账号所属字段
  159. * 若有返回集团编码,则此返回值必须作为代码向下执行的sql条件;
  160. * 若有返回accountType,需进行 SaasCommonConstant.STR_STATUS_2.equals(accountType) 账号类型的判断
  161. */
  162. public Map<String, String> validSaasMenuAuth(String accountId, String groupCode) {
  163. // 获取此账号对应的集团权限
  164. SaasAccount account = this.saasAccountService.getOne(accountId, null, null);
  165. if (account == null) {
  166. throw new BusinessException("账号信息为空");
  167. }
  168. // 添加数据访问控制,账号类型,0-所有集团项目,1-单集团所有项目,2-其他
  169. String accountType = account.getAccountType();
  170. // 1时,判断是否有权限
  171. Map<String, String> result = new HashMap<String, String>(2);
  172. if (SaasCommonConstant.STR_STATUS_1.equals(accountType)) {
  173. String allowGroupCode = account.getGroupCode();
  174. if (StringUtil.isNotBlank(groupCode) && !groupCode.equals(allowGroupCode)) {
  175. throw new BusinessException("账号无该集团的访问权限");
  176. }
  177. result.put(SaasCommonConstant.GROUP_CODE, allowGroupCode);
  178. }
  179. result.put(SaasCommonConstant.ACCOUNT_TYPE, accountType);
  180. result.put(SaasCommonConstant.ACCOUNT_BELONG, account.getAccountBelong());
  181. return result;
  182. }
  183. }