fix(鉴权): 到期级联改标量聚合,后台入口按菜单授权放开
三件事同源于 10-06「非默认公司账号进不了数富后台」这条事故链,四种死法在界面上长得一样。 1) AccountExpiryService 读本公司公司超管的 expires_at 时把单列投影成实体列表再遍历—— MyBatis 对所选列全为 NULL 的行交出的是 null 元素,而"没设到期"是绝大多数公司的常态, 于是只要有一个永不过期的公司超管,该公司所有账号的登录与每个鉴权请求全部 500。 改成 mapper 侧一条 MIN(expires_at) 标量聚合;AuthChainQueryShapeTest 扫鉴权链路的 "单列投影"形状——这个判据落在 mock 边界外,单测挡不住,只能做源码扫描。 2) 后台入口放开:requireConsoleOperator = 管理员角色链 或 本公司树里 >=1 条后台菜单授权 (探针是带 company_id 条件的标量 COUNT,与下发口径一致,否则进得来而菜单树是空的)。 逐页另设 requireConsolePageAccess 锚住本页 key,勾父分组覆盖后代的既有语义照旧。 管理类 10 支控制器继续 requireAdmin,并且出口 2 不再把这些页下发给普通账号—— 下发出去就是一排点了必 403 的死链。内部令牌通道绕过到期与会话校验,不随入口放宽。 护栏 ConsoleWideningGuardrailTest 双向钉:管理页不得被授权打开、入口落点恰好那 13 支。 3) 前端:后端失败一律 HTTP 200 + success=false,登录页从未解包,所以"什么都不提示"。 补 login-feedback 归一文案(技术串不外露、兜底不再伪装成用户名或密码错误), 并修 isLoginLocation 拿路由内路径比 location.pathname——生产实际是 /admin-vue/login, 那条"已在登录页就别整页重刷"的守卫一次都没生效过。 验证:后端 mvn -o -B test 3924 例 0 红(457 支类);前端 node --test 1738 例 0 红; vue-tsc --noEmit rc=0;入口探针 SQL 已在生产库只读三档空跑(本公司 1 / 错公司 0 / 无授权 0)。
This commit is contained in:
@@ -3,7 +3,52 @@ package com.nanri.aiimage.common.mapper;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.nanri.aiimage.common.model.entity.AdminUserEntity;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Mapper
|
||||
public interface AdminUserMapper extends BaseMapper<AdminUserEntity> {
|
||||
|
||||
/**
|
||||
* 本公司「公司超级管理员」里最早的到期时间;返回 null = 本公司无此约束(没有公司超管,
|
||||
* 或它们全部永不过期)。到期级联判据见 {@code AccountExpiryService}。
|
||||
*
|
||||
* <p>刻意用聚合标量而不是 {@code selectList(.select(expiresAt))}:MyBatis 对「所选列全为
|
||||
* NULL」的行返回 null 元素,一家公司只要有任意一个公司超管没设到期,那条列表里就混着 null,
|
||||
* 遍历时解引用即 NPE,而这里正是每次登录与每个鉴权请求都要走的读出口。MIN 忽略 NULL,
|
||||
* 无行时结果集仍是单行单列,直接映射成 null。</p>
|
||||
*/
|
||||
@Select("""
|
||||
SELECT MIN(expires_at)
|
||||
FROM users
|
||||
WHERE role = 'company_super_admin'
|
||||
AND company_id = #{companyId}
|
||||
""")
|
||||
LocalDateTime selectCompanySuperAdminEarliestExpiry(@Param("companyId") Long companyId);
|
||||
|
||||
/**
|
||||
* 该账号在<b>本公司那棵树</b>里被授了几条后台(menu_type='admin')菜单;>0 即允许进管理后台。
|
||||
*
|
||||
* <p>刻意用标量 COUNT 而不是 {@code selectList(.select(columnId))}:与上面那条同样的投影陷阱
|
||||
* (见 {@code AuthChainQueryShapeTest}),而这里是每个后台入口请求都要走的鉴权读。</p>
|
||||
*
|
||||
* <p>必须带 {@code cm.company_id} 条件:每家公司一套树之后,{@code column_id} 指向别家树的
|
||||
* 残留授权行会让账号"进得了后台、菜单树却是空的"——入口门与 {@code getUserColumnPermissions}
|
||||
* 的下发集要用同一个公司口径,否则两处各判一次就留下静默死链。</p>
|
||||
*/
|
||||
@Select("""
|
||||
<script>
|
||||
SELECT COUNT(1)
|
||||
FROM user_column_permission ucp
|
||||
JOIN columns cm ON cm.id = ucp.column_id
|
||||
WHERE ucp.user_id = #{userId}
|
||||
AND cm.menu_type = 'admin'
|
||||
<if test="companyId != null">
|
||||
AND cm.company_id = #{companyId}
|
||||
</if>
|
||||
</script>
|
||||
""")
|
||||
Long countAdminMenuGrants(@Param("userId") Long userId, @Param("companyId") Long companyId);
|
||||
}
|
||||
|
||||
+1
-11
@@ -1,6 +1,5 @@
|
||||
package com.nanri.aiimage.common.security;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.common.mapper.AdminUserMapper;
|
||||
import com.nanri.aiimage.common.mapper.ShopManageGroupMapper;
|
||||
@@ -11,7 +10,6 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 账号到期(expires_at,V146)的级联判定与拦截。
|
||||
@@ -113,15 +111,7 @@ public class AccountExpiryService {
|
||||
if (companyId == null || companyId <= 0) {
|
||||
return null;
|
||||
}
|
||||
List<AdminUserEntity> csas = adminUserMapper.selectList(new LambdaQueryWrapper<AdminUserEntity>()
|
||||
.select(AdminUserEntity::getExpiresAt)
|
||||
.eq(AdminUserEntity::getRole, "company_super_admin")
|
||||
.eq(AdminUserEntity::getCompanyId, companyId));
|
||||
LocalDateTime earliest = null;
|
||||
for (AdminUserEntity csa : csas) {
|
||||
earliest = min(earliest, csa.getExpiresAt());
|
||||
}
|
||||
return earliest;
|
||||
return adminUserMapper.selectCompanySuperAdminEarliestExpiry(companyId);
|
||||
}
|
||||
|
||||
/** 组级:该用户所属分组(成员表)的组长(created_by_id/user_id)若是管理员,取其最早 expires_at。 */
|
||||
|
||||
@@ -114,6 +114,51 @@ public class AdminAuthSupport {
|
||||
return user;
|
||||
}
|
||||
|
||||
/**
|
||||
* 后台入口门:管理员角色恒可进;普通账号持有本公司任意后台菜单授权也可进。
|
||||
*
|
||||
* <p>2026-10-06 放开。此前"进后台"与"有管理员角色"绑死,超管在权限页给普通账号勾了
|
||||
* 后台菜单也照样 403——授权动作做了,门没跟着开,界面上表现为点了没反应。放开只针对
|
||||
* <b>入口</b>:每一页仍由该页自己的门把关({@code PermissionMenuService.requireConsolePageAccess}
|
||||
* 按该页菜单键判定),管理类出口(用户/菜单/分组/密钥/公司/版本)一律继续走
|
||||
* {@link #requireAdmin},不在这里放宽。</p>
|
||||
*/
|
||||
public AdminUserEntity requireConsoleOperator(HttpServletRequest request) {
|
||||
AdminUserEntity user = requireUser(request);
|
||||
if (!isConsoleOperator(user)) {
|
||||
throw new BusinessException(403, "需要管理员权限");
|
||||
}
|
||||
return user;
|
||||
}
|
||||
|
||||
/** JWT 优先走入口门;内部令牌通道仍要求管理员角色——它绕过到期与会话校验,不随入口一起放宽。 */
|
||||
public AdminUserEntity requireConsoleOperatorOrInternal(HttpServletRequest request) {
|
||||
try {
|
||||
return requireConsoleOperator(request);
|
||||
} catch (BusinessException authFailure) {
|
||||
AdminUserEntity internalOperator = resolveInternalOperator(request, true);
|
||||
if (internalOperator != null) {
|
||||
return internalOperator;
|
||||
}
|
||||
throw authFailure;
|
||||
}
|
||||
}
|
||||
|
||||
/** 入口判定:角色链(super_admin / admin / company_super_admin)或本公司任意一条后台菜单授权。 */
|
||||
public boolean isConsoleOperator(AdminUserEntity user) {
|
||||
if (user == null) {
|
||||
return false;
|
||||
}
|
||||
if (currentRole(user) != null) {
|
||||
return true;
|
||||
}
|
||||
if (user.getId() == null || user.getId() <= 0) {
|
||||
return false;
|
||||
}
|
||||
Long grants = adminUserMapper.countAdminMenuGrants(user.getId(), user.getCompanyId());
|
||||
return grants != null && grants > 0;
|
||||
}
|
||||
|
||||
/** 计算当前用户的管理角色:super_admin / admin / null。 */
|
||||
public String currentRole(AdminUserEntity user) {
|
||||
if (user == null) {
|
||||
|
||||
Reference in New Issue
Block a user