feat(公司tab): 日志管理列表按 uid 成员集筛,麦象行走 OR 不被筛掉

生产只读取证(2026-10-05 15:1x,主机 A 业务库只读):device_log_file COUNT(*)=1028,
GROUP BY source = client 831 / maixiang 197 ⇒ 客户端占多数 ⇒ 这页留在批次 1、照声明
companyScoped(若麦象占多数,公司 tab 只会筛掉小半边,就该把这页摘出清单)。

22 行 source='client' 且 uid IS NULL 的匿名客户端日志的处置:选了公司后接受它们从
每一个 tab 里消失(条件定成 (source='maixiang' OR uid IN (成员集)),不加 uid IS NULL
那条腿)。理由是那 197 行麦象能进每一个 tab,是因为它是平台自己的采集机(基础设施),
而这 22 行只是「没登录的桌面端」——一家没有归属的行在每一家各冒充一份,既错归属也让
「这家有多少日志」这个数失去意义;tab 只是超管的视图过滤器(这页 9 个端点全在
requireSuperAdmin 之后,无非超管调用方),全量仍可由不带 company_id 的请求拿到
(readScope(operator, null) ⇒ unfiltered ⇒ memberUids=null ⇒ 一条条件都不叠)。
代价与定案写进 DeviceLogServiceTest 的 @DisplayName
「定案:那 22 行 source=client 且 uid IS NULL 的匿名客户端日志——选了公司就接受它们消失」。

