35 lines
1.1 KiB
PHP
Executable File
35 lines
1.1 KiB
PHP
Executable File
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace app\common\service;
|
|
|
|
use app\common\model\auth\SystemMenu;
|
|
|
|
/** 菜单授权只在自身与完整祖先链均存在、启用且无环时生效;is_show不参与接口授权。 */
|
|
final class ActiveMenuPermissions
|
|
{
|
|
public static function ids(): array
|
|
{
|
|
$rows = SystemMenu::field('id,pid,is_disable')->select()->toArray();
|
|
$byId = [];
|
|
foreach ($rows as $row) { $byId[(int) $row['id']] = $row; }
|
|
$enabled = [];
|
|
foreach ($byId as $id => $row) {
|
|
$current = $id;
|
|
$visited = [];
|
|
while ($current > 0) {
|
|
if (isset($visited[$current]) || !isset($byId[$current]) || (int) $byId[$current]['is_disable'] !== 0) { continue 2; }
|
|
$visited[$current] = true;
|
|
$current = (int) $byId[$current]['pid'];
|
|
}
|
|
$enabled[] = $id;
|
|
}
|
|
return $enabled;
|
|
}
|
|
|
|
public static function has(string $permission): bool
|
|
{
|
|
return SystemMenu::whereIn('id', self::ids())->where('perms', $permission)->count() > 0;
|
|
}
|
|
}
|