重构,增加 Permission 相关模块,更加清晰

This commit is contained in:
YunaiV 2020-07-13 07:50:07 +08:00
parent 2daea0060d
commit d34e555aa0
18 changed files with 334 additions and 113 deletions

View File

@ -42,7 +42,7 @@ public class PassportController {
@GetMapping("/info")
@ApiOperation(value = "获得当前管理员信息")
public CommonResult<PassportAdminVO> info() {
public CommonResult<PassportAdminVO> getInfo() {
return success(passportManager.getAdmin(AdminSecurityContextHolder.getAdminId()));
}

View File

@ -0,0 +1,41 @@
package cn.iocoder.mall.managementweb.controller.permission;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.managementweb.manager.permission.PermissionManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Set;
import static cn.iocoder.common.framework.vo.CommonResult.success;
/**
* 权限 Controller
*/
@RestController
@RequestMapping("/permission")
@Api(tags = "权限")
@Validated
public class PermissionController {
@Autowired
private PermissionManager permissionManager;
@GetMapping("/list-role-resource")
@ApiOperation("获得角色拥有的资源编号")
@ApiImplicitParam(name = "roleId", value = "角色编号", required = true)
public CommonResult<Set<Integer>> listRoleResource(Integer roleId) {
return success(permissionManager.listRoleResource(roleId));
}
public CommonResult<Boolean> assignRoleResource() {
return success(true);
}
}

View File

@ -55,13 +55,14 @@ public class RoleController {
@GetMapping("/get")
@ApiOperation("获得角色")
@ApiImplicitParam(name = "roleId", value = "角色编号", required = true)
public CommonResult<RoleVO> getRole(@RequestParam("roleId") Integer roleId) {
return success(roleManager.getRole(roleId));
}
@GetMapping("/list")
@ApiOperation("获得角色列表")
@ApiImplicitParam(name = "roleId", value = "角色编号列表", required = true)
@ApiImplicitParam(name = "roleIds", value = "角色编号列表", required = true)
public CommonResult<List<RoleVO>> getRoles(@RequestParam("roleIds") List<Integer> roleIds) {
return success(roleManager.listRole(roleIds));
}

View File

@ -0,0 +1,20 @@
package cn.iocoder.mall.managementweb.controller.permission.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotNull;
import java.util.Set;
@ApiModel("赋予角色资源 DTO")
@Data
public class PermissionAssignRoleResourceDTO {
@ApiModelProperty(value = "角色名", required = true, example = "管理员")
@NotNull(message = "角色编号不能为空")
private Integer roleId;
@ApiModelProperty(value = "资源编号列表", required = true, example = "1,3,5")
private Set<Integer> resourceIds;
}

View File

@ -0,0 +1,14 @@
package cn.iocoder.mall.managementweb.convert.permission;
import cn.iocoder.mall.systemservice.rpc.permission.dto.PermissionAssignRoleResourceDTO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
@Mapper
public interface PermissionConvert {
PermissionConvert INSTANCE = Mappers.getMapper(PermissionConvert.class);
PermissionAssignRoleResourceDTO convert(cn.iocoder.mall.managementweb.controller.permission.dto.PermissionAssignRoleResourceDTO bean);
}

View File

@ -0,0 +1,44 @@
package cn.iocoder.mall.managementweb.manager.permission;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.managementweb.controller.permission.dto.PermissionAssignRoleResourceDTO;
import cn.iocoder.mall.managementweb.convert.permission.PermissionConvert;
import cn.iocoder.mall.systemservice.rpc.permission.PermissionRpc;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;
import java.util.Set;
/**
* 权限 Manager
*/
@Service
public class PermissionManager {
@Reference(version = "${dubbo.consumer.PermissionRpc.version}", validation = "false")
private PermissionRpc permissionRpc;
/**
* 获得角色拥有的资源编号
*
* @param roleId 角色编号
* @return 资源编号列表
*/
public Set<Integer> listRoleResource(Integer roleId) {
CommonResult<Set<Integer>> listAdminRoleIdsResult = permissionRpc.listRoleResourceId(roleId);
listAdminRoleIdsResult.checkError();
return listAdminRoleIdsResult.getData();
}
/**
* 赋予角色资源
*
* @param assignRoleResourceDTO 赋予角色资源 DTO
*/
public void assignRoleResource(PermissionAssignRoleResourceDTO assignRoleResourceDTO) {
CommonResult<Boolean> assignRoleResourceResult = permissionRpc.assignRoleResource(
PermissionConvert.INSTANCE.convert(assignRoleResourceDTO));
assignRoleResourceResult.checkError();
}
}

View File

@ -34,6 +34,8 @@ dubbo:
version: 1.0.0
RoleRpc:
version: 1.0.0
PermissionRpc:
version: 1.0.0
# Swagger 配置项
swagger:

View File

@ -0,0 +1,29 @@
package cn.iocoder.mall.systemservice.rpc.permission;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.systemservice.rpc.permission.dto.PermissionAssignRoleResourceDTO;
import java.util.Set;
/**
* 权限 Rpc 接口
*/
public interface PermissionRpc {
/**
* 获得角色拥有的资源编号
*
* @param roleId 角色编号
* @return 资源编号列表
*/
CommonResult<Set<Integer>> listRoleResourceId(Integer roleId);
/**
* 赋予角色资源
*
* @param assignRoleResourceDTO 赋予角色资源 DTO
* @return 成功
*/
CommonResult<Boolean> assignRoleResource(PermissionAssignRoleResourceDTO assignRoleResourceDTO);
}

View File

@ -0,0 +1,26 @@
package cn.iocoder.mall.systemservice.rpc.permission.dto;
import lombok.Data;
import lombok.experimental.Accessors;
import javax.validation.constraints.NotNull;
import java.util.Set;
/**
* 赋予角色资源 DTO
*/
@Data
@Accessors(chain = true)
public class PermissionAssignRoleResourceDTO {
/**
* 角色编号
*/
@NotNull(message = "角色编号不能为空")
private Integer roleId;
/**
* 资源编号列表
*/
private Set<Integer> resourceIds;
}

View File

@ -0,0 +1,38 @@
package cn.iocoder.mall.systemservice.manager.permission;
import cn.iocoder.mall.systemservice.rpc.permission.dto.PermissionAssignRoleResourceDTO;
import cn.iocoder.mall.systemservice.service.permission.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Set;
/**
* 权限 Manager
*/
@Service
public class PermissionManager {
@Autowired
private PermissionService permissionService;
/**
* 获得角色拥有的资源编号
*
* @param roleId 角色编号
* @return 资源编号列表
*/
public Set<Integer> listRoleResourceId(Integer roleId) {
return permissionService.listRoleResourceId(roleId);
}
/**
* 赋予角色资源
*
* @param assignResourceDTO 赋予角色资源 DTO
*/
public void assignRoleResource(PermissionAssignRoleResourceDTO assignResourceDTO) {
permissionService.assignRoleResource(assignResourceDTO.getRoleId(), assignResourceDTO.getResourceIds());
}
}

View File

@ -0,0 +1,33 @@
package cn.iocoder.mall.systemservice.rpc.permission;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.systemservice.manager.permission.PermissionManager;
import cn.iocoder.mall.systemservice.rpc.permission.dto.PermissionAssignRoleResourceDTO;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Set;
import static cn.iocoder.common.framework.vo.CommonResult.success;
/**
* 权限 Rpc 实现类
*/
@Service(version = "${dubbo.provider.PermissionRpc.version}", validation = "false")
public class PermissionRpcImpl implements PermissionRpc {
@Autowired
private PermissionManager permissionManager;
@Override
public CommonResult<Set<Integer>> listRoleResourceId(Integer roleId) {
return success(permissionManager.listRoleResourceId(roleId));
}
@Override
public CommonResult<Boolean> assignRoleResource(PermissionAssignRoleResourceDTO assignRoleResourceDTO) {
permissionManager.assignRoleResource(assignRoleResourceDTO);
return success(true);
}
}

View File

@ -0,0 +1,81 @@
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.mybatis.enums.DeletedStatusEnum;
import cn.iocoder.mall.systemservice.dal.mysql.dataobject.permission.RoleResourceDO;
import cn.iocoder.mall.systemservice.dal.mysql.mapper.permission.AdminRoleMapper;
import cn.iocoder.mall.systemservice.dal.mysql.mapper.permission.ResourceMapper;
import cn.iocoder.mall.systemservice.dal.mysql.mapper.permission.RoleMapper;
import cn.iocoder.mall.systemservice.dal.mysql.mapper.permission.RoleResourceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import static cn.iocoder.mall.systemservice.enums.SystemErrorCodeEnum.AUTHORIZATION_ROLE_ASSIGN_RESOURCE_NOT_EXISTS;
import static cn.iocoder.mall.systemservice.enums.SystemErrorCodeEnum.ROLE_NOT_EXISTS;
/**
* 权限 Service
*/
@Service
public class PermissionService {
@Autowired
private RoleMapper roleMapper;
@Autowired
private ResourceMapper resourceMapper;
@Autowired
private AdminRoleMapper adminRoleMapper;
@Autowired
private RoleResourceMapper roleResourceMapper;
/**
* 获得角色拥有的资源编号
*
* @param roleId 角色编号
* @return 资源编号列表
*/
public Set<Integer> listRoleResourceId(Integer roleId) {
List<RoleResourceDO> roleResourceDOs = roleResourceMapper.selectListByRoleId(roleId);
return CollectionUtils.convertSet(roleResourceDOs, RoleResourceDO::getResourceId);
}
/**
* 赋予角色资源
*
* @param roleId 角色编号
* @param resourceIds 资源编号列表
*/
public void assignRoleResource(Integer roleId, Set<Integer> resourceIds) {
// 校验角色是否存在
if (roleMapper.selectById(roleId) == null) {
throw ServiceExceptionUtil.exception(ROLE_NOT_EXISTS);
}
// 校验是否有不存在的资源
if (!CollectionUtils.isEmpty(resourceIds)) {
int dbResourceSize = resourceMapper.selectCountByIdsAndType(resourceIds, null);
if (resourceIds.size() != dbResourceSize) {
throw ServiceExceptionUtil.exception(AUTHORIZATION_ROLE_ASSIGN_RESOURCE_NOT_EXISTS);
}
}
// TODO 芋艿这里先简单实现即方式是删除老的分配的资源关系然后添加新的分配的资源关系
// 标记角色原资源关系都为删除
roleResourceMapper.deleteByRoleId(roleId);
// 创建 RoleResourceDO 数组并插入到数据库
if (!CollectionUtils.isEmpty(resourceIds)) {
List<RoleResourceDO> roleResources = resourceIds.stream().map(resourceId -> {
RoleResourceDO roleResource = new RoleResourceDO().setRoleId(roleId).setResourceId(resourceId);
roleResource.setCreateTime(new Date());
roleResource.setDeleted(DeletedStatusEnum.DELETED_NO.getValue());
return roleResource;
}).collect(Collectors.toList());
roleResourceMapper.insertList(roleResources);
}
}
}

View File

@ -43,3 +43,5 @@ dubbo:
version: 1.0.0
RoleRpc:
version: 1.0.0
PermissionRpc:
version: 1.0.0

View File

@ -58,10 +58,5 @@ public interface AuthorizationService {
*/
Set<Integer> getRoleResources(AuthorizationGetRoleResourcesDTO getRoleResourcesDTO);
/**
* 给指定权限授予权限如果更新失败则抛出 {@link ServiceException} 异常
*
* @param assignRoleResourceDTO 授予权限 DTO
*/
void assignRoleResource(AuthorizationAssignRoleResourceDTO assignRoleResourceDTO);
}

View File

@ -114,17 +114,6 @@ public class AuthorizationServiceImpl implements AuthorizationService {
// return accountRoleMap;
// }
@Override
public List<ResourceTreeNodeBO> getResourceTreeByAccountId(AuthorizationGetResourcesByAccountIdDTO getResourcesByAccountIdDTO) {
// 查询管理员拥有的角色关联数据
List<AccountRoleDO> accountRoleDOs = accountRoleMapper.selectByAccountId(getResourcesByAccountIdDTO.getAccountId());
if (CollectionUtil.isEmpty(accountRoleDOs)) {
return Collections.emptyList();
}
Set<Integer> roleIds = CollectionUtil.convertSet(accountRoleDOs, AccountRoleDO::getRoleId);
}
@Override
public Set<Integer> getRoleResources(AuthorizationGetRoleResourcesDTO getRoleResourcesDTO) {
Set<Integer> roleIds = Collections.singleton(getRoleResourcesDTO.getRoleId());
@ -140,37 +129,6 @@ public class AuthorizationServiceImpl implements AuthorizationService {
return CollectionUtil.convertSet(roleResourceDOs, RoleResourceDO::getResourceId);
}
@Override
public void assignRoleResource(AuthorizationAssignRoleResourceDTO assignRoleResourceDTO) {
Integer roleId = assignRoleResourceDTO.getRoleId();
Set<Integer> resourceIds = assignRoleResourceDTO.getResourceIds();
// 校验角色是否存在
if (roleService.getRole(roleId) == null) {
throw ServiceExceptionUtil.exception(SystemErrorCodeEnum.ROLE_NOT_EXISTS.getCode());
}
// 校验是否有不存在的资源
if (!CollectionUtil.isEmpty(resourceIds)) {
int dbResourceSize = resourceService.countResource(new ResourceCountDTO().setIds(resourceIds));
if (resourceIds.size() != dbResourceSize) {
throw ServiceExceptionUtil.exception(SystemErrorCodeEnum.AUTHORIZATION_ROLE_ASSIGN_RESOURCE_NOT_EXISTS.getCode());
}
}
// TODO 芋艿这里先简单实现即方式是删除老的分配的资源关系然后添加新的分配的资源关系
// 标记角色原资源关系都为删除
roleResourceMapper.deleteByRoleId(roleId);
// 创建 RoleResourceDO 数组并插入到数据库
if (!CollectionUtil.isEmpty(resourceIds)) {
List<RoleResourceDO> roleResources = resourceIds.stream().map(resourceId -> {
RoleResourceDO roleResource = new RoleResourceDO().setRoleId(roleId).setResourceId(resourceId);
roleResource.setCreateTime(new Date());
roleResource.setDeleted(DeletedStatusEnum.DELETED_NO.getValue());
return roleResource;
}).collect(Collectors.toList());
roleResourceMapper.insertList(roleResources);
}
// TODO 插入操作日志
}
@EventListener
public void handleResourceDeleteEvent(ResourceDeleteEvent event) {
roleResourceMapper.deleteByResourceId(event.getId());

View File

@ -20,12 +20,4 @@ public interface ResourceService {
int countResource(ResourceCountDTO countDTO);
/**
* 获得资源树
*
* @param getTreeDTO 查询条件
* @return 资源树
*/
List<ResourceTreeNodeBO> getResourceTree(ResourceGetTreeDTO getTreeDTO);
}

View File

@ -28,9 +28,6 @@ public class ResourceServiceImpl implements ResourceService {
@Autowired
private ApplicationEventPublisher eventPublisher;
@Autowired
private ResourceMapper resourceMapper;
@Override
public List<ResourceBO> getResourcesByPermissions(Collection<String> permissions) {
List<ResourceDO> resourceDOs = resourceMapper.selectListByPermissions(permissions);
@ -43,17 +40,6 @@ public class ResourceServiceImpl implements ResourceService {
return ResourceConvert.INSTANCE.convertList(resourceDOs);
}
@Override
public int countResource(ResourceCountDTO countDTO) {
return resourceMapper.selectCountByIdsAndType(countDTO.getIds(), countDTO.getType());
}
@Override
public List<ResourceTreeNodeBO> getResourceTree(ResourceGetTreeDTO getTreeDTO) {
// 获得对应的资源列表
List<ResourceDO> resourceDOs = resourceMapper.selectListByIdsAndType(getTreeDTO.getIds(), getTreeDTO.getType());
// 拼装成树
}
}

View File

@ -1,41 +0,0 @@
package cn.iocoder.mall.system.rest.controller.authorization;
import cn.iocoder.common.framework.enums.MallConstants;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.security.core.annotation.RequiresPermissions;
import cn.iocoder.mall.security.core.context.AdminSecurityContextHolder;
import cn.iocoder.mall.system.biz.bo.authorization.ResourceTreeNodeBO;
import cn.iocoder.mall.system.biz.dto.authorization.ResourceAddDTO;
import cn.iocoder.mall.system.biz.dto.authorization.ResourceDeleteDTO;
import cn.iocoder.mall.system.biz.dto.authorization.ResourceGetTreeDTO;
import cn.iocoder.mall.system.biz.dto.authorization.ResourceUpdateDTO;
import cn.iocoder.mall.system.biz.service.authorization.ResourceService;
import cn.iocoder.mall.system.rest.convert.authorization.AdminsResourceConvert;
import cn.iocoder.mall.system.rest.request.authorization.AdminsResourceAddRequest;
import cn.iocoder.mall.system.rest.request.authorization.AdminsResourceUpdateRequest;
import cn.iocoder.mall.system.rest.response.authorization.AdminsResourceTreeResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping(MallConstants.ROOT_PATH_ADMIN + "/resource")
@Api(tags = "管理员 - 资源 API")
public class AdminsResourceController {
@Autowired
private ResourceService resourceService;
@GetMapping("/tree")
@ApiOperation(value = "获得所有资源,按照树形结构返回")
@RequiresPermissions("system:resource:tree")
public CommonResult<List<AdminsResourceTreeResponse>> tree() {
List<ResourceTreeNodeBO> resourceTreeNodeBOs = resourceService.getResourceTree(new ResourceGetTreeDTO());
return CommonResult.success(AdminsResourceConvert.INSTANCE.convertList(resourceTreeNodeBOs));
}
}