feat(公司隔离): 加 CompanyScopeSupport 与二期实施计划

取不到公司一律抛 BusinessException,不返回 null:null 进实体会被 MyBatis-Plus
跳过该列,落库时由 V145 的列 DEFAULT=1 静默兜住,第二家公司开通后每个漏传都
会归错公司且不报错。同时把 18 号实施计划入库(docs/plans 已不再 gitignore)。
This commit is contained in:
2026-10-04 15:09:05 +08:00
parent c9d5cb6c2d
commit 0311f9fa2a
3 changed files with 1698 additions and 0 deletions
@@ -0,0 +1,49 @@
package com.nanri.aiimage.common.service;
import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.common.mapper.AdminUserMapper;
import com.nanri.aiimage.common.model.entity.AdminUserEntity;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
/**
* 按账号解析归属公司(二期设计文档 §3.1)——数据出口只认这一个入口。
*
* <p>一期"业务表零 DDL"靠 {@code user_id → users.company_id} 派生;本期两张规则库表破例物化了
* {@code company_id}(§2.2),但<b>归属仍然从账号解析</b>,物化列只是把它固定下来、好让唯一键与
* 消费侧的集合查询用得上。</p>
*
* <p>取不到公司一律抛,不返回 null:调用方一旦把 null 传进实体,MyBatis-Plus 会跳过该列,
* 落库时被 V145 的 {@code DEFAULT 1} 静默兜住——那是"归属失真且不报错"的成因(§6.3)。</p>
*/
@Service
@RequiredArgsConstructor
public class CompanyScopeSupport {
private final AdminUserMapper adminUserMapper;
public Long requireCompanyId(Long userId) {
if (userId == null || userId <= 0) {
throw new BusinessException("归属账号不能为空");
}
return requireCompanyId(adminUserMapper.selectById(userId));
}
public Long requireCompanyId(AdminUserEntity user) {
if (user == null) {
throw new BusinessException("归属账号不存在");
}
Long companyId = user.getCompanyId();
if (companyId == null || companyId <= 0) {
throw new BusinessException(403, "当前账号没有所属公司,无法按公司读写数据");
}
return companyId;
}
/** 行级归属闸门:跨公司写/删的第一道,消息由调用方给(前端按它显示,别在这里编文案)。 */
public void assertSameCompany(Long operatorCompanyId, Long rowCompanyId, String deniedMessage) {
if (rowCompanyId == null || !rowCompanyId.equals(operatorCompanyId)) {
throw new BusinessException(403, deniedMessage);
}
}
}
@@ -0,0 +1,76 @@
package com.nanri.aiimage.common.service;
import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.common.mapper.AdminUserMapper;
import com.nanri.aiimage.common.model.entity.AdminUserEntity;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.when;
/**
* 二期数据隔离的唯一取公司出口(设计文档 §3.1)。
*
* <p>这里最重要的不是"能取到公司",而是<b>取不到时必须炸</b>:漏传 company_id 一旦退回 null,
* 下游就落进 V145 的列 DEFAULT=1,第二家公司开通后每个漏传都会静默归到厦门米椿花(§6.3 / §11.2)。</p>
*/
@ExtendWith(MockitoExtension.class)
class CompanyScopeSupportTest {
@Mock AdminUserMapper adminUserMapper;
@InjectMocks CompanyScopeSupport companyScope;
@Test
void resolvesCompanyIdFromUserIdThroughUsersTable() {
AdminUserEntity user = new AdminUserEntity();
user.setId(7L);
user.setCompanyId(3L);
when(adminUserMapper.selectById(7L)).thenReturn(user);
assertThat(companyScope.requireCompanyId(7L)).isEqualTo(3L);
}
@Test
void nullUserIdFailsFastInsteadOfReturningNull() {
assertThatThrownBy(() -> companyScope.requireCompanyId((Long) null))
.isInstanceOf(BusinessException.class)
.hasMessageContaining("归属账号");
}
@Test
void unknownUserFailsFast() {
when(adminUserMapper.selectById(99L)).thenReturn(null);
assertThatThrownBy(() -> companyScope.requireCompanyId(99L))
.isInstanceOf(BusinessException.class)
.hasMessageContaining("归属账号不存在");
}
/** V144 之后 users.company_id 已无 DEFAULT:手工 SQL 或旧 dump 重建都可能留下 NULL。 */
@Test
void userWithoutCompanyFailsFastWith403() {
AdminUserEntity user = new AdminUserEntity();
user.setId(8L);
user.setCompanyId(null);
when(adminUserMapper.selectById(8L)).thenReturn(user);
assertThatThrownBy(() -> companyScope.requireCompanyId(8L))
.isInstanceOf(BusinessException.class)
.hasMessageContaining("没有所属公司");
}
@Test
void assertSameCompanyRejectsForeignAndMissingRowCompany() {
companyScope.assertSameCompany(1L, 1L, "无权操作其他公司的数据");
assertThatThrownBy(() -> companyScope.assertSameCompany(1L, 2L, "无权操作其他公司的数据"))
.isInstanceOf(BusinessException.class).hasMessageContaining("其他公司");
assertThatThrownBy(() -> companyScope.assertSameCompany(1L, null, "无权操作其他公司的数据"))
.isInstanceOf(BusinessException.class).hasMessageContaining("其他公司");
}
}