Move channel listing logic into model
This commit is contained in:
parent
15e84fc67f
commit
af2d996c12
@ -11,14 +11,7 @@ class DashboardController extends Controller
|
||||
public function index(Request $request)
|
||||
{
|
||||
$user = $request->user();
|
||||
|
||||
$channels = [];
|
||||
if ($user->isAdmin()) {
|
||||
$channels = Channel::all()->sortBy('channel_login');
|
||||
} else {
|
||||
$permissionChannelIds = $user->channelPermissions()->pluck('channel_provider_id')->toArray();
|
||||
$channels = Channel::whereIn('channel_id', $permissionChannelIds)->orderBy('channel_login')->get();
|
||||
}
|
||||
$channels = $user->getTraceChannels();
|
||||
|
||||
return view('dashboard.index', [
|
||||
'channels' => $channels
|
||||
|
@ -9,6 +9,8 @@
|
||||
use Laravel\Sanctum\HasApiTokens;
|
||||
|
||||
use App\Models\Trace\Channel as TraceChannel;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
class User extends Authenticatable
|
||||
{
|
||||
@ -45,8 +47,10 @@ class User extends Authenticatable
|
||||
|
||||
/**
|
||||
* Get the channel permissions for the user.
|
||||
*
|
||||
* @return HasMany
|
||||
*/
|
||||
public function channelPermissions()
|
||||
public function channelPermissions() : HasMany
|
||||
{
|
||||
return $this->hasMany(ChannelPermission::class, 'user_provider_id', 'provider_id');
|
||||
}
|
||||
@ -71,6 +75,23 @@ public function hasChannelPermission(TraceChannel $channel): bool
|
||||
return $this->channelPermissions()->where('channel_provider_id', $channel->channel_id)->exists();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Trace\Channel models that the user has access to.
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function getTraceChannels() : Collection
|
||||
{
|
||||
$channels = TraceChannel::all()->sortBy('channel_login');
|
||||
|
||||
if (! $this->isAdmin()) {
|
||||
$permissionChannelIds = $this->channelPermissions()->pluck('channel_provider_id')->toArray();
|
||||
$channels = $channels->whereIn('channel_id', $permissionChannelIds);
|
||||
}
|
||||
|
||||
return $channels;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the user is an admin.
|
||||
*
|
||||
@ -78,6 +99,6 @@ public function hasChannelPermission(TraceChannel $channel): bool
|
||||
*/
|
||||
public function isAdmin() : bool
|
||||
{
|
||||
return $this->is_admin;
|
||||
return $this->is_admin === 1;
|
||||
}
|
||||
}
|
||||
|
@ -10,7 +10,7 @@
|
||||
<div class="bg-white dark:bg-gray-800 overflow-hidden shadow-sm sm:rounded-lg">
|
||||
<div class="p-6 text-gray-900 dark:text-gray-100">
|
||||
<h2 class="font-semibold text-xl text-gray-800 dark:text-gray-200 leading-tight">
|
||||
{{ $channel->username() }}
|
||||
{{ $channel->username() }}
|
||||
</h2>
|
||||
|
||||
<table class="table-auto w-full">
|
||||
|
Loading…
Reference in New Issue
Block a user