ldc-cocotte/app/Providers/AuthServiceProvider.php

96 lines
2.9 KiB
PHP
Raw Normal View History

2020-10-30 17:26:00 +01:00
<?php
2026-06-14 21:32:07 +02:00
declare(strict_types=1);
2020-10-30 17:26:00 +01:00
namespace App\Providers;
use App\Dto\FaqDto;
use App\Dto\PageDto;
use App\Dto\TagDto;
2026-06-14 21:32:07 +02:00
use App\Models\Customer;
use App\Policies\Backend\FaqPolicy;
use App\Policies\Backend\PagePolicy;
use App\Policies\Backend\TagPolicy;
2026-06-14 21:32:07 +02:00
use App\Policies\Frontend\BasketPolicy;
use App\Services\CrmService;
use Domain\Models\ValueObjects\BasketProduct;
use Domain\Services\ShopperService;
2020-10-30 17:26:00 +01:00
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
2026-06-14 21:32:07 +02:00
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Gate;
use Infrastructure\Adapters\Services\AppAdminAuthService;
use Infrastructure\Adapters\Services\AppCustomerAuthService;
2020-10-30 17:26:00 +01:00
class AuthServiceProvider extends ServiceProvider
{
2022-09-12 14:25:45 +02:00
/**
* The policy mappings for the application.
*
2026-06-14 21:32:07 +02:00
* @var array<class-string, class-string>
2022-09-12 14:25:45 +02:00
*/
2026-06-14 21:32:07 +02:00
#[\Override]
2022-09-12 14:25:45 +02:00
protected $policies = [
2026-06-14 21:32:07 +02:00
// * Policies Exceptions
BasketProduct::class => BasketPolicy::class,
FaqDto::class => FaqPolicy::class,
PageDto::class => PagePolicy::class,
TagDto::class => TagPolicy::class,
2022-09-12 14:25:45 +02:00
];
2020-10-30 17:26:00 +01:00
/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
{
2026-06-14 21:32:07 +02:00
$this->app->singleton(
AppAdminAuthService::class,
fn ($app): AppAdminAuthService => new AppAdminAuthService(
Auth::guard('backend')
)
);
$this->app->singleton(
AppCustomerAuthService::class,
fn ($app): AppCustomerAuthService => new AppCustomerAuthService(
Auth::guard('frontend'),
$app->make(CrmService::class)
)
);
Auth::shouldUse($this->currentRouteGuard());
Gate::define(
'customerPassOrders',
fn (Customer $customer): bool => resolve(ShopperService::class)
->canCustomerPassOrdersApp($customer->getId())
);
2020-10-30 17:26:00 +01:00
$this->registerPolicies();
2026-06-14 21:32:07 +02:00
Gate::guessPolicyNamesUsing(function (string $modelClass) {
$context = $this->currentRouteGuard() === 'frontend' ? 'Frontend' : 'Backend';
$classBasename = class_basename($modelClass);
$policyClass = "App\\Policies\\{$context}\\{$classBasename}Policy";
return class_exists($policyClass) ? $policyClass : null;
});
}
private function currentRouteGuard(): string
{
$request = $this->app->make(Request::class);
$defaultGuard = Config::string('auth.defaults.guard', 'frontend');
return match (true) {
$request->is('bo', 'bo/*') => 'backend',
$request->is('api', 'api/*') => 'api',
default => $defaultGuard,
};
2020-10-30 17:26:00 +01:00
}
}