ldc-boutique-cnc-sable/app/Providers/AuthServiceProvider.php

122 lines
4.1 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\DiscountDto;
use App\Dto\FaqDto;
use App\Dto\PageDto;
use App\Dto\PickupExceptionDto;
use App\Dto\ProductDto;
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\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,
DiscountDto::class => DiscountPolicy::class,
FaqDto::class => FaqPolicy::class,
PageDto::class => PagePolicy::class,
PickupExceptionDto::class => PickupExceptionPolicy::class,
ProductDto::class => ProductPolicy::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 ($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 = 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;
});
}
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
}
}