<?php
namespace AppBundle\Controller;
use App\Service\Message;
use AppBundle\Entity\Admin;
use AppBundle\Entity\Employer;
use AppBundle\Entity\Participant;
use AppBundle\Entity\Reseller;
use AppBundle\Entity\Supervisor;
use AppBundle\Entity\User;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Security;
class AccountController extends AbstractController
{
private $user;
public function __construct(Security $security)
{
$this->user = $security->getUser();
}
/**
* @Route("/", methods={"GET"}, name="login_home")
* @Route("/account/login", methods={"GET"}, name="login")
*/
public function loginAction()
{
$user = $this->getUser();
if ($user) {
$this->redirectToUser($user);
}
return $this->render('account/login.html.twig');
}
/**
* @Route("/account/forgot", methods={"GET"}, name="forgot_password")
*/
public function forgotAction()
{
return $this->render('account/password_forgot.html.twig');
}
/**
* @Route("/account/forgot", methods={"POST"}, name="forgot_password_post")
*/
public function forgotPostAction(Request $request)
{
$login = $request->request->get('login');
if (!$login) {
$this->addFlash('account', 'Please provide a user name or email address');
return $this->redirectToRoute('forgot_password');
}
$service = $this->getUserService();
if (!$service->sendResetEmail($login)) {
$this->addFlash('account', 'Account not found');
return $this->redirectToRoute('forgot_password');
}
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('password_request_sent');
}
/**
* @Route("/account/request_sent", methods={"GET"}, name="password_request_sent")
*/
public function requestSentAction()
{
return $this->render('account/password_request_sent.html.twig');
}
/**
* @param Request $request
* @Route("/account/reset", methods={"GET"}, name="reset_password")
*/
public function resetAction(Request $request)
{
try {
$user = $this->getResetUser($request->query->get('login'), $request->query->get('code'));
} catch (\Throwable $e) {
$this->addFlash('account', $e->getMessage());
return $this->redirectToRoute('login_home');
}
return $this->render('account/password_reset.html.twig', array(
'user' => $user
));
}
/**
* @Route("/account/reset", methods={"POST"}, name="reset_password_post")
*/
public function resetPostAction(Request $request)
{
try {
$user = $this->getResetUser($request->request->get('original_login'), $request->request->get('code'));
} catch (\Throwable $e) {
$this->addFlash('account', $e->getMessage());
return $this->redirectToRoute('login_home');
}
$this->getUserService()->changePassword($user, $request->request->get('password'));
$this->getDoctrine()->getManager()->flush();
$this->addFlash('account', 'Your password has been reset. Please log in with your new credentials');
return $this->redirectToRoute('login_home', array('login'=>$user->getUsername()));
}
/**
* @param string $login
* @param string $code
* @return User
* @throws \RuntimeException
*/
protected function getResetUser($login, $code)
{
$user = $this->getUserProviderService()->loadUserByUsername($login);
if (!$user) {
throw new \RuntimeException('No user account found');
}
if ($user->getPassword() !== $code) {
throw new \RuntimeException('Your password reset link has expired');
}
return $user;
}
/**
* If a user is logged in, redirect to their portal
* @param User $user
*/
protected function redirectToUser(User $user)
{
if ($user instanceof Admin) {
header('Location: /admin/index.php?LoggedIn=true');
}
elseif ($user instanceof Employer) {
header('Location: /employer/welcome.php?LoggedIn=true');
}
elseif ($user instanceof Participant) {
header('Location: /participant/index.php?LoggedIn=true');
}
elseif ($user instanceof Reseller) {
header('Location: /reseller/index.php?LoggedIn=true');
}
elseif ($user instanceof Supervisor) {
header('Location: /supervisor/index.php');
}
exit();
}
/**
* @return \AppBundle\Users\UserService
*/
protected function getUserService()
{
return $this->get2('users');
}
/**
* @return \AppBundle\Security\UserProvider
*/
protected function getUserProviderService()
{
return $this->get2('user_provider');
}
}