Files
CloudHome-Ops/lib/utils/permissions.dart

70 lines
2.9 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 账号类别与功能模块权限判定。
//
// 作为菜单可见性与路由可达性的**单一来源**:AdminHubPage 据此决定模块是否
// 显示在界面,router.dart 的 redirect 据此决定是否拦截深链/返回键,二者保持
// 完全一致,避免「界面看不到却能路由进入」或「点进去被弹回」的不一致。
//
// 判定依据:登录用户 [User] 的 [User.superAdmin] / [User.orgId] / [User.orgType]。
// - superAdmin=true 或 orgId 为空 → 管理员(可见全部模块);
// - orgType=1(渠道)→ 渠道商;orgType=2(代理商)→ 代理商;
// - 受限但类型未知 → 按渠道商兜底,确保「渠道商管理」对渠道商账号可开放
// (fail-open,符合「渠道商账号点击渠道商管理应正常打开」的修复目标)。
//
// 模块可见性规则:
// - 用户管理:所有已登录账号可见(管理员/渠道商/代理商各看自己范围内用户);
// - 渠道商管理:管理员 + 渠道商可见,代理商不可见;
// - 其余模块(设备/激活码/公司介绍/滚动字幕):所有账号可见。
import '../models/user.dart';
enum AccountCategory {
/// 管理员(超级管理员 / 无组织归属)。
admin,
/// 渠道商(orgType=1)。
channelMerchant,
/// 代理商(orgType=2)。
agent,
/// 未知(理论上不会在 UI 中出现,作为兜底占位)。
unknown,
}
/// 功能模块权限判定工具。
class SessionPermissions {
SessionPermissions._();
/// 依据当前登录用户推导账号类别。
static AccountCategory category(User? user) {
if (user == null) return AccountCategory.unknown;
if (user.superAdmin) return AccountCategory.admin;
if (user.orgId.isEmpty) return AccountCategory.admin;
switch (user.orgType) {
case 0:
return AccountCategory.admin; // 平台
case 1:
return AccountCategory.channelMerchant; // 渠道
case 2:
return AccountCategory.agent; // 代理商
default:
// 受限但类型未知:按渠道商兜底,开放「渠道商管理」。
return AccountCategory.channelMerchant;
}
}
/// 是否管理员(可访问系统管理 / 用户管理等全部模块)。
static bool isAdmin(User? user) => category(user) == AccountCategory.admin;
/// 是否可见 / 可访问「用户管理」(所有已登录账号)。
///
/// 管理员看全量,渠道商/代理商看自己组织范围内的用户;具体数据范围由
/// [UserPage] 调用后端时按会话权限收敛,不再在菜单层一刀切隐藏。
static bool canViewUserManagement(User? user) => user != null;
/// 是否可见 / 可访问「渠道商管理」(管理员 + 渠道商;代理商不可)。
static bool canViewChannelManagement(User? user) {
final c = category(user);
return c == AccountCategory.admin || c == AccountCategory.channelMerchant;
}
}