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:
+70
-3
@@ -13,6 +13,8 @@ import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
@@ -55,10 +57,10 @@ class AdminUserServiceCompanyTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void superAdminPlacesNewUserInSpecifiedCompany() {
|
||||
void superAdminPlacesNewCompanySuperAdminInSpecifiedCompany() {
|
||||
AdminUserEntity operator = operator(1L, "super_admin", 1L);
|
||||
when(authSupport.currentRole(operator)).thenReturn("super_admin");
|
||||
AdminUserCreateRequest request = request(7L);
|
||||
AdminUserCreateRequest request = request("company_super_admin", 7L);
|
||||
|
||||
service.createUser(operator, request);
|
||||
|
||||
@@ -66,6 +68,56 @@ class AdminUserServiceCompanyTest {
|
||||
verify(companyService).requireActive(7L);
|
||||
}
|
||||
|
||||
@Test
|
||||
void superAdminCannotOpenSubSuperAccountForAnotherCompany() {
|
||||
// 2026-10-07 定案:admin 不替别家建组长/员工,那家由它自己的公司超管往下搭。
|
||||
AdminUserEntity operator = operator(1L, "super_admin", 1L);
|
||||
when(authSupport.currentRole(operator)).thenReturn("super_admin");
|
||||
|
||||
for (String subSuperRole : List.of("normal", "admin")) {
|
||||
assertThatThrownBy(() -> service.createUser(operator, request(subSuperRole, 7L)))
|
||||
.as("角色 %s 属于「超管以下」,不许落在别家公司名下", subSuperRole)
|
||||
.isInstanceOfSatisfying(BusinessException.class, ex -> {
|
||||
assertThat(ex.getCode()).isEqualTo(403);
|
||||
assertThat(ex.getMessage()).contains("超级管理员");
|
||||
});
|
||||
}
|
||||
verify(userMapper, never()).insert(any(AdminUserEntity.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createdByIdMustBelongToTheSameCompanyAsTheNewAccount() {
|
||||
AdminUserEntity operator = operator(1L, "super_admin", 1L);
|
||||
when(authSupport.currentRole(operator)).thenReturn("super_admin");
|
||||
when(userMapper.selectById(50L)).thenReturn(creator(50L, "admin", 7L));
|
||||
AdminUserCreateRequest request = request("normal", null);
|
||||
request.setCreatedById(50L);
|
||||
|
||||
assertThatThrownBy(() -> service.createUser(operator, request))
|
||||
.isInstanceOfSatisfying(BusinessException.class, ex -> {
|
||||
assertThat(ex.getCode()).isEqualTo(403);
|
||||
assertThat(ex.getMessage()).contains("同一家公司");
|
||||
});
|
||||
// 挂名挂到别家时一行都不许落库:落了就等于凭空造出一条跨公司的归属链,二期补不回来
|
||||
verify(userMapper, never()).insert(any(AdminUserEntity.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createdByIdFromTheSameCompanyIsAccepted() {
|
||||
AdminUserEntity operator = operator(1L, "super_admin", 1L);
|
||||
when(authSupport.currentRole(operator)).thenReturn("super_admin");
|
||||
when(userMapper.selectById(51L)).thenReturn(creator(51L, "admin", 1L));
|
||||
AdminUserCreateRequest request = request("normal", null);
|
||||
request.setCreatedById(51L);
|
||||
|
||||
service.createUser(operator, request);
|
||||
|
||||
ArgumentCaptor<AdminUserEntity> captor = ArgumentCaptor.forClass(AdminUserEntity.class);
|
||||
verify(userMapper).insert(captor.capture());
|
||||
assertThat(captor.getValue().getCreatedById()).isEqualTo(51L);
|
||||
assertThat(captor.getValue().getCompanyId()).isEqualTo(1L);
|
||||
}
|
||||
|
||||
@Test
|
||||
void superAdminWithoutCompanyFallsBackToItsOwnCompany() {
|
||||
// 操作者所属公司故意取 3:实现里写死 1(照抄列 DEFAULT)必须被这条抓到
|
||||
@@ -124,14 +176,29 @@ class AdminUserServiceCompanyTest {
|
||||
}
|
||||
|
||||
private AdminUserCreateRequest request(Long companyId) {
|
||||
return request("normal", companyId);
|
||||
}
|
||||
|
||||
private AdminUserCreateRequest request(String role, Long companyId) {
|
||||
AdminUserCreateRequest request = new AdminUserCreateRequest();
|
||||
request.setUsername("created-user");
|
||||
request.setPassword("secret1");
|
||||
request.setRole("normal");
|
||||
request.setRole(role);
|
||||
request.setCompanyId(companyId);
|
||||
return request;
|
||||
}
|
||||
|
||||
/** 被当作「所属管理员」挂名的账号:role=admin,落在 companyId 那家。 */
|
||||
private AdminUserEntity creator(Long id, String role, Long companyId) {
|
||||
AdminUserEntity creator = new AdminUserEntity();
|
||||
creator.setId(id);
|
||||
creator.setUsername("creator-" + id);
|
||||
creator.setRole(role);
|
||||
creator.setIsAdmin(1);
|
||||
creator.setCompanyId(companyId);
|
||||
return creator;
|
||||
}
|
||||
|
||||
private AdminUserEntity operator(Long id, String role, Long companyId) {
|
||||
AdminUserEntity operator = new AdminUserEntity();
|
||||
operator.setId(id);
|
||||
|
||||
Reference in New Issue
Block a user