38 lines
1.4 KiB
Dart
38 lines
1.4 KiB
Dart
|
|
import 'package:flutter/foundation.dart';
|
|||
|
|
|
|||
|
|
/// 当前登录用户的「数据权限范围」——其所属组织子树的根节点 ID。
|
|||
|
|
///
|
|||
|
|
/// 用途:在客户端对组织树 / 门店列表做二次收敛,确保非管理员用户只能看到
|
|||
|
|
/// 自己所属组织及下属门店的数据,防止后端 `/sys/org/tree` 返回全量树时被
|
|||
|
|
/// 越权展示其他组织的数据。
|
|||
|
|
///
|
|||
|
|
/// 由 [AuthNotifier] 在登录成功与会话恢复时写入 [setCurrentOrg],登出 /
|
|||
|
|
/// 会话失效时 [clear]。读取方([OrgService])仅在 [enabled] 为 true 时过滤,
|
|||
|
|
/// [currentOrgId] 为空(管理员 / 无法识别)时不做任何裁剪,行为与后端一致。
|
|||
|
|
class SessionOrgScope {
|
|||
|
|
SessionOrgScope._();
|
|||
|
|
static final SessionOrgScope instance = SessionOrgScope._();
|
|||
|
|
|
|||
|
|
/// 当前用户所属组织 ID。空字符串表示「未限定」(管理员或无法识别),
|
|||
|
|
/// 此时客户端不过滤,完全信任后端返回。
|
|||
|
|
String currentOrgId = '';
|
|||
|
|
|
|||
|
|
/// 是否启用客户端数据权限收敛。
|
|||
|
|
bool get enabled => currentOrgId.isNotEmpty;
|
|||
|
|
|
|||
|
|
/// 写入当前用户组织范围。
|
|||
|
|
void setCurrentOrg(String? orgId) {
|
|||
|
|
currentOrgId = orgId ?? '';
|
|||
|
|
if (kDebugMode) {
|
|||
|
|
debugPrint(
|
|||
|
|
'[SessionOrgScope] currentOrgId=${currentOrgId.isEmpty ? "(未限定/管理员)" : currentOrgId}',
|
|||
|
|
);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// 清空(登出 / 会话失效)。
|
|||
|
|
void clear() {
|
|||
|
|
currentOrgId = '';
|
|||
|
|
}
|
|||
|
|
}
|