【SysRoleDataScope】优化

pull/3/head
chenjinlong 2021-02-04 16:10:31 +08:00
parent 5130d2503c
commit f5f5f177cb
7 changed files with 191 additions and 56 deletions

View File

@ -0,0 +1,22 @@
package cn.stylefeng.roses.kernel.system;
import java.util.Set;
/**
* service
*
* @author chenjinlong
* @date 2021/2/4 16:01
*/
public interface RoleDataScopeServiceApi {
/**
* id
*
* @param orgIds id
* @return
* @author chenjinlong
* @date 2021/2/4 15:56
*/
void delByOrgIds(Set<Long> orgIds);
}

View File

@ -16,15 +16,6 @@ import java.util.Set;
*/
public interface RoleServiceApi {
/**
*
*
* @param organizationIds id
* @author fengshuonan
* @date 2020/11/5 19:17
*/
void deleteRoleDataScopeListByOrgIdList(Set<Long> organizationIds);
/**
* id
*

View File

@ -0,0 +1,28 @@
package cn.stylefeng.roses.kernel.system.pojo;
import lombok.Data;
/**
*
*
* @author chenjinlong
* @date 2021/2/4 15:17
*/
@Data
public class SysRoleDataScopeRequest {
/**
*
*/
private Long roleDataScopeId;
/**
* id
*/
private Long roleId;
/**
* id
*/
private Long organizationId;
}

View File

@ -18,6 +18,7 @@ import cn.stylefeng.roses.kernel.rule.enums.YesOrNotEnum;
import cn.stylefeng.roses.kernel.rule.factory.DefaultTreeBuildFactory;
import cn.stylefeng.roses.kernel.rule.pojo.tree.DefaultTreeNode;
import cn.stylefeng.roses.kernel.rule.pojo.ztree.ZTreeNode;
import cn.stylefeng.roses.kernel.system.RoleDataScopeServiceApi;
import cn.stylefeng.roses.kernel.system.RoleServiceApi;
import cn.stylefeng.roses.kernel.system.UserOrgServiceApi;
import cn.stylefeng.roses.kernel.system.UserServiceApi;
@ -62,6 +63,9 @@ public class HrOrganizationServiceImpl extends ServiceImpl<HrOrganizationMapper,
@Resource
private UserServiceApi userServiceApi;
@Resource
private RoleDataScopeServiceApi roleDataScopeServiceApi;
@Override
public void add(HrOrganizationRequest hrOrganizationRequest) {
@ -129,7 +133,7 @@ public class HrOrganizationServiceImpl extends ServiceImpl<HrOrganizationMapper,
this.update(updateWrapper);
// 删除角色对应的组织架构数据范围
roleServiceApi.deleteRoleDataScopeListByOrgIdList(childIdList);
roleDataScopeServiceApi.delByOrgIds(childIdList);
// 删除用户对应的组织架构数据范围
userServiceApi.deleteUserDataScopeListByOrgIdList(childIdList);

View File

@ -1,11 +1,11 @@
package cn.stylefeng.roses.kernel.role.modular.service;
import cn.stylefeng.roses.kernel.role.modular.entity.SysRoleDataScope;
import com.baomidou.mybatisplus.extension.service.IService;
import cn.stylefeng.roses.kernel.system.pojo.SysRoleDataScopeRequest;
import cn.stylefeng.roses.kernel.system.pojo.role.request.SysRoleRequest;
import com.baomidou.mybatisplus.extension.service.IService;
import java.util.List;
import java.util.Set;
/**
* service
@ -34,22 +34,61 @@ public interface SysRoleDataScopeService extends IService<SysRoleDataScope> {
*/
List<Long> getRoleDataScopeIdList(List<Long> roleIdList);
/**
* id-
*
* @param orgIdList id
* @author majianguo
* @date 2020/11/5 11:21
*/
void deleteRoleDataScopeListByOrgIdList(Set<Long> orgIdList);
/**
* id-
*
*
* @param sysRoleDataScopeRequest
* @author chenjinlong
* @date 2021/1/26 12:52
*/
void add(SysRoleDataScopeRequest sysRoleDataScopeRequest);
/**
*
*
* @param sysRoleDataScopeRequest
* @author chenjinlong
* @date 2021/1/26 12:52
*/
void del(SysRoleDataScopeRequest sysRoleDataScopeRequest);
/**
* id
*
* @param roleId id
* @author majianguo
* @date 2020/11/5 11:21
* @author chenjinlong
* @date 2021/1/26 12:52
*/
void deleteRoleDataScopeListByRoleId(Long roleId);
void delByRoleId(Long roleId);
/**
*
*
* @param sysRoleDataScopeRequest
* @author chenjinlong
* @date 2021/1/26 12:52
*/
void edit(SysRoleDataScopeRequest sysRoleDataScopeRequest);
/**
* -
*
* @param sysRoleDataScopeRequest
* @author chenjinlong
* @date 2021/1/26 12:52
*/
SysRoleDataScope detail(SysRoleDataScopeRequest sysRoleDataScopeRequest);
/**
* -
*
* @param sysRoleDataScopeRequest
* @author chenjinlong
* @date 2021/1/26 12:52
*/
List<SysRoleDataScope> findList(SysRoleDataScopeRequest sysRoleDataScopeRequest);
}

View File

@ -24,19 +24,22 @@ Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意
*/
package cn.stylefeng.roses.kernel.role.modular.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.role.modular.entity.SysRoleDataScope;
import cn.stylefeng.roses.kernel.role.modular.mapper.SysRoleDataScopeMapper;
import cn.stylefeng.roses.kernel.role.modular.service.SysRoleDataScopeService;
import cn.stylefeng.roses.kernel.system.RoleDataScopeServiceApi;
import cn.stylefeng.roses.kernel.system.pojo.SysRoleDataScopeRequest;
import cn.stylefeng.roses.kernel.system.pojo.role.request.SysRoleRequest;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
/**
* service
@ -45,53 +48,106 @@ import java.util.Set;
* @date 2020/11/5 4:32
*/
@Service
public class SysRoleDataScopeServiceImpl extends ServiceImpl<SysRoleDataScopeMapper, SysRoleDataScope> implements SysRoleDataScopeService {
public class SysRoleDataScopeServiceImpl extends ServiceImpl<SysRoleDataScopeMapper, SysRoleDataScope> implements SysRoleDataScopeService, RoleDataScopeServiceApi {
@Override
public void grantDataScope(SysRoleRequest sysRoleParam) {
Long roleId = sysRoleParam.getRoleId();
LambdaQueryWrapper<SysRoleDataScope> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(SysRoleDataScope::getRoleId, roleId);
// 删除所拥该角色已绑定的范围
this.remove(queryWrapper);
this.delByRoleId(roleId);
// 授权该角色数据范围
// 批量新增-授权该角色数据范围
if (ObjectUtil.isNotEmpty(sysRoleParam.getGrantOrgIdList())) {
List<SysRoleDataScope> sysRoleDataScopeList = CollUtil.newArrayList();
sysRoleParam.getGrantOrgIdList().forEach(orgId -> {
SysRoleDataScope sysRoleDataScope = new SysRoleDataScope();
sysRoleDataScope.setRoleId(roleId);
sysRoleDataScope.setOrganizationId(orgId);
this.save(sysRoleDataScope);
sysRoleDataScopeList.add(sysRoleDataScope);
});
this.saveBatch(sysRoleDataScopeList);
}
}
@Override
public List<Long> getRoleDataScopeIdList(List<Long> roleIdList) {
List<Long> resultList = CollectionUtil.newArrayList();
if (ObjectUtil.isNotEmpty(roleIdList)) {
LambdaQueryWrapper<SysRoleDataScope> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.in(SysRoleDataScope::getRoleId, roleIdList);
this.list(queryWrapper).forEach(sysRoleDataScope -> resultList.add(sysRoleDataScope.getOrganizationId()));
}
return resultList;
LambdaQueryWrapper<SysRoleDataScope> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.in(SysRoleDataScope::getRoleId, roleIdList);
return this.list(queryWrapper).stream().map(SysRoleDataScope::getOrganizationId).collect(Collectors.toList());
}
@Override
public void add(SysRoleDataScopeRequest sysRoleDataScopeRequest) {
SysRoleDataScope sysRoleDataScope = new SysRoleDataScope();
BeanUtil.copyProperties(sysRoleDataScopeRequest, sysRoleDataScope);
this.save(sysRoleDataScope);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void deleteRoleDataScopeListByOrgIdList(Set<Long> orgIdList) {
public void del(SysRoleDataScopeRequest sysRoleDataScopeRequest) {
SysRoleDataScope sysRoleDataScope = this.querySysRoleDataScopeById(sysRoleDataScopeRequest);
this.removeById(sysRoleDataScope.getRoleDataScopeId());
}
@Override
public void delByRoleId(Long roleId) {
SysRoleDataScopeRequest sysRoleDataScopeRequest = new SysRoleDataScopeRequest();
sysRoleDataScopeRequest.setRoleId(roleId);
this.remove(this.createQueryWrapper(sysRoleDataScopeRequest));
}
@Override
public void delByOrgIds(Set<Long> orgIds) {
LambdaQueryWrapper<SysRoleDataScope> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.in(SysRoleDataScope::getOrganizationId, orgIdList);
queryWrapper.in(SysRoleDataScope::getOrganizationId, orgIds);
this.remove(queryWrapper);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void deleteRoleDataScopeListByRoleId(Long roleId) {
public void edit(SysRoleDataScopeRequest sysRoleDataScopeRequest) {
SysRoleDataScope sysRoleDataScope = this.querySysRoleDataScopeById(sysRoleDataScopeRequest);
BeanUtil.copyProperties(sysRoleDataScopeRequest, sysRoleDataScope);
this.updateById(sysRoleDataScope);
}
@Override
public SysRoleDataScope detail(SysRoleDataScopeRequest sysRoleDataScopeRequest) {
return this.getOne(this.createQueryWrapper(sysRoleDataScopeRequest), false);
}
@Override
public List<SysRoleDataScope> findList(SysRoleDataScopeRequest sysRoleDataScopeRequest) {
return this.list(this.createQueryWrapper(sysRoleDataScopeRequest));
}
/**
*
*
* @param sysRoleDataScopeRequest dto
* @author chenjinlong
* @date 2021/2/3 15:02
*/
private SysRoleDataScope querySysRoleDataScopeById(SysRoleDataScopeRequest sysRoleDataScopeRequest) {
return this.getById(sysRoleDataScopeRequest.getRoleDataScopeId());
}
/**
* QueryWrapper
*
* @param sysRoleDataScopeRequest dto
* @author chenjinlong
* @date 2021/2/3 14:54
*/
private LambdaQueryWrapper<SysRoleDataScope> createQueryWrapper(SysRoleDataScopeRequest sysRoleDataScopeRequest) {
LambdaQueryWrapper<SysRoleDataScope> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(SysRoleDataScope::getRoleId, roleId);
this.remove(queryWrapper);
// SQL拼接
queryWrapper.eq(ObjectUtil.isNotNull(sysRoleDataScopeRequest.getRoleDataScopeId()), SysRoleDataScope::getRoleDataScopeId, sysRoleDataScopeRequest.getRoleDataScopeId());
queryWrapper.eq(ObjectUtil.isNotNull(sysRoleDataScopeRequest.getRoleId()), SysRoleDataScope::getRoleId, sysRoleDataScopeRequest.getRoleId());
queryWrapper.eq(ObjectUtil.isNotNull(sysRoleDataScopeRequest.getOrganizationId()), SysRoleDataScope::getOrganizationId, sysRoleDataScopeRequest.getOrganizationId());
return queryWrapper;
}
}

View File

@ -124,16 +124,16 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
this.updateById(sysRole);
Long id = sysRole.getRoleId();
Long roleId = sysRole.getRoleId();
// 级联删除该角色对应的角色-数据范围关联信息
sysRoleDataScopeService.deleteRoleDataScopeListByRoleId(id);
sysRoleDataScopeService.delByRoleId(roleId);
// 级联删除该角色对应的用户-角色表关联信息
userServiceApi.deleteUserRoleListByRoleId(id);
userServiceApi.deleteUserRoleListByRoleId(roleId);
// 级联删除该角色对应的角色-菜单表关联信息
sysRoleResourceService.deleteRoleResourceListByRoleId(id);
sysRoleResourceService.deleteRoleResourceListByRoleId(roleId);
}
@Override
@ -319,11 +319,6 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
}
@Override
public void deleteRoleDataScopeListByOrgIdList(Set<Long> organizationIds) {
sysRoleDataScopeService.deleteRoleDataScopeListByOrgIdList(organizationIds);
}
@Override
public List<SysRoleResponse> getRolesByIds(List<Long> roleIds) {