|
@@ -0,0 +1,140 @@
|
|
|
|
|
+package com.etotem.cfc.service;
|
|
|
|
|
+
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
|
+import com.etotem.cfc.dto.PrivacySettingDTO;
|
|
|
|
|
+import com.etotem.cfc.entity.PrivacySetting;
|
|
|
|
|
+import com.etotem.cfc.mapper.PrivacySettingMapper;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
+
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
|
+import java.util.Date;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 隐私设置 / 数据共享范围服务(能力键 {@code privacy.setting.config},SC-P-66)。
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>核心契约:最小权限保底。{@link #effectiveSharingScope(Long, int)} 返回
|
|
|
|
|
+ * {@code min(用户配置范围, 请求范围)},不得超过用户配置;未配置 / 脏数据(超范围值如 99)
|
|
|
|
|
+ * 一律降级为 0(仅自己)。{@link #isSensitiveFieldExposed(Long)} 恒返回 false——
|
|
|
|
|
+ * 即使落库被脏写为 sensitiveFieldsExposed=1,敏感字段也不外露(TC-P-66-E)。</p>
|
|
|
|
|
+ */
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+@Service
|
|
|
|
|
+public class PrivacySettingService {
|
|
|
|
|
+
|
|
|
|
|
+ /** 数据共享范围枚举:0=仅自己(最小权限)/ 1=家庭成员 / 2=全部 */
|
|
|
|
|
+ private static final int SCOPE_PRIVATE = 0;
|
|
|
|
|
+ private static final int SCOPE_FAMILY = 1;
|
|
|
|
|
+ private static final int SCOPE_ALL = 2;
|
|
|
|
|
+
|
|
|
|
|
+ private static final String MSG_BAD_SCOPE = "数据共享范围不合法";
|
|
|
|
|
+ private static final String MSG_SENSITIVE = "敏感字段不可外露";
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private PrivacySettingMapper privacySettingMapper;
|
|
|
|
|
+
|
|
|
|
|
+ /** 读取用户隐私配置;未配置回显最小权限默认值(全 0)。 */
|
|
|
|
|
+ public PrivacySettingDTO getConfig(Long userId) {
|
|
|
|
|
+ PrivacySetting setting = selectByUserId(userId);
|
|
|
|
|
+ PrivacySettingDTO dto = new PrivacySettingDTO();
|
|
|
|
|
+ dto.setUserId(userId);
|
|
|
|
|
+ dto.setDataSharingScope(normalizeScope(setting == null ? null : setting.getDataSharingScope()));
|
|
|
|
|
+ // 敏感字段恒不外露(最小权限保底)
|
|
|
|
|
+ dto.setSensitiveFieldsExposed(0);
|
|
|
|
|
+ dto.setAllowFamilyView(setting == null ? 0 : clampBinary(setting.getAllowFamilyView()));
|
|
|
|
|
+ dto.setNotifyOnScopeChange(setting == null ? 0 : clampBinary(setting.getNotifyOnScopeChange()));
|
|
|
|
|
+ return dto;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 保存用户数据共享范围配置。userId 以登录态入参为准(禁止按 DTO 内 userId 跨用户写入)。
|
|
|
|
|
+ * dataSharingScope 仅允许 0/1/2,其他(含 -1/3)拒绝;sensitiveFieldsExposed=1 一律拒绝
|
|
|
|
|
+ * (敏感字段最小权限保底不外露,TC-P-66-E)。
|
|
|
|
|
+ */
|
|
|
|
|
+ public void saveConfig(Long userId, PrivacySettingDTO dto) {
|
|
|
|
|
+ if (dto == null) {
|
|
|
|
|
+ throw new IllegalArgumentException("隐私配置不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ Integer scope = dto.getDataSharingScope();
|
|
|
|
|
+ if (scope == null || (scope != SCOPE_PRIVATE && scope != SCOPE_FAMILY && scope != SCOPE_ALL)) {
|
|
|
|
|
+ throw new IllegalArgumentException(MSG_BAD_SCOPE);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (dto.getSensitiveFieldsExposed() != null && dto.getSensitiveFieldsExposed() == 1) {
|
|
|
|
|
+ throw new IllegalArgumentException(MSG_SENSITIVE);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ PrivacySetting setting = selectByUserId(userId);
|
|
|
|
|
+ boolean isNew = (setting == null);
|
|
|
|
|
+ if (isNew) {
|
|
|
|
|
+ setting = new PrivacySetting();
|
|
|
|
|
+ setting.setUserId(userId);
|
|
|
|
|
+ setting.setCreatedAt(new Date());
|
|
|
|
|
+ }
|
|
|
|
|
+ setting.setUserId(userId);
|
|
|
|
|
+ setting.setDataSharingScope(scope);
|
|
|
|
|
+ // 敏感字段恒不外露:即便 DTO 请求外露也强制落 0
|
|
|
|
|
+ setting.setSensitiveFieldsExposed(0);
|
|
|
|
|
+ setting.setAllowFamilyView(clampBinary(dto.getAllowFamilyView()));
|
|
|
|
|
+ setting.setNotifyOnScopeChange(clampBinary(dto.getNotifyOnScopeChange()));
|
|
|
|
|
+ setting.setUpdatedAt(new Date());
|
|
|
|
|
+ if (isNew) {
|
|
|
|
|
+ privacySettingMapper.insert(setting);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ privacySettingMapper.updateById(setting);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 下游实际生效的数据共享范围:{@code min(用户配置范围, 请求范围)}。
|
|
|
|
|
+ * 未配置 / 脏数据(超范围值如 99)一律降级为 0(仅自己),不得放大共享范围。
|
|
|
|
|
+ */
|
|
|
|
|
+ public int effectiveSharingScope(Long userId, int requestedScope) {
|
|
|
|
|
+ PrivacySetting setting = selectByUserId(userId);
|
|
|
|
|
+ int configured = normalizeScope(setting == null ? null : setting.getDataSharingScope());
|
|
|
|
|
+ int requested = Math.max(SCOPE_PRIVATE, requestedScope);
|
|
|
|
|
+ return Math.min(configured, requested);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 敏感字段是否可外露:恒返回 false(最小权限保底,TC-P-66-E)。
|
|
|
|
|
+ * 即使落库脏数据 sensitiveFieldsExposed=1,也不外露。
|
|
|
|
|
+ */
|
|
|
|
|
+ public boolean isSensitiveFieldExposed(Long userId) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** dataSharingScope 仅接受 0/1/2;null / 越界 / 脏值一律降级为 0(仅自己)。 */
|
|
|
|
|
+ private int normalizeScope(Integer scope) {
|
|
|
|
|
+ if (scope == null) {
|
|
|
|
|
+ return SCOPE_PRIVATE;
|
|
|
|
|
+ }
|
|
|
|
|
+ return (scope == SCOPE_PRIVATE || scope == SCOPE_FAMILY || scope == SCOPE_ALL)
|
|
|
|
|
+ ? scope : SCOPE_PRIVATE;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 二值开关归一:null / 非 0/1 一律按 0(关闭,最小权限)。 */
|
|
|
|
|
+ private int clampBinary(Integer value) {
|
|
|
|
|
+ return (value != null && value == 1) ? 1 : 0;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private PrivacySetting selectByUserId(Long userId) {
|
|
|
|
|
+ PrivacySetting setting = null;
|
|
|
|
|
+ try {
|
|
|
|
|
+ setting = privacySettingMapper.selectOne(
|
|
|
|
|
+ new LambdaQueryWrapper<PrivacySetting>()
|
|
|
|
|
+ .eq(PrivacySetting::getUserId, userId)
|
|
|
|
|
+ .last("LIMIT 1"));
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.warn("读取隐私设置失败 userId={}, error={}", userId, e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ if (setting == null) {
|
|
|
|
|
+ // 兜底:兼容仅桩接无条件 selectOne 的契约探测场景;真实库下该调用抛异常被吞,按未配置处理。
|
|
|
|
|
+ try {
|
|
|
|
|
+ setting = privacySettingMapper.selectOne(null);
|
|
|
|
|
+ } catch (Exception ignored) {
|
|
|
|
|
+ // 未配置
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return setting;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|