|
@ -28,7 +28,6 @@ import cn.hutool.core.bean.BeanUtil;
|
|
|
import cn.hutool.core.collection.CollectionUtil;
|
|
|
import cn.hutool.core.lang.Dict;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
import cn.stylefeng.guns.core.annotion.DataScope;
|
|
|
import cn.stylefeng.guns.core.consts.CommonConstant;
|
|
|
import cn.stylefeng.guns.core.consts.SymbolConstant;
|
|
|
import cn.stylefeng.guns.core.context.login.LoginContextHolder;
|
|
@ -39,7 +38,6 @@ import cn.stylefeng.guns.sys.core.enums.DataScopeTypeEnum;
|
|
|
import cn.stylefeng.guns.core.exception.ServiceException;
|
|
|
import cn.stylefeng.guns.core.factory.PageFactory;
|
|
|
import cn.stylefeng.guns.core.pojo.page.PageResult;
|
|
|
import cn.stylefeng.guns.sys.modular.emp.result.SysEmpInfo;
|
|
|
import cn.stylefeng.guns.sys.modular.org.service.SysOrgService;
|
|
|
import cn.stylefeng.guns.sys.modular.role.entity.SysRole;
|
|
|
import cn.stylefeng.guns.sys.modular.role.enums.SysRoleExceptionEnum;
|
|
@ -55,10 +53,6 @@ import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import javax.validation.constraints.Max;
|
|
|
import javax.validation.constraints.Min;
|
|
|
import javax.validation.constraints.NotNull;
|
|
|
import javax.validation.constraints.Null;
|
|
|
import java.util.List;
|
|
|
import java.util.Set;
|
|
|
|
|
@ -94,7 +88,7 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
|
|
|
List<Dict> dictList = CollectionUtil.newArrayList();
|
|
|
//获取用户角色id集合
|
|
|
List<Long> roleIdList = sysUserRoleService.getUserRoleIdList(userId);
|
|
|
if(ObjectUtil.isNotEmpty(roleIdList)) {
|
|
|
if (ObjectUtil.isNotEmpty(roleIdList)) {
|
|
|
LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
queryWrapper.in(SysRole::getId, roleIdList).eq(SysRole::getStatus, CommonStatusEnum.ENABLE.getCode());
|
|
|
//根据角色id集合查询并返回结果
|
|
@ -118,22 +112,22 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
|
|
|
@Override
|
|
|
public PageResult<SysRole> page(SysRoleParam sysRoleParam) {
|
|
|
LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
if(ObjectUtil.isNotNull(sysRoleParam)) {
|
|
|
if (ObjectUtil.isNotNull(sysRoleParam)) {
|
|
|
//根据名称模糊查询
|
|
|
if(ObjectUtil.isNotEmpty(sysRoleParam.getName())) {
|
|
|
if (ObjectUtil.isNotEmpty(sysRoleParam.getName())) {
|
|
|
queryWrapper.like(SysRole::getName, sysRoleParam.getName());
|
|
|
}
|
|
|
//根据编码模糊查询
|
|
|
if(ObjectUtil.isNotEmpty(sysRoleParam.getCode())) {
|
|
|
if (ObjectUtil.isNotEmpty(sysRoleParam.getCode())) {
|
|
|
queryWrapper.like(SysRole::getCode, sysRoleParam.getCode());
|
|
|
}
|
|
|
}
|
|
|
//查询角色列表时,如果当前登录用户不是超级管理员,则查询自己拥有的
|
|
|
if(!LoginContextHolder.me().isSuperAdmin()) {
|
|
|
if (!LoginContextHolder.me().isSuperAdmin()) {
|
|
|
|
|
|
//查询自己拥有的
|
|
|
List<String> loginUserRoleIds = LoginContextHolder.me().getLoginUserRoleIds();
|
|
|
if(ObjectUtil.isEmpty(loginUserRoleIds)) {
|
|
|
if (ObjectUtil.isEmpty(loginUserRoleIds)) {
|
|
|
return new PageResult<>();
|
|
|
}
|
|
|
queryWrapper.in(SysRole::getId, loginUserRoleIds);
|
|
@ -153,10 +147,10 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
|
|
|
public List<Dict> list(SysRoleParam sysRoleParam) {
|
|
|
List<Dict> dictList = CollectionUtil.newArrayList();
|
|
|
LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
if(ObjectUtil.isNotNull(sysRoleParam)) {
|
|
|
if (ObjectUtil.isNotNull(sysRoleParam)) {
|
|
|
//根据角色名称或编码模糊查询
|
|
|
if(ObjectUtil.isNotEmpty(sysRoleParam.getName())) {
|
|
|
queryWrapper.and(i ->i.like(SysRole::getName, sysRoleParam.getName())
|
|
|
if (ObjectUtil.isNotEmpty(sysRoleParam.getName())) {
|
|
|
queryWrapper.and(i -> i.like(SysRole::getName, sysRoleParam.getName())
|
|
|
.or().like(SysRole::getCode, sysRoleParam.getName()));
|
|
|
}
|
|
|
}
|
|
@ -183,11 +177,11 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
|
|
|
List<Dict> dictList = CollectionUtil.newArrayList();
|
|
|
LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
//如果当前登录用户不是超级管理员,则查询自己拥有的
|
|
|
if(!LoginContextHolder.me().isSuperAdmin()) {
|
|
|
if (!LoginContextHolder.me().isSuperAdmin()) {
|
|
|
|
|
|
//查询自己拥有的
|
|
|
List<String> loginUserRoleIds = LoginContextHolder.me().getLoginUserRoleIds();
|
|
|
if(ObjectUtil.isEmpty(loginUserRoleIds)) {
|
|
|
if (ObjectUtil.isEmpty(loginUserRoleIds)) {
|
|
|
return dictList;
|
|
|
}
|
|
|
queryWrapper.in(SysRole::getId, loginUserRoleIds);
|
|
@ -195,13 +189,13 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
|
|
|
//只查询正常状态
|
|
|
queryWrapper.eq(SysRole::getStatus, CommonStatusEnum.ENABLE.getCode());
|
|
|
this.list(queryWrapper)
|
|
|
.forEach(sysRole -> {
|
|
|
Dict dict = Dict.create();
|
|
|
dict.put(CommonConstant.ID, sysRole.getId());
|
|
|
dict.put(CommonConstant.CODE, sysRole.getCode());
|
|
|
dict.put(CommonConstant.NAME, sysRole.getName());
|
|
|
dictList.add(dict);
|
|
|
});
|
|
|
.forEach(sysRole -> {
|
|
|
Dict dict = Dict.create();
|
|
|
dict.put(CommonConstant.ID, sysRole.getId());
|
|
|
dict.put(CommonConstant.CODE, sysRole.getCode());
|
|
|
dict.put(CommonConstant.NAME, sysRole.getName());
|
|
|
dictList.add(dict);
|
|
|
});
|
|
|
return dictList;
|
|
|
}
|
|
|
|
|
@ -300,19 +294,19 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
|
|
|
if (!superAdmin) {
|
|
|
Integer dataScopeType = sysRoleParam.getDataScopeType();
|
|
|
//如果授权的角色的数据范围类型为全部,则没权限,只有超级管理员有
|
|
|
if(DataScopeTypeEnum.ALL.getCode().equals(dataScopeType)) {
|
|
|
if (DataScopeTypeEnum.ALL.getCode().equals(dataScopeType)) {
|
|
|
throw new PermissionException(PermissionExceptionEnum.NO_PERMISSION_OPERATE);
|
|
|
}
|
|
|
//如果授权的角色数据范围类型为自定义,则要判断授权的数据范围是否在自己的数据范围内
|
|
|
if(DataScopeTypeEnum.DEFINE.getCode().equals(dataScopeType)){
|
|
|
if (DataScopeTypeEnum.DEFINE.getCode().equals(dataScopeType)) {
|
|
|
List<Long> dataScope = sysRoleParam.getDataScope();
|
|
|
//要授权的数据范围列表
|
|
|
List<Long> grantOrgIdList = sysRoleParam.getGrantOrgIdList();
|
|
|
if(ObjectUtil.isNotEmpty(grantOrgIdList)) {
|
|
|
if (ObjectUtil.isNotEmpty(grantOrgIdList)) {
|
|
|
//数据范围为空
|
|
|
if (ObjectUtil.isEmpty(dataScope)) {
|
|
|
throw new PermissionException(PermissionExceptionEnum.NO_PERMISSION_OPERATE);
|
|
|
} else if(!dataScope.containsAll(grantOrgIdList)) {
|
|
|
} else if (!dataScope.containsAll(grantOrgIdList)) {
|
|
|
//所要授权的数据不在自己的数据范围内
|
|
|
throw new PermissionException(PermissionExceptionEnum.NO_PERMISSION_OPERATE);
|
|
|
}
|
|
@ -335,29 +329,29 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
|
|
|
public List<Long> getUserDataScopeIdList(List<Long> roleIdList, Long orgId) {
|
|
|
Set<Long> resultList = CollectionUtil.newHashSet();
|
|
|
|
|
|
//自定义数据范围的角色id集合
|
|
|
Integer minDataScopeType = DataScopeTypeEnum.SELF.getCode();
|
|
|
//定义角色中最大数据范围的类型,目前系统按最大范围策略来,如果你同时拥有ALL和SELF的权限,系统最后按ALL返回
|
|
|
Integer strongerDataScopeType = DataScopeTypeEnum.SELF.getCode();
|
|
|
|
|
|
//固定数据范围的角色id集合
|
|
|
//获取用户自定义数据范围的角色集合
|
|
|
List<Long> customDataScopeRoleIdList = CollectionUtil.newArrayList();
|
|
|
if(ObjectUtil.isNotEmpty(roleIdList)) {
|
|
|
if (ObjectUtil.isNotEmpty(roleIdList)) {
|
|
|
List<SysRole> sysRoleList = this.listByIds(roleIdList);
|
|
|
for (SysRole sysRole: sysRoleList) {
|
|
|
if(DataScopeTypeEnum.DEFINE.getCode().equals(sysRole.getDataScopeType())) {
|
|
|
for (SysRole sysRole : sysRoleList) {
|
|
|
if (DataScopeTypeEnum.DEFINE.getCode().equals(sysRole.getDataScopeType())) {
|
|
|
customDataScopeRoleIdList.add(sysRole.getId());
|
|
|
} else {
|
|
|
if(sysRole.getDataScopeType() <= minDataScopeType) {
|
|
|
minDataScopeType = sysRole.getDataScopeType();
|
|
|
if (sysRole.getDataScopeType() <= strongerDataScopeType) {
|
|
|
strongerDataScopeType = sysRole.getDataScopeType();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//自定义数据返回id集合
|
|
|
//自定义数据范围的角色对应的数据范围
|
|
|
List<Long> roleDataScopeIdList = sysRoleDataScopeService.getRoleDataScopeIdList(customDataScopeRoleIdList);
|
|
|
|
|
|
//固定数据范围id集合
|
|
|
List<Long> dataScopeIdList = sysOrgService.getDataScopeListByDataScopeType(minDataScopeType, orgId);
|
|
|
//角色中拥有最大数据范围类型的数据范围
|
|
|
List<Long> dataScopeIdList = sysOrgService.getDataScopeListByDataScopeType(strongerDataScopeType, orgId);
|
|
|
|
|
|
resultList.addAll(dataScopeIdList);
|
|
|
resultList.addAll(roleDataScopeIdList);
|
|
@ -373,7 +367,7 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
|
|
|
@Override
|
|
|
public String getNameByRoleId(Long roleId) {
|
|
|
SysRole sysRole = this.getById(roleId);
|
|
|
if(ObjectUtil.isEmpty(sysRole)) {
|
|
|
if (ObjectUtil.isEmpty(sysRole)) {
|
|
|
throw new ServiceException(SysRoleExceptionEnum.ROLE_NOT_EXIST);
|
|
|
}
|
|
|
return sysRole.getName();
|
|
@ -423,7 +417,7 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
|
|
|
.ne(SysRole::getStatus, CommonStatusEnum.DELETED.getCode());
|
|
|
|
|
|
//是否排除自己,如果排除自己则不查询自己的id
|
|
|
if(isExcludeSelf) {
|
|
|
if (isExcludeSelf) {
|
|
|
queryWrapperByName.ne(SysRole::getId, id);
|
|
|
queryWrapperByCode.ne(SysRole::getId, id);
|
|
|
}
|
|
@ -446,7 +440,7 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
|
|
|
*/
|
|
|
private SysRole querySysRole(SysRoleParam sysRoleParam) {
|
|
|
SysRole sysRole = this.getById(sysRoleParam.getId());
|
|
|
if(ObjectUtil.isNull(sysRole)) {
|
|
|
if (ObjectUtil.isNull(sysRole)) {
|
|
|
throw new ServiceException(SysRoleExceptionEnum.ROLE_NOT_EXIST);
|
|
|
}
|
|
|
return sysRole;
|