Files
sembara/app/Providers/AppServiceProvider.php

57 lines
1.5 KiB
PHP

<?php
namespace App\Providers;
use App\Auth\InsForgeUserProvider;
use App\Models\User;
use App\Services\InsForge\InsForgeAuthService;
use App\Services\InsForge\InsForgeClient;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
private const ADMIN_ONLY_ABILITIES = [
'manage-products',
'manage-stock',
'manage-drivers',
'manage-users',
'view-reports',
'view-activity-logs',
];
/**
* Register any application services.
*/
public function register(): void
{
$this->app->singleton(InsForgeClient::class, function (Application $app) {
$config = $app->make('config')->get('insforge');
return new InsForgeClient(
$config['base_url'],
$config['api_key'],
$config['timeout'],
);
});
Auth::provider('insforge', fn () => new InsForgeUserProvider($this->app->make(InsForgeAuthService::class)));
}
/**
* Bootstrap any application services.
*/
public function boot(): void
{
Gate::before(function (User $user, string $ability) {
if ($user->isAdmin()) {
return true;
}
return in_array($ability, self::ADMIN_ONLY_ABILITIES) ? false : null;
});
}
}