检测文件权限相关更新
This commit is contained in:
parent
ea24de5765
commit
b5aa15ec02
@ -9,6 +9,7 @@ import cn.iocoder.yudao.module.inspection.service.AppInspectionPartnerService;
|
||||
import cn.iocoder.yudao.module.inspection.service.IInspectionFileService;
|
||||
import cn.iocoder.yudao.module.inspection.vo.AssignAuthorityVo;
|
||||
import cn.iocoder.yudao.module.shop.entity.ShopMallPartners;
|
||||
import cn.iocoder.yudao.module.system.service.permission.RoleService;
|
||||
import cn.iocoder.yudao.util.ExcelUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
@ -33,20 +34,34 @@ public class InspectionFileController extends BaseController {
|
||||
@Autowired
|
||||
private AppInspectionPartnerService partnerService;
|
||||
|
||||
@Autowired
|
||||
private RoleService roleService;
|
||||
|
||||
/**
|
||||
* 查询inspectionFile列表
|
||||
*/
|
||||
@GetMapping("/list")
|
||||
public CommonResult list(InspectionFile inspectionFile,
|
||||
@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
|
||||
@RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) throws Exception {
|
||||
public CommonResult list(InspectionFile inspectionFile) throws Exception {
|
||||
ShopMallPartners partners = partnerService.shopInfoByUserId();
|
||||
inspectionFile.setPartnerId(partners.getPartnerId());
|
||||
|
||||
Page<InspectionFile> page = new Page<>(pageNum, pageSize);
|
||||
List<InspectionFile> list = inspectionFileService.selectInspectionFileList(inspectionFile);
|
||||
|
||||
// 使用MyBatis-Plus的分页查询,将结果转换为Page对象
|
||||
IPage<InspectionFile> list = inspectionFileService.selectInspectionFileList(page, inspectionFile);
|
||||
return success(list);
|
||||
}
|
||||
/**
|
||||
* 查询inspectionFile列表权限
|
||||
*/
|
||||
@GetMapping("/listByPermission")
|
||||
public CommonResult listByPermission(InspectionFile inspectionFile) throws Exception {
|
||||
ShopMallPartners partners = partnerService.shopInfoByUserId();
|
||||
inspectionFile.setPartnerId(partners.getPartnerId());
|
||||
//判断当前登录人是否是租户管理员 tenant_admin
|
||||
LoginUser loginUser = getLoginUser();
|
||||
// 判断当前登录人是否是租户管理员 tenant_admin
|
||||
|
||||
|
||||
List<InspectionFile> list = inspectionFileService.selectInspectionFileListByPermissio(inspectionFile, getLoginUser().getId());
|
||||
|
||||
return success(list);
|
||||
}
|
||||
@ -55,13 +70,10 @@ public class InspectionFileController extends BaseController {
|
||||
* 导出inspectionFile列表
|
||||
*/
|
||||
@PostMapping("/export")
|
||||
public void export(HttpServletResponse response, InspectionFile inspectionFile,
|
||||
@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
|
||||
@RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
|
||||
Page<InspectionFile> page = new Page<>(pageNum, pageSize);
|
||||
IPage<InspectionFile> list = inspectionFileService.selectInspectionFileList(page, inspectionFile);
|
||||
public void export(HttpServletResponse response, InspectionFile inspectionFile) {
|
||||
List<InspectionFile> list = inspectionFileService.selectInspectionFileList(inspectionFile);
|
||||
ExcelUtil<InspectionFile> util = new ExcelUtil<InspectionFile>(InspectionFile.class);
|
||||
util.exportExcel(response, list.getRecords(), "inspectionFile数据");
|
||||
util.exportExcel(response, list, "inspectionFile数据");
|
||||
}
|
||||
|
||||
/**
|
||||
@ -148,9 +160,9 @@ public class InspectionFileController extends BaseController {
|
||||
*/
|
||||
@PostMapping("/assignAuthority")
|
||||
public CommonResult assignAuthority(@RequestBody AssignAuthorityVo assignAuthorityVo) {
|
||||
if (CollUtil.isEmpty(assignAuthorityVo.getUserIds())) {
|
||||
return null;
|
||||
}
|
||||
// if (CollUtil.isEmpty(assignAuthorityVo.getUserIds())) {
|
||||
// return null;
|
||||
// }
|
||||
return success(inspectionFileService.assignAuthority(assignAuthorityVo.getUserIds(), assignAuthorityVo.getFileId()));
|
||||
}
|
||||
|
||||
|
@ -22,7 +22,7 @@ public class InspectionFileUser {
|
||||
/**
|
||||
* 文件id
|
||||
*/
|
||||
private Integer fileId;
|
||||
private Long fileId;
|
||||
|
||||
/**
|
||||
* 用户id
|
||||
|
@ -72,7 +72,7 @@ public interface IInspectionFileService extends IService<InspectionFile> {
|
||||
* @param inspectionFile 查询条件
|
||||
* @return 分页结果
|
||||
*/
|
||||
IPage<InspectionFile> selectInspectionFileList(Page<InspectionFile> page, InspectionFile inspectionFile);
|
||||
List<InspectionFile> selectInspectionFileList(InspectionFile inspectionFile);
|
||||
|
||||
/**
|
||||
* 根据id统计数量
|
||||
@ -90,7 +90,7 @@ public interface IInspectionFileService extends IService<InspectionFile> {
|
||||
* @param fileId 文件id
|
||||
* @return
|
||||
*/
|
||||
Object assignAuthority(List<Long> userIds, Integer fileId);
|
||||
Object assignAuthority(List<Long> userIds, Long fileId);
|
||||
|
||||
/**
|
||||
* 根据文件id获取有权限的用户id集合
|
||||
@ -99,4 +99,12 @@ public interface IInspectionFileService extends IService<InspectionFile> {
|
||||
* @return
|
||||
*/
|
||||
List<Long> getUserIdsByFileId(Integer fileId);
|
||||
|
||||
/**
|
||||
* 根据权限查询文件集合
|
||||
*
|
||||
* @param inspectionFile
|
||||
* @return
|
||||
*/
|
||||
List<InspectionFile> selectInspectionFileListByPermissio(InspectionFile inspectionFile, Long userId);
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
package cn.iocoder.yudao.module.inspection.service.impl;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
|
||||
import cn.iocoder.yudao.module.inspection.entity.InspectionFile;
|
||||
@ -11,15 +13,13 @@ import cn.iocoder.yudao.module.inspection.service.IWarnMessageService;
|
||||
import cn.iocoder.yudao.module.inspection.service.InspectionFileUserService;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@ -195,7 +195,7 @@ public class InspectionFileServiceImpl extends ServiceImpl<InspectionFileMapper,
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPage<InspectionFile> selectInspectionFileList(Page<InspectionFile> page, InspectionFile inspectionFile) {
|
||||
public List<InspectionFile> selectInspectionFileList(InspectionFile inspectionFile) {
|
||||
// 创建一个空的 QueryWrapper
|
||||
LambdaQueryWrapper<InspectionFile> queryWrapper = new LambdaQueryWrapper<>();
|
||||
if (ObjectUtil.isNotEmpty(inspectionFile.getFatherId())) {
|
||||
@ -207,7 +207,7 @@ public class InspectionFileServiceImpl extends ServiceImpl<InspectionFileMapper,
|
||||
queryWrapper.like(InspectionFile::getFileName, inspectionFile.getFileName());
|
||||
}
|
||||
// 直接调用 MyBatis-Plus 的 page 方法进行分页查询
|
||||
return this.page(page, queryWrapper); // 返回符合条件的分页查询结果
|
||||
return this.list(queryWrapper); // 返回符合条件的分页查询结果
|
||||
}
|
||||
|
||||
/**
|
||||
@ -234,28 +234,30 @@ public class InspectionFileServiceImpl extends ServiceImpl<InspectionFileMapper,
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Object assignAuthority(List<Long> userIds, Integer fileId) {
|
||||
//查询对应文件
|
||||
InspectionFile inspectionFile = this.getById(fileId);
|
||||
if (ObjectUtil.isNotEmpty(inspectionFile)) {
|
||||
//获取文件的id与所有父级
|
||||
String fileCode = inspectionFile.getFileCode();
|
||||
//根据逗号分割fileCode
|
||||
String[] code = fileCode.split(",");
|
||||
List<InspectionFileUser> inspectionFileUsers = new ArrayList<>();
|
||||
for (Long userId : userIds) {
|
||||
for (String s : code) {
|
||||
InspectionFileUser inspectionFileUser = new InspectionFileUser();
|
||||
inspectionFileUser.setUserId(userId);
|
||||
inspectionFileUser.setFileId(Integer.parseInt(s));
|
||||
inspectionFileUsers.add(inspectionFileUser);
|
||||
}
|
||||
}
|
||||
//删除之前的权限
|
||||
inspectionFileUserService.remove(new LambdaQueryWrapper<InspectionFileUser>().in(InspectionFileUser::getFileId, code));
|
||||
//批量保存
|
||||
inspectionFileUserService.saveBatch(inspectionFileUsers);
|
||||
public Object assignAuthority(List<Long> userIds, Long fileId) {
|
||||
// 如果选择的文件 ID为空,直接返回
|
||||
if (ObjectUtil.isEmpty(fileId)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 1. 删除这些文件的用户权限
|
||||
inspectionFileUserService.remove(new LambdaQueryWrapper<InspectionFileUser>()
|
||||
.eq(InspectionFileUser::getFileId, fileId));
|
||||
|
||||
// 2. 为每个用户和文件合赋予权限
|
||||
List<InspectionFileUser> inspectionFileUsers = new ArrayList<>();
|
||||
for (Long userId : userIds) {
|
||||
InspectionFileUser inspectionFileUser = new InspectionFileUser();
|
||||
inspectionFileUser.setUserId(userId);
|
||||
inspectionFileUser.setFileId(fileId);
|
||||
inspectionFileUser.setCreateTime(DateUtil.date().toLocalDateTime());
|
||||
inspectionFileUser.setUpdateTime(DateUtil.date().toLocalDateTime());
|
||||
inspectionFileUsers.add(inspectionFileUser);
|
||||
}
|
||||
|
||||
// 3. 批量保存新权限
|
||||
inspectionFileUserService.saveBatch(inspectionFileUsers);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -273,4 +275,141 @@ public class InspectionFileServiceImpl extends ServiceImpl<InspectionFileMapper,
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<InspectionFile> selectInspectionFileListByPermissio(InspectionFile inspectionFile, Long userId) {
|
||||
List<InspectionFile> resultFiles = new ArrayList<>();
|
||||
|
||||
// 1. 获取用户有权限的所有文件
|
||||
List<Long> accessibleFileIds = inspectionFileUserService
|
||||
.list(new LambdaQueryWrapper<InspectionFileUser>()
|
||||
.eq(InspectionFileUser::getUserId, userId))
|
||||
.stream()
|
||||
.map(InspectionFileUser::getFileId)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 如果用户没有文件权限,则返回空
|
||||
if (accessibleFileIds.isEmpty()) {
|
||||
return resultFiles;
|
||||
}
|
||||
|
||||
// 2. 获取所有文件及文件夹信息(避免多次查询数据库)
|
||||
Map<Long, InspectionFile> fileCache = this.list().stream()
|
||||
.collect(Collectors.toMap(InspectionFile::getId, file -> file));
|
||||
|
||||
// 3. 判断是否传递了fatherId参数(即点击了文件夹)
|
||||
Long fatherId = inspectionFile.getFatherId(); // 获取传递的文件夹ID
|
||||
|
||||
if (fatherId != null) {
|
||||
// 4. 如果传递了fatherId,则查询该文件夹下所有有权限的文件,包括子文件夹及其内容
|
||||
resultFiles.addAll(getFilesInFolder(fatherId, accessibleFileIds, fileCache));
|
||||
} else {
|
||||
// 5. 如果没有传递fatherId,遍历用户有权限的文件
|
||||
for (Long fileId : accessibleFileIds) {
|
||||
InspectionFile file = fileCache.get(fileId);
|
||||
if (file != null) {
|
||||
// 获取当前文件的父文件夹(递归获取最顶级父文件夹)
|
||||
List<InspectionFile> parentFiles = getParentFiles(file.getFatherId(), fileCache);
|
||||
|
||||
// 如果有父文件,则将最顶级的父文件加入返回结果,否则返回当前文件
|
||||
if (!parentFiles.isEmpty()) {
|
||||
resultFiles.add(parentFiles.get(parentFiles.size() - 1)); // 只添加最顶级父文件
|
||||
} else {
|
||||
resultFiles.add(file); // 当前文件没有父文件,直接返回当前文件
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 去重操作,优化查询时已经进行了判断,可以删除重复文件
|
||||
return resultFiles.stream().distinct().collect(Collectors.toList());
|
||||
}
|
||||
|
||||
// 获取文件夹内的所有文件(包括子文件夹的内容)
|
||||
private List<InspectionFile> getFilesInFolder(Long folderId, List<Long> accessibleFileIds, Map<Long, InspectionFile> fileCache) {
|
||||
List<InspectionFile> filesInFolder = new ArrayList<>();
|
||||
// 获取该文件夹下的所有子文件
|
||||
List<InspectionFile> folderFiles = this.list(new LambdaQueryWrapper<InspectionFile>()
|
||||
.eq(InspectionFile::getFatherId, folderId));
|
||||
|
||||
for (InspectionFile folderFile : folderFiles) {
|
||||
// 默认检查当前文件是否有权限
|
||||
boolean hasAccess = accessibleFileIds.contains(folderFile.getId());
|
||||
|
||||
// 如果是文件夹,递归向下找子文件夹内是否有权限文件
|
||||
if (folderFile.getType().equals("1")) { // 文件夹类型
|
||||
// 如果没有权限,递归检查该文件夹内是否有子文件或子文件夹有权限
|
||||
if (!hasAccess) {
|
||||
hasAccess = (hasAccessToSubFiles(folderFile, accessibleFileIds, fileCache) || checkParentFolderPermission(folderFile, accessibleFileIds, fileCache));
|
||||
}
|
||||
} else {
|
||||
// 对于文件类型,检查父文件夹的权限
|
||||
if (!hasAccess) {
|
||||
hasAccess = checkParentFolderPermission(folderFile, accessibleFileIds, fileCache);
|
||||
}
|
||||
}
|
||||
|
||||
// 如果文件未处理且用户有权限
|
||||
if (hasAccess) {
|
||||
filesInFolder.add(folderFile);
|
||||
}
|
||||
}
|
||||
|
||||
return filesInFolder;
|
||||
}
|
||||
|
||||
// 检查当前文件夹下是否有权限的子文件或子文件夹
|
||||
private boolean hasAccessToSubFiles(InspectionFile folderFile, List<Long> accessibleFileIds, Map<Long, InspectionFile> fileCache) {
|
||||
// 获取文件夹内的所有子文件
|
||||
List<InspectionFile> subFiles = this.list(new LambdaQueryWrapper<InspectionFile>()
|
||||
.eq(InspectionFile::getFatherId, folderFile.getId()));
|
||||
|
||||
// 如果文件夹下有文件且其中一个文件有权限,则返回true
|
||||
for (InspectionFile subFile : subFiles) {
|
||||
if (accessibleFileIds.contains(subFile.getId())) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// 如果是文件夹,递归检查该文件夹是否有权限文件
|
||||
if (subFile.getType().equals("1") && hasAccessToSubFiles(subFile, accessibleFileIds, fileCache)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// 检查父文件夹的权限,直到找到用户有权限的文件夹
|
||||
private boolean checkParentFolderPermission(InspectionFile file, List<Long> accessibleFileIds, Map<Long, InspectionFile> fileCache) {
|
||||
Long fatherId = file.getFatherId();
|
||||
// 如果父文件夹存在且用户有权限,则返回true
|
||||
if (fatherId != null && accessibleFileIds.contains(fatherId)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// 如果父文件夹没有权限,继续递归向上查找父文件夹
|
||||
if (fatherId != null) {
|
||||
InspectionFile parentFile = fileCache.get(fatherId);
|
||||
if (parentFile != null) {
|
||||
return checkParentFolderPermission(parentFile, accessibleFileIds, fileCache);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// 递归获取文件夹的父文件夹,直到没有父文件夹为止
|
||||
private List<InspectionFile> getParentFiles(Long fatherId, Map<Long, InspectionFile> fileCache) {
|
||||
List<InspectionFile> parentFiles = new ArrayList<>();
|
||||
// 查找父文件
|
||||
InspectionFile parentFile = fileCache.get(fatherId);
|
||||
if (parentFile != null) {
|
||||
// 如果父文件有父级文件,继续递归查找
|
||||
if (parentFile.getFatherId() != null) {
|
||||
parentFiles.addAll(getParentFiles(parentFile.getFatherId(), fileCache));
|
||||
} else {
|
||||
parentFiles.add(parentFile); // 添加父文件
|
||||
}
|
||||
}
|
||||
return parentFiles;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -20,5 +20,5 @@ public class AssignAuthorityVo {
|
||||
/**
|
||||
* 文件id
|
||||
*/
|
||||
Integer fileId;
|
||||
Long fileId;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user