Files
dafuweng-saiadmin6.x/server/app/dice/helper/AdminScopeHelper.php

61 lines
1.8 KiB
PHP
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.
<?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);
}
}