This change centralizes the URL generation logic for email verification and password reset, improving maintainability.
187 lines
6.4 KiB
PHP
187 lines
6.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Providers;
|
|
|
|
use App\Dto\BasketDto;
|
|
use App\Dto\BasketProductDto;
|
|
use App\Dto\DiscountDto;
|
|
use App\Dto\FaqDto;
|
|
use App\Dto\PageDto;
|
|
use App\Dto\PickupExceptionDto;
|
|
use App\Dto\ProductDto;
|
|
use App\Dto\ProductWithPricingDto;
|
|
use App\Dto\RangeDto;
|
|
use App\Dto\SettingDto;
|
|
use App\Dto\ShopDto;
|
|
use App\Dto\SlideDto;
|
|
use App\Dto\TagDto;
|
|
use App\Jobs\FetchAndUpsertArcadiaEmployees;
|
|
use App\Models\Customer;
|
|
use App\Models\Setting;
|
|
use App\Policies\Backend\DiscountPolicy;
|
|
use App\Policies\Backend\EmployeePolicy;
|
|
use App\Policies\Backend\FaqPolicy;
|
|
use App\Policies\Backend\PagePolicy;
|
|
use App\Policies\Backend\PickupExceptionPolicy;
|
|
use App\Policies\Backend\ProductPolicy;
|
|
use App\Policies\Backend\RangePolicy;
|
|
use App\Policies\Backend\SettingPolicy;
|
|
use App\Policies\Backend\ShopPolicy;
|
|
use App\Policies\Backend\SlidePolicy;
|
|
use App\Policies\Backend\TagPolicy;
|
|
use App\Policies\Frontend\BasketPolicy;
|
|
use App\Policies\Frontend\ProductPolicy as FrontendProductPolicy;
|
|
use App\Services\CrmService;
|
|
use Domain\Services\ShopperService;
|
|
use Illuminate\Auth\Notifications\ResetPassword;
|
|
use Illuminate\Auth\Notifications\VerifyEmail;
|
|
use Illuminate\Foundation\Application;
|
|
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Config;
|
|
use Illuminate\Support\Facades\Date;
|
|
use Illuminate\Support\Facades\Gate;
|
|
use Illuminate\Support\Facades\URL;
|
|
use Infrastructure\Adapters\Services\AppAdminAuthService;
|
|
use Infrastructure\Adapters\Services\AppCustomerAuthService;
|
|
|
|
class AuthServiceProvider extends ServiceProvider
|
|
{
|
|
/**
|
|
* The policy mappings for the application.
|
|
*
|
|
* @var array<class-string, class-string>
|
|
*/
|
|
#[\Override]
|
|
protected $policies = [
|
|
// * Policies Exceptions
|
|
BasketDto::class => BasketPolicy::class,
|
|
BasketProductDto::class => BasketPolicy::class,
|
|
DiscountDto::class => DiscountPolicy::class,
|
|
FaqDto::class => FaqPolicy::class,
|
|
PageDto::class => PagePolicy::class,
|
|
PickupExceptionDto::class => PickupExceptionPolicy::class,
|
|
ProductDto::class => ProductPolicy::class,
|
|
ProductWithPricingDto::class => FrontendProductPolicy::class,
|
|
RangeDto::class => RangePolicy::class,
|
|
Setting::class => SettingPolicy::class,
|
|
SettingDto::class => SettingPolicy::class,
|
|
ShopDto::class => ShopPolicy::class,
|
|
SlideDto::class => SlidePolicy::class,
|
|
TagDto::class => TagPolicy::class,
|
|
FetchAndUpsertArcadiaEmployees::class => EmployeePolicy::class,
|
|
];
|
|
|
|
/**
|
|
* Register any authentication / authorization services.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function boot()
|
|
{
|
|
$this->app->singleton(
|
|
AppAdminAuthService::class,
|
|
fn (Application $app): AppAdminAuthService => new AppAdminAuthService(
|
|
Auth::guard('backend')
|
|
)
|
|
);
|
|
|
|
$this->app->singleton(
|
|
AppCustomerAuthService::class,
|
|
fn (Application $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())
|
|
);
|
|
|
|
$this->registerPolicies();
|
|
|
|
Gate::guessPolicyNamesUsing(function (string $modelClass) {
|
|
$context = $this->currentRouteGuard() === 'frontend' ? 'Frontend' : 'Backend';
|
|
$classBasename = str_replace('Dto', '', class_basename($modelClass));
|
|
|
|
$policyClass = "App\\Policies\\{$context}\\{$classBasename}Policy";
|
|
|
|
return class_exists($policyClass) ? $policyClass : null;
|
|
});
|
|
|
|
$this->authUrlCallbacksSetup();
|
|
}
|
|
|
|
private function authUrlCallbacksSetup(): void
|
|
{
|
|
VerifyEmail::createUrlUsing($this->createEmailVerificationUrl(...));
|
|
ResetPassword::createUrlUsing($this->createPasswordResetUrl(...));
|
|
}
|
|
|
|
private function createEmailVerificationUrl(mixed $notifiable): string
|
|
{
|
|
if (! $notifiable instanceof Customer) {
|
|
throw new \InvalidArgumentException('Invalid notifiable provided.');
|
|
}
|
|
|
|
$appKey = Config::string('app.key');
|
|
|
|
if (empty(trim($appKey))) {
|
|
throw new \UnexpectedValueException('App key is not set.');
|
|
}
|
|
|
|
return url(
|
|
URL::temporarySignedRoute(
|
|
'fo.auth.verification.verify',
|
|
Date::now()->addMinutes(Config::integer('auth.verification.expire', 60)),
|
|
[
|
|
'id' => $notifiable->getKey(),
|
|
'hash' => hash_hmac(
|
|
'sha256',
|
|
$notifiable->getEmailForVerification(),
|
|
$appKey
|
|
),
|
|
],
|
|
false
|
|
)
|
|
);
|
|
}
|
|
|
|
private function createPasswordResetUrl(mixed $notifiable, string $token): string
|
|
{
|
|
if (! $notifiable instanceof Customer) {
|
|
throw new \InvalidArgumentException('Invalid notifiable provided.');
|
|
}
|
|
|
|
return url(
|
|
URL::temporarySignedRoute(
|
|
'fo.auth.reset-password',
|
|
Date::now()->addMinutes(Config::integer('auth.reset.expire', 60)),
|
|
[
|
|
'token' => $token,
|
|
'email' => $notifiable->getEmailForPasswordReset(),
|
|
],
|
|
false
|
|
)
|
|
);
|
|
}
|
|
|
|
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,
|
|
};
|
|
}
|
|
}
|