完成角色权限分配功能的迁移

This commit is contained in:
YunaiV
2020-07-13 19:24:42 +08:00
parent 99c591d6f9
commit 61e5cfe617
54 changed files with 314 additions and 691 deletions

View File

@@ -1,15 +0,0 @@
package cn.iocoder.mall.system.biz.bo.authorization;
import lombok.Data;
import lombok.experimental.Accessors;
/**
* 授权模块 - 授权信息 BO
*/
@Data
@Accessors(chain = true)
public class AuthorizationBO {
}

View File

@@ -1,24 +0,0 @@
package cn.iocoder.mall.system.biz.bo.authorization;
import lombok.Data;
import lombok.experimental.Accessors;
import java.util.List;
/**
* 授权模块 - 资源信息树节点 BO
*/
@Data
@Accessors(chain = true)
public class ResourceTreeNodeBO {
/**
* 当前节点
*/
private ResourceBO node;
/**
* 子节点们
*/
private List<ResourceTreeNodeBO> children;
}

View File

@@ -1,36 +0,0 @@
package cn.iocoder.mall.system.biz.bo.authorization;
import lombok.Data;
import lombok.experimental.Accessors;
import java.util.Date;
/**
* 授权模块 - 角色信息 BO
*/
@Data
@Accessors(chain = true)
public class RoleBO {
/**
* 角色编号
*/
private Integer id;
/**
* 角色名字
*/
private String name;
/**
* 角色编码
*/
private String code;
/**
* 角色类型
*/
private Integer type;
/**
* 添加时间
*/
private Date createTime;
}

View File

@@ -1,22 +0,0 @@
package cn.iocoder.mall.system.biz.convert.authorization;
import cn.iocoder.mall.system.biz.bo.authorization.ResourceBO;
import cn.iocoder.mall.system.biz.bo.authorization.ResourceTreeNodeBO;
import cn.iocoder.mall.system.biz.dataobject.authorization.ResourceDO;
import cn.iocoder.mall.system.biz.dto.authorization.ResourceAddDTO;
import cn.iocoder.mall.system.biz.dto.authorization.ResourceUpdateDTO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
import java.util.List;
@Mapper
public interface ResourceConvert {
ResourceConvert INSTANCE = Mappers.getMapper(ResourceConvert.class);
@Mapping(source = "bean", target = "node")
ResourceTreeNodeBO convertTreeNode(ResourceDO bean);
}

View File

@@ -1,27 +0,0 @@
package cn.iocoder.mall.system.biz.dto.authorization;
import lombok.Data;
import lombok.experimental.Accessors;
import javax.validation.constraints.NotNull;
import java.util.Set;
/**
* 授权模块 - 分配角色资源 DTO
*/
@Data
@Accessors(chain = true)
public class AuthorizationAssignRoleResourceDTO {
@NotNull(message = "管理员编号不能为空")
private Integer adminId;
@NotNull(message = "角色编号不能为空")
private Integer roleId;
/**
* 资源编号数组
*/
private Set<Integer> resourceIds;
}

View File

@@ -1,22 +0,0 @@
package cn.iocoder.mall.system.biz.dto.authorization;
import lombok.Data;
import lombok.experimental.Accessors;
import javax.validation.constraints.NotNull;
/**
* 授权模块 - 获得账号所拥有的资源列表 DTO
*/
@Data
@Accessors(chain = true)
public class AuthorizationGetResourcesByAccountIdDTO {
@NotNull(message = "账号编号不能为空")
private Integer accountId;
/**
* 资源类型
*/
private Integer type;
}

View File

@@ -1,19 +0,0 @@
package cn.iocoder.mall.system.biz.dto.authorization;
import lombok.Data;
import lombok.experimental.Accessors;
import javax.validation.constraints.NotNull;
import java.util.Collection;
/**
* 授权模块 - 获得每个账号所拥有的角色 DTO
*/
@Data
@Accessors(chain = true)
public class AuthorizationGetRoleMapByAccountIdsDTO {
@NotNull(message = "账号编号数组不能为空")
private Collection<Integer> accountIds;
}

View File

