'string', ]; /** * @return BelongsToMany */ public function clients(): BelongsToMany { return $this->belongsToMany(Client::class, 'admin_client') ->withTimestamps(); } /** * 检查是否是超级管理员 */ public function isSuperAdmin(): bool { return $this->role === self::ROLE_SUPER; } /** * 检查是否是普通管理员 */ public function isAdmin(): bool { return $this->role === self::ROLE_ADMIN; } /** * 检查是否有效的管理员(包括超级管理员和普通管理员) */ public function isValidAdmin(): bool { return in_array($this->role, self::VALID_ROLES, true); } /** * 检查是否可以管理指定的客户 * * @param int $clientId */ public function canManageClient(int $clientId): bool { if ($this->isSuperAdmin()) { return true; } return $this->clients()->where('client_id', $clientId)->exists(); } /** * 检查是否可以管理指定的LLM提供商 * * @param int $providerId */ public function canManageLlmProvider(int $providerId): bool { if ($this->isSuperAdmin()) { return true; } // 普通管理员只能管理与其关联客户绑定的LLM提供商 return $this->clients() ->whereHas('llmProvider', function ($query) use ($providerId) { $query->where('id', $providerId); }) ->exists(); } }