refactor: 更新权限管理与请求验证逻辑
- 在多个控制器中将权限检查从 hasAdminPermission 更新为 hasPermissionCode,以增强权限管理的灵活性。 - 引入 AdminScopePolicy,优化基于代理节点的权限和数据过滤逻辑,确保管理员能够更精确地控制访问权限。 - 在请求验证中添加 agent_node_id 字段,确保 API 接口支持代理节点的相关操作。 - 更新 AdminUser 模型,新增 hasPermissionCode 方法,以支持更细粒度的权限检查。 - 优化审计日志记录逻辑,确保在处理请求时能够准确记录管理员的操作。
This commit is contained in:
@@ -5,6 +5,7 @@ namespace App\Models;
|
||||
use Laravel\Sanctum\HasApiTokens;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use App\Support\AdminPermissionBridge;
|
||||
use App\Models\AgentNode;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
@@ -127,20 +128,32 @@ final class AdminUser extends Authenticatable
|
||||
{
|
||||
$agentId = $this->primaryAgentNodeId();
|
||||
if ($agentId !== null) {
|
||||
$fromAgent = DB::table('admin_user_agent_roles as uar')
|
||||
->join('admin_role_menu_actions as rma', 'rma.role_id', '=', 'uar.role_id')
|
||||
->join('admin_menu_actions as ma', 'ma.id', '=', 'rma.menu_action_id')
|
||||
->where('uar.admin_user_id', $this->id)
|
||||
->where('uar.agent_node_id', $agentId)
|
||||
->where('ma.status', 1)
|
||||
->pluck('ma.permission_code')
|
||||
->all();
|
||||
|
||||
if ($fromAgent !== []) {
|
||||
return $fromAgent;
|
||||
}
|
||||
return $this->agentRoleMenuActionPermissionCodes($agentId);
|
||||
}
|
||||
|
||||
return $this->siteRoleMenuActionPermissionCodes();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<string>
|
||||
*/
|
||||
private function agentRoleMenuActionPermissionCodes(int $agentId): array
|
||||
{
|
||||
return DB::table('admin_user_agent_roles as uar')
|
||||
->join('admin_role_menu_actions as rma', 'rma.role_id', '=', 'uar.role_id')
|
||||
->join('admin_menu_actions as ma', 'ma.id', '=', 'rma.menu_action_id')
|
||||
->where('uar.admin_user_id', $this->id)
|
||||
->where('uar.agent_node_id', $agentId)
|
||||
->where('ma.status', 1)
|
||||
->pluck('ma.permission_code')
|
||||
->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<string>
|
||||
*/
|
||||
private function siteRoleMenuActionPermissionCodes(): array
|
||||
{
|
||||
return DB::table('admin_user_site_roles as usr')
|
||||
->join('admin_role_menu_actions as rma', 'rma.role_id', '=', 'usr.role_id')
|
||||
->join('admin_menu_actions as ma', 'ma.id', '=', 'rma.menu_action_id')
|
||||
@@ -150,6 +163,16 @@ final class AdminUser extends Authenticatable
|
||||
->all();
|
||||
}
|
||||
|
||||
public function effectiveRoleSource(): string
|
||||
{
|
||||
$agentId = $this->primaryAgentNodeId();
|
||||
if ($agentId !== null) {
|
||||
return $this->agentRoleMenuActionPermissionCodes($agentId) !== [] ? 'agent' : 'agent_empty';
|
||||
}
|
||||
|
||||
return $this->siteRoleMenuActionPermissionCodes() !== [] ? 'site' : 'none';
|
||||
}
|
||||
|
||||
public function syncRoleSlugsForDefaultSite(array $slugs): void
|
||||
{
|
||||
$siteId = self::defaultAdminSiteId();
|
||||
@@ -325,6 +348,22 @@ final class AdminUser extends Authenticatable
|
||||
return count(array_intersect($needed, $effective)) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 仅按 permission_code 判定(不处理 prd.* 映射)。
|
||||
*/
|
||||
public function hasPermissionCode(string $permissionCode): bool
|
||||
{
|
||||
if ($permissionCode === '') {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->isSuperAdmin()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return in_array($permissionCode, $this->effectiveMenuActionPermissionCodes(), true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<string> 与 Next 侧栏兼容的 `prd.*` slug 列表
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user