2020-12-01 17:02:10 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Providers;
|
|
|
|
|
|
2023-10-06 15:00:42 +02:00
|
|
|
use App\Enums\Users\RoleEnum;
|
2021-08-09 17:35:28 +02:00
|
|
|
use App\Lib\Helper;
|
|
|
|
|
use App\Models\Service;
|
2023-10-06 15:00:42 +02:00
|
|
|
use App\Models\User;
|
2025-07-28 09:46:22 +02:00
|
|
|
use App\Policies\StatisticPolicy;
|
2021-08-09 17:35:28 +02:00
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
2020-12-01 17:02:10 +01:00
|
|
|
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
|
2021-08-09 17:35:28 +02:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
2023-10-06 15:00:42 +02:00
|
|
|
use Illuminate\Support\Facades\Gate;
|
2020-12-01 17:02:10 +01:00
|
|
|
|
|
|
|
|
class AuthServiceProvider extends ServiceProvider
|
|
|
|
|
{
|
2023-06-20 17:37:15 +02:00
|
|
|
/**
|
2023-10-06 15:00:42 +02:00
|
|
|
* The model to policy mappings for the application.
|
2023-06-20 17:37:15 +02:00
|
|
|
*
|
2023-10-06 15:00:42 +02:00
|
|
|
* @var array<class-string, class-string>
|
2023-06-20 17:37:15 +02:00
|
|
|
*/
|
2023-06-26 09:24:42 +02:00
|
|
|
protected $policies = [];
|
2023-06-20 17:37:15 +02:00
|
|
|
|
2020-12-01 17:02:10 +01:00
|
|
|
/**
|
|
|
|
|
* Register any authentication / authorization services.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2023-09-06 17:24:53 +02:00
|
|
|
public function boot(): void
|
2020-12-01 17:02:10 +01:00
|
|
|
{
|
2023-06-20 17:37:15 +02:00
|
|
|
// Register policies.
|
|
|
|
|
$this->registerPolicies();
|
2021-08-09 17:35:28 +02:00
|
|
|
|
2023-10-06 15:00:42 +02:00
|
|
|
Gate::define('isConceptor', function (User $user) {
|
|
|
|
|
return $user->role === RoleEnum::conceptor;
|
|
|
|
|
});
|
|
|
|
|
|
2025-07-28 09:46:22 +02:00
|
|
|
Gate::define('viewStatsByBrand', [StatisticPolicy::class, 'viewStatsByBrand']);
|
|
|
|
|
Gate::define('viewByQuestion', [StatisticPolicy::class, 'viewByQuestion']);
|
|
|
|
|
Gate::define('answersByDays', [StatisticPolicy::class, 'answersByDays']);
|
|
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
Auth::viaRequest('api-hmac', function (Request $request) {
|
|
|
|
|
|
2021-08-09 17:35:28 +02:00
|
|
|
/**
|
2021-12-19 15:57:24 +01:00
|
|
|
* Un token devrais être fourni pour chaque requete
|
2021-08-09 17:35:28 +02:00
|
|
|
*/
|
2021-12-19 15:57:24 +01:00
|
|
|
if (is_null($request->bearer_token)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2021-08-09 17:35:28 +02:00
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
// * Si la route actuelle n'as pas de nom alors on refuse de la traiter.
|
|
|
|
|
if (!(Helper::getRoute($request))) {
|
|
|
|
|
// @codeCoverageIgnoreStart
|
|
|
|
|
return null;
|
|
|
|
|
// @codeCoverageIgnoreEnd
|
2021-08-09 17:35:28 +02:00
|
|
|
}
|
|
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
$service = null;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// * On récupère le service actuel correspondant au token fourni.
|
|
|
|
|
$service = Service::getFromBearerToken($request->bearer_token ?? '');
|
|
|
|
|
} catch (ModelNotFoundException $e) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// * Preload the brand as we will access it often later.
|
|
|
|
|
$service = $service->load('brand');
|
|
|
|
|
|
2021-08-09 17:35:28 +02:00
|
|
|
/**
|
2023-09-06 17:24:53 +02:00
|
|
|
** All ApiV4 routes must pass Brand model to controller action since a Brand owns
|
2021-08-09 17:35:28 +02:00
|
|
|
** a service token, otherwise the api cannot be called.
|
|
|
|
|
** This hack is a custom binder instead of passing
|
|
|
|
|
** this to route as a parameter
|
|
|
|
|
*/
|
2021-09-21 12:08:28 +02:00
|
|
|
app()->bind(Service::class, function () use ($service) {
|
|
|
|
|
return $service;
|
|
|
|
|
});
|
2021-12-19 15:57:24 +01:00
|
|
|
|
|
|
|
|
// * On authenticate la request en vérifiant sa signature.
|
|
|
|
|
if ($service->authenticateApi($request)) {
|
2021-09-21 12:08:28 +02:00
|
|
|
return $service;
|
|
|
|
|
}
|
2021-12-19 15:57:24 +01:00
|
|
|
|
|
|
|
|
return null;
|
2021-08-09 17:35:28 +02:00
|
|
|
});
|
2020-12-01 17:02:10 +01:00
|
|
|
}
|
|
|
|
|
}
|