<?php
/**
* Created by PhpStorm.
* User: samuelp
* Date: 10/20/17
* Time: 10:33 PM
*/
namespace App\Auth;
use App\Entity\OrganizationUser;
use App\Entity\User;
use App\Entity\UserStation;
use App\Form\LoginForm;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\Exception\CustomUserMessageAccountStatusException;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
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\UserBadge;
use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials;
use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
use UnexpectedValueException;
class Authenticator extends AbstractLoginFormAuthenticator implements PasswordAuthenticatedUserInterface{
private EntityManagerInterface $entityManager;
private UserPasswordHasherInterface $passwordHasher;
private UrlGeneratorInterface $urlGenerator;
private FormFactoryInterface $formFactory;
private User $user;
public function __construct(UrlGeneratorInterface $urlGenerator,
EntityManagerInterface $entityManager,
CsrfTokenManagerInterface $csrfTokenManager,
UserPasswordHasherInterface $passwordHasher,
Security $security)
{
$this->urlGenerator = $urlGenerator;
$this->entityManager = $entityManager;
$this->csrfTokenManager = $csrfTokenManager;
$this->passwordHasher = $passwordHasher;
$this->security = $security;
}
protected function getLoginUrl(Request $request): string
{
// TODO: Implement getLoginUrl() method.
return $this->urlGenerator->generate('security_signin');
}
public function authenticate(Request $request) {
// TODO: Implement getCredentials() method.
$username = $request->request->get('username', '');
// dump($username);
// die;
$request->getSession()->set(Security::LAST_USERNAME, $username);
$user = $this->entityManager->getRepository(User::class)->findOneBy([
'username' => $username
]);
if(!$user){
throw new CustomUserMessageAccountStatusException("invalid Username or Password ");
}
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
{
$user = $token->getUser();
/** @var OrganizationUser $saccoUser */
$saccoUser = $this->entityManager->getRepository( OrganizationUser::class)->findOneBy([
'user' => $user
]);
$request->getSession()->set('ORGANIZATION', $saccoUser->getOrganization()->getId());
if(!$saccoUser) {
return new RedirectResponse($this->urlGenerator->generate('no_mans_land'));
}
/** @var UserStation $userStation */
$userStation = $this->entityManager->getRepository(UserStation::class)->findOneBy([
'user' => $user,
'isActive' => true
]);
if($userStation) {
$request->getSession()->set('STATION', $userStation->getStation()->getId());
return new RedirectResponse($this->urlGenerator->generate('parcels_homepage'));
}
return new RedirectResponse($this->urlGenerator->generate('homepage'));
}
public function getPassword(): ?string
{
return '';
}
}