src/Security/LegacyUserProvider.php line 28

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
  4. use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
  5. use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
  6. use Symfony\Component\Security\Core\User\UserInterface;
  7. use Symfony\Component\Security\Core\User\UserProviderInterface;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use App\Entity\Users;
  10. class LegacyUserProvider extends AbstractController implements UserProviderInterfacePasswordUpgraderInterface
  11. {
  12.     const ROLES_FOR_CLIENT = array('ROLE_CLIENT');
  13.     const ROLES_FOR_WORKER = array('ROLE_WORKER');
  14.     const ROLES_FOR_ADMIN = array('ROLE_WORKER');
  15.     /**
  16.      * Symfony calls this method if you use features like switch_user
  17.      * or remember_me.
  18.      *
  19.      * @return UserInterface
  20.      * @throws UsernameNotFoundException if the user is not found
  21.      */
  22.     public function loadUserByUsername($username)
  23.     {
  24.         $user $this->getDoctrine()->getRepository(Users::class)->findOneBy(['email' => $username]);
  25.         if (is_null($user)) {
  26.             throw new UsernameNotFoundException();
  27.         }
  28.         
  29.         $userData = array(
  30.             'password' => $user->getPassword(),
  31.             'firstName' => $user->getFirstName(),
  32.             'lastName' => $user->getLastName(),
  33.             'type' => $user->getType()
  34.         );
  35.         switch ($userData['type']) {
  36.             case 'admin':
  37.                 $roles =  self::ROLES_FOR_ADMIN;
  38.                 break;
  39.             case 'worker':
  40.                 $roles =  self::ROLES_FOR_WORKER;
  41.                 break;
  42.             case 'client':
  43.                 $roles =  self::ROLES_FOR_CLIENT;
  44.                 break;
  45.         }
  46.         $loadedUser = new ShopUser();
  47.         $loadedUser
  48.             ->setEmail($username)
  49.             ->setPassword($userData['password'])
  50.             ->setFirstName($userData['firstName'])
  51.             ->setLastName($userData['lastName'])
  52.             ->setType($userData['type'])
  53.             ->setRoles($roles);
  54.         return $loadedUser;
  55.     }
  56.     /**
  57.      * Refreshes the user after being reloaded from the session.
  58.      *
  59.      * When a user is logged in, at the beginning of each request, the
  60.      * User object is loaded from the session and then this method is
  61.      * called. Your job is to make sure the user's data is still fresh by,
  62.      * for example, re-querying for fresh User data.
  63.      *
  64.      * If your firewall is "stateless: true" (for a pure API), this
  65.      * method is not called.
  66.      *
  67.      * @return UserInterface
  68.      */
  69.     public function refreshUser(UserInterface $user)
  70.     {
  71.         if (!$user instanceof ShopUser) {
  72.             throw new UnsupportedUserException(sprintf('Invalid user class "%s".'get_class($user)));
  73.         }
  74.         // Return a User object after making sure its data is "fresh".
  75.         // Or throw a UsernameNotFoundException if the user no longer exists.
  76.         $userExistence $this->loadUserByUsername($user->getEmail());
  77.         if (is_null($userExistence)) {
  78.             throw new UsernameNotFoundException();
  79.         }
  80.         return $user;
  81.     }
  82.     /**
  83.      * Tells Symfony to use this provider for this User class.
  84.      */
  85.     public function supportsClass($class)
  86.     {
  87.         return ShopUser::class === $class;
  88.     }
  89.     /**
  90.      * Upgrades the encoded password of a user, typically for using a better hash algorithm.
  91.      */
  92.     public function upgradePassword(UserInterface $userstring $newEncodedPassword): void
  93.     {
  94.         // TODO: when encoded passwords are in use, this method should:
  95.         // 1. persist the new password in the user storage
  96.         // 2. update the $user object with $user->setPassword($newEncodedPassword);
  97.     }
  98. }