@@ -1,18 +0,0 @@
package cn.iocoder.mall.system.biz.dto.authorization;
import lombok.Data;
import lombok.experimental.Accessors;
import javax.validation.constraints.NotNull;
/**
* 授权模块 - 获得角色拥有资源集合 DTO
*/
@Data
@Accessors(chain = true)
public class AuthorizationGetRoleResourcesDTO {
@NotNull(message = "角色编号不能为空")
private Integer roleId;
}

View File

@@ -1,25 +0,0 @@
package cn.iocoder.mall.system.biz.dto.authorization;
import lombok.Data;
import lombok.experimental.Accessors;
import java.util.Collection;
/**
* 资源模块 - 获得资源总数 DTO
*/
@Data
@Accessors(chain = true)
public class ResourceCountDTO {
/**
* 资源编号数组
*/
private Collection<Integer> ids;
/**
* 资源类型
*/
private Integer type;
}

View File

@@ -1,27 +0,0 @@
package cn.iocoder.mall.system.biz.dto.authorization;
import lombok.Data;
import lombok.experimental.Accessors;
import java.util.Collection;
/**
* 资源模块 - 获得资源列表 DTO
*/
@Data
@Accessors(chain = true)
public class ResourceGetListDTO {
/**
* 资源编号数组
*
* 如果传入空,则不进行资源编号的过滤
*/
private Collection<Integer> ids;
/**
* 资源类型
*/
private Integer type;
}

View File

@@ -1,25 +0,0 @@
package cn.iocoder.mall.system.biz.dto.authorization;
import lombok.Data;
import lombok.experimental.Accessors;
import java.util.Collection;
/**
* 资源模块 - 获得资源树 DTO
*/
@Data
@Accessors(chain = true)
public class ResourceGetTreeDTO {
/**
* 资源编号数组
*/
private Collection<Integer> ids;
/**
* 资源类型
*/
private Integer type;
}

View File

@@ -1,20 +0,0 @@
package cn.iocoder.mall.system.biz.dto.oatuh2;
import lombok.Data;
import lombok.experimental.Accessors;
import javax.validation.constraints.NotEmpty;
/**
* 认证模块 - 访问令牌认证 DTO
*/
@Data
@Accessors(chain = true)
public class OAuth2AccessTokenAuthenticateDTO {
@NotEmpty(message = "访问令牌不能为空")
private String accessToken;
@NotEmpty(message = "IP 不能为空")
private String ip;
}

View File

@@ -1,14 +0,0 @@
package cn.iocoder.mall.system.biz.dto.oatuh2;
import lombok.Data;
import lombok.experimental.Accessors;
// TODO 注释
@Data
@Accessors(chain = true)
public class OAuth2MobileCodAuthenticateDTO {
private String mobile;
private String code;
}

View File

@@ -1,15 +0,0 @@
package cn.iocoder.mall.system.biz.dto.oatuh2;
import lombok.Data;
import lombok.experimental.Accessors;
// TODO 注释
@Data
@Accessors(chain = true)
public class OAuth2MobileCodeAuthenticateDTO {
private String mobile;
private String code;
private String ip;
}

View File

@@ -1,14 +0,0 @@
package cn.iocoder.mall.system.biz.dto.oatuh2;
import lombok.Data;
import lombok.experimental.Accessors;
// TODO 注释
@Data
@Accessors(chain = true)
public class OAuth2MobileCodeSendDTO {
private String mobile;
private String ip;
}

View File

@@ -1,20 +0,0 @@
package cn.iocoder.mall.system.biz.dto.oatuh2;
import lombok.Data;
import lombok.experimental.Accessors;
import javax.validation.constraints.NotEmpty;
/**
* 认证模块 - 访问令牌认证 DTO
*/
@Data
@Accessors(chain = true)
public class OAuth2RefreshTokenAuthenticateDTO {
@NotEmpty(message = "访问令牌不能为空")
private String refreshToken;
@NotEmpty(message = "IP 不能为空")
private String ip;
}

View File

@@ -1,21 +0,0 @@
package cn.iocoder.mall.system.biz.dto.oatuh2;
import lombok.Data;
import lombok.experimental.Accessors;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
/**
* 认证模块 - 账号 + 密码认证 DTO
*/
@Data
@Accessors(chain = true)
public class OAuth2UsernameAuthenticateDTO {
@NotEmpty(message = "账号不能为空")
private String username;
@NotNull(message = "密码不能为空")
private String password;
}

