增加管理员拥有的菜单列表
This commit is contained in:
@@ -1,38 +0,0 @@
|
||||
package cn.iocoder.mall.systemservice.dal.mysql.mapper.permission;
|
||||
|
||||
import cn.iocoder.mall.systemservice.dal.mysql.dataobject.permission.AdminRoleDO;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface AccountRoleMapper extends BaseMapper<AdminRoleDO> {
|
||||
|
||||
default List<AdminRoleDO> selectByAccountId(Integer accountId) {
|
||||
return selectList(new QueryWrapper<AdminRoleDO>().eq("account_id", accountId));
|
||||
}
|
||||
|
||||
default List<AdminRoleDO> selectListByAccountIds(Collection<Integer> accountIds) {
|
||||
return selectList(new QueryWrapper<AdminRoleDO>().in("account_id", accountIds));
|
||||
}
|
||||
|
||||
default int deleteByAccountId(Integer accountId) {
|
||||
return delete(new QueryWrapper<AdminRoleDO>().eq("account_id", accountId));
|
||||
}
|
||||
|
||||
default int deleteByRoleId(Integer roleId) {
|
||||
return delete(new QueryWrapper<AdminRoleDO>().eq("role_id", roleId));
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量插入。因为 MyBaits Plus 的批量插入是基于 Service 实现,所以只好写 XML
|
||||
*
|
||||
* @param accountRoleDOs 数组
|
||||
*/
|
||||
int insertList(@Param("accountRoleDOs") List<AdminRoleDO> accountRoleDOs);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package cn.iocoder.mall.systemservice.dal.mysql.mapper.permission;
|
||||
|
||||
import cn.iocoder.mall.systemservice.dal.mysql.dataobject.permission.AdminRoleDO;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface AdminRoleMapper extends BaseMapper<AdminRoleDO> {
|
||||
|
||||
default List<AdminRoleDO> selectByAdminId(Integer adminId) {
|
||||
return selectList(new QueryWrapper<AdminRoleDO>().eq("admin_id", adminId));
|
||||
}
|
||||
|
||||
// default List<AdminRoleDO> selectListByAccountIds(Collection<Integer> accountIds) {
|
||||
// return selectList(new QueryWrapper<AdminRoleDO>().in("account_id", accountIds));
|
||||
// }
|
||||
//
|
||||
// default int deleteByAccountId(Integer accountId) {
|
||||
// return delete(new QueryWrapper<AdminRoleDO>().eq("account_id", accountId));
|
||||
// }
|
||||
//
|
||||
// default int deleteByRoleId(Integer roleId) {
|
||||
// return delete(new QueryWrapper<AdminRoleDO>().eq("role_id", roleId));
|
||||
// }
|
||||
|
||||
/**
|
||||
* 批量插入。因为 MyBaits Plus 的批量插入是基于 Service 实现,所以只好写 XML
|
||||
*
|
||||
* @param accountRoleDOs 数组
|
||||
*/
|
||||
int insertList(@Param("accountRoleDOs") List<AdminRoleDO> accountRoleDOs);
|
||||
|
||||
}
|
||||
@@ -16,6 +16,16 @@ public interface ResourceMapper extends BaseMapper<ResourceDO> {
|
||||
return selectOne(new QueryWrapper<ResourceDO>().eq("permission", permission));
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询指定类型的资源列表
|
||||
*
|
||||
* @param type 资源类型,允许空
|
||||
* @return 资源列表
|
||||
*/
|
||||
default List<ResourceDO> selectListByType(Integer type) {
|
||||
return selectList(new QueryWrapperX<ResourceDO>().eqIfPresent("type", type));
|
||||
}
|
||||
|
||||
default ResourceDO selectByPidAndName(Integer pid, String name) {
|
||||
return selectOne(new QueryWrapperX<ResourceDO>().eqIfPresent("pid", pid)
|
||||
.eqIfPresent("name", name));
|
||||
|
||||
@@ -5,10 +5,12 @@ import cn.iocoder.mall.systemservice.rpc.permission.dto.ResourceCreateDTO;
|
||||
import cn.iocoder.mall.systemservice.rpc.permission.dto.ResourceUpdateDTO;
|
||||
import cn.iocoder.mall.systemservice.rpc.permission.vo.ResourceVO;
|
||||
import cn.iocoder.mall.systemservice.service.permission.ResourceService;
|
||||
import cn.iocoder.mall.systemservice.service.permission.RoleService;
|
||||
import cn.iocoder.mall.systemservice.service.permission.bo.ResourceBO;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -19,6 +21,8 @@ public class ResourceManager {
|
||||
|
||||
@Autowired
|
||||
private ResourceService resourceService;
|
||||
@Autowired
|
||||
private RoleService roleService;
|
||||
|
||||
/**
|
||||
* 创建资源
|
||||
@@ -71,4 +75,22 @@ public class ResourceManager {
|
||||
return ResourceConvert.INSTANCE.convertList02(resourceBOs);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得指定角色的资源列表
|
||||
*
|
||||
* @param roleIds 角色编号列表
|
||||
* @param type 资源类型,允许空
|
||||
* @return 资源列表
|
||||
*/
|
||||
public List<ResourceVO> listRoleResource(Collection<Integer> roleIds, Integer type) {
|
||||
List<ResourceBO> resourceBOs;
|
||||
// 判断是否为超管。若是超管,默认有所有权限
|
||||
if (roleService.hasSuperAdmin(roleIds)) {
|
||||
resourceBOs = resourceService.listResourceByType(type);
|
||||
} else {
|
||||
resourceBOs = resourceService.listRoleResourceByType(roleIds, type);
|
||||
}
|
||||
return ResourceConvert.INSTANCE.convertList02(resourceBOs);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 角色 Manager
|
||||
@@ -84,4 +85,14 @@ public class RoleManager {
|
||||
return RoleConvert.INSTANCE.convertPage(pageResultBO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得管理员拥有的角色编号列表
|
||||
*
|
||||
* @param adminId 管理员编号
|
||||
* @return 角色编号列表
|
||||
*/
|
||||
public Set<Integer> listAdminRoleIds(Integer adminId) {
|
||||
return roleService.listAdminRoleIds(adminId);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import cn.iocoder.mall.systemservice.rpc.permission.vo.ResourceVO;
|
||||
import org.apache.dubbo.config.annotation.Service;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import static cn.iocoder.common.framework.vo.CommonResult.success;
|
||||
@@ -48,4 +49,9 @@ public class ResourceRpcImpl implements ResourceRpc {
|
||||
return success(resourceManager.listResource(resourceIds));
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<List<ResourceVO>> listRoleResource(Collection<Integer> roleIds, Integer type) {
|
||||
return success(resourceManager.listRoleResource(roleIds, type));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import org.apache.dubbo.config.annotation.Service;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static cn.iocoder.common.framework.vo.CommonResult.success;
|
||||
|
||||
@@ -55,4 +56,9 @@ public class RoleRpcImpl implements RoleRpc {
|
||||
return success(roleManager.pageRole(pageDTO));
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<Set<Integer>> listAdminRoleIds(Integer adminId) {
|
||||
return success(roleManager.listAdminRoleIds(adminId));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
package cn.iocoder.mall.systemservice.service.permission;
|
||||
|
||||
import cn.iocoder.common.framework.util.CollectionUtils;
|
||||
import cn.iocoder.common.framework.util.ServiceExceptionUtil;
|
||||
import cn.iocoder.mall.systemservice.convert.permission.ResourceConvert;
|
||||
import cn.iocoder.mall.systemservice.dal.mysql.dataobject.permission.ResourceDO;
|
||||
import cn.iocoder.mall.systemservice.dal.mysql.dataobject.permission.RoleResourceDO;
|
||||
import cn.iocoder.mall.systemservice.dal.mysql.mapper.permission.ResourceMapper;
|
||||
import cn.iocoder.mall.systemservice.dal.mysql.mapper.permission.RoleResourceMapper;
|
||||
import cn.iocoder.mall.systemservice.enums.SystemErrorCodeEnum;
|
||||
import cn.iocoder.mall.systemservice.enums.permission.ResourceIdEnum;
|
||||
import cn.iocoder.mall.systemservice.enums.permission.ResourceTypeEnum;
|
||||
@@ -15,6 +18,8 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static cn.iocoder.mall.systemservice.enums.SystemErrorCodeEnum.*;
|
||||
@@ -28,6 +33,8 @@ public class ResourceService {
|
||||
|
||||
@Autowired
|
||||
private ResourceMapper resourceMapper;
|
||||
@Autowired
|
||||
private RoleResourceMapper roleResourceMapper;
|
||||
|
||||
/**
|
||||
* 创建资源
|
||||
@@ -112,6 +119,34 @@ public class ResourceService {
|
||||
return ResourceConvert.INSTANCE.convertList(resourceDOs);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得指定类型的资源列表
|
||||
*
|
||||
* @param type 资源类型,允许空
|
||||
* @return 资源列表
|
||||
*/
|
||||
public List<ResourceBO> listResourceByType(Integer type) {
|
||||
List<ResourceDO> resourceDOs = resourceMapper.selectListByType(type);
|
||||
return ResourceConvert.INSTANCE.convertList(resourceDOs);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得角色拥有的资源列表
|
||||
*
|
||||
* @param roleIds 角色编号
|
||||
* @param type 资源类型,允许空
|
||||
* @return 资源列表
|
||||
*/
|
||||
public List<ResourceBO> listRoleResourceByType(Collection<Integer> roleIds, Integer type) {
|
||||
List<RoleResourceDO> roleResourceDOs = roleResourceMapper.selectListByRoleIds(roleIds);
|
||||
if (CollectionUtils.isEmpty(roleResourceDOs)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
List<ResourceDO> resourceDOs = resourceMapper.selectListByIdsAndType(
|
||||
CollectionUtils.convertSet(roleResourceDOs, RoleResourceDO::getResourceId), type);
|
||||
return ResourceConvert.INSTANCE.convertList(resourceDOs);
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验父资源是否合法
|
||||
*
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
package cn.iocoder.mall.systemservice.service.permission;
|
||||
|
||||
import cn.iocoder.common.framework.util.CollectionUtils;
|
||||
import cn.iocoder.common.framework.util.ServiceExceptionUtil;
|
||||
import cn.iocoder.common.framework.vo.PageResult;
|
||||
import cn.iocoder.mall.systemservice.convert.permission.RoleConvert;
|
||||
import cn.iocoder.mall.systemservice.dal.mysql.dataobject.permission.AdminRoleDO;
|
||||
import cn.iocoder.mall.systemservice.dal.mysql.dataobject.permission.RoleDO;
|
||||
import cn.iocoder.mall.systemservice.dal.mysql.mapper.permission.AdminRoleMapper;
|
||||
import cn.iocoder.mall.systemservice.dal.mysql.mapper.permission.RoleMapper;
|
||||
import cn.iocoder.mall.systemservice.enums.permission.RoleCodeEnum;
|
||||
import cn.iocoder.mall.systemservice.enums.permission.RoleTypeEnum;
|
||||
import cn.iocoder.mall.systemservice.service.permission.bo.RoleBO;
|
||||
import cn.iocoder.mall.systemservice.service.permission.bo.RoleCreateBO;
|
||||
@@ -17,7 +21,9 @@ import org.springframework.util.StringUtils;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static cn.iocoder.mall.systemservice.enums.SystemErrorCodeEnum.*;
|
||||
|
||||
@@ -30,6 +36,8 @@ public class RoleService {
|
||||
|
||||
@Autowired
|
||||
private RoleMapper roleMapper;
|
||||
@Autowired
|
||||
private AdminRoleMapper adminRoleMapper;
|
||||
|
||||
/**
|
||||
* 创建角色
|
||||
@@ -151,4 +159,31 @@ public class RoleService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得管理员拥有的角色编号列表
|
||||
*
|
||||
* @param adminId 管理员编号
|
||||
* @return 角色编号列表
|
||||
*/
|
||||
public Set<Integer> listAdminRoleIds(Integer adminId) {
|
||||
List<AdminRoleDO> adminRoleDOs = adminRoleMapper.selectByAdminId(adminId);
|
||||
return CollectionUtils.convertSet(adminRoleDOs, AdminRoleDO::getRoleId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断角色是否有超级管理员
|
||||
*
|
||||
* @param roleIds 角色编号列表
|
||||
* @return 是否有超级管理员
|
||||
*/
|
||||
public boolean hasSuperAdmin(Collection<Integer> roleIds) {
|
||||
List<RoleDO> roleDOs = roleMapper.selectBatchIds(roleIds);
|
||||
for (RoleDO roleDO : roleDOs) {
|
||||
if (RoleCodeEnum.SUPER_ADMIN.getCode().equals(roleDO.getCode())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user