更新
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user