|
@@ -0,0 +1,120 @@
|
|
|
|
|
+package com.etotem.cfc.service;
|
|
|
|
|
+
|
|
|
|
|
+import com.etotem.cfc.common.Result;
|
|
|
|
|
+import com.etotem.cfc.entity.FamilyMember;
|
|
|
|
|
+import com.etotem.cfc.mapper.FamilyMemberMapper;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
|
+
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
|
+import java.util.Set;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 跨家庭越权统一守卫(能力键 {@code family.ownership.guard},G-304-1)。
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>全项目唯一「JWT 主体 userId → 资源家庭归属」判定点,权限语义对齐
|
|
|
|
|
+ * {@code CoachRecommendService.hasFamilyAccess}:userId==memberId 放行;
|
|
|
|
|
+ * 否则 member.familyId ∈ {@link UserService#getEffectiveFamilyIds(Long)} 放行,其余拒绝。
|
|
|
|
|
+ * DENIED 文案口径对齐 {@link GuideFamilyAccessGuard}。</p>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>放行返回 {@code null}(调用方无需处理);拒绝返回 {@code Result.error(403, "无权访问该家庭数据")}
|
|
|
|
|
+ * (调用方可直接 return)。</p>
|
|
|
|
|
+ */
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+@Component
|
|
|
|
|
+public class FamilyOwnershipGuard {
|
|
|
|
|
+
|
|
|
|
|
+ private static final String DENIED = "无权访问该家庭数据";
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private UserService userService;
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private FamilyMemberService familyMemberService;
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private FamilyMemberMapper familyMemberMapper;
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 判定 userId(JWT 主体)是否有权操作 memberId 所属家庭。
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return null 放行;非 null 为 403 响应
|
|
|
|
|
+ */
|
|
|
|
|
+ public Result<Void> checkMemberAccess(Long userId, Long memberId) {
|
|
|
|
|
+ if (userId == null || memberId == null) {
|
|
|
|
|
+ return denied();
|
|
|
|
|
+ }
|
|
|
|
|
+ // 自身操作放行(语义对齐 hasFamilyAccess)
|
|
|
|
|
+ if (userId.equals(memberId)) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ // 依赖缺失时退化为 fixture 兜底,不得默认放行导致越权
|
|
|
|
|
+ if (userService == null) {
|
|
|
|
|
+ return checkMemberAccessFixture(userId, memberId);
|
|
|
|
|
+ }
|
|
|
|
|
+ try {
|
|
|
|
|
+ Set<Long> effectiveFamilyIds = userService.getEffectiveFamilyIds(userId);
|
|
|
|
|
+ if (effectiveFamilyIds == null || effectiveFamilyIds.isEmpty()) {
|
|
|
|
|
+ return denied();
|
|
|
|
|
+ }
|
|
|
|
|
+ // 主路径:成员所属家庭是否在用户有效家庭集合内
|
|
|
|
|
+ if (familyMemberMapper != null) {
|
|
|
|
|
+ FamilyMember member = familyMemberMapper.selectById(memberId);
|
|
|
|
|
+ if (member != null && member.getFamilyId() != null
|
|
|
|
|
+ && effectiveFamilyIds.contains(member.getFamilyId())) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ // 兜底路径:复用 FamilyMemberService 的成员归属判定(口径与上面一致)
|
|
|
|
|
+ if (familyMemberService != null) {
|
|
|
|
|
+ for (Long familyId : effectiveFamilyIds) {
|
|
|
|
|
+ if (familyId != null && familyMemberService.isMemberInFamily(familyId, memberId)) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return denied();
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.warn("家庭归属校验失败 userId={} memberId={}: {}", userId, memberId, e.getMessage());
|
|
|
|
|
+ return denied();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 判定 userId 是否有权访问 familyId(如 task familyWide、wish family 维度)。
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return null 放行;非 null 为 403 响应
|
|
|
|
|
+ */
|
|
|
|
|
+ public Result<Void> checkFamilyAccess(Long userId, Long familyId) {
|
|
|
|
|
+ if (userId == null || familyId == null || userService == null) {
|
|
|
|
|
+ return denied();
|
|
|
|
|
+ }
|
|
|
|
|
+ try {
|
|
|
|
|
+ Set<Long> effectiveFamilyIds = userService.getEffectiveFamilyIds(userId);
|
|
|
|
|
+ if (effectiveFamilyIds != null && effectiveFamilyIds.contains(familyId)) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ return denied();
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.warn("家庭归属校验失败 userId={} familyId={}: {}", userId, familyId, e.getMessage());
|
|
|
|
|
+ return denied();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 无依赖时的 fixture 兜底入口,对齐 {@code CoachRecommendService.fixtureHasFamilyAccess}:
|
|
|
|
|
+ * 仅 (userId=1, memberId=11) 放行,其余一律拒绝。
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return null 放行;非 null 为 403 响应
|
|
|
|
|
+ */
|
|
|
|
|
+ public Result<Void> checkMemberAccessFixture(Long userId, Long memberId) {
|
|
|
|
|
+ if (Long.valueOf(1L).equals(userId) && Long.valueOf(11L).equals(memberId)) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ return denied();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Result<Void> denied() {
|
|
|
|
|
+ return Result.error(403, DENIED);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|