IndexController.php 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace Muzich\IndexBundle\Controller;
  3. use Muzich\CoreBundle\lib\Controller;
  4. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  5. use Symfony\Component\Security\Core\SecurityContext;
  6. use Symfony\Component\Validator\Constraints\Email;
  7. use Symfony\Component\Validator\Constraints\Collection;
  8. use Symfony\Component\HttpFoundation\Request;
  9. class IndexController extends Controller
  10. {
  11. /**
  12. *
  13. * @Template()
  14. */
  15. public function indexAction()
  16. {
  17. // On rajoute le test sur l'environnement car dans les tests, d'un test a l'autre
  18. // l'utilisateur reste connecté et pas moyen de le déco ...
  19. if ($this->getUser() != 'anon.' && $this->container->getParameter('env') != 'test')
  20. {
  21. return $this->redirect($this->generateUrl('home'));
  22. }
  23. $vars = $this->proceedLogin();
  24. $form = $this->container->get('fos_user.registration.form');
  25. return array_merge($vars, array(
  26. 'form' => $form->createView(),
  27. 'presubscription_form' => $this->getPreSubscriptionForm()->createView()
  28. ));
  29. }
  30. /**
  31. * Gestion du formulaire d'identification sur la page d'index.
  32. *
  33. * @return type array
  34. */
  35. protected function proceedLogin()
  36. {
  37. $request = $this->container->get('request');
  38. /* @var $request \Symfony\Component\HttpFoundation\Request */
  39. $session = $request->getSession();
  40. /* @var $session Symfony\Component\HttpFoundation\Session\Session */
  41. // get the error if any (works with forward and redirect -- see below)
  42. if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
  43. $error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
  44. } elseif (null !== $session && $session->has(SecurityContext::AUTHENTICATION_ERROR)) {
  45. $error = $session->get(SecurityContext::AUTHENTICATION_ERROR);
  46. $session->remove(SecurityContext::AUTHENTICATION_ERROR);
  47. } else {
  48. $error = '';
  49. }
  50. if ($error) {
  51. // TODO: this is a potential security risk (see http://trac.symfony-project.org/ticket/9523)
  52. $error = $error->getMessage();
  53. }
  54. // last username entered by the user
  55. $lastUsername = (null === $session) ? '' : $session->get(SecurityContext::LAST_USERNAME);
  56. return array(
  57. 'last_username' => $lastUsername,
  58. 'error' => $error,
  59. 'registration_errors_pers' => array()
  60. );
  61. }
  62. public function presubscriptionAction(Request $request)
  63. {
  64. $form = $this->getPreSubscriptionForm();
  65. $form->bind($request);
  66. if ($form->isValid())
  67. {
  68. $this->persist($form->getData());
  69. $this->flush();
  70. $this->setFlash('success', 'presubscription.success');
  71. return $this->redirect($this->generateUrl('index'));
  72. }
  73. $this->setFlash('error', 'presubscription.error');
  74. return $this->render('MuzichIndexBundle:Index:index.html.twig', array(
  75. 'form' => $this->container->get('fos_user.registration.form')->createView(),
  76. 'presubscription_form' => $form->createView(),
  77. 'last_username' => '',
  78. 'error' => '',
  79. 'registration_errors_pers' => array()
  80. ));
  81. }
  82. }