mirror of https://gitee.com/stylefeng/roses
【role】完善角色绑定数据范围的方法
parent
5c7d5159d5
commit
996bbb0442
|
@ -56,7 +56,12 @@ public enum SysRoleExceptionEnum implements AbstractExceptionEnum {
|
|||
/**
|
||||
* 超级管理员不能被删除
|
||||
*/
|
||||
SUPER_ADMIN_CANT_DELETE(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + SystemConstants.SYSTEM_EXCEPTION_STEP_CODE + "44", "超级管理员不能被删除");
|
||||
SUPER_ADMIN_CANT_DELETE(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + SystemConstants.SYSTEM_EXCEPTION_STEP_CODE + "44", "超级管理员不能被删除"),
|
||||
|
||||
/**
|
||||
* 必须选择公司范围集合
|
||||
*/
|
||||
PLEASE_FILL_DATA_SCOPE(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + SystemConstants.SYSTEM_EXCEPTION_STEP_CODE + "46", "指定部门类型的数据范围必须选择组织机构");
|
||||
|
||||
/**
|
||||
* 错误编码
|
||||
|
|
|
@ -74,4 +74,16 @@ public class HrOrganizationRequest extends BaseRequest {
|
|||
*/
|
||||
private String orgRemark;
|
||||
|
||||
/**
|
||||
* 角色id
|
||||
*/
|
||||
@NotNull(message = "角色id不能为空", groups = orgZTree.class)
|
||||
private Long roleId;
|
||||
|
||||
/**
|
||||
* 组织机构树zTree形式
|
||||
*/
|
||||
public @interface orgZTree {
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -48,7 +48,7 @@ public class SysRoleRequest extends BaseRequest {
|
|||
/**
|
||||
* 主键
|
||||
*/
|
||||
@NotNull(message = "roleId不能为空", groups = {edit.class, delete.class, detail.class, updateStatus.class, grantResource.class, grantData.class})
|
||||
@NotNull(message = "roleId不能为空", groups = {edit.class, delete.class, detail.class, updateStatus.class, grantResource.class, grantDataScope.class})
|
||||
private Long roleId;
|
||||
|
||||
/**
|
||||
|
@ -80,7 +80,7 @@ public class SysRoleRequest extends BaseRequest {
|
|||
* 数据范围类型:10-仅本人数据,20-本部门数据,30-本部门及以下数据,40-指定部门数据,50-全部数据
|
||||
*/
|
||||
@Null(message = "数据范围类型应该为空, 请移除dataScopeType参数", groups = {add.class, edit.class})
|
||||
@NotNull(message = "数据范围类型不能为空,请检查dataScopeType参数", groups = {grantData.class})
|
||||
@NotNull(message = "数据范围类型不能为空,请检查dataScopeType参数", groups = {grantDataScope.class})
|
||||
private Integer dataScopeType;
|
||||
|
||||
/**
|
||||
|
@ -102,7 +102,6 @@ public class SysRoleRequest extends BaseRequest {
|
|||
/**
|
||||
* 授权数据
|
||||
*/
|
||||
@NotNull(message = "授权数据不能为空,请检查grantOrgIdList参数", groups = {grantData.class})
|
||||
private List<Long> grantOrgIdList;
|
||||
|
||||
/**
|
||||
|
@ -114,7 +113,7 @@ public class SysRoleRequest extends BaseRequest {
|
|||
/**
|
||||
* 参数校验分组:授权数据
|
||||
*/
|
||||
public @interface grantData {
|
||||
public @interface grantDataScope {
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -7,11 +7,13 @@ import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
|
|||
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
|
||||
import cn.stylefeng.roses.kernel.system.modular.organization.service.HrOrganizationService;
|
||||
import cn.stylefeng.roses.kernel.system.pojo.organization.HrOrganizationRequest;
|
||||
import cn.stylefeng.roses.kernel.system.pojo.ztree.ZTreeNode;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 系统组织机构控制器
|
||||
|
@ -129,4 +131,15 @@ public class HrOrganizationController {
|
|||
return new SuccessResponseData(hrOrganizationService.treeLayui(hrOrganizationRequest));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取zTree形式的组织机构树(用于角色配置数据范围类型,并且数据范围类型是指定组织机构时)(layui版本)
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2021/1/9 18:37
|
||||
*/
|
||||
@GetResource(name = "获取zTree形式的组织机构树(用于角色配置数据范围类型,并且数据范围类型是指定组织机构时)(layui版本)", path = "/hrOrganization/zTree")
|
||||
public List<ZTreeNode> layuiSelectParentMenuTreeList(@Validated(HrOrganizationRequest.orgZTree.class) HrOrganizationRequest hrOrganizationRequest) {
|
||||
return hrOrganizationService.orgZTree(hrOrganizationRequest);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -2,6 +2,10 @@ package cn.stylefeng.roses.kernel.system.modular.organization.factory;
|
|||
|
||||
import cn.stylefeng.roses.kernel.system.modular.organization.entity.HrOrganization;
|
||||
import cn.stylefeng.roses.kernel.system.pojo.organization.layui.LayuiOrganizationTreeNode;
|
||||
import cn.stylefeng.roses.kernel.system.pojo.ztree.ZTreeNode;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 组织机构实体转化
|
||||
|
@ -27,4 +31,23 @@ public class OrganizationFactory {
|
|||
return treeNode;
|
||||
}
|
||||
|
||||
/**
|
||||
* 实体转zTree形式
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2021/1/9 18:43
|
||||
*/
|
||||
public static List<ZTreeNode> parseZTree(List<HrOrganization> organizationList) {
|
||||
ArrayList<ZTreeNode> zTreeNodes = new ArrayList<>();
|
||||
for (HrOrganization hrOrganization : organizationList) {
|
||||
ZTreeNode zTreeNode = new ZTreeNode();
|
||||
zTreeNode.setId(hrOrganization.getOrgId());
|
||||
zTreeNode.setpId(hrOrganization.getOrgParentId());
|
||||
zTreeNode.setName(hrOrganization.getOrgName());
|
||||
zTreeNode.setOpen(true);
|
||||
zTreeNodes.add(zTreeNode);
|
||||
}
|
||||
return zTreeNodes;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import cn.stylefeng.roses.kernel.rule.pojo.tree.DefaultTreeNode;
|
|||
import cn.stylefeng.roses.kernel.system.modular.organization.entity.HrOrganization;
|
||||
import cn.stylefeng.roses.kernel.system.pojo.organization.HrOrganizationRequest;
|
||||
import cn.stylefeng.roses.kernel.system.pojo.organization.layui.LayuiOrganizationTreeNode;
|
||||
import cn.stylefeng.roses.kernel.system.pojo.ztree.ZTreeNode;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
||||
import java.util.List;
|
||||
|
@ -114,4 +115,14 @@ public interface HrOrganizationService extends IService<HrOrganization> {
|
|||
*/
|
||||
Set<Long> findAllLevelParentIdsByOrganizations(Set<Long> organizationIds);
|
||||
|
||||
}
|
||||
/**
|
||||
* 获取ztree形式的组织机构树(用于角色配置数据范围类型,并且数据范围类型是指定组织机构时)(layui版本)
|
||||
*
|
||||
* @param hrOrganizationRequest 请求参数
|
||||
* @return ztree形式的组织机构树
|
||||
* @author fengshuonan
|
||||
* @date 2021/1/9 18:40
|
||||
*/
|
||||
List<ZTreeNode> orgZTree(HrOrganizationRequest hrOrganizationRequest);
|
||||
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package cn.stylefeng.roses.kernel.system.modular.organization.service.impl;
|
|||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.collection.ListUtil;
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
@ -27,6 +28,7 @@ import cn.stylefeng.roses.kernel.system.modular.organization.mapper.HrOrganizati
|
|||
import cn.stylefeng.roses.kernel.system.modular.organization.service.HrOrganizationService;
|
||||
import cn.stylefeng.roses.kernel.system.pojo.organization.HrOrganizationRequest;
|
||||
import cn.stylefeng.roses.kernel.system.pojo.organization.layui.LayuiOrganizationTreeNode;
|
||||
import cn.stylefeng.roses.kernel.system.pojo.ztree.ZTreeNode;
|
||||
import cn.stylefeng.roses.kernel.system.util.DataScopeUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||
|
@ -305,6 +307,30 @@ public class HrOrganizationServiceImpl extends ServiceImpl<HrOrganizationMapper,
|
|||
return allLevelParentIds;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ZTreeNode> orgZTree(HrOrganizationRequest hrOrganizationRequest) {
|
||||
|
||||
// 获取角色id
|
||||
Long roleId = hrOrganizationRequest.getRoleId();
|
||||
|
||||
// 获取所有组织机构列表
|
||||
LambdaQueryWrapper<HrOrganization> wrapper = createWrapper(hrOrganizationRequest);
|
||||
List<HrOrganization> list = this.list(wrapper);
|
||||
List<ZTreeNode> zTreeNodes = OrganizationFactory.parseZTree(list);
|
||||
|
||||
// 获取角色目前绑定的组织机构范围
|
||||
List<Long> roleDataScopes = roleServiceApi.getRoleDataScopes(ListUtil.toList(roleId));
|
||||
|
||||
// 设置绑定的组织机构范围为已选则状态
|
||||
for (ZTreeNode zTreeNode : zTreeNodes) {
|
||||
if (roleDataScopes.contains(zTreeNode.getId())) {
|
||||
zTreeNode.setChecked(true);
|
||||
}
|
||||
}
|
||||
|
||||
return zTreeNodes;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建组织架构的通用条件查询wrapper
|
||||
*
|
||||
|
@ -381,4 +407,4 @@ public class HrOrganizationServiceImpl extends ServiceImpl<HrOrganizationMapper,
|
|||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -84,14 +84,14 @@ public class SysRoleController {
|
|||
}
|
||||
|
||||
/**
|
||||
* 授权数据
|
||||
* 设置角色绑定的数据范围类型和数据范围
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2020/3/28 16:05
|
||||
*/
|
||||
@PostResource(name = "授权数据", path = "/sysRole/grantData")
|
||||
public ResponseData grantData(@RequestBody @Validated(SysRoleRequest.grantData.class) SysRoleRequest sysRoleParam) {
|
||||
sysRoleService.grantData(sysRoleParam);
|
||||
@PostResource(name = "设置角色绑定的数据范围类型和数据范围", path = "/sysRole/grantDataScope")
|
||||
public ResponseData grantData(@RequestBody @Validated(SysRoleRequest.grantDataScope.class) SysRoleRequest sysRoleParam) {
|
||||
sysRoleService.grantDataScope(sysRoleParam);
|
||||
return new SuccessResponseData();
|
||||
}
|
||||
|
||||
|
|
|
@ -75,7 +75,7 @@ public interface SysRoleService extends IService<SysRole> {
|
|||
* @author majianguo
|
||||
* @date 2020/11/5 上午11:14
|
||||
*/
|
||||
void grantData(SysRoleRequest sysRoleRequest);
|
||||
void grantDataScope(SysRoleRequest sysRoleRequest);
|
||||
|
||||
/**
|
||||
* 查看系统角色
|
||||
|
|
|
@ -58,12 +58,14 @@ public class SysRoleDataScopeServiceImpl extends ServiceImpl<SysRoleDataScopeMap
|
|||
this.remove(queryWrapper);
|
||||
|
||||
// 授权该角色数据范围
|
||||
sysRoleParam.getGrantOrgIdList().forEach(orgId -> {
|
||||
SysRoleDataScope sysRoleDataScope = new SysRoleDataScope();
|
||||
sysRoleDataScope.setRoleId(roleId);
|
||||
sysRoleDataScope.setOrganizationId(orgId);
|
||||
this.save(sysRoleDataScope);
|
||||
});
|
||||
if (ObjectUtil.isNotEmpty(sysRoleParam.getGrantOrgIdList())) {
|
||||
sysRoleParam.getGrantOrgIdList().forEach(orgId -> {
|
||||
SysRoleDataScope sysRoleDataScope = new SysRoleDataScope();
|
||||
sysRoleDataScope.setRoleId(roleId);
|
||||
sysRoleDataScope.setOrganizationId(orgId);
|
||||
this.save(sysRoleDataScope);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -140,7 +140,7 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
|
|||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void grantData(SysRoleRequest sysRoleRequest) {
|
||||
public void grantDataScope(SysRoleRequest sysRoleRequest) {
|
||||
SysRole sysRole = this.querySysRole(sysRoleRequest);
|
||||
|
||||
// 获取当前用户是否是超级管理员
|
||||
|
@ -160,6 +160,9 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
|
|||
|
||||
// 数据范围类型为自定义,则判断当前用户有没有该公司的权限
|
||||
if (DataScopeTypeEnum.DEFINE.getCode().equals(dataScopeType)) {
|
||||
if (ObjectUtil.isEmpty(sysRoleRequest.getGrantOrgIdList())) {
|
||||
throw new SystemModularException(SysRoleExceptionEnum.PLEASE_FILL_DATA_SCOPE);
|
||||
}
|
||||
for (Long orgId : sysRoleRequest.getGrantOrgIdList()) {
|
||||
DataScopeUtil.quickValidateDataScope(orgId);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue