|
@@ -0,0 +1,86 @@
|
|
|
+package cn.com.qmth.examcloud.core.basic.service.cache;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+import java.util.Set;
|
|
|
+
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import com.google.common.collect.Sets;
|
|
|
+
|
|
|
+import cn.com.qmth.examcloud.commons.exception.StatusException;
|
|
|
+import cn.com.qmth.examcloud.core.basic.dao.OrgRepo;
|
|
|
+import cn.com.qmth.examcloud.core.basic.dao.PrivilegeRepo;
|
|
|
+import cn.com.qmth.examcloud.core.basic.dao.RolePrivilegeRelationRepo;
|
|
|
+import cn.com.qmth.examcloud.core.basic.dao.RoleRepo;
|
|
|
+import cn.com.qmth.examcloud.core.basic.dao.entity.OrgEntity;
|
|
|
+import cn.com.qmth.examcloud.core.basic.dao.entity.PrivilegeEntity;
|
|
|
+import cn.com.qmth.examcloud.core.basic.dao.entity.RoleEntity;
|
|
|
+import cn.com.qmth.examcloud.core.basic.dao.entity.RolePrivilegeRelationEntity;
|
|
|
+import cn.com.qmth.examcloud.support.cache.bean.PrivilegeRolesCacheBean;
|
|
|
+import cn.com.qmth.examcloud.web.cache.RandomObjectRedisCache;
|
|
|
+import cn.com.qmth.examcloud.web.helpers.GlobalHelper;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class PrrivilegeRolesCache extends RandomObjectRedisCache<PrivilegeRolesCacheBean> {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ PrivilegeRepo privilegeRepo;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ OrgRepo orgRepo;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ RoleRepo roleRepo;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ RolePrivilegeRelationRepo rolePrivilegeRelationRepo;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public PrivilegeRolesCacheBean loadFromResource(Object... keys) {
|
|
|
+ Long rootOrgId = (Long) keys[0];
|
|
|
+ String privilegeCode = (String) keys[1];
|
|
|
+
|
|
|
+ OrgEntity orgEntity = GlobalHelper.getPresentEntity(orgRepo, rootOrgId, OrgEntity.class);
|
|
|
+ if (null != orgEntity.getParentId()) {
|
|
|
+ throw new StatusException("120001", "org [id=" + rootOrgId + "] is not a root org");
|
|
|
+ }
|
|
|
+
|
|
|
+ PrivilegeEntity privilegeEntity = privilegeRepo.findByCode(privilegeCode);
|
|
|
+ if (null == privilegeEntity) {
|
|
|
+ throw new StatusException("120000",
|
|
|
+ "privilegeCode[" + privilegeCode + "] is not present");
|
|
|
+ }
|
|
|
+ List<RolePrivilegeRelationEntity> relationList = rolePrivilegeRelationRepo
|
|
|
+ .findAllByPrivilegeIdAndRootOrgId(privilegeEntity.getId(), rootOrgId);
|
|
|
+
|
|
|
+ Set<Long> roleIdList = Sets.newHashSet();
|
|
|
+ Set<String> roleCodeList = Sets.newHashSet();
|
|
|
+
|
|
|
+ for (RolePrivilegeRelationEntity cur : relationList) {
|
|
|
+ roleIdList.add(cur.getRoleId());
|
|
|
+ RoleEntity roleEntity = GlobalHelper.getPresentEntity(roleRepo, cur.getRoleId(),
|
|
|
+ RoleEntity.class);
|
|
|
+ roleCodeList.add(roleEntity.getCode());
|
|
|
+ }
|
|
|
+
|
|
|
+ PrivilegeRolesCacheBean bean = new PrivilegeRolesCacheBean();
|
|
|
+ bean.setRootOrgId(rootOrgId);
|
|
|
+ bean.setRoleIdList(roleIdList);
|
|
|
+ bean.setRoleCodeList(roleCodeList);
|
|
|
+
|
|
|
+ return bean;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected String getKeyPrefix() {
|
|
|
+ return "$_PRIV_ROLES:";
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected int getTimeout() {
|
|
|
+ // 5分钟
|
|
|
+ return 60 * 5;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|