diff --git a/app/Http/Controllers/DashboardController.php b/app/Http/Controllers/DashboardController.php index 824cb69..2791e21 100644 --- a/app/Http/Controllers/DashboardController.php +++ b/app/Http/Controllers/DashboardController.php @@ -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 diff --git a/app/Models/User.php b/app/Models/User.php index 46505f4..f68c762 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -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; } } diff --git a/resources/views/dashboard/channel.blade.php b/resources/views/dashboard/channel.blade.php index 0b21097..768c079 100644 --- a/resources/views/dashboard/channel.blade.php +++ b/resources/views/dashboard/channel.blade.php @@ -10,7 +10,7 @@