<?php
namespace App\Security;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use App\Entity\Users;
class LegacyUserProvider extends AbstractController implements UserProviderInterface, PasswordUpgraderInterface
{
const ROLES_FOR_CLIENT = array('ROLE_CLIENT');
const ROLES_FOR_WORKER = array('ROLE_WORKER');
const ROLES_FOR_ADMIN = array('ROLE_WORKER');
/**
* Symfony calls this method if you use features like switch_user
* or remember_me.
*
* @return UserInterface
* @throws UsernameNotFoundException if the user is not found
*/
public function loadUserByUsername($username)
{
$user = $this->getDoctrine()->getRepository(Users::class)->findOneBy(['email' => $username]);
if (is_null($user)) {
throw new UsernameNotFoundException();
}
$userData = array(
'password' => $user->getPassword(),
'firstName' => $user->getFirstName(),
'lastName' => $user->getLastName(),
'type' => $user->getType()
);
switch ($userData['type']) {
case 'admin':
$roles = self::ROLES_FOR_ADMIN;
break;
case 'worker':
$roles = self::ROLES_FOR_WORKER;
break;
case 'client':
$roles = self::ROLES_FOR_CLIENT;
break;
}
$loadedUser = new ShopUser();
$loadedUser
->setEmail($username)
->setPassword($userData['password'])
->setFirstName($userData['firstName'])
->setLastName($userData['lastName'])
->setType($userData['type'])
->setRoles($roles);
return $loadedUser;
}
/**
* 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.
*
* @return UserInterface
*/
public function refreshUser(UserInterface $user)
{
if (!$user instanceof ShopUser) {
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.
$userExistence = $this->loadUserByUsername($user->getEmail());
if (is_null($userExistence)) {
throw new UsernameNotFoundException();
}
return $user;
}
/**
* Tells Symfony to use this provider for this User class.
*/
public function supportsClass($class)
{
return ShopUser::class === $class;
}
/**
* Upgrades the encoded password of a user, typically for using a better hash algorithm.
*/
public function upgradePassword(UserInterface $user, string $newEncodedPassword): void
{
// TODO: when encoded passwords are in use, this method should:
// 1. persist the new password in the user storage
// 2. update the $user object with $user->setPassword($newEncodedPassword);
}
}