Adds Auth Gates for Pulse, Horizon, and Nova

This commit is contained in:
Refringe 2024-06-19 13:20:51 -04:00
parent 5aafbe94d1
commit d434bc20de
Signed by: Refringe
SSH Key Fingerprint: SHA256:t865XsQpfTeqPRBMN2G6+N8wlDjkgUCZF3WGW6O9N/k
4 changed files with 34 additions and 28 deletions

View File

@ -8,6 +8,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable; use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Str;
use Laravel\Fortify\TwoFactorAuthenticatable; use Laravel\Fortify\TwoFactorAuthenticatable;
use Laravel\Jetstream\HasProfilePhoto; use Laravel\Jetstream\HasProfilePhoto;
use Laravel\Sanctum\HasApiTokens; use Laravel\Sanctum\HasApiTokens;
@ -71,6 +72,11 @@ class User extends Authenticatable implements MustVerifyEmail
return $this->belongsTo(UserRole::class, 'user_role_id'); return $this->belongsTo(UserRole::class, 'user_role_id');
} }
public function isAdmin(): bool
{
return Str::lower($this->role->name) === 'administrator';
}
protected function casts(): array protected function casts(): array
{ {
return [ return [

View File

@ -2,6 +2,8 @@
namespace App\Providers; namespace App\Providers;
use App\Models\User;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider class AppServiceProvider extends ServiceProvider
@ -19,6 +21,9 @@ class AppServiceProvider extends ServiceProvider
*/ */
public function boot(): void public function boot(): void
{ {
// // This gate determines who can access the Pulse dashboard.
Gate::define('viewPulse', function (User $user) {
return $user->isAdmin();
});
} }
} }

View File

@ -3,7 +3,6 @@
namespace App\Providers; namespace App\Providers;
use Illuminate\Support\Facades\Gate; use Illuminate\Support\Facades\Gate;
use Laravel\Horizon\Horizon;
use Laravel\Horizon\HorizonApplicationServiceProvider; use Laravel\Horizon\HorizonApplicationServiceProvider;
class HorizonServiceProvider extends HorizonApplicationServiceProvider class HorizonServiceProvider extends HorizonApplicationServiceProvider
@ -28,9 +27,7 @@ class HorizonServiceProvider extends HorizonApplicationServiceProvider
protected function gate(): void protected function gate(): void
{ {
Gate::define('viewHorizon', function ($user) { Gate::define('viewHorizon', function ($user) {
return in_array($user->email, [ return $user->isAdmin();
//
]);
}); });
} }
} }

View File

@ -20,6 +20,16 @@ class NovaServiceProvider extends NovaApplicationServiceProvider
Nova::withBreadcrumbs(); Nova::withBreadcrumbs();
} }
/**
* Get the tools that should be listed in the Nova sidebar.
*
* @return array
*/
public function tools()
{
return [];
}
/** /**
* Register the Nova routes. * Register the Nova routes.
* *
@ -33,6 +43,16 @@ class NovaServiceProvider extends NovaApplicationServiceProvider
->register(); ->register();
} }
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
/** /**
* Register the Nova gate. * Register the Nova gate.
* *
@ -43,9 +63,7 @@ class NovaServiceProvider extends NovaApplicationServiceProvider
protected function gate() protected function gate()
{ {
Gate::define('viewNova', function ($user) { Gate::define('viewNova', function ($user) {
return in_array($user->email, [ return $user->isAdmin();
//
]);
}); });
} }
@ -60,24 +78,4 @@ class NovaServiceProvider extends NovaApplicationServiceProvider
new \App\Nova\Dashboards\Main, new \App\Nova\Dashboards\Main,
]; ];
} }
/**
* Get the tools that should be listed in the Nova sidebar.
*
* @return array
*/
public function tools()
{
return [];
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
} }