PermissionInterfaceImpl.java 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package jnpf.permissions;
  2. import cn.dev33.satoken.SaManager;
  3. import cn.dev33.satoken.session.SaSession;
  4. import cn.dev33.satoken.stp.StpInterface;
  5. import cn.dev33.satoken.stp.StpUtil;
  6. import com.alibaba.fastjson.JSONObject;
  7. import jnpf.base.UserInfo;
  8. import jnpf.model.BaseSystemInfo;
  9. import jnpf.properties.SecurityProperties;
  10. import jnpf.util.StringUtil;
  11. import jnpf.util.TenantHolder;
  12. import jnpf.util.TenantProvider;
  13. import jnpf.util.UserProvider;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.stereotype.Component;
  16. import java.util.*;
  17. import static jnpf.util.Constants.ADMIN_KEY;
  18. /**
  19. * 权限认证接口实现
  20. *
  21. * @author JNPF开发平台组
  22. * @copyright 引迈信息技术有限公司
  23. */
  24. @Component
  25. public class PermissionInterfaceImpl implements StpInterface {
  26. public static final String PERMISSION_KEY = "user_permission";
  27. public static final String ROLE_KEY = "user_roles";
  28. public static final String COLUMN_KEY = "user_columns";
  29. public static final String FORM_KEY = "user_forms";
  30. public static final String OTHER_PERMISSION = "permission:";
  31. public static final String USER_AUTH = "user_auth";
  32. @Autowired
  33. private SecurityProperties securityProperties;
  34. @Override
  35. public List<String> getPermissionList(Object loginId, String loginType) {
  36. if (!securityProperties.isEnablePreAuth()) {
  37. return Collections.emptyList();
  38. }
  39. return getListByType(PERMISSION_KEY);
  40. }
  41. @Override
  42. public List<String> getRoleList(Object loginId, String loginType) {
  43. if (!securityProperties.isEnablePreAuth()) {
  44. return Collections.emptyList();
  45. }
  46. return getListByType(ROLE_KEY);
  47. }
  48. public static Map<String, Object> getColumnMap() {
  49. return getMapByType(COLUMN_KEY);
  50. }
  51. public static Map<String, Object> getFormMap() {
  52. return getMapByType(FORM_KEY);
  53. }
  54. //
  55. // public static void setAuthorityList(String userAccount, Set<String> authority, BaseSystemInfo baseSystemInfo) {
  56. // userAccount = UserProvider.splicingLoginId(userAccount);
  57. // try {
  58. // TenantProvider.setBaseSystemInfo(baseSystemInfo);
  59. // StpUtil.getSessionByLoginId(userAccount, true).set(PERMISSION_KEY, new ArrayList<>(authority));
  60. // } finally {
  61. // TenantProvider.clearBaseSystemIfo();
  62. // }
  63. // }
  64. //
  65. // public static void setRoleList(String userAccount, Set<String> role, BaseSystemInfo baseSystemInfo) {
  66. // userAccount = UserProvider.splicingLoginId(userAccount);
  67. // try {
  68. // TenantProvider.setBaseSystemInfo(baseSystemInfo);
  69. // StpUtil.getSessionByLoginId(userAccount, true).set(ROLE_KEY, new ArrayList<>(role));
  70. // } finally {
  71. // TenantProvider.clearBaseSystemIfo();
  72. // }
  73. // }
  74. //
  75. //
  76. // public static void setColumnMap(String userAccount, Map<String, List<Map<String, Object>>> columnList, BaseSystemInfo baseSystemInfo) {
  77. // userAccount = UserProvider.splicingLoginId(userAccount);
  78. // try {
  79. // TenantProvider.setBaseSystemInfo(baseSystemInfo);
  80. // StpUtil.getSessionByLoginId(userAccount, true).set(COLUMN_KEY, columnList);
  81. // } finally {
  82. // TenantProvider.clearBaseSystemIfo();
  83. // }
  84. // }
  85. //
  86. // public static void setFormMap(String userAccount, Map<String, List<Map<String, Object>>> formList, BaseSystemInfo baseSystemInfo) {
  87. // userAccount = UserProvider.splicingLoginId(userAccount);
  88. // try {
  89. // TenantProvider.setBaseSystemInfo(baseSystemInfo);
  90. // StpUtil.getSessionByLoginId(userAccount, true).set(FORM_KEY, formList);
  91. // } finally {
  92. // TenantProvider.clearBaseSystemIfo();
  93. // }
  94. // }
  95. public static void setMap(String userAccount, String sysId, Map<String, List<Map<String, Object>>> columnsMap, Map<String, List<Map<String, Object>>> formMap) {
  96. userAccount = UserProvider.splicingLoginId(userAccount);
  97. String tenantId = TenantHolder.getDatasourceId();
  98. Map<String, Object> map = getMap();
  99. Map<String, Object> mapRes = new HashMap<>(map);
  100. Map<String, Object> mapAuth = new HashMap<>();
  101. if (map.containsKey(sysId)) {
  102. mapAuth = (Map<String, Object>) map.get(sysId);
  103. }
  104. if (!columnsMap.isEmpty()) {
  105. mapAuth.put(PermissionInterfaceImpl.COLUMN_KEY, columnsMap);
  106. }
  107. if (!formMap.isEmpty()) {
  108. mapAuth.put(PermissionInterfaceImpl.FORM_KEY, formMap);
  109. }
  110. mapRes.put(sysId, mapAuth);
  111. SaManager.getSaTokenDao().set(OTHER_PERMISSION + tenantId + "_" + userAccount, JSONObject.toJSONString(mapRes), 60 * 60 * 24);
  112. }
  113. public static Map<String, Object> getMap() {
  114. UserInfo userInfo = UserProvider.getUser();
  115. String account = userInfo.getIsAdministrator() ? ADMIN_KEY : userInfo.getUserId();
  116. account = UserProvider.splicingLoginId(account);
  117. String tenantId = TenantHolder.getDatasourceId();
  118. String json = SaManager.getSaTokenDao().get(OTHER_PERMISSION + tenantId + "_" + account);
  119. if (StringUtil.isEmpty(json)) {
  120. return Collections.emptyMap();
  121. }
  122. return JSONObject.parseObject(json);
  123. }
  124. private static Map<String, Object> getMapByType(String typeKey) {
  125. Map<String, Object> map = getMap();
  126. Map<String, Object> res = new HashMap<>();
  127. for (String key : map.keySet()) {
  128. Map<String, Object> obj = (Map<String, Object>) map.get(key);
  129. if (obj != null && obj.containsKey(typeKey)) {
  130. res.putAll((Map<String, Object>) obj.get(typeKey));
  131. }
  132. }
  133. return res;
  134. }
  135. /**
  136. * 添加系统权限map
  137. * map(系统id,和全部权限map)
  138. *
  139. * @param userAccount
  140. * @param baseSystemInfo
  141. */
  142. public static void setUserAuth(String userAccount, String sysId, Set<String> authorityList, Set<String> roleAuthorityList, BaseSystemInfo baseSystemInfo) {
  143. userAccount = UserProvider.splicingLoginId(userAccount);
  144. try {
  145. TenantProvider.setBaseSystemInfo(baseSystemInfo);
  146. Map<String, Object> map = PermissionInterfaceImpl.getUserAuth();
  147. Map<String, Object> mapRes = new HashMap<>(map);
  148. Map<String, Object> mapAuth = new HashMap<>();
  149. if (map.containsKey(sysId)) {
  150. mapAuth = (Map<String, Object>) map.get(sysId);
  151. }
  152. if (!authorityList.isEmpty()) {
  153. mapAuth.put(PermissionInterfaceImpl.PERMISSION_KEY, authorityList);
  154. }
  155. if (!roleAuthorityList.isEmpty()) {
  156. mapAuth.put(PermissionInterfaceImpl.ROLE_KEY, roleAuthorityList);
  157. }
  158. mapRes.put(sysId, mapAuth);
  159. StpUtil.getSessionByLoginId(userAccount, true).set(USER_AUTH, mapRes);
  160. } finally {
  161. TenantProvider.clearBaseSystemIfo();
  162. }
  163. }
  164. public static Map<String, Object> getUserAuth() {
  165. UserInfo userInfo = UserProvider.getUser();
  166. String account = userInfo.getIsAdministrator() ? ADMIN_KEY : userInfo.getUserId();
  167. account = UserProvider.splicingLoginId(account);
  168. SaSession saSession = StpUtil.getSessionByLoginId(account, false);
  169. if (saSession == null) {
  170. return Collections.emptyMap();
  171. }
  172. return saSession.get(USER_AUTH, Collections.emptyMap());
  173. }
  174. private static List<String> getListByType(String typeKey) {
  175. Map<String, Object> userAuth = PermissionInterfaceImpl.getUserAuth();
  176. List<String> list = new ArrayList<>();
  177. if (userAuth != null) {
  178. for (String key : userAuth.keySet()) {
  179. Map<String, Object> obj = (Map<String, Object>) userAuth.get(key);
  180. if (obj != null && obj.containsKey(typeKey)) {
  181. list.addAll((Set<String>) obj.get(typeKey));
  182. }
  183. }
  184. }
  185. return list;
  186. }
  187. }