<?phpnamespace App\Security;use App\Entity\User;use Doctrine\ORM\EntityManagerInterface;use Symfony\Component\Security\Core\User\UserInterface;use Symfony\Component\Security\Core\User\UserProviderInterface;use Symfony\Component\Security\Core\Exception\UserNotFoundException;use Symfony\Component\Security\Core\Exception\UnsupportedUserException;use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;class UserProvider implements UserProviderInterface{ private $em; private $repoUser; public function __construct(EntityManagerInterface $em) { $this->em = $em; $this->repoUser = $em->getRepository(User::class); } private function defaultAdmin() { return new User( 'admin', 'tallertecno112132', ['ROLE_ADMIN'] ); } private function fetchByUsername($username) { $user = $this->repoUser->findOneBy(['username' => $username]); //exit(json_encode($this->adminUser['roles'])); if ($user === null && $username === 'admin') { $user = $this->defaultAdmin(); $this->em->persist($user); } if ($user && $user->getUsername() === 'admin') { $usingDefaultPass = $user->comparePassword($this->defaultAdmin()->getPassword()); $user->needsPassSetup = $usingDefaultPass; $user->needsEmailSetup = $user->getEmail() === null; } return $user; } /** * * @return UserInterface * * @throws UsernameNotFoundException if the user is not found */ public function loadUserByUsername($username) { $user = $this->fetchByUsername($username); if ($user === null) { throw new UsernameNotFoundException(); } return $user; } public function loadUserByIdentifier(string $identifier): UserInterface { $user = $this->fetchByUsername($identifier); if ($user === null) { $e = new UserNotFoundException(sprintf('User "%s" not found.', $identifier)); $e->setUserIdentifier($identifier); throw $e; } return $user; } public function getIdentifier(): string { return 'username'; } /** * * @return UserInterface */ public function refreshUser(UserInterface $user) { if (!$user instanceof User) { throw new UnsupportedUserException(sprintf('Invalid user class "%s".', get_class($user))); } return $this->fetchByUsername($user->getUsername()); } /** * Tells Symfony to use this provider for this User class. */ public function supportsClass($class) { return User::class === $class; }}