feat(公司tab): 接线 视频任务记录——company_id 走 DataReadScope,detail/zip 用同一把 ReadScope 门住
Task 8 的前半页(生成记录那半页 BLOCKED,见账本裁定)。 - controller 接住 requireAdminOrInternal 的返回值算范围,page 侧 readScope(operator, companyId) .userIdsOr(base):显式 user_id 与公司 tab 是交集,不是替换。 - mapper 的 userId 单值条件保留、并列 userIds IN <foreach>;null=不筛,空集在 service 首行早退, 绝不把 [] 交给 mapper(渲染成 IN () 是 500)。 - selectAdminZipRows 补 t.user_id:download-zip 的闸门要不越权就得先拿得到行主人。 - detail 的 404 文案拆开:404 只留给"真查不到行",越权是 403。zip 整单任一条越界 ⇒ 403, 不借 X-Archive-Error-Count 的"部分失败"语义盖越权。 - 前端 company_id 落在 builder 之后、发请求之前的唯一一处(normalizeHistoryParams 那类白名单 构造会把键静默丢掉);页面 watch(activeId) + 读 requestCompanyId。 验证(我自己复跑,不是转述):干净树定向 7/0(新 5 支 + G2 2 支)、全量前端 1726/1725/1 (G1 缺口清单 8 页→7 页,admin_image_video_tasks 已消失)、vue-tsc rc=0。
This commit is contained in:
+24
-8
@@ -2,6 +2,8 @@ package com.nanri.aiimage.modules.imagevideo.controller;
|
||||
|
||||
import com.nanri.aiimage.common.api.ApiResponse;
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.common.model.entity.AdminUserEntity;
|
||||
import com.nanri.aiimage.common.service.DataReadScope;
|
||||
import com.nanri.aiimage.common.util.DownloadHeaderUtil;
|
||||
import com.nanri.aiimage.common.security.AdminAuthSupport;
|
||||
import com.nanri.aiimage.modules.imagevideo.service.ImageVideoAdminTaskService;
|
||||
@@ -63,6 +65,7 @@ public class AdminImageVideoTaskController {
|
||||
|
||||
private final ImageVideoAdminTaskService adminTaskService;
|
||||
private final AdminAuthSupport adminAuthSupport;
|
||||
private final DataReadScope dataReadScope;
|
||||
|
||||
@GetMapping
|
||||
@Operation(summary = "分页查询图生视频任务", description = "管理端按用户/任务状态/提交时间等条件分页查询近 3 天的工作流任务")
|
||||
@@ -75,8 +78,12 @@ public class AdminImageVideoTaskController {
|
||||
@RequestParam(name = "coze_execute_id", required = false) String executeId,
|
||||
@RequestParam(name = "submitted_from", required = false) String submittedFrom,
|
||||
@RequestParam(name = "submitted_to", required = false) String submittedTo,
|
||||
@RequestParam(name = "company_id", required = false) Long companyId,
|
||||
HttpServletRequest request) {
|
||||
adminAuthSupport.requireAdminOrInternal(request);
|
||||
AdminUserEntity operator = adminAuthSupport.requireAdminOrInternal(request);
|
||||
// 既有语义保留:显式传 user_id 时按那一个人筛;公司 tab 叠在它上面(交集)。
|
||||
List<Long> base = userId != null && userId > 0 ? List.of(userId) : null;
|
||||
List<Long> scoped = dataReadScope.readScope(operator, companyId).userIdsOr(base);
|
||||
int safePage = Math.max(1, page == null ? 1 : page);
|
||||
int safePageSize = Math.min(100, Math.max(10, pageSize == null ? 20 : pageSize));
|
||||
String cleanUsername = username == null ? null : username.trim();
|
||||
@@ -84,26 +91,26 @@ public class AdminImageVideoTaskController {
|
||||
String cleanExecuteId = executeId == null ? null : executeId.trim();
|
||||
LocalDateTime from = parseAdminDatetimeParam("submitted_from", submittedFrom);
|
||||
LocalDateTime to = parseAdminDatetimeParam("submitted_to", submittedTo);
|
||||
log.info("[image-video] 管理端查询图生视频任务 page={} pageSize={} userId={} username={} status={}",
|
||||
safePage, safePageSize, userId, cleanUsername, cleanStatus);
|
||||
log.info("[image-video] 管理端查询图生视频任务 page={} pageSize={} userId={} username={} status={} "
|
||||
+ "companyId={}", safePage, safePageSize, userId, cleanUsername, cleanStatus, companyId);
|
||||
return ApiResponse.success(adminTaskService.page(userId, cleanUsername, cleanStatus,
|
||||
cleanExecuteId, from, to, safePage, safePageSize));
|
||||
cleanExecuteId, from, to, safePage, safePageSize, scoped));
|
||||
}
|
||||
|
||||
@GetMapping("/{taskId}")
|
||||
@Operation(summary = "查询图生视频任务详情", description = "返回单个任务详情(含脱敏后的请求参数与结果 JSON)")
|
||||
public ApiResponse<Map<String, Object>> detail(@PathVariable Long taskId, HttpServletRequest request) {
|
||||
adminAuthSupport.requireAdminOrInternal(request);
|
||||
return ApiResponse.success(adminTaskService.detail(taskId));
|
||||
AdminUserEntity operator = adminAuthSupport.requireAdminOrInternal(request);
|
||||
return ApiResponse.success(adminTaskService.detail(taskId, scopeOf(operator)));
|
||||
}
|
||||
|
||||
@PostMapping("/download-zip")
|
||||
@Operation(summary = "批量打包下载视频", description = "按任务+视频下标批量打包 zip;部分失败通过响应头 X-Archive-Error-Count 提示")
|
||||
public ResponseEntity<StreamingResponseBody> downloadZip(@RequestBody(required = false) Map<String, Object> body,
|
||||
HttpServletRequest request) {
|
||||
adminAuthSupport.requireAdminOrInternal(request);
|
||||
AdminUserEntity operator = adminAuthSupport.requireAdminOrInternal(request);
|
||||
List<VideoSelection> selections = parseSelections(body);
|
||||
ZipBundle bundle = adminTaskService.buildZip(selections);
|
||||
ZipBundle bundle = adminTaskService.buildZip(selections, scopeOf(operator));
|
||||
String filename = "video-tasks-" + LocalDateTime.now().format(ZIP_FILENAME_FORMATTER) + ".zip";
|
||||
StreamingResponseBody stream = outputStream -> writeZipFile(bundle.file(), outputStream);
|
||||
return ResponseEntity.ok()
|
||||
@@ -114,6 +121,15 @@ public class AdminImageVideoTaskController {
|
||||
.body(stream);
|
||||
}
|
||||
|
||||
/**
|
||||
* 按 id 的两个读出口(详情/打包下载)与列表同一把范围,判据都是行主人落不落得进同一批成员集。
|
||||
* <p>它们收不到 tab 的公司 id(tab 不是安全边界),所以 base 与 requestedCompanyId 都传 null:
|
||||
* 超管=不筛(返回 null),非超管/内部令牌=按会话裁剪到本公司成员。</p>
|
||||
*/
|
||||
private List<Long> scopeOf(AdminUserEntity operator) {
|
||||
return dataReadScope.readScope(operator, null).userIdsOr(null);
|
||||
}
|
||||
|
||||
private void writeZipFile(Path file, OutputStream output) throws IOException {
|
||||
try (InputStream input = Files.newInputStream(file)) {
|
||||
input.transferTo(output);
|
||||
|
||||
+9
-1
@@ -35,6 +35,9 @@ public interface ImageVideoAdminTaskMapper {
|
||||
WHERE t.task_type = 'IMAGE_VIDEO_WORKFLOW'
|
||||
AND t.submitted_at >= DATE_SUB(NOW(), INTERVAL 3 DAY)
|
||||
<if test="userId != null"> AND t.user_id = #{userId}</if>
|
||||
<if test="userIds != null"> AND t.user_id IN
|
||||
<foreach collection="userIds" item="uid" open="(" separator="," close=")">#{uid}</foreach>
|
||||
</if>
|
||||
<if test="username != null and username != ''"> AND u.username LIKE CONCAT('%', #{username}, '%')</if>
|
||||
<if test="status != null and status != ''"> AND t.status = #{status}</if>
|
||||
<if test="executeId != null and executeId != ''"> AND t.coze_execute_id LIKE CONCAT('%', #{executeId}, '%')</if>
|
||||
@@ -46,6 +49,7 @@ public interface ImageVideoAdminTaskMapper {
|
||||
""")
|
||||
List<Map<String, Object>> selectAdminTaskPage(
|
||||
@Param("userId") Long userId,
|
||||
@Param("userIds") List<Long> userIds,
|
||||
@Param("username") String username,
|
||||
@Param("status") String status,
|
||||
@Param("executeId") String executeId,
|
||||
@@ -62,6 +66,9 @@ public interface ImageVideoAdminTaskMapper {
|
||||
WHERE t.task_type = 'IMAGE_VIDEO_WORKFLOW'
|
||||
AND t.submitted_at >= DATE_SUB(NOW(), INTERVAL 3 DAY)
|
||||
<if test="userId != null"> AND t.user_id = #{userId}</if>
|
||||
<if test="userIds != null"> AND t.user_id IN
|
||||
<foreach collection="userIds" item="uid" open="(" separator="," close=")">#{uid}</foreach>
|
||||
</if>
|
||||
<if test="username != null and username != ''"> AND u.username LIKE CONCAT('%', #{username}, '%')</if>
|
||||
<if test="status != null and status != ''"> AND t.status = #{status}</if>
|
||||
<if test="executeId != null and executeId != ''"> AND t.coze_execute_id LIKE CONCAT('%', #{executeId}, '%')</if>
|
||||
@@ -71,6 +78,7 @@ public interface ImageVideoAdminTaskMapper {
|
||||
""")
|
||||
Long countAdminTaskPage(
|
||||
@Param("userId") Long userId,
|
||||
@Param("userIds") List<Long> userIds,
|
||||
@Param("username") String username,
|
||||
@Param("status") String status,
|
||||
@Param("executeId") String executeId,
|
||||
@@ -79,7 +87,7 @@ public interface ImageVideoAdminTaskMapper {
|
||||
|
||||
@Select("""
|
||||
<script>
|
||||
SELECT t.id, t.video_urls_json, t.archived_videos_json
|
||||
SELECT t.id, t.user_id, t.video_urls_json, t.archived_videos_json
|
||||
FROM biz_image_video_async_task t
|
||||
WHERE t.task_type = 'IMAGE_VIDEO_WORKFLOW'
|
||||
AND t.submitted_at >= DATE_SUB(NOW(), INTERVAL 3 DAY)
|
||||
|
||||
+41
-6
@@ -70,15 +70,27 @@ public class ImageVideoAdminTaskService {
|
||||
|
||||
/**
|
||||
* 分页查询图生视频工作流任务,返回 {items, total, page, page_size}。
|
||||
*
|
||||
* <p>{@code userIds} 是 controller 经 {@code DataReadScope} 算好的公司成员集:
|
||||
* {@code null} = 不筛(超管没选公司,保持今天的行为),空集 = 这家公司没有成员账号。</p>
|
||||
*/
|
||||
public Map<String, Object> page(Long userId, String username, String status, String executeId,
|
||||
LocalDateTime submittedFrom, LocalDateTime submittedTo,
|
||||
int page, int pageSize) {
|
||||
int page, int pageSize, List<Long> userIds) {
|
||||
if (userIds != null && userIds.isEmpty()) {
|
||||
// 空集 = 这个公司今天没有任何成员账号 ⇒ 空页。绝不退化成"不筛"(那是越权)。
|
||||
Map<String, Object> empty = new LinkedHashMap<>();
|
||||
empty.put("items", List.of());
|
||||
empty.put("total", 0L);
|
||||
empty.put("page", (long) page);
|
||||
empty.put("page_size", (long) pageSize);
|
||||
return empty;
|
||||
}
|
||||
int offset = (page - 1) * pageSize;
|
||||
List<Map<String, Object>> rows = adminTaskMapper.selectAdminTaskPage(
|
||||
userId, username, status, executeId, submittedFrom, submittedTo, pageSize, offset);
|
||||
userId, userIds, username, status, executeId, submittedFrom, submittedTo, pageSize, offset);
|
||||
Long total = adminTaskMapper.countAdminTaskPage(
|
||||
userId, username, status, executeId, submittedFrom, submittedTo);
|
||||
userId, userIds, username, status, executeId, submittedFrom, submittedTo);
|
||||
List<Map<String, Object>> items = new ArrayList<>(rows.size());
|
||||
for (Map<String, Object> row : rows) {
|
||||
items.add(toListItem(row, false));
|
||||
@@ -93,19 +105,27 @@ public class ImageVideoAdminTaskService {
|
||||
|
||||
/**
|
||||
* 单任务详情(含 request/submit_response/result 原始 JSON,request 脱敏)。
|
||||
*
|
||||
* <p>闸门与列表同一把范围(spec §6.1「列表筛了、按 id 照读」= 越权):
|
||||
* {@code userIds} 非 null 而行主人不在里面 ⇒ 403;404 只留给真查不到行。</p>
|
||||
*/
|
||||
public Map<String, Object> detail(Long taskId) {
|
||||
public Map<String, Object> detail(Long taskId, List<Long> userIds) {
|
||||
Map<String, Object> row = adminTaskMapper.selectAdminTaskDetail(taskId);
|
||||
if (row == null) {
|
||||
throw new BusinessException(404, "视频任务不存在或无权访问");
|
||||
throw new BusinessException(404, "视频任务不存在");
|
||||
}
|
||||
assertRowInScope(row, userIds);
|
||||
return Map.of("item", toListItem(row, true));
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量打包下载选中的视频。逐文件远程拉取写入 zip,部分失败时记录错误清单文件。
|
||||
*
|
||||
* <p>范围闸门在打包之前:一次请求里任一条越界就整单 403,绝不并进
|
||||
* {@code X-Archive-Error-Count} 的"部分失败"语义(那是给拉不到文件用的,
|
||||
* 拿它掩盖越权等于把越权报成网络问题)。</p>
|
||||
*/
|
||||
public ZipBundle buildZip(List<VideoSelection> selections) {
|
||||
public ZipBundle buildZip(List<VideoSelection> selections, List<Long> userIds) {
|
||||
List<Long> taskIds = selections.stream()
|
||||
.map(VideoSelection::taskId)
|
||||
.distinct()
|
||||
@@ -113,6 +133,7 @@ public class ImageVideoAdminTaskService {
|
||||
.collect(Collectors.toList());
|
||||
Map<Long, Map<String, Object>> tasks = new HashMap<>();
|
||||
for (Map<String, Object> row : adminTaskMapper.selectAdminZipRows(taskIds)) {
|
||||
assertRowInScope(row, userIds);
|
||||
if (row.get("id") instanceof Number id) {
|
||||
tasks.put(id.longValue(), row);
|
||||
}
|
||||
@@ -187,6 +208,20 @@ public class ImageVideoAdminTaskService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 行主人落不落得进列表那条同样的成员集:{@code userIds == null} 才是"这一页不筛",
|
||||
* 空集与非 null 集合都按"落不进就 403"处理(读不出主人同样算越界,不放行)。
|
||||
*/
|
||||
private void assertRowInScope(Map<String, Object> row, List<Long> userIds) {
|
||||
if (userIds == null) {
|
||||
return;
|
||||
}
|
||||
Object ownerId = row.get("user_id");
|
||||
if (!(ownerId instanceof Number owner) || !userIds.contains(owner.longValue())) {
|
||||
throw new BusinessException(403, "该任务不属于当前公司");
|
||||
}
|
||||
}
|
||||
|
||||
private HttpURLConnection openRemoteConnection(String url) throws IOException {
|
||||
HttpURLConnection connection = (HttpURLConnection) URI.create(url).toURL().openConnection();
|
||||
connection.setConnectTimeout(HTTP_CONNECT_TIMEOUT_MILLIS);
|
||||
|
||||
Reference in New Issue
Block a user