feat(youlai-admin): 开启部门和角色数据权限

开启部门和角色数据权限
This commit is contained in:
chuan 2022-09-22 00:44:16 +08:00
parent e5e34ca7aa
commit ae12a34008
4 changed files with 107 additions and 20 deletions

View File

@ -13,7 +13,7 @@ import java.util.List;
@Mapper
public interface SysDeptMapper extends BaseMapper<SysDept> {
// @DataPermission
@DataPermission()
@Override
List<SysDept> selectList(@Param(Constants.WRAPPER) Wrapper<SysDept> queryWrapper);
}

View File

@ -1,8 +1,14 @@
package com.youlai.admin.mapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.youlai.admin.pojo.entity.SysDept;
import com.youlai.admin.pojo.entity.SysRole;
import com.youlai.admin.pojo.entity.SysUser;
import com.youlai.common.mybatis.annotation.DataPermission;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Mapper;
@ -12,4 +18,11 @@ import java.util.List;
public interface SysRoleMapper extends BaseMapper<SysRole> {
@DataPermission()
@Override
List<SysRole> selectList(@Param(Constants.WRAPPER) Wrapper<SysRole> queryWrapper);
@DataPermission()
<E extends IPage<SysRole>> E selectPage(E page, @Param("ew") Wrapper<SysRole> queryWrapper);
}

View File

@ -125,10 +125,12 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
.select(SysDept::getId, SysDept::getParentId, SysDept::getName)
.orderByAsc(SysDept::getSort)
);
List<Option> options = recurDeptTreeOptions(SystemConstants.ROOT_DEPT_ID, deptList);
// List<Option> options = recurDeptTreeOptions(rootId, deptList);
List<Option> options = buildDeptTree(deptList);
return options;
}
@Override
public Long saveDept(DeptForm formData) {
SysDept entity = deptConverter.form2Entity(formData);
@ -180,6 +182,93 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
return list;
}
/**
* 递归生成部门表格层级列表
*
* @param depts
* @return
*/
public List<Option> buildDeptTree(List<SysDept> depts)
{
if (CollectionUtil.isEmpty(depts)) {
return Collections.EMPTY_LIST;
}
List<Option> returnList = new ArrayList<Option>();
List<Long> tempList = new ArrayList<Long>();
for (SysDept dept : depts)
{
tempList.add(dept.getId());
}
for (SysDept dept : depts)
{
// 如果是顶级节点, 遍历该父节点的所有子节点
if (!tempList.contains(dept.getParentId()))
{
Option option = new Option(dept.getId(), dept.getName());
recursionFn(depts, option);
returnList.add(option);
}
}
if (returnList.isEmpty())
{
depts.stream().forEach(dept -> {
Option option = new Option(dept.getId(), dept.getName());
returnList.add(option);
});
}
return returnList;
}
/**
* 递归列表
*/
private void recursionFn(List<SysDept> list, Option t)
{
// 得到子节点列表
List<Option> childList = getChildList(list, t);
t.setChildren(childList);
for (Option tChild : childList)
{
if (hasChild(list, tChild))
{
recursionFn(list, tChild);
}
}
}
/**
* 得到子节点列表
*/
private List<Option> getChildList(List<SysDept> list, Option t)
{
List<Option> tlist = new ArrayList<Option>();
Iterator<SysDept> it = list.iterator();
while (it.hasNext())
{
SysDept n = (SysDept) it.next();
if (n.getParentId() != null && n.getParentId() == t.getValue())
{
Option option = new Option(n.getId(), n.getName());
tlist.add(option);
}
}
return tlist;
}
/**
* 判断是否有子节点
*/
private boolean hasChild(List<SysDept> list, Option t)
{
return getChildList(list, t).size() > 0;
}
/**
* 删除部门

View File

@ -90,14 +90,12 @@ public class MyDataPermissionHandler implements DataPermissionHandler {
public static Expression dataScopeFilter(String deptAlias,String userAlias, Expression where) {
// 获取当前的用户数据权限
List<Integer> dataScopes = UserUtils.getDataScopes();
List<String> roles = UserUtils.getRoles();
Long deptId = JwtUtils.getJwtPayload().getLong("deptId");
Long userId = JwtUtils.getJwtPayload().getLong("userId");
String deptIdColumn =StrUtil.isEmptyIfStr(deptAlias)?"id":deptAlias+".id";
Expression newWhere = null;
for (int i=0;i<dataScopes.size();i++) {
Integer dataScope = dataScopes.get(i);
String role = roles.get(i);
if(dataScope == DATA_SCOPE_ALL){
break;
}else if(dataScope == DATA_SCOPE_DEPT){
@ -116,6 +114,9 @@ public class MyDataPermissionHandler implements DataPermissionHandler {
}
}
}
if(newWhere == null){
return where;
}
Expression expression = ObjectUtil.isEmpty(where)? newWhere : new AndExpression(where,newWhere);
return expression ;
}
@ -137,21 +138,5 @@ public class MyDataPermissionHandler implements DataPermissionHandler {
}
/**
* 当前用户的部门id
*
* @return
*/
private static Expression getDeptId() {
LongValue deptId = new LongValue(JwtUtils.getJwtPayload().getLong("deptId"));
return deptId;
}
private static Expression getUserId() {
LongValue userId = new LongValue(JwtUtils.getJwtPayload().getLong("userId"));
return userId;
}
}