<?php
namespace SymfonyDev\AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use SymfonyDev\AppBundle\Entity\User;
class SecurityController extends BaseController
{
/**
* @Route("/login", name="app_security_login")
* @Template
*/
public function loginAction(Request $request)
{
if ($this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) {
return $this->redirect($this->generateUrl('app_security_redirect'));
}
$authUtils = $this->get('security.authentication_utils');
return array(
'last_username' => $authUtils->getLastUsername(),
'error' => $authUtils->getLastAuthenticationError(),
);
}
/**
* @Route("/logout", name="app_security_logout")
* @Template
*/
public function logoutAction(Request $request)
{
$this->get('security.token_storage')->setToken(null);
$request->getSession()->invalidate();
return $this->redirect($this->generateUrl('app_security_login'));
}
/**
* @Route("/redirect", name="app_security_redirect")
*/
public function redirectAfterLoginAction()
{
$user = $this->getUser();
$route = 'app_security_login';
if ($user instanceof \Symfony\Component\Security\Core\User\UserInterface) {
if ($user->hasRole(\SymfonyDev\AppBundle\Entity\Role::ROLE_USER)) {
$route = 'app_dashboard_index';
} else {
$route = 'app_dashboard_index';
}
}
return $this->redirect($this->generateUrl($route));
}
/**
* @Route("/register/{type}", name="app_security_register")
* @Template
*/
public function registerAction(Request $request, $type)
{
if ($this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) {
return $this->redirect($this->generateUrl('app_security_redirect'));
}
$type = ucwords($type);
if (!in_array(ucwords($type), array(User::USER_USER_TYPE_PRACTITIONER, User::USER_USER_TYPE_STUDENT, User::USER_USER_TYPE_USER))) {
throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException();
}
$user = new User();
$user->addAddress(new \SymfonyDev\AppBundle\Entity\Address());
$form = $this->createForm(\SymfonyDev\AppBundle\Form\RegisterType::class, $user, array('user_type' => $type));
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
$data->setUserType($type);
$data->setIsEnable(false);
$data->setIsPwdSecure(true);
$this->get('user_manager')
->create($data);
$this->get('session')->getFlashBag()->add(
'ALERT_SUCCESS',
'You are registered successfully.'
);
return $this->redirectToRoute('app_dashboard_index');
}
return array(
'form' => $form->createView()
);
}
/**
* @Route("/forgot-password", name="app_security_forgot_password")
* @Template
*/
public function forgotPasswordAction(Request $request)
{
if ($this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) {
return $this->redirect($this->generateUrl('app_security_redirect'));
}
$dbm = $this->getDBM();
$obj = new \SymfonyDev\AppBundle\Entity\ForgotPassword();
$form = $this->createForm(\SymfonyDev\AppBundle\Form\ForgotPasswordType::class, $obj);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$obj = $form->getData();
$user = $dbm->getRepository('SymfonyDevAppBundle:User')
->findOneBy(array('email' => $obj->getTmpEmail()));
if (!$user) {
$this->get('session')->getFlashBag()->add(
'ALERT_ERROR',
'User is not registed with this email. Please check your email ID.'
);
return $this->redirectToRoute('app_security_forgot_password');
} else {
$token = uniqid(rand(100, 999));
$resetPasswordLink = $this->generateUrl('app_security_reset_password', array('id' => $user->getId(), 'key' => $token), \Symfony\Component\Routing\Generator\UrlGeneratorInterface::ABSOLUTE_URL);
$this->sendEmailToForgotPasswordUser($user, $resetPasswordLink);
$this->get('session')->getFlashBag()->add(
'ALERT_SUCCESS',
'Please check your registered email with us, click on the link in email to reset your password.'
);
$user->setResetPasswordToken($token);
$dbm->persist($user);
$dbm->flush();
return $this->redirectToRoute('app_security_login');
}
}
return array(
'form' => $form->createView()
);
}
/**
* Send forget password email to user
*/
private function sendEmailToForgotPasswordUser($obj, $resetPasswordLink)
{
$fromEmail = $this->get('site_parameter_manager')->getParam('from_email', 'info@safflower.com.au');
$message = \Swift_Message::newInstance()
->setSubject('Safflower: Reset Password')
->setFrom(array($fromEmail => 'Safflower Staff'))
->setTo($obj->getEmail())
->setContentType('text/html')
->setBody(
$this->renderView(
'@SymfonyDevApp/email_template/forgot_password.html.twig',
array('user' => $obj, 'resetLink' => $resetPasswordLink)
),
'text/html'
);
return $this->get('mailer')->send($message);
}
/**
* @Route("/reset-password/{id}/{key}", name="app_security_reset_password")
* @Template
*/
public function resetPasswordAction(Request $request, $id, $key)
{
if ($this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) {
return $this->redirect($this->generateUrl('app_security_redirect'));
}
$user = $this->getRepository('SymfonyDevAppBundle:User')->find($id);
if (!$user) {
$this->get('session')->getFlashBag()->add('ALERT_ERROR', 'Invalid request!');
return $this->redirectToRoute('app_security_login');
}
if ($key != $user->getResetPasswordToken()) {
$this->get('session')->getFlashBag()->add('ALERT_ERROR', 'Invalid request!');
return $this->redirectToRoute('app_security_login');
}
$dbm = $this->getDBM();
$form = $this->createForm(\SymfonyDev\AppBundle\Form\UserChangePasswordType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
$data->setResetPasswordToken(null);
$data->setIsPwdSecure(true);
$dbm->persist($data);
$dbm->flush();
$this->get('session')->getFlashBag()->add(
'ALERT_SUCCESS',
'Your password is reset successfully.'
);
return $this->redirectToRoute('app_security_login');
}
return array(
'form' => $form->createView()
);
}
/**
* @Route("/online-consultation", name="app_security_online_consultation")
* @Template
*/
public function onlineConsultationAction(Request $request)
{
$isSuccess = false;
$dbm = $this->getDBM();
$obj = new \SymfonyDev\AppBundle\Entity\Consultation();
$form = $this->createForm(\SymfonyDev\AppBundle\Form\ConsultationType::class, $obj);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
$dbm->persist($data);
$dbm->flush();
$this->get('session')->getFlashBag()->add(
'ALERT_SUCCESS',
'Your details submitted successfully.'
);
$isSuccess = true;
}
return array(
'form' => $form->createView(),
'qKeys' => \SymfonyDev\AppBundle\Entity\Consultation::Q_KEY,
'isSuccess' => $isSuccess
);
}
}