3 namespace Rapsys\UserBundle\Controller
; 
   5 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController
; 
   6 use Symfony\Component\DependencyInjection\ContainerInterface
; 
   7 use Symfony\Bundle\FrameworkBundle\Translation\Translator
; 
   8 use Symfony\Component\HttpFoundation\Request
; 
   9 use Symfony\Component\Routing\Generator\UrlGeneratorInterface
; 
  10 use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface
; 
  11 use Symfony\Component\Security\Http\Authentication\AuthenticationUtils
; 
  12 use Symfony\Component\Form\FormError
; 
  13 use Rapsys\UserBundle\Utils\Slugger
; 
  15 class DefaultController 
extends AbstractController 
{ 
  20         protected $translator; 
  22         public function __construct(ContainerInterface 
$container, Translator 
$translator) { 
  24                 $this->config 
= $container->getParameter($this->getAlias()); 
  27                 $this->translator 
= $translator; 
  30         //FIXME: we need to change the $this->container->getParameter($alias.'.xyz') to $this->container->getParameter($alias)['xyz'] 
  31         public function loginAction(Request 
$request, AuthenticationUtils 
$authenticationUtils) { 
  33                 $template = $this->config
['login']['template']; 
  35                 $context = $this->config
['login']['context']; 
  37                 //Create the form according to the FormType created previously. 
  38                 //And give the proper parameters 
  39                 $form = $this->createForm('Rapsys\UserBundle\Form\LoginType', null, array( 
  40                         // To set the action use $this->generateUrl('route_identifier') 
  41                         'action' => $this->generateUrl('rapsys_user_login'), 
  45                 //Get the login error if there is one 
  46                 if ($error = $authenticationUtils->getLastAuthenticationError()) { 
  47                         //Get translated error 
  48                         $error = $this->translator
->trans($error->getMessageKey()); 
  50                         //Add error message to mail field 
  51                         $form->get('mail')->addError(new FormError($error)); 
  54                 //Last username entered by the user 
  55                 if ($lastUsername = $authenticationUtils->getLastUsername()) { 
  56                         $form->get('mail')->setData($lastUsername); 
  60                 return $this->render($template, $context+
array('form' => $form->createView(), 'error' => $error)); 
  63         public function registerAction(Request 
$request, UserPasswordEncoderInterface 
$encoder) { 
  65                 $mailTemplate = $this->config
['register']['mail_template']; 
  67                 $mailContext = $this->config
['register']['mail_context']; 
  69                 $template = $this->config
['register']['template']; 
  71                 $context = $this->config
['register']['context']; 
  73                 $homeName = $this->config
['contact']['home_name']; 
  75                 $homeArgs = $this->config
['contact']['home_args']; 
  77                 $contactName = $this->config
['contact']['name']; 
  79                 $contactMail = $this->config
['contact']['mail']; 
  80                 //TODO: check if doctrine orm replacement is enough with default classes here 
  82                 $classUser = $this->config
['class']['user']; 
  84                 $classGroup = $this->config
['class']['group']; 
  86                 $classTitle = $this->config
['class']['title']; 
  88                 //Create the form according to the FormType created previously. 
  89                 //And give the proper parameters 
  90                 $form = $this->createForm('Rapsys\UserBundle\Form\RegisterType', null, array( 
  91                         // To set the action use $this->generateUrl('route_identifier') 
  92                         'class_title' => $classTitle, 
  93                         'action' => $this->generateUrl('rapsys_user_register'), 
  97                 if ($request->isMethod('POST')) { 
  98                         // Refill the fields in case the form is not valid. 
  99                         $form->handleRequest($request); 
 101                         if ($form->isValid()) { 
 103                                 $data = $form->getData(); 
 106                                 $mailContext['title'] = $this->translator
->trans($mailContext['title']); 
 109                                 $mailContext['subtitle'] = $this->translator
->trans($mailContext['subtitle'], array('%name%' => $data['forename'].' '.$data['surname'].' ('.$data['pseudonym'].')')); 
 112                                 $mailContext['subject'] = $this->translator
->trans($mailContext['subject'], array('%title%' => $mailContext['title'])); 
 115                                 $mailContext['message'] = $this->translator
->trans($mailContext['message'], array('%title%' => $mailContext['title'])); 
 118                                 $message = \Swift_Message
::newInstance() 
 119                                         ->setSubject($mailContext['subject']) 
 120                                         ->setFrom(array($contactMail => $contactName)) 
 121                                         ->setTo(array($data['mail'] => $data['forename'].' '.$data['surname'])) 
 122                                         ->setBody($mailContext['message']) 
 127                                                                 'home' => $this->get('router')->generate($homeName, $homeArgs, UrlGeneratorInterface
::ABSOLUTE_URL
) 
 134                                 $doctrine = $this->getDoctrine(); 
 137                                 $manager = $doctrine->getManager(); 
 140                                 $reflection = new \
ReflectionClass($classUser); 
 143                                 $user = $reflection->newInstance(); 
 145                                 $user->setMail($data['mail']); 
 146                                 $user->setPseudonym($data['pseudonym']); 
 147                                 $user->setForename($data['forename']); 
 148                                 $user->setSurname($data['surname']); 
 149                                 $user->setPassword($encoder->encodePassword($user, $data['password'])); 
 150                                 $user->setActive(true); 
 151                                 $user->setTitle($data['title']); 
 152                                 //TODO: see if we can't modify group constructor to set role directly from args 
 153                                 //XXX: see vendor/symfony/symfony/src/Symfony/Component/Security/Core/Role/Role.php 
 154                                 $user->addGroup($doctrine->getRepository($classGroup)->findOneByRole('ROLE_USER')); 
 155                                 $user->setCreated(new \
DateTime('now')); 
 156                                 $user->setUpdated(new \
DateTime('now')); 
 159                                 $manager->persist($user); 
 166                                         if ($this->get('mailer')->send($message)) { 
 167                                                 //Redirect to cleanup the form 
 168                                                 return $this->redirectToRoute('rapsys_user_register', array('sent' => 1)); 
 170                                 } catch (\Doctrine\DBAL\Exception\UniqueConstraintViolationException 
$e) { 
 171                                         //Add error message mail already exists 
 172                                         $form->get('mail')->addError(new FormError($this->translator
->trans('Account already exists: %mail%', array('%mail%' => $data['mail'])))); 
 178                 return $this->render($template, $context+
array('form' => $form->createView(), 'sent' => $request->query
->get('sent', 0))); 
 181         public function recoverAction(Request 
$request, Slugger 
$slugger) { 
 183                 $mailTemplate = $this->config
['recover']['mail_template']; 
 185                 $mailContext = $this->config
['recover']['mail_context']; 
 187                 $template = $this->config
['recover']['template']; 
 189                 $context = $this->config
['recover']['context']; 
 191                 $urlName = $this->config
['recover']['url_name']; 
 193                 $urlArgs = $this->config
['recover']['url_args']; 
 195                 $homeName = $this->config
['contact']['home_name']; 
 197                 $homeArgs = $this->config
['contact']['home_args']; 
 199                 $contactName = $this->config
['contact']['name']; 
 201                 $contactMail = $this->config
['contact']['mail']; 
 203                 $classUser = $this->config
['class']['user']; 
 205                 //Create the form according to the FormType created previously. 
 206                 //And give the proper parameters 
 207                 $form = $this->createForm('Rapsys\UserBundle\Form\RecoverType', null, array( 
 208                         // To set the action use $this->generateUrl('route_identifier') 
 209                         'action' => $this->generateUrl('rapsys_user_recover'), 
 213                 if ($request->isMethod('POST')) { 
 214                         // Refill the fields in case the form is not valid. 
 215                         $form->handleRequest($request); 
 217                         if ($form->isValid()) { 
 219                                 $doctrine = $this->getDoctrine(); 
 222                                 $data = $form->getData(); 
 225                                 $mailContext['title'] = $this->translator
->trans($mailContext['title']); 
 228                                 if ($user = $doctrine->getRepository($classUser)->findOneByMail($data['mail'])) { 
 230                                         $mailContext['subtitle'] = $this->translator
->trans($mailContext['subtitle'], array('%name%' => $user->getForename().' '.$user->getSurname().' ('.$user->getPseudonym().')')); 
 233                                         $mailContext['subject'] = $this->translator
->trans($mailContext['subject'], array('%title%' => $mailContext['title'])); 
 236                                         $mailContext['raw'] = $this->translator
->trans($mailContext['raw'], array('%title%' => $mailContext['title'], '%url%' => $this->get('router')->generate($urlName, $urlArgs+
array('mail' => $slugger->short($user->getMail()), 'hash' => $slugger->hash($user->getPassword())), UrlGeneratorInterface
::ABSOLUTE_URL
))); 
 239                                         $message = \Swift_Message
::newInstance() 
 240                                                 ->setSubject($mailContext['subject']) 
 241                                                 ->setFrom(array($contactMail => $contactName)) 
 242                                                 ->setTo(array($user->getMail() => $user->getForename().' '.$user->getSurname())) 
 243                                                 ->setBody(strip_tags($mailContext['raw'])) 
 248                                                                         'home' => $this->get('router')->generate($homeName, $homeArgs, UrlGeneratorInterface
::ABSOLUTE_URL
) 
 255                                         if ($this->get('mailer')->send($message)) { 
 256                                                 //Redirect to cleanup the form 
 257                                                 return $this->redirectToRoute('rapsys_user_recover', array('sent' => 1)); 
 261                                         //Add error message to mail field 
 262                                         $form->get('mail')->addError(new FormError($this->translator
->trans('Unable to find account: %mail%', array('%mail%' => $data['mail'])))); 
 268                 return $this->render($template, $context+
array('form' => $form->createView(), 'sent' => $request->query
->get('sent', 0))); 
 271         public function recoverMailAction(Request 
$request, UserPasswordEncoderInterface 
$encoder, Slugger 
$slugger, $mail, $hash) { 
 273                 $mailTemplate = $this->config
['recover_mail']['mail_template']; 
 275                 $mailContext = $this->config
['recover_mail']['mail_context']; 
 277                 $template = $this->config
['recover_mail']['template']; 
 279                 $context = $this->config
['recover_mail']['context']; 
 281                 $urlName = $this->config
['recover_mail']['url_name']; 
 283                 $urlArgs = $this->config
['recover_mail']['url_args']; 
 285                 $homeName = $this->config
['contact']['home_name']; 
 287                 $homeArgs = $this->config
['contact']['home_args']; 
 289                 $contactName = $this->config
['contact']['name']; 
 291                 $contactMail = $this->config
['contact']['mail']; 
 293                 $classUser = $this->config
['class']['user']; 
 295                 //Create the form according to the FormType created previously. 
 296                 //And give the proper parameters 
 297                 $form = $this->createForm('Rapsys\UserBundle\Form\RecoverMailType', null, array( 
 298                         // To set the action use $this->generateUrl('route_identifier') 
 299                         'action' => $this->generateUrl('rapsys_user_recover_mail', array('mail' => $mail, 'hash' => $hash)), 
 304                 $doctrine = $this->getDoctrine(); 
 310                 if (($user = $doctrine->getRepository($classUser)->findOneByMail($slugger->unshort($mail))) && $hash == $slugger->hash($user->getPassword())) { 
 314                         if ($request->isMethod('POST')) { 
 315                                 // Refill the fields in case the form is not valid. 
 316                                 $form->handleRequest($request); 
 318                                 if ($form->isValid()) { 
 320                                         $data = $form->getData(); 
 323                                         $mailContext['title'] = $this->translator
->trans($mailContext['title']); 
 326                                         $mailContext['subtitle'] = $this->translator
->trans($mailContext['subtitle'], array('%name%' => $user->getForename().' '.$user->getSurname().' ('.$user->getPseudonym().')')); 
 329                                         $mailContext['subject'] = $this->translator
->trans($mailContext['subject'], array('%title%' => $mailContext['title'])); 
 332                                         $user->setPassword($encoder->encodePassword($user, $data['password'])); 
 335                                         $mailContext['raw'] = $this->translator
->trans($mailContext['raw'], array('%title%' => $mailContext['title'], '%url%' => $this->get('router')->generate($urlName, $urlArgs+
array('mail' => $slugger->short($user->getMail()), 'hash' => $slugger->hash($user->getPassword())), UrlGeneratorInterface
::ABSOLUTE_URL
))); 
 338                                         $manager = $doctrine->getManager(); 
 341                                         $manager->persist($user); 
 347                                         $message = \Swift_Message
::newInstance() 
 348                                                 ->setSubject($mailContext['subject']) 
 349                                                 ->setFrom(array($contactMail => $contactName)) 
 350                                                 ->setTo(array($user->getMail() => $user->getForename().' '.$user->getSurname())) 
 351                                                 ->setBody(strip_tags($mailContext['raw'])) 
 356                                                                         'home' => $this->get('router')->generate($homeName, $homeArgs, UrlGeneratorInterface
::ABSOLUTE_URL
) 
 363                                         if ($this->get('mailer')->send($message)) { 
 364                                                 //Redirect to cleanup the form 
 365                                                 return $this->redirectToRoute('rapsys_user_recover_mail', array('mail' => $mail, 'hash' => $hash, 'sent' => 1)); 
 372                 return $this->render($template, $context+
array('form' => $form->createView(), 'sent' => $request->query
->get('sent', 0), 'notfound' => $notfound)); 
 378         public function getAlias() { 
 379                 return 'rapsys_user';