feature
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user