feat(api): implémentation du service KazApiService et intégration dans le UserController
- Création du service `KazApiService` pour gérer les interactions avec l'API Kaz. - Authentification via token JWT. - Vérification de l'existence d'un utilisateur dans le service LDAP. - Ajout d'une route dans le `UserController` pour rendre une vue utilisateur basée sur les données obtenues via l'API. - Configuration du client HTTP `kaz_api.client` dans `framework.yaml` avec une base URI et des headers par défaut. - Ajout des paramètres d'environnement liés à l'API (`KAZ_API_USER`, `KAZ_API_PASSWORD`, `KAZ_API_BASE_URL`) dans le fichier `.env`. - Mise à jour des services dans `services.yaml` pour inclure les dépendances nécessaires.
This commit is contained in:
39
src/Controller/UserController.php
Normal file
39
src/Controller/UserController.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Service\KazApiService;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
|
||||
|
||||
class UserController extends AbstractController
|
||||
{
|
||||
/**
|
||||
* Permet de vérifier si un utilisateur existe dans le ldap.
|
||||
*
|
||||
* @param string $email L'adresse e-mail de l'utilisateur.
|
||||
* @param KazApiService $apiClient Le service utilisé pour récupérer les données utilisateur.
|
||||
*
|
||||
* @return Response La page index utilisateur rendue.
|
||||
* @throws ClientExceptionInterface
|
||||
* @throws DecodingExceptionInterface
|
||||
* @throws RedirectionExceptionInterface
|
||||
* @throws ServerExceptionInterface
|
||||
* @throws TransportExceptionInterface
|
||||
*/
|
||||
#[Route('/user/{email}')]
|
||||
public function index(string $email, KazApiService $apiClient): Response
|
||||
{
|
||||
$exist = $apiClient->getUserData($email);
|
||||
|
||||
return $this->render('user/index.html.twig', [
|
||||
'exist' => $exist,
|
||||
]);
|
||||
}
|
||||
}
|
||||
80
src/Service/KazApiService.php
Normal file
80
src/Service/KazApiService.php
Normal file
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
|
||||
use Exception;
|
||||
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
|
||||
class KazApiService
|
||||
{
|
||||
private ?string $token = null;
|
||||
|
||||
public function __construct(
|
||||
private readonly HttpClientInterface $kazApiClient,
|
||||
private readonly string $apiUser,
|
||||
private readonly string $apiPassword
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Récupère le token JWT via l'authentification Basic
|
||||
*
|
||||
* @return string
|
||||
* @throws ClientExceptionInterface
|
||||
* @throws DecodingExceptionInterface
|
||||
* @throws RedirectionExceptionInterface
|
||||
* @throws ServerExceptionInterface
|
||||
* @throws TransportExceptionInterface
|
||||
*/
|
||||
private function getToken(): string
|
||||
{
|
||||
if ($this->token) {
|
||||
return $this->token;
|
||||
}
|
||||
|
||||
$response = $this->kazApiClient->request('POST', '/get_token', [
|
||||
'auth_basic' => [$this->apiUser, $this->apiPassword]
|
||||
]);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Impossible de récupérer le token JWT');
|
||||
}
|
||||
|
||||
$data = $response->toArray();
|
||||
$this->token = $data['token']; // Ajustez la clé selon le format de votre API
|
||||
|
||||
return $this->token;
|
||||
}
|
||||
|
||||
/**
|
||||
* Permet de vérifier si un utilisateur existe dans le ldap.
|
||||
*
|
||||
* @param string $email L'adresse e-mail de l'utilisateur à rechercher.
|
||||
*
|
||||
* @return array Les données utilisateur renvoyées par l'API.
|
||||
*
|
||||
* @throws ClientExceptionInterface
|
||||
* @throws DecodingExceptionInterface
|
||||
* @throws RedirectionExceptionInterface
|
||||
* @throws ServerExceptionInterface
|
||||
* @throws TransportExceptionInterface
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getUserData(string $email): array
|
||||
{
|
||||
$options['headers']['Authorization'] = 'Bearer ' . $this->getToken();
|
||||
|
||||
$response = $this->kazApiClient->request('GET', "/ldap/user/$email", $options);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Erreur lors de l\'appel API : ' . $response->getStatusCode());
|
||||
}
|
||||
|
||||
return $response->toArray();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user