Commit e177d169 authored by chenyuxi's avatar chenyuxi

feat: 部门里的属性调整,项目下用户权限

parent 2004a412
......@@ -209,6 +209,11 @@ public interface CacheConstants {
public static final String WX_JSAPI_TICKET = "WX_JSAPI_TICKET";
// 商险不购买审批流分布式锁key InsuranceUnpurchase
// 商险不购买审批流分布式锁key
public static final String INSURANCEUNPURCHASE_PROCESS_CACHE = ServiceNameConstants.SERVICE_NAME_MVP + "_" + "ProcessCache:InsuranceUnpurchase";
/**
* 项目下用户部门及权限缓存
*/
String PROJECT_USER_PERMISSION = "mvp_project_user_permission:";
}
......@@ -78,4 +78,14 @@ public class SysDept extends BaseEntity {
@TableLogic
private String delFlag;
/**
* V1.9.7:新增以下3个字段
*/
@Schema(description = "关联项目编码")
private String projectNo;
@Schema(description = "归属系统 0 mvp 1 甲方客户端")
private String client;
@Schema(description = "祖级列表")
private String ancestors;
}
......@@ -157,7 +157,7 @@ public class SysUser extends BaseEntity {
/**
* @Author fxj
* @Description 0 系统用户 1 LDAP用户 2 B端用户 3.微信用户 默认系统用户
* @Description 0 系统用户 1 LDAP用户 2 B端用户 3.微信用户 默认系统用户 4.甲方客户端
* @Date 14:56 2023/1/6
**/
private String type;
......
/*
* Copyright (c) 2020 yifu4cloud Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.yifu.cloud.plus.v1.yifu.admin.api.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* 项目下用户部门及权限
*
* @author chenyuxi
* @since 1.9.7
*/
@Data
@EqualsAndHashCode(callSuper = true)
public class SysUserDeptPermission extends Model<SysUserDeptPermission> {
private static final long serialVersionUID = 1L;
@TableId(value = "user_id", type = IdType.ASSIGN_ID)
@Schema(description = "主键id")
private String id;
@Schema(description = "用户id")
private String userId;
@Schema(description = "部门id")
private Long deptId;
@Schema(description = "关联项目编码")
private String projectNo;
@Schema(description = "归属系统 0 mvp 1 甲方客户端")
private String client;
@Schema(description = "账号类型:0 管理员, 1 普通成员")
private String userType;
@Schema(description = "数据权限(0:全部数据,1:二级部门及以下,2:本部门及以下,3:指定部门)")
private String permissionsType;
@Schema(description = "指定部门范围(部门id用逗号隔开)")
private String appointDeptScope;
}
package com.yifu.cloud.plus.v1.yifu.admin.api.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.io.Serializable;
import java.util.Map;
import java.util.Set;
/**
* 项目下用户数据权限封装类
* @author chenyuxi
* @date 2025/2/24
* @since 1.9.7
*/
@Data
public class UserPermissionVo implements Serializable {
/**
* 全部权限(默认不是)
*/
private boolean haveAll = false;
/**
* 部门ID集合
*/
private Set<String> departIdSet;
/**
* 账号类型:0 管理员, 1 普通成员
*/
private String userType;
/**
* 用户ID
*/
private String userId;
private Map<String,UserPermissionVo> permissionVoMap;
}
......@@ -45,4 +45,6 @@ public interface SysDeptMapper extends BaseMapper<SysDept> {
int updateDeptById(@Param("dept") SysDept dept);
SysDept selectDeptDn(@Param("deptDn")String deptDn);
List<SysDept> getDeptListByDeptId(@Param("deptIdSplit") String deptIdSplit, @Param("deptId") Long deptId);
}
/*
* Copyright (c) 2020 yifu4cloud Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.yifu.cloud.plus.v1.yifu.admin.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yifu.cloud.plus.v1.yifu.admin.api.entity.SysUserDeptPermission;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* 项目下用户部门及权限
*
* @author chenyuxi
* @since 1.9.7
*/
@Mapper
public interface SysUserDeptPermissionMapper extends BaseMapper<SysUserDeptPermission> {
SysUserDeptPermission getUserDeptPermission(@Param("userId")String userId, @Param("projectNo") String projectNo);
List<SysUserDeptPermission> getUserDeptPermissionList(@Param("userId")String userId);
}
/*
* Copyright (c) 2020 yifu4cloud Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.yifu.cloud.plus.v1.yifu.admin.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.yifu.cloud.plus.v1.yifu.admin.api.entity.SysUserDeptPermission;
import com.yifu.cloud.plus.v1.yifu.admin.api.vo.UserPermissionVo;
import java.util.Map;
/**
* 项目下用户部门及权限
*
* @author chenyuxi
* @since 1.9.7
*/
public interface SysUserDeptPermissionService extends IService<SysUserDeptPermission> {
/**
* 项目下用户数据权限
* @param projectNo 项目编号
* @param userId 用户ID
*
* @return UserPermissionVo 用户在指定项目下的权限
*/
UserPermissionVo getUserPermissionVo(String projectNo, String userId);
/**
* 用户所有项目的数据权限
* @param userId 用户ID
*
* @return Map<String, UserPermissionVo> 用户不同项目的数据权限
*/
Map<String, UserPermissionVo> getUserPermissionFlush(String userId);
}
/*
* Copyright (c) 2020 yifu4cloud Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.yifu.cloud.plus.v1.yifu.admin.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yifu.cloud.plus.v1.yifu.admin.api.entity.SysDept;
import com.yifu.cloud.plus.v1.yifu.admin.api.entity.SysUserDeptPermission;
import com.yifu.cloud.plus.v1.yifu.admin.api.vo.UserPermissionVo;
import com.yifu.cloud.plus.v1.yifu.admin.mapper.SysDeptMapper;
import com.yifu.cloud.plus.v1.yifu.admin.mapper.SysUserDeptPermissionMapper;
import com.yifu.cloud.plus.v1.yifu.admin.service.SysUserDeptPermissionService;
import com.yifu.cloud.plus.v1.yifu.common.core.constant.CacheConstants;
import com.yifu.cloud.plus.v1.yifu.common.core.constant.CommonConstants;
import com.yifu.cloud.plus.v1.yifu.common.core.util.Common;
import com.yifu.cloud.plus.v1.yifu.common.core.util.RedisUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.stream.Collectors;
/**
* 项目下用户部门及权限
*
* @author chenyuxi
* @since 1.9.7
*/
@Slf4j
@Service
@RequiredArgsConstructor
public class SysUserDeptPermissionServiceImpl extends ServiceImpl<SysUserDeptPermissionMapper, SysUserDeptPermission> implements SysUserDeptPermissionService {
private final RedisUtil redisUtil;
private final SysDeptMapper sysDeptMapper;
@Override
public UserPermissionVo getUserPermissionVo(String projectNo, String userId) {
UserPermissionVo authority = new UserPermissionVo();
if (Common.isEmpty(projectNo) && Common.isEmpty(userId)) {
log.error("获取用户权限失败,缺少用户或项目信息");
return authority;
}
Object cacheValue = redisUtil.get(CacheConstants.PROJECT_USER_PERMISSION + projectNo +"_"+ userId);
if (null != cacheValue) {
authority = (UserPermissionVo) cacheValue;
} else {
Map<String, UserPermissionVo> tPermissionMapByProject = getUserPermissionFlush(userId);
authority = tPermissionMapByProject.get(projectNo);
}
return authority;
}
@Override
public Map<String, UserPermissionVo> getUserPermissionFlush(String userId) {
UserPermissionVo authority;
Map<String, UserPermissionVo> tPermissionMapByProject = new HashMap<>();
if (Common.isEmpty(userId)) {
log.error("获取用户权限失败,缺少用户信息");
return tPermissionMapByProject;
}
String deptIdSplit = "";
Long deptId = 0L;
Set<String> permissionDbSet = null;
List<SysUserDeptPermission> tPermissionInfoList = baseMapper.getUserDeptPermissionList(userId);
if (Common.isEmpty(tPermissionInfoList)) {
log.error("获取用户权限失败,未获取到用户在项目下的权限信息>>用户"+userId);
return tPermissionMapByProject;
}
Set<String> projectNoSet = tPermissionInfoList.stream().map(SysUserDeptPermission::getProjectNo).collect(Collectors.toSet());
Map<String, SysUserDeptPermission> tPermissionMap = tPermissionInfoList.stream().collect(Collectors.toMap(SysUserDeptPermission::getProjectNo, v -> v, (v1, v2) -> v1));
SysUserDeptPermission tPermissionInfo;
for (String projectNoStr : projectNoSet) {
tPermissionInfo = tPermissionMap.get(projectNoStr);
// 没有配置部门
if(Common.isEmpty(tPermissionInfo)){
authority = new UserPermissionVo();
log.error("获取用户权限失败,该用户指定项目权限{}", userId);
authority.setHaveAll(false);
redisUtil.set(CacheConstants.PROJECT_USER_PERMISSION + projectNoStr +"_"+ userId, authority);
continue;
}else {
authority = new UserPermissionVo();
if (CommonConstants.ZERO_STRING.equals(tPermissionInfo.getPermissionsType())) {
// 全部数据
authority.setHaveAll(true);
// redisUtil.set(CacheConstants.PROJECT_USER_PERMISSION + projectNoStr +"_"+ userId, authority);
} else if (CommonConstants.THREE_STRING.equals(tPermissionInfo.getPermissionsType())) {
// 指定部门权限
if (Common.isNotKong(tPermissionInfo.getAppointDeptScope())) {
authority.setHaveAll(false);
String[] appointDeptIdArr = tPermissionInfo.getAppointDeptScope().split(CommonConstants.COMMA_STRING);
Set<String> appointDeptIdSet = Arrays.stream(appointDeptIdArr).collect(Collectors.toSet());
authority.setDepartIdSet(appointDeptIdSet);
authority.setUserId(userId);
// redisUtil.set(CacheConstants.PROJECT_USER_PERMISSION + projectNoStr +"_"+ userId, authority);
} else {
log.error("获取用户权限失败,该用户指定部门权限为空{}", userId);
authority.setHaveAll(false);
redisUtil.set(CacheConstants.PROJECT_USER_PERMISSION + projectNoStr +"_"+ userId, authority);
continue;
}
} else {
SysDept sysDept = sysDeptMapper.selectById(tPermissionInfo.getDeptId());
if (Common.isNotKong(sysDept)) {
String ancestors = sysDept.getAncestors();
if (Common.isEmpty(ancestors)) {
log.error("获取用户权限失败,该用户所属部门关系为空{}", sysDept);
authority.setHaveAll(false);
redisUtil.set(CacheConstants.PROJECT_USER_PERMISSION + projectNoStr +"_"+ userId, authority);
continue;
}
String[] ancestorsSplit = ancestors.split(CommonConstants.COMMA_STRING);
if (CommonConstants.ONE_STRING.equals(tPermissionInfo.getPermissionsType())) {
// 二级部门及以下
if (ancestorsSplit.length == 2) {
deptIdSplit = ancestorsSplit[0] + CommonConstants.COMMA_STRING + ancestorsSplit[1] + CommonConstants.COMMA_STRING + sysDept.getDeptId();
deptId = sysDept.getDeptId();
} else if (ancestorsSplit.length > 2) {
deptIdSplit = ancestorsSplit[0] + CommonConstants.COMMA_STRING + ancestorsSplit[1] + CommonConstants.COMMA_STRING + ancestorsSplit[2];
deptId = Long.valueOf(ancestorsSplit[2]);
} else {
log.error("获取用户权限失败,该用户所在部门为顶级部门,未查到权限{}", sysDept);
authority.setHaveAll(false);
redisUtil.set(CacheConstants.PROJECT_USER_PERMISSION + projectNoStr +"_"+ userId, authority);
continue;
}
} else if (CommonConstants.TWO_STRING.equals(tPermissionInfo.getPermissionsType())) {
// 本级部门及以下
deptIdSplit = ancestors + CommonConstants.COMMA_STRING + sysDept.getDeptId();
deptId = sysDept.getDeptId();
}
// 获取指定部门及下级部门的数据
List<SysDept> sysDeptList = sysDeptMapper.getDeptListByDeptId(deptIdSplit, deptId);
if (Common.isNotKong(sysDeptList)) {
permissionDbSet = new HashSet<>();
for(SysDept deptOne : sysDeptList){
permissionDbSet.add(String.valueOf(deptOne.getDeptId()));
}
authority.setHaveAll(false);
authority.setDepartIdSet(permissionDbSet);
authority.setUserId(userId);
//redisUtil.set(CacheConstants.PROJECT_USER_PERMISSION + projectNoStr +"_"+ userId, authority);
} else {
log.error("获取用户权限失败,该用户部门及以下权限为空{}", userId);
authority.setHaveAll(false);
redisUtil.set(CacheConstants.PROJECT_USER_PERMISSION + projectNoStr +"_"+ userId, authority);
continue;
}
} else {
log.error("获取用户权限失败,该用户所属部门不存在{}", userId);
authority.setHaveAll(false);
redisUtil.set(CacheConstants.PROJECT_USER_PERMISSION + projectNoStr +"_"+ userId, authority);
continue;
}
}
}
// 赋值用户在不同项目下的权限
tPermissionMapByProject.put(projectNoStr,authority);
redisUtil.set(CacheConstants.PROJECT_USER_PERMISSION + projectNoStr +"_"+ userId, authority);
}
return tPermissionMapByProject;
}
}
......@@ -29,6 +29,9 @@
<result column="update_by" property="updateBy"/>
<result column="update_time" property="updateTime"/>
<result column="del_flag" property="delFlag"/>
<result column="project_no" property="projectNo"/>
<result column="client" property="client"/>
<result column="ancestors" property="ancestors"/>
</resultMap>
<select id="selectCountId" resultType="com.yifu.cloud.plus.v1.yifu.admin.api.entity.SysDept">
......@@ -49,6 +52,17 @@
</where>
</select>
<select id="getDeptListByDeptId" resultType="com.yifu.cloud.plus.v1.yifu.admin.api.entity.SysDept">
select
dept_id as deptId,
dept_name as deptName
from
sys_dept
where
del_flag = '0'
and (dept_id = #{deptId} or ancestors like concat(#{deptIdSplit}, '%'))
</select>
<update id="updateDeptById">
update sys_dept
<trim prefix="set" suffixOverrides=",">
......
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2020 yifu4cloud Authors. All Rights Reserved.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yifu.cloud.plus.v1.yifu.admin.mapper.SysUserDeptPermissionMapper">
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="com.yifu.cloud.plus.v1.yifu.admin.api.entity.SysUserDeptPermission">
<id column="id" property="id"/>
<result column="user_id" property="userId"/>
<result column="dept_id" property="deptId"/>
<result column="project_no" property="projectNo"/>
<result column="client" property="client"/>
<result column="user_type" property="userType"/>
<result column="permissions_type" property="permissionsType"/>
<result column="appoint_dept_scope" property="appointDeptScope"/>
</resultMap>
<sql id="Base_Column_List">
a.id,
a.user_id,
a.dept_id,
a.project_no,
a.client,
a.user_type,
a.permissions_type,
a.appoint_dept_scope
</sql>
<select id="getUserDeptPermission" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List"/>
FROM sys_user_dept_permission a
WHERE a.user_id = #{userId} and a.project_no = #{projectNo}
limit one
</select>
<select id="getUserDeptPermissionList" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List"/>
FROM sys_user_dept_permission a
WHERE a.user_id = #{userId}
</select>
</mapper>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment