相关记录表admin_id关联当前管理员id

This commit is contained in:
2026-03-10 12:30:56 +08:00
parent b1efeb8b31
commit fdd8f6dffa
18 changed files with 290 additions and 26 deletions

View File

@@ -0,0 +1,60 @@
<?php
declare(strict_types=1);
namespace app\dice\helper;
use plugin\saiadmin\app\model\system\SystemUser;
/**
* 管理员数据范围辅助类
* 用于获取当前管理员及其部门下属管理员可访问的数据范围
*/
class AdminScopeHelper
{
/**
* 获取当前管理员可访问的 admin_id 列表
* 超级管理员(id=1) 返回 null 表示不限制
* 普通管理员返回其本人及部门下属管理员的 id 列表
*
* @param array|null $adminInfo 当前登录管理员信息(含 id、deptList
* @return int[]|null null=不限制(超级管理员),否则为可访问的 admin_id 数组
*/
public static function getAllowedAdminIds(?array $adminInfo): ?array
{
if (empty($adminInfo) || !isset($adminInfo['id'])) {
return [];
}
$adminId = (int) $adminInfo['id'];
if ($adminId <= 1) {
return null;
}
$deptList = $adminInfo['deptList'] ?? [];
if (empty($deptList) || !isset($deptList['id'])) {
return [$adminId];
}
$query = SystemUser::field('id');
$query->auth($deptList);
$ids = $query->column('id');
return array_map('intval', $ids ?: []);
}
/**
* 对查询应用 admin_id 范围过滤
*
* @param object $query ThinkORM 查询对象
* @param array|null $adminInfo 当前登录管理员信息
* @return void
*/
public static function applyAdminScope($query, ?array $adminInfo): void
{
$allowedIds = self::getAllowedAdminIds($adminInfo);
if ($allowedIds === null) {
return;
}
if (empty($allowedIds)) {
$query->whereRaw('1=0');
return;
}
$query->whereIn('admin_id', $allowedIds);
}
}