接线口径:controller 收 @RequestParam(name = "company_id", required = false) 并在 handler
体内算 dataReadScope.readScope(...).userIdsOr(null) 当实参交给 service(G2 断言 C 认的形状);
条件加在 wrapperBuilder 闭包内部,selectCount 与 selectList 共用同一把尺子;空成员集退化
成 eq(source,'maixiang') 而不是不叠条件;content/download/delete/devices 与三个写端点刻意
不收 company_id,由两支 SourceScan 用例钉住。
This commit is contained in:
2026-10-05 17:20:57 +08:00
parent 66f9178557
commit c772a07fbe
6 changed files with 233 additions and 15 deletions
@@ -4,6 +4,7 @@ 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.security.AdminAuthSupport;
import com.nanri.aiimage.common.service.DataReadScope;
import com.nanri.aiimage.modules.devicelog.model.entity.DeviceLogConfigEntity;
import com.nanri.aiimage.modules.devicelog.model.entity.DeviceLogFileEntity;
import com.nanri.aiimage.modules.devicelog.model.vo.DeviceLogContentVo;
@@ -48,11 +49,14 @@ public class AdminDeviceLogController {
private final DeviceLogService deviceLogService;
private final DeviceLogConfigService deviceLogConfigService;
private final AdminAuthSupport adminAuthSupport;
/** 公司 tab 的唯一读范围出口(spec §7):只有 {@link #files} 用它,理由见 {@link #requireSuperAdmin}。 */
private final DataReadScope dataReadScope;
@GetMapping("/files")
@Operation(summary = "日志文件分页列表",
description = "source=client/maixiang;uid 按上报用户精确过滤(未登录/采集机的行 uid 为空,不匹配任何 uid);"
+ "日期为闭区间(早于保留窗口自动收紧)。")
+ "日期为闭区间(早于保留窗口自动收紧);"
+ "company_id 只收窄到该公司成员的 uid,麦象采集机的行(uid 恒空)照常出现,不传=不筛。")
public ApiResponse<DeviceLogPageVo> files(
HttpServletRequest request,
@Parameter(description = "来源") @RequestParam(required = false) String source,
@@ -62,9 +66,14 @@ public class AdminDeviceLogController {
@Parameter(description = "结束日期(含)") @RequestParam(required = false)
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate,
@RequestParam(defaultValue = "1") Long page,
@RequestParam(defaultValue = "20") Long pageSize) {
requireSuperAdmin(request);
return ApiResponse.success(deviceLogService.page(source, uid, startDate, endDate, page, pageSize));
@RequestParam(defaultValue = "20") Long pageSize,
@Parameter(description = "公司 ID(超管 tab;不传=不筛)")
@RequestParam(name = "company_id", required = false) Long companyId) {
AdminUserEntity operator = requireSuperAdmin(request);
// 只有列表按公司收窄:成员集为空 = 这家一个账号都没有,service 那侧退化成「只看麦象」。
List<Long> memberUids = dataReadScope.readScope(operator, companyId).userIdsOr(null);
return ApiResponse.success(
deviceLogService.page(source, uid, startDate, endDate, page, pageSize, memberUids));
}
@GetMapping("/content")
@@ -148,13 +157,18 @@ public class AdminDeviceLogController {
/**
* 日志可能含账号/代理等敏感信息,这里比常规后台更严:仅超管(requireAdmin 不含)。
*
* <p>返回操作者是为了让唯一的列表端点算公司成员集;其余端点按 fileId / 配置主键工作,
* 不收 company_id(这页没有超管之外的调用方,按 id 再判一次公司只会误伤 uid 为空的麦象日志)。
* 需要把范围补进按 id 的读出口时,照 {@code AdminImageVideoTaskController} 的 scopeOf 那一样写。</p>
*/
private void requireSuperAdmin(HttpServletRequest request) {
private AdminUserEntity requireSuperAdmin(HttpServletRequest request) {
AdminUserEntity user = adminAuthSupport.requireAdmin(request);
if (!"super_admin".equals(adminAuthSupport.currentRole(user))) {
log.warn("[device-log] 非超管访问日志管理被拒 userId={} username={}",
user.getId(), user.getUsername());
throw new BusinessException(403, "仅超级管理员可访问日志管理");
}
return user;
}
}
@@ -159,8 +159,15 @@ public class DeviceLogService {
// ---------------------------------------------------------------- 查询
/**
* 列表查询。
*
* @param memberUids 公司 tab 收窄到的成员账号集(由 {@code DataReadScope.readScope} 在 controller
* 里算好传进来,service 不自己判权限)。{@code null} = 不筛(超管没选公司,行为与
* 上线前逐字一致);非 null 的空集 = 这家一个成员都没有,只剩麦象采集机的基础设施日志。
*/
public DeviceLogPageVo page(String source, Long uid, LocalDate startDate, LocalDate endDate,
Long pageParam, Long pageSizeParam) {
Long pageParam, Long pageSizeParam, List<Long> memberUids) {
int retentionDays = properties.retentionDaysOrDefault();
LocalDate minDate = LocalDate.now().minusDays(retentionDays - 1L);
LocalDate from = startDate == null || startDate.isBefore(minDate) ? minDate : startDate;
@@ -176,6 +183,18 @@ public class DeviceLogService {
if (uid != null) {
qw.eq(DeviceLogFileEntity::getUid, uid);
}
// memberUids:超管选了公司才非 null。麦象上报的行 uid 恒为 NULL,
// 叠等值会把跟价机的日志从每个 tab 里抹掉(spec §9.2),所以它必须走 OR 那半边。
// 必须加在这个闭包里:selectCount 与 selectList 共用这一个 Function,加在外面任何一侧
// 都会得到"total 与行集不同宽 = 分页错位",而且不报错。
if (memberUids != null) {
if (memberUids.isEmpty()) {
qw.eq(DeviceLogFileEntity::getSource, "maixiang");
} else {
qw.and(w -> w.eq(DeviceLogFileEntity::getSource, "maixiang")
.or(o -> o.in(DeviceLogFileEntity::getUid, memberUids)));
}
}
qw.ge(DeviceLogFileEntity::getLogDate, from);
if (endDate != null) {
qw.le(DeviceLogFileEntity::getLogDate, endDate);
@@ -204,8 +223,9 @@ public class DeviceLogService {
vo.setPage(safePage);
vo.setPageSize(safeSize);
vo.setRetentionDays(retentionDays);
log.info("[device-log] 列表查询 source={} uid={} 起={} 止={} page={} size={} 命中={}",
source, uid, from, endDate, safePage, safeSize, total);
log.info("[device-log] 列表查询 source={} uid={} 起={} 止={} page={} size={} 成员集={} 命中={}",
source, uid, from, endDate, safePage, safeSize,
memberUids == null ? "不筛" : memberUids.size(), total);
return vo;
}