fix(invariants): Phase 08 复验 —— 修复 INV-004 搜索/详情绕过 Block + matches.source 取值越界
复验(KEEP-VERIFY)在既有 Phase 08 实现中发现 3 处真实缺陷并修复:
D1(严重)DiscoverService.searchUsers 不含查看者身份,查询条件既无排除自己也无任何 Block 条件,被拉黑者仍可被搜到并点开 —— INV-004「搜索不可见」整体失效。改为接收 viewerUserId,排除自己,并用新增的 UserRelationMapper.findBlockedUserIdsBothWays(UNION 双向)一次取回后 notIn 批量排除,避免 N+1。
D2 详情路径(DiscoverService / UserProfileService)均无 Block 判断,知道 userId 即可读到被拉黑者资料。两条路径统一加双向判断并返回 null,由 Controller 转 404(不用「已隐藏」以免被用来探测存在);UserProfileService 中该判断排在隐私设置之前,拉黑不能被对方「允许搜索可见」覆盖。
D3(潜伏,仅 PostgreSQL 暴露)RelationshipService.like() 把关系类型值 "liked" 当 match source 写入 matches.source,违反 V003:91 的 chk_match_source。测试库 schema-h2.sql 该列是裸 VARCHAR(32)、无约束,故长期静默通过。改传 MATCH_SOURCE_USER_LIKE。canonical 路径 RelationService.like 传值本就正确,线上未触发。
测试保真度对齐:schema-h2.sql 补 chk_match_source(与 V003 逐字对齐)与 user_profiles 表(此前测试库无此表,资料查询无法端到端测试)。
新增 DiscoverBlockInvisibilityIntegrationTest(9 用例,@SpringBootTest 真实 H2 + 真实 SQL),覆盖正向/反向拉黑、对称性、排除自己、详情 404 与查自己不受限。P0FixVerificationTest 补 source 回归守卫。DiscoverServiceTest 同步构造签名。
负向验证:临时还原 D3 修复后,4 个用例因 CHK_MATCH_SOURCE 约束违反报错,证明守卫有效。
证据:后端全量 308 tests / 0 failures / 0 errors / 3 skipped(基线 299);UI 契约 13/13 GREEN。报告 docs/Phase-08-V11-Recovery-Completion-Report-2026-09-16.md。
同步 STATUS.md / JIRA_BACKLOG.md(EPIC-P08-V11 置 ✅)/ 客户需求基线计划 Phase 08 复选框;首轮报告加状态注记(其「Known Issues 无」已被推翻)。.gitignore 忽略 .workbuddy/。
This commit is contained in:
+4
-2
@@ -48,18 +48,20 @@ public class DiscoverController {
|
||||
if (maritalStatus != null) tempFilter.put("maritalStatus", maritalStatus);
|
||||
if (educationCode != null) tempFilter.put("educationCode", educationCode);
|
||||
|
||||
return ApiResponse.ok(discoverService.searchUsers(myGender, tempFilter, page, pageSize));
|
||||
return ApiResponse.ok(discoverService.searchUsers(userId, myGender, tempFilter, page, pageSize));
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看用户详情
|
||||
* GET /api/v1/discover/users/{userId}
|
||||
*
|
||||
* INV-004:双向拉黑时 service 返回 null,这里按 404 处理(不泄露"存在但不可见")。
|
||||
*/
|
||||
@GetMapping("/users/{userId}")
|
||||
public ApiResponse<Map<String, Object>> getProfile(
|
||||
@AuthenticationPrincipal UserPrincipal principal,
|
||||
@PathVariable String userId) {
|
||||
Map<String, Object> profile = discoverService.getPublicProfile(userId);
|
||||
Map<String, Object> profile = discoverService.getPublicProfile(principal.id(), userId);
|
||||
if (profile == null) {
|
||||
return ApiResponse.error(com.danyang.match.platform.shared.enums.ApiResponseCode.NOT_FOUND, "用户不存在");
|
||||
}
|
||||
|
||||
+28
-3
@@ -1,7 +1,7 @@
|
||||
package com.danyang.match.platform.discover.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.danyang.match.platform.match.dto.RecommendationCandidateDTO;
|
||||
import com.danyang.match.platform.match.mapper.UserRelationMapper;
|
||||
import com.danyang.match.platform.profile.entity.UserProfile;
|
||||
import com.danyang.match.platform.profile.mapper.UserProfileMapper;
|
||||
import com.danyang.match.platform.shared.constant.Constants;
|
||||
@@ -14,29 +14,45 @@ import java.util.stream.Collectors;
|
||||
/**
|
||||
* 找对象搜索服务
|
||||
* 支持临时筛选(不修改用户择偶条件)
|
||||
*
|
||||
* INV-004(Block 优先级最高)在本模块的两条检索路径上都必须生效:
|
||||
* 列表搜索排除双向拉黑者,详情查看对双向拉黑者返回不存在。
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class DiscoverService {
|
||||
|
||||
private final UserProfileMapper userProfileMapper;
|
||||
private final UserRelationMapper relationMapper;
|
||||
|
||||
/**
|
||||
* 搜索用户列表(临时筛选)
|
||||
*
|
||||
* @param viewerUserId 当前登录用户(认证主体)—— 用于排除自己与双向拉黑者
|
||||
* @param myGender 当前用户性别(用于过滤相反性别)
|
||||
* @param tempFilter 临时筛选条件(不影响用户择偶条件)
|
||||
* @param page 页码
|
||||
* @param pageSize 每页数量
|
||||
* @return 搜索结果
|
||||
*/
|
||||
public Map<String, Object> searchUsers(String myGender, Map<String, Object> tempFilter,
|
||||
public Map<String, Object> searchUsers(String viewerUserId, String myGender, Map<String, Object> tempFilter,
|
||||
int page, int pageSize) {
|
||||
LambdaQueryWrapper<UserProfile> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.ne(UserProfile::getGender, myGender)
|
||||
.eq(UserProfile::getProfileStatus, Constants.PROFILE_STATUS_APPROVED)
|
||||
.isNull(UserProfile::getDeletedAt);
|
||||
|
||||
// INV-004 搜索不可见:排除自己 + 双向拉黑者。
|
||||
// 用一次 UNION 查询取回全部拉黑 id 后批量 NOT IN,避免逐条 countBlockedEitherWay 造成 N+1。
|
||||
// viewerUserId 为 null 只出现在非法/未认证调用路径上,此时不做排除(上层鉴权已拒绝)。
|
||||
if (viewerUserId != null) {
|
||||
wrapper.ne(UserProfile::getUserId, viewerUserId);
|
||||
List<String> blockedUserIds = relationMapper.findBlockedUserIdsBothWays(viewerUserId);
|
||||
if (blockedUserIds != null && !blockedUserIds.isEmpty()) {
|
||||
wrapper.notIn(UserProfile::getUserId, blockedUserIds);
|
||||
}
|
||||
}
|
||||
|
||||
// 应用临时筛选条件
|
||||
if (tempFilter.containsKey("minAge")) {
|
||||
Integer minAge = (Integer) tempFilter.get("minAge");
|
||||
@@ -88,8 +104,17 @@ public class DiscoverService {
|
||||
|
||||
/**
|
||||
* 获取单个用户公开详情
|
||||
*
|
||||
* @param viewerUserId 查看者(认证主体)
|
||||
* @param userId 被查看者
|
||||
* @return 双向拉黑时返回 null,上层据此返回 404 —— 不返回"已隐藏"这类可被用来探测存在的响应
|
||||
*/
|
||||
public Map<String, Object> getPublicProfile(String userId) {
|
||||
public Map<String, Object> getPublicProfile(String viewerUserId, String userId) {
|
||||
// INV-004 搜索不可见:任一方向拉黑后互相不可见;查看自己的资料不受此限制
|
||||
if (viewerUserId != null && !viewerUserId.equals(userId)
|
||||
&& relationMapper.countBlockedEitherWay(viewerUserId, userId) > 0) {
|
||||
return null;
|
||||
}
|
||||
return userProfileMapper.findByUserId(userId)
|
||||
.map(profile -> {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
|
||||
@@ -50,6 +50,17 @@ public interface UserRelationMapper extends BaseMapper<UserRelation> {
|
||||
@Select("SELECT * FROM user_relations WHERE user_id = #{userId} AND relation_type = 'passed' AND deleted_at IS NULL ORDER BY created_at DESC LIMIT 100")
|
||||
List<UserRelation> findPassedByUser(String userId);
|
||||
|
||||
/**
|
||||
* 与我有拉黑关系的全部用户 id(双向 UNION 去重)—— 供列表/搜索批量排除。
|
||||
*
|
||||
* INV-004:Block 优先级最高,拉黑后「搜索不可见」是双向的:
|
||||
* 我拉黑的人看不到我,拉黑我的人也看不到我。单方向的 findBlockedByUser 不足以支撑该语义。
|
||||
*/
|
||||
@Select("SELECT target_user_id FROM user_relations WHERE user_id = #{userId} AND relation_type = 'blocked' AND deleted_at IS NULL "
|
||||
+ "UNION "
|
||||
+ "SELECT user_id FROM user_relations WHERE target_user_id = #{userId} AND relation_type = 'blocked' AND deleted_at IS NULL")
|
||||
List<String> findBlockedUserIdsBothWays(@Param("userId") String userId);
|
||||
|
||||
/** 插入关系(依赖唯一约束去重,返回 0 表示已存在) */
|
||||
@org.apache.ibatis.annotations.Insert("INSERT INTO user_relations (id, user_id, target_user_id, relation_type) VALUES (#{id}, #{userId}, #{targetUserId}, #{relationType})")
|
||||
int insertIgnore(UserRelation relation);
|
||||
|
||||
+9
@@ -8,6 +8,7 @@ import com.danyang.match.platform.profile.entity.UserPrivacySettings;
|
||||
import com.danyang.match.platform.profile.entity.UserProfile;
|
||||
import com.danyang.match.platform.profile.mapper.UserPreferenceMapper;
|
||||
import com.danyang.match.platform.profile.mapper.UserPrivacySettingsMapper;
|
||||
import com.danyang.match.platform.match.mapper.UserRelationMapper;
|
||||
import com.danyang.match.platform.profile.mapper.UserProfileMapper;
|
||||
import com.danyang.match.platform.shared.constant.Constants;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@@ -25,6 +26,7 @@ public class UserProfileService {
|
||||
private final UserProfileMapper userProfileMapper;
|
||||
private final UserPreferenceMapper userPreferenceMapper;
|
||||
private final UserPrivacySettingsMapper privacySettingsMapper;
|
||||
private final UserRelationMapper relationMapper;
|
||||
|
||||
/**
|
||||
* 获取自己完整资料(含隐私设置)
|
||||
@@ -141,6 +143,13 @@ public class UserProfileService {
|
||||
UserProfile profile = userProfileMapper.findByUserId(targetUserId).orElse(null);
|
||||
if (profile == null) return null;
|
||||
|
||||
// INV-004:Block 优先级最高 —— 任一方向拉黑后互相不可见。
|
||||
// 必须排在隐私设置之前:拉黑是用户的显式阻断,不能被对方"允许搜索可见"的设置覆盖。
|
||||
if (viewerId != null && !viewerId.equals(targetUserId)
|
||||
&& relationMapper.countBlockedEitherWay(viewerId, targetUserId) > 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 隐私检查:若对方关闭了搜索可见性,则不展示
|
||||
UserPrivacySettings privacy = privacySettingsMapper.findByUserId(targetUserId).orElse(null);
|
||||
if (privacy != null && !privacy.getVisibleInSearch()) {
|
||||
|
||||
+4
-1
@@ -76,7 +76,10 @@ public class RelationshipService {
|
||||
result.put("mutual", mutual);
|
||||
|
||||
if (mutual) {
|
||||
createMatch(userId, targetUserId, Constants.RELATION_TYPE_LIKED);
|
||||
// matches.source 的取值域由 V003 的 chk_match_source 约束限定为
|
||||
// user_like / activity_mutual / matchmaker。此处必须传 MATCH_SOURCE_USER_LIKE;
|
||||
// 早期误传 RELATION_TYPE_LIKED("liked") 在 H2(测试库无该约束)能过,在 PostgreSQL 上必违反约束。
|
||||
createMatch(userId, targetUserId, Constants.MATCH_SOURCE_USER_LIKE);
|
||||
result.put("matchCreated", true);
|
||||
log.info("双向Match创建: userId={}, targetUserId={}", userId, targetUserId);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user