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)
|
public function index(Request $request)
|
||||||
{
|
{
|
||||||
$user = $request->user();
|
$user = $request->user();
|
||||||
|
$channels = $user->getTraceChannels();
|
||||||
$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();
|
|
||||||
}
|
|
||||||
|
|
||||||
return view('dashboard.index', [
|
return view('dashboard.index', [
|
||||||
'channels' => $channels
|
'channels' => $channels
|
||||||
|
@ -9,6 +9,8 @@
|
|||||||
use Laravel\Sanctum\HasApiTokens;
|
use Laravel\Sanctum\HasApiTokens;
|
||||||
|
|
||||||
use App\Models\Trace\Channel as TraceChannel;
|
use App\Models\Trace\Channel as TraceChannel;
|
||||||
|
use Illuminate\Database\Eloquent\Collection;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
|
|
||||||
class User extends Authenticatable
|
class User extends Authenticatable
|
||||||
{
|
{
|
||||||
@ -45,8 +47,10 @@ class User extends Authenticatable
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the channel permissions for the user.
|
* 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');
|
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();
|
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.
|
* Determine if the user is an admin.
|
||||||
*
|
*
|
||||||
@ -78,6 +99,6 @@ public function hasChannelPermission(TraceChannel $channel): bool
|
|||||||
*/
|
*/
|
||||||
public function isAdmin() : bool
|
public function isAdmin() : bool
|
||||||
{
|
{
|
||||||
return $this->is_admin;
|
return $this->is_admin === 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user