<?php
namespace App\Security;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use http\Message;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\Exception\CustomUserMessageAccountStatusException;
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\CsrfTokenBadge;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\PasswordUpgradeBadge;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials;
use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface;
use Symfony\Component\Security\Http\Util\TargetPathTrait;
class LoginFormAuthenticator extends AbstractLoginFormAuthenticator implements PasswordAuthenticatedUserInterface{
use TargetPathTrait;
public const LOGIN_ROUTE = 'app_login';
private UrlGeneratorInterface $urlGenerator;
private EntityManagerInterface $entityManager;
private CsrfTokenManagerInterface $csrfTokenManager;
private UserPasswordEncoderInterface $passwordEncoder;
private Security $security;
public function __construct(UrlGeneratorInterface $urlGenerator, EntityManagerInterface $entityManager, CsrfTokenManagerInterface $csrfTokenManager, UserPasswordEncoderInterface $userPasswordEncoder, Security $security)
{
$this->urlGenerator = $urlGenerator;
$this->entityManager = $entityManager;
$this->csrfTokenManager = $csrfTokenManager;
$this->passwordEncoder = $userPasswordEncoder;
$this->security = $security;
}
public function supports(Request $request): bool
{
return self::LOGIN_ROUTE === $request->attributes->get('_route') && $request->isMethod('POST');
}
public function authenticate(Request $request): PassportInterface
{
$phone = $request->request->get('phone', '');
$request->getSession()->set(Security::LAST_USERNAME, $phone);
$user = $this->entityManager->getRepository(User::class)->findOneBy([
'phone' => $phone
]);
if (!$user){
throw new CustomUserMessageAccountStatusException("Wrong username or password");
}
if(!$user->getIsACtive()){
throw new CustomUserMessageAccountStatusException("deactivated user! contact admin");
}
if(!in_array('ROLE_WEB', $user->getRoles())){
throw new CustomUserMessageAccountStatusException("you can not access this data");
}
if(in_array('ROLE_WEB_BRANCH', $user->getRoles())){
$branch = $user->getBranch();
$request->getSession()->set('branch', $branch->getId());
}
// dd($user);
return new Passport(
new UserBadge($user),
new PasswordCredentials($request->request->get('password', '')),
[
new CsrfTokenBadge('authenticate', $request->get('_csrf_token')),
]
);
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response
{
/** @var User $user */
$user = $token->getUser();
$request->getSession()->set('branch_name', 'NEEMA OPTICIANS '.$user->getBranch()->getBranchName());
// For example:
// return new RedirectResponse($this->urlGenerator->generate('some_route'));
// throw new \Exception('TODO: provide a valid redirect inside '.__FILE__);
// if(in_array('ROLE_WEB', $token->getUser()->getRoles(), true)){
//
// return new RedirectResponse($this->urlGenerator->generate('homeRoute'));
// }
return new RedirectResponse($this->urlGenerator->generate('homeRoute'));
}
protected function getLoginUrl(Request $request): string
{
return $this->urlGenerator->generate(self::LOGIN_ROUTE);
}
public function getPassword(): ?string
{
// TODO: Implement getPassword() method.
return '';
}
}