Browse Source

优化了一下描述说明

fengshuonan 4 years ago
parent
commit
c17e307ea2

+ 16 - 18
guns-base-support/guns-system/src/main/java/cn/stylefeng/guns/sys/modular/org/service/impl/SysOrgServiceImpl.java

@ -186,12 +186,12 @@ public class SysOrgServiceImpl extends ServiceImpl<SysOrgMapper, SysOrg> impleme
        //如果登录用户不是超级管理员
        if (!superAdmin) {
            //如果新增的机构父id不是0,则进行数据权限校验
            if(!pid.equals(0L)) {
            if (!pid.equals(0L)) {
                List<Long> dataScope = sysOrgParam.getDataScope();
                //数据范围为空
                if (ObjectUtil.isEmpty(dataScope)) {
                    throw new PermissionException(PermissionExceptionEnum.NO_PERMISSION_OPERATE);
                } else if(!dataScope.contains(pid)) {
                } else if (!dataScope.contains(pid)) {
                    //所添加的组织机构的父机构不在自己的数据范围内
                    throw new PermissionException(PermissionExceptionEnum.NO_PERMISSION_OPERATE);
                }
@ -225,7 +225,7 @@ public class SysOrgServiceImpl extends ServiceImpl<SysOrgMapper, SysOrg> impleme
            //数据范围为空
            if (ObjectUtil.isEmpty(dataScope)) {
                throw new PermissionException(PermissionExceptionEnum.NO_PERMISSION_OPERATE);
            } else if(!dataScope.contains(id)) {
            } else if (!dataScope.contains(id)) {
                //所操作的数据不在自己的数据范围内
                throw new PermissionException(PermissionExceptionEnum.NO_PERMISSION_OPERATE);
            }
@ -275,7 +275,7 @@ public class SysOrgServiceImpl extends ServiceImpl<SysOrgMapper, SysOrg> impleme
            //数据范围为空
            if (ObjectUtil.isEmpty(dataScope)) {
                throw new PermissionException(PermissionExceptionEnum.NO_PERMISSION_OPERATE);
            } else if(!dataScope.contains(id)) {
            } else if (!dataScope.contains(id)) {
                //所操作的数据不在自己的数据范围内
                throw new PermissionException(PermissionExceptionEnum.NO_PERMISSION_OPERATE);
            }
@ -363,21 +363,19 @@ public class SysOrgServiceImpl extends ServiceImpl<SysOrgMapper, SysOrg> impleme
            return CollectionUtil.newArrayList();
        }
        //本部门id集合,即自己
        List<Long> thisOrgIdList = CollectionUtil.newArrayList();
        thisOrgIdList.add(orgId);
        //本部门及子节点id集合,包含自己
        List<Long> thisOrgWithChildIdList = this.getChildIdListWithSelfById(orgId);
        //1全部数据 2本部门及以下数据 3本部门数据 4仅本人数据
        // 如果是范围类型是全部数据,则获取当前系统所有的组织架构id
        if (DataScopeTypeEnum.ALL.getCode().equals(dataScopeType)) {
            resultList = this.getOrgIdAll();
        } else if (DataScopeTypeEnum.DEPT_WITH_CHILD.getCode().equals(dataScopeType)) {
            resultList = thisOrgWithChildIdList;
        } else if (DataScopeTypeEnum.DEPT.getCode().equals(dataScopeType)) {
            resultList = thisOrgIdList;
        }
        // 如果范围类型是本部门及以下部门,则查询本节点和子节点集合,包含本节点
        else if (DataScopeTypeEnum.DEPT_WITH_CHILD.getCode().equals(dataScopeType)) {
            resultList = this.getChildIdListWithSelfById(orgId);
        }
        // 如果数据范围是本部门,不含子节点,则直接返回本部门
        else if (DataScopeTypeEnum.DEPT.getCode().equals(dataScopeType)) {
            resultList.add(orgId);
        }
        return resultList;
    }
@ -410,9 +408,9 @@ public class SysOrgServiceImpl extends ServiceImpl<SysOrgMapper, SysOrg> impleme
        String code = sysOrgParam.getCode();
        Long pid = sysOrgParam.getPid();
        //如果父id不是根节点
        if(!pid.equals(0L)) {
        if (!pid.equals(0L)) {
            SysOrg pOrg = this.getById(pid);
            if(ObjectUtil.isNull(pOrg)) {
            if (ObjectUtil.isNull(pOrg)) {
                //父机构不存在
                throw new ServiceException(SysOrgExceptionEnum.ORG_NOT_EXIST);
            }

+ 36 - 42
guns-base-support/guns-system/src/main/java/cn/stylefeng/guns/sys/modular/role/service/impl/SysRoleServiceImpl.java

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

+ 3 - 3
guns-base-support/guns-system/src/main/java/cn/stylefeng/guns/sys/modular/user/service/SysUserRoleService.java

@ -58,15 +58,15 @@ public interface SysUserRoleService extends IService<SysUserRole> {
    void grantRole(SysUserParam sysUserParam);
    /**
     * 获取用户的数据范围(组织机构id集合)
     * 获取用户所有角色的数据范围(组织机构id集合)
     *
     * @param userId 用户id
     * @param orgId 组织机构id
     * @param orgId  组织机构id
     * @return 数据范围id集合(组织机构id集合)
     * @author xuyuxiang
     * @date 2020/4/5 17:31
     */
    List<Long> getUserDataScopeIdList(Long userId, Long orgId);
    List<Long> getUserRoleDataScopeIdList(Long userId, Long orgId);
    /**
     * 根据角色id删除对应的用户-角色表关联信息

+ 6 - 2
guns-base-support/guns-system/src/main/java/cn/stylefeng/guns/sys/modular/user/service/impl/SysUserRoleServiceImpl.java

@ -94,12 +94,16 @@ public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUs
     * @date 2020/4/5 17:32
     */
    @Override
    public List<Long> getUserDataScopeIdList(Long userId, Long orgId) {
    public List<Long> getUserRoleDataScopeIdList(Long userId, Long orgId) {
        List<Long> roleIdList = CollectionUtil.newArrayList();
        // 获取用户所有角色
        LambdaQueryWrapper<SysUserRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserRole::getUserId, userId);
        this.list(queryWrapper).forEach(sysUserRole -> roleIdList.add(sysUserRole.getRoleId()));
        if(ObjectUtil.isNotEmpty(roleIdList)) {
        // 获取这些角色对应的数据范围
        if (ObjectUtil.isNotEmpty(roleIdList)) {
            return sysRoleService.getUserDataScopeIdList(roleIdList, orgId);
        }
        return CollectionUtil.newArrayList();

+ 4 - 1
guns-base-support/guns-system/src/main/java/cn/stylefeng/guns/sys/modular/user/service/impl/SysUserServiceImpl.java

@ -448,10 +448,13 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
    public List<Long> getUserDataScopeIdList(Long userId, Long orgId) {
        Set<Long> userDataScopeIdSet = CollectionUtil.newHashSet();
        if(ObjectUtil.isAllNotEmpty(userId, orgId)) {
            //获取该用户对应的数据范围集合
            List<Long> userDataScopeIdListForUser = sysUserDataScopeService.getUserDataScopeIdList(userId);
            //获取该用户的角色对应的数据范围集合
            List<Long> userDataScopeIdListForRole = sysUserRoleService.getUserDataScopeIdList(userId, orgId);
            List<Long> userDataScopeIdListForRole = sysUserRoleService.getUserRoleDataScopeIdList(userId, orgId);
            userDataScopeIdSet.addAll(userDataScopeIdListForUser);
            userDataScopeIdSet.addAll(userDataScopeIdListForRole);
        }