Browse Source

🎉 数据权限第一版

test
smallchill 6 years ago
parent
commit
9262d58f00
  1. 7
      blade-auth/src/main/java/org/springblade/auth/service/BladeUserDetails.java
  2. 2
      blade-auth/src/main/java/org/springblade/auth/service/BladeUserDetailsServiceImpl.java
  3. 1
      blade-auth/src/main/java/org/springblade/auth/support/BladeJwtTokenEnhancer.java
  4. 1
      blade-auth/src/main/java/org/springblade/auth/utils/TokenUtil.java
  5. 3
      blade-service-api/blade-desk-api/src/main/java/org/springblade/desk/vo/NoticeVO.java
  6. 14
      blade-service/blade-desk/src/main/java/org/springblade/desk/controller/NoticeController.java
  7. 5
      blade-service/blade-desk/src/main/java/org/springblade/desk/mapper/NoticeMapper.java
  8. 41
      blade-service/blade-desk/src/main/java/org/springblade/desk/mapper/NoticeMapper.xml
  9. 3
      blade-service/blade-desk/src/main/java/org/springblade/desk/service/INoticeService.java
  10. 6
      blade-service/blade-desk/src/main/java/org/springblade/desk/service/impl/NoticeServiceImpl.java
  11. 76
      doc/sql/bladex-datascope-update.sql
  12. 26
      doc/sql/bladex-tenantid-update.sql
  13. 4
      pom.xml

7
blade-auth/src/main/java/org/springblade/auth/service/BladeUserDetails.java

