<?php
namespace App\Security;
use App\Entity\Client;
use App\Services\CallApiServices;
use App\Services\QuestionMailService;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
class UserProvider implements UserProviderInterface, PasswordUpgraderInterface
{
private $callApiServices;
private $encoder;
private $questionMailService;
public function __construct(CallApiServices $callApiServices, UserPasswordHasherInterface $encoder, QuestionMailService $questionMailService)
{
$this->encoder = $encoder;
$this->callApiServices = $callApiServices;
$this->questionMailService = $questionMailService;
}
/**
* Symfony calls this method if you use features like switch_user
* or remember_me.
*
* If you're not using these features, you do not need to implement
* this method.
*
* @throws UserNotFoundException
*
*
* not found
*/
public function loadUserByIdentifier($identifier): UserInterface
{
$password = $_REQUEST['password'];
$email = $identifier;
// $token = $this->callApiServices->authApi($email, $password);
// $user = $this->callApiServices->getUserDetails($token['token']);
$user = $this->callApiServices->clientAuthentification($email, $password, 1033);
if (isset($user['responseCode'])){
$error404 = $user['message'];
throw new UserNotFoundException("Error Processing Request", 1);
}else{
$client = new Client();
$client->setApiToken($user['apiToken']);
$client->setEmail($user['username']);
$client->setApiToken($user['apiToken']);
$client->setLimitToken($user['limitToken']);
$client->setFirstName($user['prenom']);
$client->setLastName($user['nom']);
$client->setClientId($user['clientId']);
$client->setServiceId($user['serviceId']);
$client->setGenre($user['genre']);
$client->setSolde($user['solde']);
$client->setCredit($user['credit']);
$client->setCode($user['code']);
$client->setAddressIP($user['clientIp']);
// $client->setRoles($user['roles']);
$passHash = $this->encoder->hashPassword($client, $password);
$client->setPassword($passHash);
$client = $this->questionMailService->getEmail($this->callApiServices, $client);
}
// Load a User object from your data source or throw UserNotFoundException.
// The $identifier argument may not actually be a username:
// it is whatever value is being returned by the getUserIdentifier()
// method in your User class.
// throw new \Exception('TODO: fill in loadUserByIdentifier() inside ' . __FILE__);
return $client;
}
/**
* @deprecated since Symfony 5.3, loadUserByIdentifier() is used instead
*/
public function loadUserByUsername($username): UserInterface
{
return $this->loadUserByIdentifier($username);
}
/**
* Refreshes the user after being reloaded from the session.
*
* When a user is logged in, at the beginning of each request, the
* User object is loaded from the session and then this method is
* called. Your job is to make sure the user's data is still fresh by,
* for example, re-querying for fresh User data.
*
* If your firewall is "stateless: true" (for a pure API), this
* method is not called.
*/
public function refreshUser(UserInterface $user): UserInterface
{
if (!$user instanceof Client) {
throw new UnsupportedUserException(sprintf('Invalid user class "%s".', get_class($user)));
}
// Return a User object after making sure its data is "fresh".
// Or throw a UsernameNotFoundException if the user no longer exists.
return $user;
}
/**
* Tells Symfony to use this provider for this User class.
*/
public function supportsClass($class): bool
{
return Client::class === $class || is_subclass_of($class, Client::class);
}
/**
* Upgrades the hashed password of a user, typically for using a better hash algorithm.
*/
public function upgradePassword(PasswordAuthenticatedUserInterface $user, string $newHashedPassword): void
{
// TODO: when hashed passwords are in use, this method should:
// 1. persist the new password in the user storage
// 2. update the $user object with $user->setPassword($newHashedPassword);
}
}