ldc-cocotte/app/Providers/AuthServiceProvider.php

188 lines
6.4 KiB
PHP
Raw Permalink 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\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;
feat: implement pagination using Pagination value object - Updated DataTableService to accept a Pagination object for pagination parameters. - Refactored paginateQuery method to utilize pageSize and pageNumber from Pagination. refactor: replace ActionExecutor with Executor in controller traits - Updated ChangesModelOrder, TogglesModelStar, and TogglesModelStatus traits to use Executor interface instead of ActionExecutor. feat: add PictureMetadata and Slug value objects - Introduced PictureMetadata for managing image metadata with validation. - Added Slug value object for slug generation and validation. feat: update breadcrumbs to support RangeDto - Modified breadcrumbs-bo.php to accept RangeDto in the edit route. test: add tests for RangesController - Added tests for accessing, creating, updating, and deleting ranges. - Included tests for toggling range status. test: add integration tests for range actions - Created tests for CreateRange, DeleteRange, and UpdateRange actions. - Validated input handling and database interactions. test: add unit tests for range input classes - Implemented tests for CreateRangeInput and UpdateRangeInput to validate input constraints. test: add unit tests for value objects - Added tests for PictureMetadata and Slug to ensure proper validation and instantiation. test: add unit tests for RangeDto - Created tests to verify RangeDto mapping from Range model. test: add unit tests for RangePolicy - Implemented tests for RangePolicy to validate permissions based on user roles.
2026-09-29 16:59:07 +02:00
use App\Dto\RangeDto;
use App\Dto\SettingDto;
use App\Dto\ShopDto;
use App\Dto\SlideDto;
use App\Dto\TagDto;
use App\Jobs\FetchAndUpsertArcadiaEmployees;
2026-06-14 21:32:07 +02:00
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;
feat: implement pagination using Pagination value object - Updated DataTableService to accept a Pagination object for pagination parameters. - Refactored paginateQuery method to utilize pageSize and pageNumber from Pagination. refactor: replace ActionExecutor with Executor in controller traits - Updated ChangesModelOrder, TogglesModelStar, and TogglesModelStatus traits to use Executor interface instead of ActionExecutor. feat: add PictureMetadata and Slug value objects - Introduced PictureMetadata for managing image metadata with validation. - Added Slug value object for slug generation and validation. feat: update breadcrumbs to support RangeDto - Modified breadcrumbs-bo.php to accept RangeDto in the edit route. test: add tests for RangesController - Added tests for accessing, creating, updating, and deleting ranges. - Included tests for toggling range status. test: add integration tests for range actions - Created tests for CreateRange, DeleteRange, and UpdateRange actions. - Validated input handling and database interactions. test: add unit tests for range input classes - Implemented tests for CreateRangeInput and UpdateRangeInput to validate input constraints. test: add unit tests for value objects - Added tests for PictureMetadata and Slug to ensure proper validation and instantiation. test: add unit tests for RangeDto - Created tests to verify RangeDto mapping from Range model. test: add unit tests for RangePolicy - Implemented tests for RangePolicy to validate permissions based on user roles.
2026-09-29 16:59:07 +02:00
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;
2026-06-14 21:32:07 +02:00
use App\Policies\Frontend\BasketPolicy;
use App\Policies\Frontend\ProductPolicy as FrontendProductPolicy;
2026-06-14 21:32:07 +02:00
use App\Services\CrmService;
use Domain\Services\ShopperService;
use Illuminate\Auth\Notifications\ResetPassword;
use Illuminate\Auth\Notifications\VerifyEmail;
use Illuminate\Foundation\Application;
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\Date;
2026-06-14 21:32:07 +02:00
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\URL;
2026-06-14 21:32:07 +02:00
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
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,
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 (Application $app): AppAdminAuthService => new AppAdminAuthService(
2026-06-14 21:32:07 +02:00
Auth::guard('backend')
)
);
$this->app->singleton(
AppCustomerAuthService::class,
fn (Application $app): AppCustomerAuthService => new AppCustomerAuthService(
2026-06-14 21:32:07 +02:00
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 = str_replace('Dto', '', class_basename($modelClass));
2026-06-14 21:32:07 +02:00
$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
)
);
2026-06-14 21:32:07 +02:00
}
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
}
}