View File

@@ -1,29 +0,0 @@
package cn.iocoder.mall.system.biz.event.authorization;
import cn.iocoder.mall.system.biz.dataobject.authorization.ResourceDO;
import org.springframework.context.ApplicationEvent;
/**
* {@link ResourceDO} 删除事件
*/
public class ResourceDeleteEvent extends ApplicationEvent {
/**
* 资源编号
*/
private Integer id;
public ResourceDeleteEvent(Object source) {
super(source);
}
public ResourceDeleteEvent(Object source, Integer id) {
super(source);
this.id = id;
}
public Integer getId() {
return id;
}
}

View File

@@ -1,28 +0,0 @@
package cn.iocoder.mall.system.biz.event.authorization;
import cn.iocoder.mall.system.biz.dataobject.authorization.RoleDO;
import org.springframework.context.ApplicationEvent;
/**
* {@link RoleDO} 删除事件
*/
public class RoleDeleteEvent extends ApplicationEvent {
/**
* 角色编号
*/
private Integer id;
public RoleDeleteEvent(Object source) {
super(source);
}
public RoleDeleteEvent(Object source, Integer id) {
super(source);
this.id = id;
}
public Integer getId() {
return id;
}
}

View File

@@ -73,62 +73,6 @@ public class AuthorizationServiceImpl implements AuthorizationService {
}
}
@Override
public List<ResourceBO> getResourcesByAccountId(AuthorizationGetResourcesByAccountIdDTO getResourcesByAccountIdDTO) {
// 查询管理员拥有的角色关联数据
List<AccountRoleDO> accountRoleDOs = accountRoleMapper.selectByAccountId(getResourcesByAccountIdDTO.getAccountId());
if (CollectionUtil.isEmpty(accountRoleDOs)) {
return Collections.emptyList();
}
Set<Integer> roleIds = CollectionUtil.convertSet(accountRoleDOs, AccountRoleDO::getRoleId);
// 判断是否为超管。若是超管,默认有所有权限
if (roleService.hasSuperAdmin(roleIds)) {
return resourceService.getResources(new ResourceGetListDTO().setType(getResourcesByAccountIdDTO.getType()));
}
// 查询角色拥有的资源关联数据
List<RoleResourceDO> roleResourceDOs = roleResourceMapper.selectListByRoleIds(roleIds);
if (CollectionUtil.isEmpty(roleResourceDOs)) {
return Collections.emptyList();
}
Set<Integer> resourceIds = CollectionUtil.convertSet(roleResourceDOs, RoleResourceDO::getResourceId);
// 查询对应资源列表
return resourceService.getResources(new ResourceGetListDTO().setIds(resourceIds).setType(getResourcesByAccountIdDTO.getType()));
}
@Override
public Map<Integer, Set<RoleBO>> getRoleMapByAccountIds(AuthorizationGetRoleMapByAccountIdsDTO getRoleMapByAccountIdsDTO) {
return null;
}
// @Override
// public Map<Integer, Set<RoleBO>> getRoleIdMapByAccountIds(AuthorizationGetRoleMapByAccountIdsDTO getRoleMapByAccountIdsDTO) {
// // 查询管理员拥有的角色关联数据
// List<AccountRoleDO> accountRoleDOs = accountRoleMapper.selectListByAccountIds(getRoleMapByAccountIdsDTO.getAccountIds());
// if (CollectionUtil.isEmpty(accountRoleDOs)) {
// return Collections.emptyMap();
// }
// // 构建结果
// Map<Integer, Set<Integer>> accountRoleMap = CollectionUtil.convertMultiMap2(accountRoleDOs,
// AccountRoleDO::getAccountId, AccountRoleDO::getRoleId);
// getRoleMapByAccountIdsDTO.getAccountIds().forEach(accountId -> accountRoleMap.putIfAbsent(accountId, Collections.emptySet()));
// return accountRoleMap;
// }
@Override
public Set<Integer> getRoleResources(AuthorizationGetRoleResourcesDTO getRoleResourcesDTO) {
Set<Integer> roleIds = Collections.singleton(getRoleResourcesDTO.getRoleId());
// 判断是否为超管。若是超管,默认有所有权限
if (roleService.hasSuperAdmin(roleIds)) {
return CollectionUtil.convertSet(resourceService.getResources(new ResourceGetListDTO()), ResourceBO::getId);
}
// 查询角色拥有的资源关联数据
List<RoleResourceDO> roleResourceDOs = roleResourceMapper.selectListByRoleIds(roleIds);
if (CollectionUtil.isEmpty(roleResourceDOs)) {
return Collections.emptySet();
}
return CollectionUtil.convertSet(roleResourceDOs, RoleResourceDO::getResourceId);
}
@EventListener
public void handleResourceDeleteEvent(ResourceDeleteEvent event) {
roleResourceMapper.deleteByResourceId(event.getId());

View File

@@ -1,23 +0,0 @@
package cn.iocoder.mall.system.biz.service.authorization;
import cn.iocoder.mall.system.biz.bo.authorization.ResourceBO;
import cn.iocoder.mall.system.biz.bo.authorization.ResourceTreeNodeBO;
import cn.iocoder.mall.system.biz.dto.authorization.ResourceCountDTO;
import cn.iocoder.mall.system.biz.dto.authorization.ResourceGetListDTO;
import cn.iocoder.mall.system.biz.dto.authorization.ResourceGetTreeDTO;
import java.util.Collection;
import java.util.List;
/**
* 资源模块 - Service 接口
*/
public interface ResourceService {
List<ResourceBO> getResourcesByPermissions(Collection<String> permissions);
List<ResourceBO> getResources(ResourceGetListDTO getListDTO);
int countResource(ResourceCountDTO countDTO);
}

View File

@@ -1,45 +0,0 @@
package cn.iocoder.mall.system.biz.service.authorization;
import cn.iocoder.common.framework.util.ServiceExceptionUtil;
import cn.iocoder.mall.mybatis.enums.DeletedStatusEnum;
import cn.iocoder.mall.system.biz.bo.authorization.ResourceBO;
import cn.iocoder.mall.system.biz.bo.authorization.ResourceTreeNodeBO;
import cn.iocoder.mall.system.biz.convert.authorization.ResourceConvert;
import cn.iocoder.mall.system.biz.dao.authorization.ResourceMapper;
import cn.iocoder.mall.system.biz.dataobject.authorization.ResourceDO;
import cn.iocoder.mall.system.biz.dto.authorization.*;
import cn.iocoder.mall.system.biz.enums.SystemErrorCodeEnum;
import cn.iocoder.mall.system.biz.enums.authorization.ResourceIdEnum;
import cn.iocoder.mall.system.biz.enums.authorization.ResourceTypeEnum;
import cn.iocoder.mall.system.biz.event.authorization.ResourceDeleteEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
import java.util.stream.Collectors;
@Service
@Slf4j
public class ResourceServiceImpl implements ResourceService {
@Autowired
private ApplicationEventPublisher eventPublisher;
@Override
public List<ResourceBO> getResourcesByPermissions(Collection<String> permissions) {
List<ResourceDO> resourceDOs = resourceMapper.selectListByPermissions(permissions);
return ResourceConvert.INSTANCE.convertList(resourceDOs);
}
@Override
public List<ResourceBO> getResources(ResourceGetListDTO getListDTO) {
List<ResourceDO> resourceDOs = resourceMapper.selectListByIdsAndType(getListDTO.getIds(), getListDTO.getType());
return ResourceConvert.INSTANCE.convertList(resourceDOs);
}
}

View File

@@ -42,45 +42,6 @@ public class AdminsAuthorizationController {
@Autowired
private RoleService roleService;
@GetMapping("/menu-resource-tree")
@ApiOperation(value = "获得当前账号的菜单资源树", notes = "以树结构返回")
public CommonResult<List<AdminsAuthorizationMenuTreeResponse>> menuResourceTree() {
List<ResourceTreeNodeBO> resourceTreeNodeBOs = authorizationService.getResourceTreeByAccountId(new AuthorizationGetResourcesByAccountIdDTO()
.setAccountId(AdminSecurityContextHolder.getAccountId()).setType(ResourceTypeEnum.MENU.getType()));
return CommonResult.success(AdminsAuthorizationConvert.INSTANCE.convertList(resourceTreeNodeBOs));
}
@GetMapping("/resource-permissions")
@ApiOperation(value = "获得当前账号的资源权限列表")
public CommonResult<Set<String>> resourcePermissions() {
List<ResourceBO> resources = authorizationService.getResourcesByAccountId(new AuthorizationGetResourcesByAccountIdDTO()
.setAccountId(AdminSecurityContextHolder.getAccountId()));
return CommonResult.success(resources.stream().map(ResourceBO::getRoute).collect(Collectors.toSet()));
}
@GetMapping("/role_resource_tree")
@ApiOperation(value = "获得角色拥有的菜单权限", notes = "以树结构返回。注意,返回的资源树是完整的结构,会标记每个资源节点是否被角色所拥有")
@ApiImplicitParam(name = "roleId", value = "角色编号", required = true, example = "1")
@RequiresPermissions("system:authorization:assign_role_resource")
public CommonResult<List<AdminsAuthorizationRoleResourceTreeResponse>> roleResourceTree(@RequestParam("roleId") Integer roleId) {
// 1. 获得完整的资源树
List<ResourceTreeNodeBO> resourceTreeNodeBOs = resourceService.getResourceTree(new ResourceGetTreeDTO());
// 2. 获得角色拥有的子树
Set<Integer> roleResourceIds = authorizationService.getRoleResources(new AuthorizationGetRoleResourcesDTO().setRoleId(roleId));
// 3. 拼接,返回结果
return CommonResult.success(AdminsAuthorizationConvert.INSTANCE.convertList(resourceTreeNodeBOs, roleResourceIds));
}
@PostMapping("/assign_role_resource")
@ApiOperation(value = "分配角色资源")
@RequiresPermissions("system:authorization:assign_role_resource")
public CommonResult<Boolean> assignRoleResource(AdminsAuthorizationAssignRoleResourceRequest request) {
AuthorizationAssignRoleResourceDTO authorizationAssignRoleResourceDTO = AdminsAuthorizationConvert.INSTANCE.convert(request)
.setAdminId(AdminSecurityContextHolder.getAdminId());
authorizationService.assignRoleResource(authorizationAssignRoleResourceDTO);
return CommonResult.success(true);
}
// @GetMapping("/role_list")
// @ApiOperation(value = "指定账号拥有的角色列表")
// @ApiImplicitParam(name = "accountId", value = "账号编号", required = true, example = "1")

View File

@@ -1,4 +0,0 @@
/**
* 提供 system 服务的 RPC 接口的实现,提供内部调用
*/
package cn.iocoder.mall.system.rpc;

View File

@@ -1,30 +0,0 @@
package cn.iocoder.mall.system.rpc.rpc.oauth2;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.system.biz.bo.ouath2.OAuth2AuthenticateBO;
import cn.iocoder.mall.system.biz.dto.oatuh2.OAuth2AccessTokenAuthenticateDTO;
import cn.iocoder.mall.system.biz.service.oauth2.OAuth2Service;
import cn.iocoder.mall.system.rpc.api.oauth2.OAuth2RPC;
import cn.iocoder.mall.system.rpc.convert.oauth2.OAuth2Convert;
import cn.iocoder.mall.system.rpc.request.oauth2.OAuth2AccessTokenAuthenticateRequest;
import cn.iocoder.mall.system.rpc.response.oauth2.OAuth2AccessTokenResponse;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
@Service(version = "${dubbo.provider.OAuth2RPC.version}", validation = "true")
public class OAuth2RPCImpl implements OAuth2RPC {
@Autowired
private OAuth2Service oauth2Service;
@Override
public CommonResult<OAuth2AccessTokenResponse> authenticate(OAuth2AccessTokenAuthenticateRequest authenticateRequest) {
// 执行认证
OAuth2AccessTokenAuthenticateDTO authenticateDTO = OAuth2Convert.INSTANCE.convert(authenticateRequest);
OAuth2AuthenticateBO accessTokenBO = oauth2Service.authenticate(authenticateDTO);
// 返回结果
OAuth2AccessTokenResponse accessTokenResponse = OAuth2Convert.INSTANCE.convert(accessTokenBO);
return CommonResult.success(accessTokenResponse);
}
}