<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\Security\Http\Util\TargetPathTrait;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use App\Entity\User;
use App\Repository\UserRepository;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
/**
* Controller used to manage the application security.
* See https://symfony.com/doc/current/security/form_login_setup.html.
*
* @author Ryan Weaver <weaverryan@gmail.com>
* @author Javier Eguiluz <javier.eguiluz@gmail.com>
*/
class SecurityController extends AbstractController {
use TargetPathTrait;
private $security;
public function __construct(Security $security) {
$this->security = $security;
}
#[Route('/login', name: 'security_login')]
public function login(Request $request, AuthenticationUtils $helper, UserPasswordHasherInterface $passwordHasher, ManagerRegistry $entityManager): Response {
// if user is already logged in, don't display the login page again
if ($this->getUser()) {
if ($this->security->isGranted('ROLE_ADMIN')) {
return $this->redirectToRoute('admin_dashboard');
}
}
// create the user and hash its password
// $user = new User();
// $user->setFullName("Visitor");
// $user->setUsername("user");
// $user->setEmail("visitor@evenews.com");
// $user->setRoles(['ROLE_USER']);
// $user->setEnabled('1');
//
// $hashedPassword = $passwordHasher->hashPassword($user, '123456');
// $user->setPassword($hashedPassword);
//
// $entityManager->persist($user);
// $entityManager->flush();
///end create the user
// this statement solves an edge-case: if you change the locale in the login
// page, after a successful login you are redirected to a page in the previous
// locale. This code regenerates the referrer URL whenever the login page is
// browsed, to ensure that its locale is always the current one.
if ($this->security->isGranted('ROLE_ADMIN')) {
$this->saveTargetPath($request->getSession(), 'main', $this->generateUrl('admin_dashboard'));
} else if ($this->security->isGranted('ROLE_CHIEF')) {
$this->saveTargetPath($request->getSession(), 'main', $this->generateUrl('chief_dashboard'));
} else if ($this->security->isGranted('ROLE_EDITOR')) {
$this->saveTargetPath($request->getSession(), 'main', $this->generateUrl('editor_dashboard'));
}
return $this->render('security/login.html.twig', [
// last username entered by the user (if any)
'last_username' => $helper->getLastUsername(),
// last authentication error (if any)
'error' => $helper->getLastAuthenticationError(),
]);
}
/**
* This is the route the user can use to logout.
*
* But, this will never be executed. Symfony will intercept this first
* and handle the logout automatically. See logout in config/packages/security.yaml
*/
#[Route('/logout', name: 'security_logout')]
public function logout(): void {
throw new \Exception('This should never be reached!');
}
}