重构,增加 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

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