Commit b8493227 by zzrdark

1.增加数据库数据

2.增加数据权限拦截
3.增加分页插件
4.增加系统模块查询
parent ec75ecf6
......@@ -108,7 +108,7 @@ CREATE TABLE `sys_func` (
`parent_id` bigint(20) NOT NULL,
`name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`url` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`perms` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`perms` varchar(1000) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`type` int(11) DEFAULT NULL,
`order_num` int(11) DEFAULT NULL,
PRIMARY KEY (`func_id`) USING BTREE
......
......@@ -17,7 +17,8 @@
<mybatis.spring.boot.version>1.3.0</mybatis.spring.boot.version>
<pagehelper.spring.boot.version>1.2.5</pagehelper.spring.boot.version>
<mysql.version>8.0.16</mysql.version>
<mybatisplus.version>3.1.2</mybatisplus.version>
<!-- <mybatisplus.version>3.1.2</mybatisplus.version>-->
<mybatisplus.version>3.0.7.1</mybatisplus.version>
<druid.version>1.1.13</druid.version>
</properties>
......
......@@ -4,6 +4,8 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.mx.cneeds.server.entity.SysDeptEntity;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/**
* 部门表
*
......@@ -14,4 +16,10 @@ import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface SysDeptDao extends BaseMapper<SysDeptEntity> {
/**
* 查询子部门ID列表
* @param parentId 上级部门ID
*/
List<Long> queryDetpIdList(Long parentId);
}
......@@ -4,6 +4,8 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.mx.cneeds.server.entity.SysRoleDeptEntity;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/**
* 部门与角色映射表
*
......@@ -14,4 +16,9 @@ import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface SysRoleDeptDao extends BaseMapper<SysRoleDeptEntity> {
/**
* 根据角色ID,获取部门ID列表
*/
List<Long> queryDeptIdList(Long[] roleIds);
}
......@@ -4,6 +4,8 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.mx.cneeds.server.entity.SysUserRoleEntity;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/**
* 角色用户映射表
*
......@@ -14,4 +16,9 @@ import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface SysUserRoleDao extends BaseMapper<SysUserRoleEntity> {
/**
* 根据用户ID,获取角色ID列表
*/
List<Long> queryRoleIdList(Long userId);
}
......@@ -14,4 +14,8 @@
</resultMap>
<select id="queryDetpIdList" resultType="long">
select dept_id from sys_dept where parent_id = #{value} and del_flag = 0
</select>
</mapper>
\ No newline at end of file
......@@ -11,4 +11,11 @@
</resultMap>
<select id="queryDeptIdList" resultType="long">
select dept_id from sys_role_dept where role_id in
<foreach item="roleId" collection="array" open="(" separator="," close=")">
#{roleId}
</foreach>
</select>
</mapper>
\ No newline at end of file
......@@ -10,5 +10,8 @@
<result property="roleId" column="role_id"/>
</resultMap>
<select id="queryRoleIdList" resultType="long">
select role_id from sys_user_role where user_id = #{value}
</select>
</mapper>
\ No newline at end of file
package com.mx.cneeds.common.dto;
import lombok.Data;
import java.io.Serializable;
/**
* @ClassName FuncDto
* @Author zzrdark
* @Date 2020-03-09 16:17
* @Description TODO
**/
@Data
public class FuncDto implements Serializable {
private Long funcId;
private Long parentId;
private String name;
private String url;
private String perms;
private Integer type;
private Integer orderNum;
}
package com.mx.cneeds.common.dto;
import lombok.Data;
import java.io.Serializable;
import java.util.List;
/**
* @ClassName PageResult
* @Author zzrdark
* @Date 2020-03-09 16:01
* @Description TODO
**/
@Data
public class PageDto implements Serializable {
/**
* 总记录数
*/
private int totalCount;
/**
* 每页记录数
*/
private int pageSize;
/**
* 总页数
*/
private int totalPage;
/**
* 当前页数
*/
private int currPage;
/**
* 列表数据
*/
private List<?> list;
/**
* 分页
* @param list 列表数据
* @param totalCount 总记录数
* @param pageSize 每页记录数
* @param currPage 当前页数
*/
public PageDto(List<?> list, int totalCount, int pageSize, int currPage) {
this.list = list;
this.totalCount = totalCount;
this.pageSize = pageSize;
this.currPage = currPage;
this.totalPage = (int) Math.ceil((double)totalCount/pageSize);
}
}
......@@ -13,7 +13,8 @@
<artifactId>cneeds-common-utils</artifactId>
<properties>
<mybatisplus.version>3.1.2</mybatisplus.version>
<!-- <mybatisplus.version>3.1.2</mybatisplus.version>-->
<mybatisplus.version>3.0.7.1</mybatisplus.version>
<commons.lang.version>2.6</commons.lang.version>
<commons.io.version>2.5</commons.io.version>
</properties>
......
/**
* Copyright (c) 2016-2019 人人开源 All rights reserved.
*
* https://www.renren.io
*
* 版权所有,侵权必究!
*/
package com.mx.cneeds.common.annotation;
import java.lang.annotation.*;
/**
* 数据过滤
*
* @author Mark sunlightcs@gmail.com
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DataFilter {
/** 表的别名 */
String tableAlias() default "";
/** true:没有本部门数据权限,也能查询本人数据 */
boolean user() default true;
/** true:拥有子部门数据权限 */
boolean subDept() default false;
/** 部门ID */
String deptId() default "dept_id";
/** 用户ID */
String userId() default "user_id";
}
......@@ -9,7 +9,6 @@
package com.mx.cneeds.common.pager;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mx.cneeds.common.xss.SQLFilter;
import org.apache.commons.lang.StringUtils;
......@@ -53,9 +52,9 @@ public class Query<T> {
//前端字段排序
if(StringUtils.isNotEmpty(orderField) && StringUtils.isNotEmpty(order)){
if(Constant.ASC.equalsIgnoreCase(order)) {
return page.addOrder(OrderItem.asc(orderField));
return page.setAsc(orderField);
}else {
return page.addOrder(OrderItem.desc(orderField));
return page.setDesc(orderField);
}
}
......@@ -66,9 +65,9 @@ public class Query<T> {
//默认排序
if(isAsc) {
page.addOrder(OrderItem.asc(defaultOrderField));
page.setAsc(defaultOrderField);
}else {
page.addOrder(OrderItem.desc(defaultOrderField));
page.setDesc(defaultOrderField);
}
return page;
......
package com.mx.cneeds.server.datashow.client;
import com.mx.cneeds.common.dto.JwtToken;
import com.mx.cneeds.common.dto.PageDto;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
/**
* @ClassName UserClient
* @Author zzrdark
* @Date 2020-03-09 15:21
* @Description TODO
**/
@FeignClient(name = "CNEEDS-SERVER-USER")
public interface UserClient {
@PostMapping("/sys/func/list")
PageDto funcList();
}
package com.mx.cneeds.server.datashow.web.system;
import com.mx.cneeds.common.dto.PageDto;
import com.mx.cneeds.common.result.R;
import com.mx.cneeds.server.datashow.client.UserClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @ClassName SystemController
* @Author zzrdark
* @Date 2020-03-09 15:19
* @Description TODO
**/
@RestController
@Slf4j
@RequestMapping("/sys")
public class SystemController {
@Autowired
private UserClient userClient;
@PostMapping("/module/list")
public R funcList(){
PageDto pageDto = userClient.funcList();
return new R().put("data",pageDto);
}
}
package com.mx.cneeds.server.datashow.web.user;
package com.mx.cneeds.server.datashow.web.system;
import com.mx.cneeds.common.result.R;
import com.mx.cneeds.common.vo.UserInfoVo;
import com.mx.cneeds.server.datashow.client.AuthorizationClient;
import com.mx.cneeds.common.dto.JwtToken;
import com.mx.cneeds.server.datashow.client.UserClient;
import com.netflix.client.http.HttpResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -29,6 +30,9 @@ public class UserController {
private AuthorizationClient authenticatorClient;
@Autowired
private UserClient userClient;
/**
* 登陆接口
* @param username
......@@ -44,14 +48,16 @@ public class UserController {
}
@RequestMapping("/info")
public R getInfo(HttpServletResponse response){
public R getInfo(){
UserInfoVo userInfoVo = new UserInfoVo();
log.info("getInfo");
userInfoVo.setName("admin");
userInfoVo.setIntroduction("manager");
userInfoVo.setRoles("admin");
userInfoVo.setRoles("book:editor,admin");
userInfoVo.setAvatar("avatar");
return new R().put("data",userInfoVo);
}
}
/**
* Copyright (c) 2016-2019 人人开源 All rights reserved.
*
* https://www.renren.io
*
* 版权所有,侵权必究!
*/
package com.mx.cneeds.server.user.common;
import com.mx.cneeds.common.annotation.DataFilter;
import com.mx.cneeds.common.exception.RRException;
import com.mx.cneeds.common.pager.Constant;
import com.mx.cneeds.server.user.service.SysDeptService;
import com.mx.cneeds.server.user.service.SysRoleDeptService;
import com.mx.cneeds.server.user.service.SysUserRoleService;
import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.JoinPoint;
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.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* 数据过滤,切面处理类
*
* @author Mark sunlightcs@gmail.com
*/
@Aspect
@Component
public class DataFilterAspect {
@Autowired
private SysDeptService sysDeptService;
@Autowired
private SysUserRoleService sysUserRoleService;
@Autowired
private SysRoleDeptService sysRoleDeptService;
@Pointcut("@annotation(com.mx.cneeds.common.annotation.DataFilter)")
public void dataFilterCut() {
}
@Before("dataFilterCut()")
public void dataFilter(JoinPoint point) throws Throwable {
Object params = point.getArgs()[0];
if(params != null && params instanceof Map){
// SysUserEntity user = ShiroUtils.getUserEntity();
Long userId = (Long) ((Map) params).get("userId");
//如果不是超级管理员,则进行数据过滤
if(userId != Constant.SUPER_ADMIN){
Map map = (Map)params;
map.put(Constant.SQL_FILTER, getSQLFilter(userId, point));
}
return ;
}
throw new RRException("数据权限接口,只能是Map类型参数,且不能为NULL");
}
/**
* 获取数据过滤的SQL
*/
private String getSQLFilter(Long userId, JoinPoint point){
MethodSignature signature = (MethodSignature) point.getSignature();
DataFilter dataFilter = signature.getMethod().getAnnotation(DataFilter.class);
//获取表的别名
String tableAlias = dataFilter.tableAlias();
if(StringUtils.isNotBlank(tableAlias)){
tableAlias += ".";
}
//部门ID列表
Set<Long> deptIdList = new HashSet<>();
//用户角色对应的部门ID列表
List<Long> roleIdList = sysUserRoleService.queryRoleIdList(userId);
if(roleIdList.size() > 0){
List<Long> userDeptIdList = sysRoleDeptService.queryDeptIdList(roleIdList.toArray(new Long[roleIdList.size()]));
deptIdList.addAll(userDeptIdList);
}
//用户子部门ID列表
if(dataFilter.subDept()){
List<Long> subDeptIdList = sysDeptService.getSubDeptIdList(userId);
deptIdList.addAll(subDeptIdList);
}
StringBuilder sqlFilter = new StringBuilder();
sqlFilter.append(" (");
if(deptIdList.size() > 0){
sqlFilter.append(tableAlias).append(dataFilter.deptId()).append(" in(").append(StringUtils.join(deptIdList, ",")).append(")");
}
//没有本部门数据权限,也能查询本人数据
if(dataFilter.user()){
if(deptIdList.size() > 0){
sqlFilter.append(" or ");
}
sqlFilter.append(tableAlias).append(dataFilter.userId()).append("=").append(userId);
}
sqlFilter.append(")");
if(sqlFilter.toString().trim().equals("()")){
return null;
}
return sqlFilter.toString();
}
}
package com.mx.cneeds.server.user.config;
import com.baomidou.mybatisplus.core.injector.ISqlInjector;
import com.baomidou.mybatisplus.extension.injector.LogicSqlInjector;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MybatisPlusConfig {
/**
* 分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
@Bean
public ISqlInjector sqlInjector() {
return new LogicSqlInjector();
}
}
\ No newline at end of file
......@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.extension.service.IService;
import com.mx.cneeds.common.pager.PageUtils;
import com.mx.cneeds.server.entity.SysDeptEntity;
import java.util.List;
import java.util.Map;
/**
......@@ -16,5 +17,12 @@ import java.util.Map;
public interface SysDeptService extends IService<SysDeptEntity> {
PageUtils queryPage(Map<String, Object> params);
/**
* 获取子部门ID,用于数据过滤
*/
List<Long> getSubDeptIdList(Long deptId);
List<Long> queryDetpIdList(Long parentId);
}
......@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.extension.service.IService;
import com.mx.cneeds.common.pager.PageUtils;
import com.mx.cneeds.server.entity.SysRoleDeptEntity;
import java.util.List;
import java.util.Map;
/**
......@@ -16,5 +17,10 @@ import java.util.Map;
public interface SysRoleDeptService extends IService<SysRoleDeptEntity> {
PageUtils queryPage(Map<String, Object> params);
/**
* 根据角色ID,获取部门ID列表
*/
List<Long> queryDeptIdList(Long[] roleIds) ;
}
......@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.extension.service.IService;
import com.mx.cneeds.common.pager.PageUtils;
import com.mx.cneeds.server.entity.SysUserRoleEntity;
import java.util.List;
import java.util.Map;
/**
......@@ -16,5 +17,11 @@ import java.util.Map;
public interface SysUserRoleService extends IService<SysUserRoleEntity> {
PageUtils queryPage(Map<String, Object> params);
/**
* 根据用户ID,获取角色ID列表
* @param userId
*/
List<Long> queryRoleIdList(Long userId);
}
......@@ -6,6 +6,9 @@ import com.mx.cneeds.server.dao.SysDeptDao;
import com.mx.cneeds.server.entity.SysDeptEntity;
import com.mx.cneeds.server.user.service.SysDeptService;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
......@@ -26,4 +29,35 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptDao, SysDeptEntity> i
return new PageUtils(page);
}
@Override
public List<Long> getSubDeptIdList(Long deptId){
//部门及子部门ID列表
List<Long> deptIdList = new ArrayList<>();
//获取子部门ID
List<Long> subIdList = queryDetpIdList(deptId);
getDeptTreeList(subIdList, deptIdList);
return deptIdList;
}
@Override
public List<Long> queryDetpIdList(Long parentId) {
return baseMapper.queryDetpIdList(parentId);
}
/**
* 递归
*/
private void getDeptTreeList(List<Long> subIdList, List<Long> deptIdList){
for(Long deptId : subIdList){
List<Long> list = queryDetpIdList(deptId);
if(list.size() > 0){
getDeptTreeList(list, deptIdList);
}
deptIdList.add(deptId);
}
}
}
......@@ -6,6 +6,8 @@ import com.mx.cneeds.server.dao.SysRoleDeptDao;
import com.mx.cneeds.server.entity.SysRoleDeptEntity;
import com.mx.cneeds.server.user.service.SysRoleDeptService;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
......@@ -25,4 +27,9 @@ public class SysRoleDeptServiceImpl extends ServiceImpl<SysRoleDeptDao, SysRoleD
return new PageUtils(page);
}
@Override
public List<Long> queryDeptIdList(Long[] roleIds) {
return baseMapper.queryDeptIdList(roleIds);
}
}
......@@ -6,6 +6,8 @@ import com.mx.cneeds.server.dao.SysUserRoleDao;
import com.mx.cneeds.server.entity.SysUserRoleEntity;
import com.mx.cneeds.server.user.service.SysUserRoleService;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
......@@ -25,4 +27,10 @@ public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleDao, SysUserR
return new PageUtils(page);
}
@Override
public List<Long> queryRoleIdList(Long userId) {
return baseMapper.queryRoleIdList(userId);
}
}
......@@ -32,10 +32,9 @@ public class SysFuncController {
*/
@RequestMapping("/list")
// @RequiresPermissions("sys:sysfunc:list")
public R list(@RequestParam Map<String, Object> params){
public PageUtils list(@RequestParam Map<String, Object> params){
PageUtils page = sysFuncService.queryPage(params);
return R.ok().put("page", page);
return page;
}
......
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