src/Security/UserProvider.php line 53

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use App\Entity\Client;
  4. use App\Services\CallApiServices;
  5. use App\Services\QuestionMailService;
  6. use Symfony\Component\Security\Core\User\UserInterface;
  7. use Symfony\Component\Security\Core\User\UserProviderInterface;
  8. use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
  9. use Symfony\Component\Security\Core\Exception\UserNotFoundException;
  10. use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
  11. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  12. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  13. class UserProvider implements UserProviderInterfacePasswordUpgraderInterface
  14. {
  15.     private $callApiServices;
  16.     private $encoder;
  17.     private $questionMailService;
  18.     public function __construct(CallApiServices $callApiServicesUserPasswordHasherInterface $encoderQuestionMailService $questionMailService)
  19.     {
  20.         $this->encoder $encoder;
  21.         $this->callApiServices $callApiServices;
  22.         $this->questionMailService $questionMailService;
  23.     }
  24.     /**
  25.      * Symfony calls this method if you use features like switch_user
  26.      * or remember_me.
  27.      *
  28.      * If you're not using these features, you do not need to implement
  29.      * this method.
  30.      *
  31.      * @throws UserNotFoundException
  32.      * 
  33.      * 
  34.      *  not found
  35.      */
  36.     public function loadUserByIdentifier($identifier): UserInterface
  37.     {
  38.         $password $_REQUEST['password'];
  39.         $email $identifier;
  40.       
  41.         // $token = $this->callApiServices->authApi($email, $password);
  42.         // $user = $this->callApiServices->getUserDetails($token['token']);
  43.         $user $this->callApiServices->clientAuthentification($email$password1033);
  44.       
  45.         if (isset($user['responseCode'])){
  46.             $error404 $user['message'];
  47.             throw new UserNotFoundException("Error Processing Request"1); 
  48.         }else{
  49.             $client = new Client();
  50.             $client->setApiToken($user['apiToken']);
  51.             $client->setEmail($user['username']);
  52.             $client->setApiToken($user['apiToken']);
  53.             $client->setLimitToken($user['limitToken']);
  54.             $client->setFirstName($user['prenom']);
  55.             $client->setLastName($user['nom']);
  56.             $client->setClientId($user['clientId']);
  57.             $client->setServiceId($user['serviceId']);
  58.             $client->setGenre($user['genre']);
  59.             $client->setSolde($user['solde']);
  60.             $client->setCredit($user['credit']);
  61.             $client->setCode($user['code']);
  62.             $client->setAddressIP($user['clientIp']);
  63.             // $client->setRoles($user['roles']);
  64.             $passHash $this->encoder->hashPassword($client$password);
  65.             $client->setPassword($passHash);
  66.             $client $this->questionMailService->getEmail($this->callApiServices$client);
  67.         }
  68.      
  69.       
  70.         // Load a User object from your data source or throw UserNotFoundException.
  71.         // The $identifier argument may not actually be a username:
  72.         // it is whatever value is being returned by the getUserIdentifier()
  73.         // method in your User class.
  74.         // throw new \Exception('TODO: fill in loadUserByIdentifier() inside ' . __FILE__);
  75.         return $client;
  76.     }
  77.     /**
  78.      * @deprecated since Symfony 5.3, loadUserByIdentifier() is used instead
  79.      */
  80.     public function loadUserByUsername($username): UserInterface
  81.     {
  82.         return $this->loadUserByIdentifier($username);
  83.     }
  84.     /**
  85.      * Refreshes the user after being reloaded from the session.
  86.      *
  87.      * When a user is logged in, at the beginning of each request, the
  88.      * User object is loaded from the session and then this method is
  89.      * called. Your job is to make sure the user's data is still fresh by,
  90.      * for example, re-querying for fresh User data.
  91.      *
  92.      * If your firewall is "stateless: true" (for a pure API), this
  93.      * method is not called.
  94.      */
  95.     public function refreshUser(UserInterface $user): UserInterface
  96.     {
  97.         if (!$user instanceof Client) {
  98.             throw new UnsupportedUserException(sprintf('Invalid user class "%s".'get_class($user)));
  99.         }
  100.         // Return a User object after making sure its data is "fresh".
  101.         // Or throw a UsernameNotFoundException if the user no longer exists.
  102.         return $user;
  103.     }
  104.     /**
  105.      * Tells Symfony to use this provider for this User class.
  106.      */
  107.     public function supportsClass($class): bool
  108.     {
  109.         return Client::class === $class || is_subclass_of($classClient::class);
  110.     }
  111.     /**
  112.      * Upgrades the hashed password of a user, typically for using a better hash algorithm.
  113.      */
  114.     public function upgradePassword(PasswordAuthenticatedUserInterface $userstring $newHashedPassword): void
  115.     {
  116.         // TODO: when hashed passwords are in use, this method should:
  117.         // 1. persist the new password in the user storage
  118.         // 2. update the $user object with $user->setPassword($newHashedPassword);
  119.     }
  120. }