fix(多公司): 站点超管不替别家公司建下级账号,admin 候选与首任管理员都收在本公司

- AdminUserService:建号前两道不变式——ensureSubSuperAccountStaysWithOperatorCompany(超管只能往别家开"那家的公司超管",往下的组长/员工一律 403)+ ensureCreatorSharesCompany 提成执行体(created_by_id 与 company_id 不得分属两家,超管是唯一豁免),建号与移人共用一份,避免"另一侧照样落库"
- 列表侧 admin 候选按 companyIdFilter 过滤:全平台列 admin 就是"admin 替别家建账号"的入口
- ShopManageController 同步一处;配套 AdminUserServiceCompanyTest 9 例(本机 mvn -o test 19 例全绿,含 PublishBrandLabel/PublishWorkbook 两支)
- 前端 CreateUserDialog/GroupEditorDialog/UsersPage + 两个 model 与 types 随之收口,新增 company-user-create.test.ts 61 行用例
- 注:这批改动原为另一路会话在飞文件,按用户"所有的都提交推送"指示收口,提交前已跑其自带用例
This commit is contained in:
2026-10-09 09:11:30 +08:00
parent 39fc5d537f
commit 65b40e5cc0
11 changed files with 305 additions and 27 deletions
@@ -124,9 +124,11 @@ public class AdminUserService {
Map<Long, String> companyMap = loadCompanyNameMap(rows);
List<AdminUserItemVo> items = rows.stream().map(r -> toItem(r, creatorMap, companyMap)).toList();
// 候选只给当前公司 tab 的组长:全平台列 admin 就是「admin 替别家建账号」的入口(2026-10-07 收口)。
List<AdminBriefVo> admins = "super_admin".equals(role)
? adminUserMapper.selectList(new LambdaQueryWrapper<AdminUserEntity>()
.eq(AdminUserEntity::getRole, "admin")
.eq(companyIdFilter != null, AdminUserEntity::getCompanyId, companyIdFilter)
.orderByAsc(AdminUserEntity::getId)).stream()
.map(u -> new AdminBriefVo(u.getId(), u.getUsername()))
.toList()
@@ -174,6 +176,9 @@ public class AdminUserService {
throw new BusinessException("密码至少6个字符");
}
Long targetCompanyId = resolveTargetCompanyId(role, currentUser, request.getCompanyId());
ensureSubSuperAccountStaysWithOperatorCompany(role, currentUser, wantRole, targetCompanyId);
Long wantCreatedBy;
if ("admin".equals(wantRole) || "company_super_admin".equals(wantRole)) {
// 组长/公司超管的创建人就是操作者本人:公司超管不能替别的主管“认领”管理员
@@ -183,6 +188,7 @@ public class AdminUserService {
if (wantCreatedBy == null) {
AdminUserEntity firstAdmin = adminUserMapper.selectOne(new LambdaQueryWrapper<AdminUserEntity>()
.eq(AdminUserEntity::getRole, "admin")
.eq(AdminUserEntity::getCompanyId, targetCompanyId)
.orderByAsc(AdminUserEntity::getId)
.last("LIMIT 1"));
wantCreatedBy = firstAdmin != null ? firstAdmin.getId() : currentUser.getId();
@@ -190,6 +196,12 @@ public class AdminUserService {
} else {
wantCreatedBy = currentUser.getId();
}
// 创建人是操作者本人时它的公司已知:非超管那条分支上 targetCompanyId 就是操作者公司,
// 超管自己则是下面这条不变式唯一豁免的对象。两种情况都不必再查一次库。
if (!currentUser.getId().equals(wantCreatedBy)) {
ensureCreatorSharesCompany(wantCreatedBy, targetCompanyId,
"所属管理员不存在,无法确认与账号同公司", "所属管理员与账号不在同一家公司");
}
int isAdmin = ("super_admin".equals(wantRole) || "admin".equals(wantRole)
|| "company_super_admin".equals(wantRole)) ? 1 : 0;
@@ -199,7 +211,7 @@ public class AdminUserService {
entity.setIsAdmin(isAdmin);
entity.setRole(wantRole);
entity.setCreatedById(wantCreatedBy);
entity.setCompanyId(resolveTargetCompanyId(role, currentUser, request.getCompanyId()));
entity.setCompanyId(targetCompanyId);
entity.setExpiresAt(parseExpiresAt(request.getExpiresAt()));
try {
adminUserMapper.insert(entity);
@@ -269,6 +281,9 @@ public class AdminUserService {
*
* <p>永远返回一个具体值并写进实体:列 DEFAULT 1 只为发布期的旧 jar 存在(§8.2),
* 新代码不靠它,否则第二家公司开通后每个未传公司的建号都会被静默塞进厦门米椿花。</p>
*
* <p>"超管可指定任意公司"只对<b>公司超管</b>那一档成立,组长/员工另有一道
* {@link #ensureSubSuperAccountStaysWithOperatorCompany} 收在操作者本公司。</p>
*/
private Long resolveTargetCompanyId(String operatorRole, AdminUserEntity operator, Long requestedCompanyId) {
if (!"super_admin".equals(operatorRole)) {
@@ -284,6 +299,25 @@ public class AdminUserService {
return requestedCompanyId;
}
/**
* 站点超管不替其他公司建「超管以下」的账号(组长 / 普通员工)——2026-10-07 定案。
*
* <p>超管在别家公司名下能开的只有一级:那家自己的公司超级管理员。往下的层级由该公司超管
* 自己搭。这条不做进代码的话,唯一防线是"前端建号请求里不带 company_id"——而 {@code users}
* 的 {@code created_by_id} 与 {@code company_id} 一旦分别来自两家公司,二期那 77 处按账号过滤的
* 出口就全错位了(同一形态见 {@link #ensureCreatorSharesCompany})。</p>
*/
private void ensureSubSuperAccountStaysWithOperatorCompany(String operatorRole, AdminUserEntity operator,
String wantRole, Long targetCompanyId) {
if (!"super_admin".equals(operatorRole) || "company_super_admin".equals(wantRole)) {
return;
}
if (!targetCompanyId.equals(operator.getCompanyId())) {
throw new BusinessException(403,
"只能在本公司名下创建管理员和普通账号;其他公司请先建该公司的超级管理员,由它开设下级账号");
}
}
/**
* 移人(设计文档 §2.2):返回要把账号移去的公司 id,返回 null 表示「这次没动公司」。
*
@@ -309,19 +343,31 @@ public class AdminUserService {
* "同公司"和"同一人"两套口径就会交叉,越权只能逐出口重新补。</p>
*/
private void ensureCreatorSharesCompany(AdminUserEntity target, Long newCompanyId) {
Long createdById = target.getCreatedById();
ensureCreatorSharesCompany(target.getCreatedById(), newCompanyId,
"该账号的创建人已不存在,无法确认同公司,不能移入其他公司",
"只能把账号移到其创建人所在的公司");
}
/**
* 归属不变式的执行体:{@code created_by_id} 指向的账号必须与 {@code companyId} 那家同属一家,
* 除非创建人是站点超管(它是平台自己的身份,本来就不属于任何一家)。
*
* <p>建号与移人共用这一份:两边各写一遍的话,"挂名挂到别家"只会在一侧被挡下,而另一侧照样落库。</p>
*/
private void ensureCreatorSharesCompany(Long createdById, Long companyId, String missingCreatorMessage,
String deniedMessage) {
if (createdById == null) {
return;
}
AdminUserEntity creator = adminUserMapper.selectById(createdById);
if (creator == null) {
throw new BusinessException(403, "该账号的创建人已不存在,无法确认同公司,不能移入其他公司");
throw new BusinessException(403, missingCreatorMessage);
}
if ("super_admin".equals(creator.getRole())) {
if (DeviceSessionPolicy.isSuperAdmin(creator.getRole(), creator.getIsAdmin(), creator.getCreatedById())) {
return;
}
if (!newCompanyId.equals(creator.getCompanyId())) {
throw new BusinessException(403, "只能把账号移到其创建人所在的公司");
if (!companyId.equals(creator.getCompanyId())) {
throw new BusinessException(403, deniedMessage);
}
}