作者 朱兆平

add:

1. 增加用户数据权限查询接口
2. 增加数据权限管理接口
3. 用户类增加数据权限列表属性
4. 数据库增加数据权限表和组织机构与数据权限关系表
# 分支描述
- SAAS化用户集中管理鉴权平台
- 基于HQPT-USER分支增加用户数据权限判定
... ...
... ... @@ -11,7 +11,7 @@
</parent>
<groupId>com.tianbo</groupId>
<artifactId>warehouse</artifactId>
<version>4.0Beta-organization-update</version>
<version>5.0Beta-DataPermission</version>
<name>usercenter</name>
<description>usercenter for springcloud</description>
... ...
... ... @@ -4,7 +4,9 @@ import com.github.pagehelper.PageInfo;
import com.tianbo.warehouse.annotation.LogAnnotation;
import com.tianbo.warehouse.controller.response.ResultJson;
import com.tianbo.warehouse.model.PERMISSION;
import com.tianbo.warehouse.model.USERS;
import com.tianbo.warehouse.service.PermissionService;
import com.tianbo.warehouse.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
... ... @@ -23,6 +25,9 @@ public class PermssionController {
@Autowired
PermissionService permissionService;
@Autowired
UserService userService;
@GetMapping("/list")
public PageInfo<PERMISSION> list(@RequestParam(value = "pageNum",required = false,defaultValue = "1")
int pageNum,
... ... @@ -135,4 +140,14 @@ public class PermssionController {
return new ResultJson("200","success",result);
}
@GetMapping("/getUserPermByToken")
public ResultJson get(
@RequestHeader("Authorization") String token,
@RequestParam(value = "path") String path
){
System.out.println("im in");
USERS userInfo = userService.getUserDataPermissionsByPath(token, path);
return new ResultJson("200","get user data permissions success",userInfo);
}
}
... ...
package com.tianbo.warehouse.dao;
import com.tianbo.warehouse.model.DataPermission;
public interface DataPermissionDao {
int deleteByPrimaryKey(Integer data_perm_id);
int insert(DataPermission record);
int insertSelective(DataPermission record);
DataPermission selectByPrimaryKey(Integer data_perm_id);
int updateByPrimaryKeySelective(DataPermission record);
int updateByPrimaryKey(DataPermission record);
}
\ No newline at end of file
... ...
... ... @@ -24,4 +24,6 @@ public interface USERSMapper {
List<USERS> selectAllUser(USERS users);
USERS getUserDataPermissionsByPath(@Param("userId") Integer userId,@Param("path") String path);
}
... ...
package com.tianbo.warehouse.model;
import java.io.Serializable;
import java.util.Date;
import lombok.Data;
/**
* data_permission
* @author
*/
@Data
public class DataPermission implements Serializable {
private Integer data_perm_id;
/**
* 数据权限名称
*/
private String perm_name;
/**
* 数据权限描述
*/
private String perm_des;
/**
* 权限排序
*/
private Byte perm_sort;
/**
* 权限禁用状态 - 0 不禁用 / 1 禁用
*/
private Boolean perm_status;
/**
* 创建时间
*/
private Date create_time;
/**
* 更新时间
*/
private Date update_time;
/**
* 创建人id/名称
*/
private String create_user;
/**
* 条件行冗余,数组转字符串orJson字符串
可以为* ,代表所有行
*/
private String row_condition;
/**
* 行条件对应的实体类名
*/
private String row_condition_property;
/**
* 条件列冗余,数组转字符串orJson字符串
可以为* 代表所有列
*/
private String cols_list;
/**
* 对应访问权限id
*/
private Integer permission_id;
/**
* 对应接口访问地址
*/
private String path;
/**
* 接口名称冗余
*/
private String interface_name;
/**
* 涉及微服务名称冗余
*/
private String service_name;
/**
* 涉及微服务描述
*/
private String service_name_des;
/**
* 关键字
*/
private String perm_keyword;
/**
* 权限类别-row:行权限,col:列权限
*/
private String perm_type;
private static final long serialVersionUID = 1L;
}
... ...
... ... @@ -70,6 +70,9 @@ public class USERS implements UserDetails {
@JSONField(serialzeFeatures= {SerializerFeature.WriteMapNullValue,SerializerFeature.WriteNullStringAsEmpty})
private List<PERMISSION> permissions;
@JSONField(serialzeFeatures= {SerializerFeature.WriteMapNullValue,SerializerFeature.WriteNullStringAsEmpty})
private List<DataPermission> dataPermissions;
public Integer getUserId() {
return userId;
}
... ... @@ -200,6 +203,14 @@ public class USERS implements UserDetails {
this.permissions = permissions;
}
public List<DataPermission> getDataPermissions() {
return dataPermissions;
}
public void setDataPermissions(List<DataPermission> dataPermissions) {
this.dataPermissions = dataPermissions;
}
public String getToken() {
return token;
}
... ...
package com.tianbo.warehouse.service;
public interface DataPermissionService {
Boolean getPermission(String token,String url,String name);
}
... ...
... ... @@ -23,4 +23,6 @@ public interface UserService {
ROLE getUserCompany(Integer company_id);
USERS selectByUserId(Integer userid);
USERS getUserDataPermissionsByPath(String token,String path);
}
... ...
package com.tianbo.warehouse.service.imp;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
... ... @@ -14,6 +15,8 @@ import com.tianbo.warehouse.model.USERS;
import com.tianbo.warehouse.model.UserRole;
import com.tianbo.warehouse.service.PermissionService;
import com.tianbo.warehouse.service.UserService;
import com.tianbo.warehouse.util.RedisUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
... ... @@ -42,6 +45,9 @@ public class UserServiceImpl implements UserService{
@Autowired
private PermissionService permissionService;
@Autowired
RedisUtils redisUtils;
@Override
public USERS loadByUsername(String username){
List<USERS> userList = usersMapper.selectByUsername(username);
... ... @@ -192,4 +198,22 @@ public class UserServiceImpl implements UserService{
return usersMapper.selectByPrimaryKey(userid);
}
@Override
public USERS getUserDataPermissionsByPath(String token,String path){
USERS userinfo = new USERS();
try {
if(token != null && token.startsWith("Bearer ")) {
token = token.substring(7); // 7 是 "Bearer " 的长度
String userJsonStr = redisUtils.get(token);
if (StringUtils.isNotEmpty(userJsonStr)) {
USERS user = JSONObject.parseObject(userJsonStr, USERS.class);
userinfo = usersMapper.getUserDataPermissionsByPath(user.getUserId(),path);
}
}
}catch (Exception e){
e.printStackTrace();
}
return userinfo;
}
}
... ...
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.tianbo.warehouse.dao.DataPermissionDao">
<resultMap id="BaseResultMap" type="com.tianbo.warehouse.model.DataPermission">
<id column="data_perm_id" jdbcType="INTEGER" property="data_perm_id" />
<result column="perm_name" jdbcType="VARCHAR" property="perm_name" />
<result column="perm_des" jdbcType="VARCHAR" property="perm_des" />
<result column="perm_sort" jdbcType="TINYINT" property="perm_sort" />
<result column="perm_status" jdbcType="BOOLEAN" property="perm_status" />
<result column="create_time" jdbcType="TIMESTAMP" property="create_time" />
<result column="update_time" jdbcType="TIMESTAMP" property="update_time" />
<result column="create_user" jdbcType="VARCHAR" property="create_user" />
<result column="row_condition" jdbcType="VARCHAR" property="row_condition" />
<result column="row_condition_property" jdbcType="VARCHAR" property="row_condition_property" />
<result column="cols_list" jdbcType="VARCHAR" property="cols_list" />
<result column="permission_id" jdbcType="INTEGER" property="permission_id" />
<result column="path" jdbcType="VARCHAR" property="path" />
<result column="interface_name" jdbcType="VARCHAR" property="interface_name" />
<result column="service_name" jdbcType="VARCHAR" property="service_name" />
<result column="service_name_des" jdbcType="VARCHAR" property="service_name_des" />
<result column="perm_keyword" jdbcType="VARCHAR" property="perm_keyword" />
<result column="perm_type" jdbcType="VARCHAR" property="perm_type" />
</resultMap>
<sql id="Base_Column_List">
data_perm_id, perm_name, perm_des, perm_sort, perm_status, create_time, update_time,
create_user, row_condition, row_condition_property, cols_list, permission_id, `path`,
interface_name, service_name, service_name_des, perm_keyword, perm_type
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from data_permission
where data_perm_id = #{data_perm_id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from data_permission
where data_perm_id = #{data_perm_id,jdbcType=INTEGER}
</delete>
<insert id="insert" keyColumn="data_perm_id" keyProperty="data_perm_id" parameterType="com.tianbo.warehouse.model.DataPermission" useGeneratedKeys="true">
insert into data_permission (perm_name, perm_des, perm_sort,
perm_status, create_time, update_time,
create_user, row_condition, row_condition_property,
cols_list, permission_id, `path`,
interface_name, service_name, service_name_des,
perm_keyword, perm_type)
values (#{perm_name,jdbcType=VARCHAR}, #{perm_des,jdbcType=VARCHAR}, #{perm_sort,jdbcType=TINYINT},
#{perm_status,jdbcType=BOOLEAN}, #{create_time,jdbcType=TIMESTAMP}, #{update_time,jdbcType=TIMESTAMP},
#{create_user,jdbcType=VARCHAR}, #{row_condition,jdbcType=VARCHAR}, #{row_condition_property,jdbcType=VARCHAR},
#{cols_list,jdbcType=VARCHAR}, #{permission_id,jdbcType=INTEGER}, #{path,jdbcType=VARCHAR},
#{interface_name,jdbcType=VARCHAR}, #{service_name,jdbcType=VARCHAR}, #{service_name_des,jdbcType=VARCHAR},
#{perm_keyword,jdbcType=VARCHAR}, #{perm_type,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" keyColumn="data_perm_id" keyProperty="data_perm_id" parameterType="com.tianbo.warehouse.model.DataPermission" useGeneratedKeys="true">
insert into data_permission
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="perm_name != null">
perm_name,
</if>
<if test="perm_des != null">
perm_des,
</if>
<if test="perm_sort != null">
perm_sort,
</if>
<if test="perm_status != null">
perm_status,
</if>
<if test="create_time != null">
create_time,
</if>
<if test="update_time != null">
update_time,
</if>
<if test="create_user != null">
create_user,
</if>
<if test="row_condition != null">
row_condition,
</if>
<if test="row_condition_property != null">
row_condition_property,
</if>
<if test="cols_list != null">
cols_list,
</if>
<if test="permission_id != null">
permission_id,
</if>
<if test="path != null">
`path`,
</if>
<if test="interface_name != null">
interface_name,
</if>
<if test="service_name != null">
service_name,
</if>
<if test="service_name_des != null">
service_name_des,
</if>
<if test="perm_keyword != null">
perm_keyword,
</if>
<if test="perm_type != null">
perm_type,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="perm_name != null">
#{perm_name,jdbcType=VARCHAR},
</if>
<if test="perm_des != null">
#{perm_des,jdbcType=VARCHAR},
</if>
<if test="perm_sort != null">
#{perm_sort,jdbcType=TINYINT},
</if>
<if test="perm_status != null">
#{perm_status,jdbcType=BOOLEAN},
</if>
<if test="create_time != null">
#{create_time,jdbcType=TIMESTAMP},
</if>
<if test="update_time != null">
#{update_time,jdbcType=TIMESTAMP},
</if>
<if test="create_user != null">
#{create_user,jdbcType=VARCHAR},
</if>
<if test="row_condition != null">
#{row_condition,jdbcType=VARCHAR},
</if>
<if test="row_condition_property != null">
#{row_condition_property,jdbcType=VARCHAR},
</if>
<if test="cols_list != null">
#{cols_list,jdbcType=VARCHAR},
</if>
<if test="permission_id != null">
#{permission_id,jdbcType=INTEGER},
</if>
<if test="path != null">
#{path,jdbcType=VARCHAR},
</if>
<if test="interface_name != null">
#{interface_name,jdbcType=VARCHAR},
</if>
<if test="service_name != null">
#{service_name,jdbcType=VARCHAR},
</if>
<if test="service_name_des != null">
#{service_name_des,jdbcType=VARCHAR},
</if>
<if test="perm_keyword != null">
#{perm_keyword,jdbcType=VARCHAR},
</if>
<if test="perm_type != null">
#{perm_type,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.tianbo.warehouse.model.DataPermission">
update data_permission
<set>
<if test="perm_name != null">
perm_name = #{perm_name,jdbcType=VARCHAR},
</if>
<if test="perm_des != null">
perm_des = #{perm_des,jdbcType=VARCHAR},
</if>
<if test="perm_sort != null">
perm_sort = #{perm_sort,jdbcType=TINYINT},
</if>
<if test="perm_status != null">
perm_status = #{perm_status,jdbcType=BOOLEAN},
</if>
<if test="create_time != null">
create_time = #{create_time,jdbcType=TIMESTAMP},
</if>
<if test="update_time != null">
update_time = #{update_time,jdbcType=TIMESTAMP},
</if>
<if test="create_user != null">
create_user = #{create_user,jdbcType=VARCHAR},
</if>
<if test="row_condition != null">
row_condition = #{row_condition,jdbcType=VARCHAR},
</if>
<if test="row_condition_property != null">
row_condition_property = #{row_condition_property,jdbcType=VARCHAR},
</if>
<if test="cols_list != null">
cols_list = #{cols_list,jdbcType=VARCHAR},
</if>
<if test="permission_id != null">
permission_id = #{permission_id,jdbcType=INTEGER},
</if>
<if test="path != null">
`path` = #{path,jdbcType=VARCHAR},
</if>
<if test="interface_name != null">
interface_name = #{interface_name,jdbcType=VARCHAR},
</if>
<if test="service_name != null">
service_name = #{service_name,jdbcType=VARCHAR},
</if>
<if test="service_name_des != null">
service_name_des = #{service_name_des,jdbcType=VARCHAR},
</if>
<if test="perm_keyword != null">
perm_keyword = #{perm_keyword,jdbcType=VARCHAR},
</if>
<if test="perm_type != null">
perm_type = #{perm_type,jdbcType=VARCHAR},
</if>
</set>
where data_perm_id = #{data_perm_id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.tianbo.warehouse.model.DataPermission">
update data_permission
set perm_name = #{perm_name,jdbcType=VARCHAR},
perm_des = #{perm_des,jdbcType=VARCHAR},
perm_sort = #{perm_sort,jdbcType=TINYINT},
perm_status = #{perm_status,jdbcType=BOOLEAN},
create_time = #{create_time,jdbcType=TIMESTAMP},
update_time = #{update_time,jdbcType=TIMESTAMP},
create_user = #{create_user,jdbcType=VARCHAR},
row_condition = #{row_condition,jdbcType=VARCHAR},
row_condition_property = #{row_condition_property,jdbcType=VARCHAR},
cols_list = #{cols_list,jdbcType=VARCHAR},
permission_id = #{permission_id,jdbcType=INTEGER},
`path` = #{path,jdbcType=VARCHAR},
interface_name = #{interface_name,jdbcType=VARCHAR},
service_name = #{service_name,jdbcType=VARCHAR},
service_name_des = #{service_name_des,jdbcType=VARCHAR},
perm_keyword = #{perm_keyword,jdbcType=VARCHAR},
perm_type = #{perm_type,jdbcType=VARCHAR}
where data_perm_id = #{data_perm_id,jdbcType=INTEGER}
</update>
</mapper>
\ No newline at end of file
... ...
... ... @@ -76,6 +76,30 @@
<result column="component" property="component" jdbcType="VARCHAR" />
</collection>
</resultMap>
<resultMap id="DataPermissionResultMap" type="com.tianbo.warehouse.model.USERS" extends="BaseResultMap">
<result column="role_id" property="companyId" jdbcType="INTEGER" />
<result column="role_name" property="companyName" jdbcType="VARCHAR" />
<collection property="dataPermissions" javaType="java.util.ArrayList" ofType="com.tianbo.warehouse.model.DataPermission">
<id column="data_perm_id" jdbcType="INTEGER" property="data_perm_id" />
<result column="perm_name" jdbcType="VARCHAR" property="perm_name" />
<result column="perm_des" jdbcType="VARCHAR" property="perm_des" />
<result column="perm_sort" jdbcType="TINYINT" property="perm_sort" />
<result column="perm_status" jdbcType="BOOLEAN" property="perm_status" />
<result column="row_condition" jdbcType="VARCHAR" property="row_condition" />
<result column="row_condition_property" jdbcType="VARCHAR" property="row_condition_property" />
<result column="cols_list" jdbcType="VARCHAR" property="cols_list" />
<result column="permission_id" jdbcType="INTEGER" property="permission_id" />
<result column="path" jdbcType="VARCHAR" property="path" />
<result column="perm_type" jdbcType="VARCHAR" property="perm_type" />
</collection>
</resultMap>
<sql id="Data_Perm_List">
u.user_id,u.username,r.role_name,r.role_id,dp.data_perm_id,dp.perm_name,dp.perm_des,
dp.perm_sort,dp.perm_status,dp.row_condition,dp.row_condition_property,dp.cols_list,
dp.path,dp.perm_type
</sql>
<sql id="Base_Column_List" >
user_id, username, password, birthday, sex, address, state, mobilePhone, creatTime,
updateTime, userFace, realName, email, age,company_id
... ... @@ -327,4 +351,18 @@
age = #{age,jdbcType=INTEGER}
where user_id = #{userId,jdbcType=INTEGER}
</update>
<select id="getUserDataPermissionsByPath" resultMap="DataPermissionResultMap" >
select
<include refid="Data_Perm_List" />
from
users u
LEFT JOIN user_role ur ON u.user_id = ur.user_id
LEFT JOIN role r ON ur.role_id = r.role_id
LEFT JOIN role_data_permission rdp ON r.role_id = rdp.role_id
LEFT JOIN data_permission dp ON rdp.permission_id = dp.data_perm_id
where u.user_id = #{userId,jdbcType=INTEGER}
and path = #{path,jdbcType=VARCHAR}
and dp.perm_status = 0
</select>
</mapper>
... ...