ExamCloudResourceManager.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package cn.com.qmth.examcloud.ws.config;
  2. import cn.com.qmth.examcloud.api.commons.enums.DataRuleType;
  3. import cn.com.qmth.examcloud.api.commons.security.bean.*;
  4. import cn.com.qmth.examcloud.api.commons.security.enums.RoleMeta;
  5. import cn.com.qmth.examcloud.commons.util.PropertiesUtil;
  6. import cn.com.qmth.examcloud.commons.util.RegExpUtil;
  7. import cn.com.qmth.examcloud.support.cache.CacheHelper;
  8. import cn.com.qmth.examcloud.support.cache.bean.AppCacheBean;
  9. import cn.com.qmth.examcloud.web.redis.RedisClient;
  10. import cn.com.qmth.examcloud.web.security.ResourceManager;
  11. import cn.com.qmth.examcloud.web.support.ApiInfo;
  12. import com.google.common.collect.Sets;
  13. import org.apache.commons.collections4.CollectionUtils;
  14. import org.apache.commons.lang3.StringUtils;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.stereotype.Component;
  17. import java.util.List;
  18. import java.util.Set;
  19. /**
  20. * 资源管理器
  21. *
  22. * @author WANGWEI
  23. * @date 2019年2月18日
  24. * @Copyright (c) 2018-2020 WANGWEI [QQ:522080330] All Rights Reserved.
  25. */
  26. @Component
  27. public class ExamCloudResourceManager implements ResourceManager {
  28. @Autowired
  29. RedisClient redisClient;
  30. // @Autowired
  31. // UserDataRuleCloudService userDataRuleCloudService;
  32. static {
  33. PropertiesUtil.loadFromResource("security.properties");
  34. }
  35. @Override
  36. public AccessApp getAccessApp(Long appId) {
  37. AppCacheBean appCacheBean = CacheHelper.getApp(appId);
  38. AccessApp app = new AccessApp();
  39. app.setAppId(appCacheBean.getId());
  40. app.setAppCode(appCacheBean.getCode());
  41. app.setAppName(appCacheBean.getName());
  42. app.setSecretKey(appCacheBean.getSecretKey());
  43. app.setTimeRange(appCacheBean.getTimeRange());
  44. return app;
  45. }
  46. @Override
  47. public boolean isNaked(ApiInfo apiInfo, String mapping) {
  48. if (null == apiInfo) {
  49. return true;
  50. }
  51. if (mapping.matches(".*swagger.*")) {
  52. return true;
  53. }
  54. if (null != apiInfo) {
  55. if (apiInfo.isNaked()) {
  56. return true;
  57. }
  58. }
  59. return false;
  60. }
  61. @Override
  62. public boolean hasPermission(User user, ApiInfo apiInfo, String mapping) {
  63. // 学生鉴权
  64. if (user.getUserType().equals(UserType.STUDENT)) {
  65. String key = "[s]" + mapping;
  66. return PropertiesUtil.getBoolean(key, false);
  67. }
  68. List<Role> roleList = user.getRoleList();
  69. if (CollectionUtils.isEmpty(roleList)) {
  70. return false;
  71. }
  72. for (Role role : roleList) {
  73. if (role.getRoleCode().equals(RoleMeta.SUPER_ADMIN.name())) {
  74. return true;
  75. }
  76. }
  77. // 权限组集合
  78. String privilegeGroups = PropertiesUtil.getString(mapping);
  79. if (StringUtils.isBlank(privilegeGroups)) {
  80. return true;
  81. }
  82. // 用户权限集合
  83. Set<String> rolePrivilegeList = Sets.newHashSet();
  84. Long rootOrgId = user.getRootOrgId();
  85. for (Role role : roleList) {
  86. String key = "$_P_" + rootOrgId + "_" + role.getRoleId();
  87. String rolePrivileges = redisClient.get(key, String.class);
  88. List<String> rpList = RegExpUtil.findAll(rolePrivileges, "\\w+");
  89. rolePrivilegeList.addAll(rpList);
  90. }
  91. List<String> privilegeGroupList = RegExpUtil.findAll(privilegeGroups, "[^\\;]+");
  92. for (String pg : privilegeGroupList) {
  93. pg = pg.trim();
  94. if (StringUtils.isBlank(pg)) {
  95. continue;
  96. }
  97. List<String> pList = RegExpUtil.findAll(pg, "[^\\,]+");
  98. if (rolePrivilegeList.containsAll(pList)) {
  99. return true;
  100. } else {
  101. continue;
  102. }
  103. }
  104. return false;
  105. }
  106. @Override
  107. public UserDataRule loadUserDataRule(Long userId, DataRuleType dataRuleType) {
  108. // ignore
  109. return new UserDataRule();
  110. }
  111. }