@ -46,6 +46,10 @@ public class BladeUserDetails extends User {
* 账号
*/
private String account;
/**
* 部门id
*/
private String deptId;
/**
* 角色id
*/
@ -59,12 +63,13 @@ public class BladeUserDetails extends User {
*/
private String avatar;
public BladeUserDetails(Long userId, String tenantId, String name, String roleId, String roleName, String avatar, String username, String password, boolean enabled, boolean accountNonExpired, boolean credentialsNonExpired, boolean accountNonLocked, Collection<? extends GrantedAuthority> authorities) {
public BladeUserDetails(Long userId, String tenantId, String name, String deptId, String roleId, String roleName, String avatar, String username, String password, boolean enabled, boolean accountNonExpired, boolean credentialsNonExpired, boolean accountNonLocked, Collection<? extends GrantedAuthority> authorities) {
super(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities);
this.userId = userId;
this.tenantId = tenantId;
this.name = name;
this.account = username;
this.deptId = deptId;
this.roleId = roleId;
this.roleName = roleName;
this.avatar = avatar;

2
blade-auth/src/main/java/org/springblade/auth/service/BladeUserDetailsServiceImpl.java

@ -73,7 +73,7 @@ public class BladeUserDetailsServiceImpl implements UserDetailsService {
throw new UsernameNotFoundException(TokenUtil.USER_NOT_FOUND);
}
return new BladeUserDetails(user.getId(),
user.getTenantId(), user.getName(), user.getRoleId(), Func.join(result.getData().getRoles()), TokenUtil.DEFAULT_AVATAR,
user.getTenantId(), user.getName(), user.getDeptId(), user.getRoleId(), Func.join(result.getData().getRoles()), TokenUtil.DEFAULT_AVATAR,
username, AuthConstant.ENCRYPT + user.getPassword(), true, true, true, true,
AuthorityUtils.commaSeparatedStringToAuthorityList(Func.join(result.getData().getRoles())));
} else {

1
blade-auth/src/main/java/org/springblade/auth/support/BladeJwtTokenEnhancer.java

@ -39,6 +39,7 @@ public class BladeJwtTokenEnhancer implements TokenEnhancer {
Map<String, Object> info = new HashMap<>(16);
info.put(TokenUtil.CLIENT_ID, TokenUtil.getClientIdFromHeader());
info.put(TokenUtil.USER_ID, Func.toStr(principal.getUserId()));
info.put(TokenUtil.DEPT_ID, Func.toStr(principal.getDeptId()));
info.put(TokenUtil.ROLE_ID, Func.toStr(principal.getRoleId()));
info.put(TokenUtil.TENANT_ID, principal.getTenantId());
info.put(TokenUtil.ACCOUNT, principal.getAccount());

1
blade-auth/src/main/java/org/springblade/auth/utils/TokenUtil.java

@ -39,6 +39,7 @@ public class TokenUtil {
public final static String USER_NAME = TokenConstant.USER_NAME;
public final static String NICK_NAME = TokenConstant.NICK_NAME;
public final static String USER_ID = TokenConstant.USER_ID;
public final static String DEPT_ID = TokenConstant.DEPT_ID;
public final static String ROLE_ID = TokenConstant.ROLE_ID;
public final static String ROLE_NAME = TokenConstant.ROLE_NAME;
public final static String TENANT_ID = TokenConstant.TENANT_ID;

3
blade-service-api/blade-desk-api/src/main/java/org/springblade/desk/vo/NoticeVO.java

@ -17,4 +17,7 @@ public class NoticeVO extends Notice {
@ApiModelProperty(value = "通知类型名")
private String categoryName;
@ApiModelProperty(value = "租户编号")
private String tenantId;
}

14
blade-service/blade-desk/src/main/java/org/springblade/desk/controller/NoticeController.java

@ -71,6 +71,20 @@ public class NoticeController extends BladeController implements CacheNames {
return R.data(NoticeWrapper.build().pageVO(pages));
}
/**
* 多表联合查询自定义分页
*/
@GetMapping("/page")
@ApiImplicitParams({
@ApiImplicitParam(name = "category", value = "公告类型", paramType = "query", dataType = "integer"),
@ApiImplicitParam(name = "title", value = "公告标题", paramType = "query", dataType = "string")
})
@ApiOperation(value = "分页", notes = "传入notice", position = 1)
public R<IPage<NoticeVO>> page(@ApiIgnore NoticeVO notice, Query query) {
IPage<NoticeVO> pages = noticeService.selectNoticePage(Condition.getPage(query), notice);
return R.data(pages);
}
/**
* 新增
*/

5
blade-service/blade-desk/src/main/java/org/springblade/desk/mapper/NoticeMapper.java

@ -18,7 +18,9 @@ package org.springblade.desk.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.datascope.annotation.DataAuth;
import org.springblade.desk.entity.Notice;
import org.springblade.desk.vo.NoticeVO;
import java.util.List;
@ -42,6 +44,7 @@ public interface NoticeMapper extends BaseMapper<Notice> {
* @param notice
* @return
*/
List<Notice> selectNoticePage(IPage page, Notice notice);
@DataAuth(resourceCode = "notice")
List<NoticeVO> selectNoticePage(IPage page, NoticeVO notice);
}

41
blade-service/blade-desk/src/main/java/org/springblade/desk/mapper/NoticeMapper.xml

@ -16,24 +16,39 @@
<result column="content" property="content"/>
</resultMap>
<!-- 通用查询结果列 -->
<sql id="baseColumnList">
select id,
create_user AS createUser,
create_time AS createTime,
update_user AS updateUser,
update_time AS updateTime,
status,
is_deleted AS isDeleted,
title, content
</sql>
<!-- 通用查询映射结果 -->
<resultMap id="noticeVOResultMap" type="org.springblade.desk.vo.NoticeVO">
<result column="id" property="id"/>
<result column="create_user" property="createUser"/>
<result column="create_time" property="createTime"/>
<result column="update_user" property="updateUser"/>
<result column="update_time" property="updateTime"/>
<result column="status" property="status"/>
<result column="is_deleted" property="isDeleted"/>
<result column="release_time" property="releaseTime"/>
<result column="title" property="title"/>
<result column="content" property="content"/>
</resultMap>
<select id="topList" resultMap="noticeResultMap">
select * from blade_notice limit #{number}
</select>
<select id="selectNoticePage" resultMap="noticeResultMap">
select * from blade_notice where title like concat('%', #{notice.title}, '%') and is_deleted = 0
<select id="selectNoticePage" resultMap="noticeVOResultMap">
SELECT
n.*,
d.dict_value AS categoryName
FROM
blade_notice n
LEFT JOIN ( SELECT * FROM blade_dict WHERE CODE = 'notice' ) d ON n.category = d.dict_key
WHERE
n.is_deleted = 0 and n.tenant_id = #{notice.tenantId}
<if test="notice.title!=null">
and n.title like concat('%', #{notice.title}, '%')
</if>
<if test="notice.category!=null">
and n.category = #{notice.category}
</if>
</select>
</mapper>

3
blade-service/blade-desk/src/main/java/org/springblade/desk/service/INoticeService.java

@ -19,6 +19,7 @@ package org.springblade.desk.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.mp.base.BaseService;
import org.springblade.desk.entity.Notice;
import org.springblade.desk.vo.NoticeVO;
/**
* 服务类
@ -33,6 +34,6 @@ public interface INoticeService extends BaseService<Notice> {
* @param notice
* @return
*/
IPage<Notice> selectNoticePage(IPage<Notice> page, Notice notice);
IPage<NoticeVO> selectNoticePage(IPage<NoticeVO> page, NoticeVO notice);
}

6
blade-service/blade-desk/src/main/java/org/springblade/desk/service/impl/NoticeServiceImpl.java

@ -18,9 +18,11 @@ package org.springblade.desk.service.impl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.secure.utils.SecureUtil;
import org.springblade.desk.entity.Notice;
import org.springblade.desk.mapper.NoticeMapper;
import org.springblade.desk.service.INoticeService;
import org.springblade.desk.vo.NoticeVO;
import org.springframework.stereotype.Service;
/**
@ -32,7 +34,9 @@ import org.springframework.stereotype.Service;
public class NoticeServiceImpl extends BaseServiceImpl<NoticeMapper, Notice> implements INoticeService {
@Override
public IPage<Notice> selectNoticePage(IPage<Notice> page, Notice notice) {
public IPage<NoticeVO> selectNoticePage(IPage<NoticeVO> page, NoticeVO notice) {
// 若不使用mybatis-plus自带的分页方法,则不会自动带入tenantId,所以我们需要自行注入
notice.setTenantId(SecureUtil.getTenantId());
return page.setRecords(baseMapper.selectNoticePage(page, notice));
}

76
doc/sql/bladex-datascope-update.sql

@ -0,0 +1,76 @@
/*
Navicat Premium Data Transfer
Source Server : mysql_localhost
Source Server Type : MySQL
Source Server Version : 50723
Source Host : localhost:3306
Source Schema : bladex_boot
Target Server Type : MySQL
Target Server Version : 50723
File Encoding : 65001
Date: 20/06/2019 00:31:21
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for blade_data_scope
-- ----------------------------
DROP TABLE IF EXISTS `blade_data_scope`;
CREATE TABLE `blade_data_scope` (
`id` bigint(64) NOT NULL COMMENT '主键',
`resource_code` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '资源编号',
`scope_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '数据权限名称',
`scope_column` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '数据权限字段',
`scope_type` int(2) NULL DEFAULT NULL COMMENT '数据权限类型',
`scope_value` varchar(2000) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '数据权限值域',
`remark` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '数据权限备注',
`create_user` bigint(64) NULL DEFAULT NULL COMMENT '创建人',
`create_time` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',
`update_user` bigint(64) NULL DEFAULT NULL COMMENT '修改人',
`update_time` datetime(0) NULL DEFAULT NULL COMMENT '修改时间',
`status` int(2) NULL DEFAULT NULL COMMENT '状态',
`is_deleted` int(2) NULL DEFAULT NULL COMMENT '是否已删除',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci;
-- ----------------------------
-- Table structure for blade_role_scope
-- ----------------------------
DROP TABLE IF EXISTS `blade_role_scope`;
CREATE TABLE `blade_role_scope` (
`id` bigint(64) NOT NULL COMMENT '主键',
`scope_id` bigint(64) NULL DEFAULT NULL COMMENT '数据权限id',
`role_id` bigint(64) NULL DEFAULT NULL COMMENT '角色id',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci;
SET FOREIGN_KEY_CHECKS = 1;
-- ----------------------------
-- 部门表增加字段
-- ----------------------------
ALTER TABLE `blade_dept`
ADD COLUMN `ancestors` varchar(2000) NULL COMMENT '祖级列表' AFTER `parent_id`,
ADD COLUMN `dept_category` int(2) NULL COMMENT '部门类型' AFTER `ancestors`;
UPDATE `blade_dept` SET `tenant_id` = '000000', `parent_id` = 0, `ancestors` = '0', `dept_category` = 1, `dept_name` = '刀锋科技', `full_name` = '江苏刀锋科技有限公司', `sort` = 1, `remark` = NULL, `is_deleted` = 0 WHERE `id` = 1123598813738675201;
UPDATE `blade_dept` SET `tenant_id` = '000000', `parent_id` = 1123598813738675201, `ancestors` = '0,1123598813738675201', `dept_category` = 1, `dept_name` = '常州刀锋', `full_name` = '常州刀锋科技有限公司', `sort` = 1, `remark` = NULL, `is_deleted` = 0 WHERE `id` = 1123598813738675202;
UPDATE `blade_dept` SET `tenant_id` = '000000', `parent_id` = 1123598813738675201, `ancestors` = '0,1123598813738675201', `dept_category` = 1, `dept_name` = '苏州刀锋', `full_name` = '苏州刀锋科技有限公司', `sort` = 1, `remark` = NULL, `is_deleted` = 0 WHERE `id` = 1123598813738675203;
-- ----------------------------
-- 增加字典项
-- ----------------------------
INSERT INTO `blade_dict`(`id`, `parent_id`, `code`, `dict_key`, `dict_value`, `sort`, `remark`, `is_deleted`) VALUES (1123598814738675227, 0, 'org_category', -1, '机构类型', 7, NULL, 0);
INSERT INTO `blade_dict`(`id`, `parent_id`, `code`, `dict_key`, `dict_value`, `sort`, `remark`, `is_deleted`) VALUES (1123598814738675228, 1123598814738675227, 'org_category', 1, '公司', 1, NULL, 0);
INSERT INTO `blade_dict`(`id`, `parent_id`, `code`, `dict_key`, `dict_value`, `sort`, `remark`, `is_deleted`) VALUES (1123598814738675229, 1123598814738675227, 'org_category', 2, '部门', 2, NULL, 0);
INSERT INTO `blade_dict`(`id`, `parent_id`, `code`, `dict_key`, `dict_value`, `sort`, `remark`, `is_deleted`) VALUES (1123598814738675230, 0, 'scope_type', -1, '数据权限类别', 8, NULL, 0);
INSERT INTO `blade_dict`(`id`, `parent_id`, `code`, `dict_key`, `dict_value`, `sort`, `remark`, `is_deleted`) VALUES (1123598814738675231, 1123598814738675230, 'scope_type', 1, '全部可见', 1, NULL, 0);
INSERT INTO `blade_dict`(`id`, `parent_id`, `code`, `dict_key`, `dict_value`, `sort`, `remark`, `is_deleted`) VALUES (1123598814738675232, 1123598814738675230, 'scope_type', 2, '本人可见', 2, NULL, 0);
INSERT INTO `blade_dict`(`id`, `parent_id`, `code`, `dict_key`, `dict_value`, `sort`, `remark`, `is_deleted`) VALUES (1123598814738675233, 1123598814738675230, 'scope_type', 3, '所在机构可见', 3, NULL, 0);
INSERT INTO `blade_dict`(`id`, `parent_id`, `code`, `dict_key`, `dict_value`, `sort`, `remark`, `is_deleted`) VALUES (1123598814738675234, 1123598814738675230, 'scope_type', 4, '所在机构及子级可见', 4, NULL, 0);
INSERT INTO `blade_dict`(`id`, `parent_id`, `code`, `dict_key`, `dict_value`, `sort`, `remark`, `is_deleted`) VALUES (1123598814738675235, 1123598814738675230, 'scope_type', 5, '自定义', 5, NULL, 0);

26
doc/sql/bladex-tenantid-update.sql

@ -1,26 +0,0 @@
ALTER TABLE `blade_dept`
CHANGE COLUMN `tenant_code` `tenant_id` varchar(12) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '000000' COMMENT '租户ID' AFTER `id`;
ALTER TABLE `blade_log_api`
CHANGE COLUMN `tenant_code` `tenant_id` varchar(12) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '000000' COMMENT '租户ID' AFTER `id`;
ALTER TABLE `blade_log_error`
CHANGE COLUMN `tenant_code` `tenant_id` varchar(12) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '000000' COMMENT '租户ID' AFTER `id`;
ALTER TABLE `blade_log_usual`
CHANGE COLUMN `tenant_code` `tenant_id` varchar(12) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '000000' COMMENT '租户ID' AFTER `id`;
ALTER TABLE `blade_notice`
CHANGE COLUMN `tenant_code` `tenant_id` varchar(12) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '000000' COMMENT '租户ID' AFTER `id`;
ALTER TABLE `blade_oss`
CHANGE COLUMN `tenant_code` `tenant_id` varchar(12) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '000000' COMMENT '租户ID' AFTER `id`;
ALTER TABLE `blade_role`
CHANGE COLUMN `tenant_code` `tenant_id` varchar(12) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '000000' COMMENT '租户ID' AFTER `id`;
ALTER TABLE `blade_tenant`
CHANGE COLUMN `tenant_code` `tenant_id` varchar(12) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '000000' COMMENT '租户ID' AFTER `id`;
ALTER TABLE `blade_user`
CHANGE COLUMN `tenant_code` `tenant_id` varchar(12) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '000000' COMMENT '租户ID' AFTER `id`;

4
pom.xml

@ -25,11 +25,11 @@
<protostuff.version>1.6.0</protostuff.version>
<flowable.version>6.4.1</flowable.version>
<mysql.connector.version>8.0.16</mysql.connector.version>
<spring.boot.admin.version>2.1.4</spring.boot.admin.version>
<spring.boot.admin.version>2.1.5</spring.boot.admin.version>
<alibaba.cloud.version>0.9.0.RELEASE</alibaba.cloud.version>
<spring.boot.version>2.1.5.RELEASE</spring.boot.version>
<spring.boot.version>2.1.6.RELEASE</spring.boot.version>
<spring.cloud.version>Greenwich.SR1</spring.cloud.version>
<spring.platform.version>Cairo-SR8</spring.platform.version>

Loading…
Cancel
Save