Răsfoiți Sursa

数据权限

czh 2 ani în urmă
părinte
comite
cfc1606de3

+ 6 - 0
sckw-auth/src/main/java/com/sckw/auth/service/impl/AuthServiceImpl.java

@@ -567,6 +567,12 @@ public class AuthServiceImpl implements IAuthService {
             loginUserInfo.setIsMain(user.getIsMain());
             loginUserInfo.setEntId(user.getEntId());
             loginUserInfo.setEntName(enterprise != null ? enterprise.getFirmName() : null);
+
+            //普通用户需要填充数据权限
+            if (user.getIsMain().equals(Global.NO)) {
+                loginUserInfo.setAuthUserIdList(remoteUserService.queryAuthUserList(user.getId()));
+            }
+
             int expireTime = ClientTypeEnum.expireTime(loginBase.getClientType());
             String key = Global.getFullUserLoginKey(loginUserInfo.getSystemType(), loginUserInfo.getId());
             RedissonUtils.putString(key, JSON.toJSONString(loginUserInfo), Global.APP_TOKEN_EXPIRE);

+ 13 - 26
sckw-common/sckw-common-core/src/main/java/com/sckw/core/aspect/NoRepeatSubmitAspect.java

@@ -3,40 +3,26 @@ package com.sckw.core.aspect;
 import com.sckw.core.annotation.RepeatSubmit;
 import com.sckw.core.exception.SystemException;
 import com.sckw.core.model.constant.Global;
-import com.sckw.core.model.page.PageRes;
-import com.sckw.core.utils.EncryUtil;
-import com.sckw.core.utils.NumberUtils;
 import com.sckw.core.utils.StringUtils;
+import com.sckw.core.web.context.LoginUserHolder;
 import com.sckw.redis.utils.RedissonUtils;
 import jakarta.servlet.http.HttpServletRequest;
-import jakarta.servlet.http.HttpServletResponse;
 import lombok.extern.slf4j.Slf4j;
-import org.apache.commons.io.IOUtils;
-import org.aspectj.lang.JoinPoint;
 import org.aspectj.lang.ProceedingJoinPoint;
 import org.aspectj.lang.annotation.Around;
 import org.aspectj.lang.annotation.Aspect;
-import org.aspectj.lang.annotation.Before;
-import org.aspectj.lang.annotation.Pointcut;
 import org.aspectj.lang.reflect.MethodSignature;
-import org.springframework.stereotype.Component;
 import org.springframework.web.context.request.RequestAttributes;
 import org.springframework.web.context.request.RequestContextHolder;
 import org.springframework.web.context.request.ServletRequestAttributes;
 
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStream;
 import java.lang.reflect.Method;
-import java.nio.charset.StandardCharsets;
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.Map;
+import java.util.Objects;
 import java.util.concurrent.TimeUnit;
 
 /**
  * @author czh
- * @desc TODO
+ * @desc 防重复提交
  * @date 2023/9/1
  */
 @Aspect
@@ -45,22 +31,23 @@ public class NoRepeatSubmitAspect {
 
     @Around("@annotation(com.sckw.core.annotation.RepeatSubmit)")
     public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
-        // 获取request
         RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
         ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
         HttpServletRequest request = servletRequestAttributes.getRequest();
-        String requestURI = request.getRequestURI();
-        String token = request.getHeader(Global.ACCESS_TOKEN);
-        Map<String, Object> tokenMap = EncryUtil.descryV2(Global.PRI_KEY, token);
-        Long userId = NumberUtils.parseLong(tokenMap.get("userId"));
-        String key = Global.getRepeatSubmitKey(userId, requestURI);
-        String string = RedissonUtils.getString(key);
+        String url = request.getRequestURI();
+        Long userId = LoginUserHolder.getUserId();
+        if (Objects.isNull(userId)) {
+            return pjp.proceed();
+        }
+
+        String key = Global.getRepeatSubmitKey(userId, url);
+        String res = RedissonUtils.getString(key);
         MethodSignature signature = (MethodSignature) pjp.getSignature();
         Method method = signature.getMethod();
         RepeatSubmit annotation = method.getAnnotation(RepeatSubmit.class);
         long interval = annotation.interval();
-        if (StringUtils.isNotBlank(string)) {
-            if (System.currentTimeMillis() - Long.parseLong(string) < interval) {
+        if (StringUtils.isNotBlank(res)) {
+            if (System.currentTimeMillis() - Long.parseLong(res) < interval) {
                 throw new SystemException(annotation.message());
             }
             return pjp.proceed();

+ 9 - 0
sckw-common/sckw-common-core/src/main/java/com/sckw/core/web/context/LoginUserHolder.java

@@ -2,6 +2,9 @@ package com.sckw.core.web.context;
 
 import com.sckw.core.web.model.LoginUserInfo;
 
+import java.util.Collections;
+import java.util.List;
+
 /**
  * 当前登录用户的临时保存容器
  * @Author zk
@@ -137,5 +140,11 @@ public class LoginUserHolder {
         return LONGIN_USER_HOLDER.get() == null ? null : LONGIN_USER_HOLDER.get().getDeptIds();
     }
 
+    /**
+     * 用户权限
+     */
+    public static List<Long> authUserIdList() {
+        return LONGIN_USER_HOLDER.get() == null ? Collections.emptyList() : LONGIN_USER_HOLDER.get().getAuthUserIdList();
+    }
 
 }

+ 5 - 1
sckw-common/sckw-common-core/src/main/java/com/sckw/core/web/model/LoginUserInfo.java

@@ -2,6 +2,8 @@ package com.sckw.core.web.model;
 
 import lombok.Data;
 
+import java.util.List;
+
 /**
  * 登录中的用户信息
  * @Author zhaokang
@@ -55,12 +57,13 @@ public class LoginUserInfo {
      */
     private String entName;
 
+    private List<Long> authUserIdList;
 
     public LoginUserInfo() {
     }
 
     public LoginUserInfo(Long id, Integer systemType, String account, String userName, String phone,
-                         int isMain, int status, Long entId, String clientType, String deptIds) {
+                         int isMain, int status, Long entId, String clientType, String deptIds, List<Long> authUserIdList) {
         this.id = id;
         this.systemType = systemType;
         this.account = account;
@@ -71,5 +74,6 @@ public class LoginUserInfo {
         this.entId = entId;
         this.clientType = clientType;
         this.deptIds = deptIds;
+        this.authUserIdList = authUserIdList;
     }
 }

+ 9 - 0
sckw-modules-api/sckw-system-api/src/main/java/com/sckw/system/api/RemoteUserService.java

@@ -193,4 +193,13 @@ public interface RemoteUserService {
      * @date: 2023/9/1
      */
     PageResult queryEntInfoByCityCodeAndEntTypesWithPage(Integer cityCode, List<Integer> entTypeList, Integer page, Integer pageSize);
+
+    /**
+     * @param id 用户id
+     * @return List
+     * @desc: 根据用户id查数据权限
+     * @author: czh
+     * @date: 2023/9/4
+     */
+    List<Long> queryAuthUserList(Long id);
 }

+ 9 - 0
sckw-modules/sckw-system/src/main/java/com/sckw/system/dao/KwsUserDao.java

@@ -98,4 +98,13 @@ public interface KwsUserDao {
      * @return list
      */
     List<KwsUser> getUserByName(String name);
+
+    /**
+     * @param deptIds 机构id
+     * @return KwsUser
+     * @desc: 查机构下的用户
+     * @author: czh
+     * @date: 2023/9/4
+     */
+    List<KwsUser> selectByDeptIds(@Param(value = "list") List<Long> deptIds);
 }

+ 26 - 0
sckw-modules/sckw-system/src/main/java/com/sckw/system/dubbo/RemoteBaseService.java

@@ -6,6 +6,7 @@ import com.sckw.core.utils.CollectionUtils;
 import com.sckw.system.api.model.dto.res.REnterpriseVo;
 import com.sckw.system.api.model.dto.res.*;
 import com.sckw.system.api.model.pojo.DeptInfoPojo;
+import com.sckw.system.dao.KwsAuthorityDao;
 import com.sckw.system.dao.KwsEntDeptDao;
 import com.sckw.system.dao.KwsEntTypeDao;
 import com.sckw.system.model.*;
@@ -34,6 +35,9 @@ public class RemoteBaseService {
     @Resource
     private KwsEnterpriseService kwsEnterpriseService;
 
+    @Resource
+    private KwsAuthorityDao kwsAuthorityDao;
+
     @Resource
     private KwsDeptService kwsDeptService;
 
@@ -195,4 +199,26 @@ public class RemoteBaseService {
     public List<KwsEnterprise> queryEntInfoByCityCodeAndEntTypesWithPage(Integer cityCode, List<Integer> entTypeList) {
         return kwsEnterpriseService.queryEntInfoByCityCodeAndEntTypesWithPage(cityCode, entTypeList);
     }
+
+    /**
+     * @param roleIds 角色id
+     * @return KwsAuthority
+     * @desc: 根据角色id查数据权限信息
+     * @author: czh
+     * @date: 2023/9/4
+     */
+    public List<KwsAuthority> queryAuthorityByRoles(List<Long> roleIds) {
+        return kwsAuthorityDao.selectByRoleIds(roleIds);
+    }
+
+    /**
+     * @param deptIds 机构id
+     * @return KwsUser
+     * @desc: 查机构下的用户
+     * @author: czh
+     * @date: 2023/9/4
+     */
+    public List<KwsUser> queryUserByDeptIds(List<Long> deptIds) {
+        return kwsUserService.queryUserByDeptIds(deptIds);
+    }
 }

+ 40 - 0
sckw-modules/sckw-system/src/main/java/com/sckw/system/dubbo/RemoteUserServiceImpl.java

@@ -3,6 +3,7 @@ package com.sckw.system.dubbo;
 import com.github.pagehelper.PageHelper;
 import com.github.pagehelper.PageInfo;
 import com.sckw.core.exception.SystemException;
+import com.sckw.core.model.constant.Global;
 import com.sckw.core.model.page.PageHelperUtil;
 import com.sckw.core.model.page.PageResult;
 import com.sckw.core.utils.BeanUtils;
@@ -189,6 +190,45 @@ public class RemoteUserServiceImpl implements RemoteUserService {
         return PageHelperUtil.getPageResult(new PageInfo<>(list));
     }
 
+
+    /**
+     * @param id 用户id
+     * @return List
+     * @desc: 根据用户id查数据权限
+     * @author: czh
+     * @date: 2023/9/4
+     */
+    @Override
+    public List<Long> queryAuthUserList(Long id) {
+        if (Objects.isNull(id)) {
+            return Collections.emptyList();
+        }
+
+        List<KwsRoleResDto> kwsRoleResDtos = remoteBaseService.queryRoleInfoByUserId(id);
+        if (CollectionUtils.isEmpty(kwsRoleResDtos)) {
+            return Collections.emptyList();
+        }
+
+        List<KwsAuthority> kwsAuthorities = remoteBaseService.queryAuthorityByRoles(kwsRoleResDtos.stream().map(KwsRoleResDto::getId).toList());
+        if (CollectionUtils.isEmpty(kwsAuthorities)) {
+            return Collections.emptyList();
+        }
+
+        List<Long> deptIds = kwsAuthorities.stream().map(KwsAuthority::getDeptId).toList();
+        List<KwsUser> kwsUsers = remoteBaseService.queryUserByDeptIds(deptIds);
+        if(CollectionUtils.isEmpty(kwsUsers)) {
+            return Collections.emptyList();
+        }
+
+        return kwsUsers.stream().filter(item -> item.getIsMain().equals(Global.NO)).map(KwsUser::getId).distinct().toList();
+    }
+
+
+    /**
+     * 根据用户名查用户信息
+     * @param username 用户账号
+     * @return
+     */
     @Override
     public KwsUserResDto getUserByAccount(String username) {
         return remoteBaseService.getUserByAccount(username);

+ 13 - 3
sckw-modules/sckw-system/src/main/java/com/sckw/system/service/KwsUserService.java

@@ -1,5 +1,6 @@
 package com.sckw.system.service;
 
+import com.alibaba.fastjson.JSON;
 import com.sckw.core.annotation.RepeatSubmit;
 import com.sckw.core.common.enums.enums.DictEnum;
 import com.sckw.core.exception.SystemException;
@@ -611,9 +612,8 @@ public class KwsUserService {
     }
 
     public List<AreaTreeFrontResDto> test(List<Integer> list) {
-        PageResult pageResult = remoteUserService.queryEntInfoByCityCodeAndEntTypesWithPage(null, Collections.singletonList(1), 1, 10);
-
-
+        System.out.println(JSON.toJSONString(LoginUserHolder.authUserIdList()));
+//        PageResult pageResult = remoteUserService.queryEntInfoByCityCodeAndEntTypesWithPage(null, Collections.singletonList(1), 1, 10);
         return null;
     }
 
@@ -630,4 +630,14 @@ public class KwsUserService {
         return BeanUtils.copyToList(list, KwsUserResDto.class);
     }
 
+    /**
+     * @param deptIds 机构id
+     * @return KwsUser
+     * @desc: 查机构下的用户
+     * @author: czh
+     * @date: 2023/9/4
+     */
+    public List<KwsUser> queryUserByDeptIds(List<Long> deptIds) {
+        return kwsUserDao.selectByDeptIds(deptIds);
+    }
 }

+ 13 - 0
sckw-modules/sckw-system/src/main/resources/mapper/KwsUserDao.xml

@@ -346,4 +346,17 @@
        and a.name like concat('%', #{name}, '%')
   </select>
 
+    <select id="selectByDeptIds" resultType="com.sckw.system.model.KwsUser">
+      select a.*
+       from kws_user a
+       left join kws_user_dept b on a.id = b.user_id
+       left join kws_dept c on b.dept_id = c.id
+      where a.del_flag = 0
+        and c.del_flag = 0
+        and c.id in
+        <foreach collection="list" open="(" close=")" separator="," item="item">
+          #{item}
+        </foreach>
+    </select>
+
 </mapper>