1
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
namespace plugin\admin\app\common;
|
||||
|
||||
|
||||
use plugin\admin\app\model\Admin;
|
||||
use plugin\admin\app\model\AdminAccess;
|
||||
use plugin\admin\app\model\AdminRole;
|
||||
use plugin\admin\app\model\AdminRule;
|
||||
|
||||
class Auth
|
||||
{
|
||||
/**
|
||||
* 获取权限范围内的所有角色id
|
||||
* @param bool $with_self
|
||||
* @return array
|
||||
*/
|
||||
public static function getScopeRoleIds(bool $with_self = false): array
|
||||
{
|
||||
if (!$admin = admin()) {
|
||||
return [];
|
||||
}
|
||||
$role_ids = $admin['roles'];
|
||||
$rules = AdminRole::whereIn('id', $role_ids)->column('rules');//->toArray();
|
||||
if ($rules && in_array('*', $rules)) {
|
||||
return AdminRole::column('id');//->toArray();
|
||||
}
|
||||
|
||||
$roles = AdminRole::select();
|
||||
$tree = new Tree($roles);
|
||||
$descendants = $tree->getDescendant($role_ids, $with_self);
|
||||
return array_column($descendants, 'id');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取权限范围内的所有管理员id
|
||||
* @param bool $with_self
|
||||
* @return array
|
||||
*/
|
||||
public static function getScopeAdminIds(bool $with_self = false): array
|
||||
{
|
||||
$role_ids = static::getScopeRoleIds();
|
||||
$admin_ids = AdminAccess::whereIn('role_id', $role_ids)->column('admin_id');//->toArray();
|
||||
if ($with_self) {
|
||||
$admin_ids[] = admin_id();
|
||||
}
|
||||
return array_unique($admin_ids);
|
||||
}
|
||||
|
||||
/**
|
||||
* 兼容旧版本
|
||||
* @param int $admin_id
|
||||
* @deprecated
|
||||
* @return bool
|
||||
*/
|
||||
public static function isSupperAdmin(int $admin_id = 0): bool
|
||||
{
|
||||
return static::isSuperAdmin($admin_id);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否是超级管理员
|
||||
* @param int $admin_id
|
||||
* @return bool
|
||||
*/
|
||||
public static function isSuperAdmin(int $admin_id = null): bool
|
||||
{
|
||||
if(!$admin_id){
|
||||
$admin_id = admin()['id'];
|
||||
}
|
||||
if($admin_id <=1){
|
||||
return true;
|
||||
}
|
||||
if (!$admin_id) {
|
||||
if (!$roles = admin('roles')) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
$roles = AdminAccess::where('admin_id', $admin_id)->column('role_id');
|
||||
}
|
||||
$rules = AdminRole::whereIn('id', $roles)->column('rules');
|
||||
return $rules && in_array('*', $rules);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user