This commit is contained in:
hashbro
2026-08-07 05:21:44 +08:00
parent 5145b6f719
commit 78b61f85d2
325 changed files with 9874 additions and 1738 deletions
+63
View File
@@ -0,0 +1,63 @@
<?php
namespace App\Support;
use App\Models\Channel;
use App\Models\User;
use Illuminate\Database\Eloquent\Builder;
final class AgentScope
{
/**
* Channel IDs owned by the agent (empty list means no access).
*
* @return list<string>
*/
public static function channelIdsFor(?User $agent): array
{
if ($agent === null) {
return [];
}
return Channel::query()
->where('user_id', $agent->id)
->pluck('channel_id')
->all();
}
/**
* Restrict a devices query (or anything with devices.channel_id) to an agent.
* Admin passes null agent → no restriction.
*/
public static function applyDeviceChannelScope(Builder $query, ?User $agent, string $channelColumn = 'devices.channel_id'): Builder
{
if ($agent === null) {
return $query;
}
$ids = self::channelIdsFor($agent);
if ($ids === []) {
return $query->whereRaw('1 = 0');
}
return $query->whereIn($channelColumn, $ids);
}
public static function applyAgentUserFilter(Builder $devicesQuery, ?int $agentUserId): Builder
{
if ($agentUserId === null || $agentUserId <= 0) {
return $devicesQuery;
}
$ids = Channel::query()
->where('user_id', $agentUserId)
->pluck('channel_id')
->all();
if ($ids === []) {
return $devicesQuery->whereRaw('1 = 0');
}
return $devicesQuery->whereIn('devices.channel_id', $ids);
}
}