【role】完善角色绑定数据范围的方法

pull/3/head
fengshuonan 2021-01-09 19:25:12 +08:00
parent 5c7d5159d5
commit 996bbb0442
11 changed files with 113 additions and 19 deletions

View File

@ -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", "指定部门类型的数据范围必须选择组织机构");
/**
*

View File

@ -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 {
}
}

View File

@ -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 {
}
}

View File

@ -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));
}
/**
* zTreelayui
*
* @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);
}
}

View File

@ -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;
}
}

View File

@ -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);
}
/**
* ztreelayui
*
* @param hrOrganizationRequest
* @return ztree
* @author fengshuonan
* @date 2021/1/9 18:40
*/
List<ZTreeNode> orgZTree(HrOrganizationRequest hrOrganizationRequest);
}

View File

@ -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,
}
}
}
}

View File

@ -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();
}

View File

@ -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);
/**
*

View File

@ -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

View File

@ -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);
}