3 namespace Rapsys\UserBundle\Controller
;
5 use Symfony\Bundle\FrameworkBundle\Controller\Controller
;
6 use Symfony\Component\HttpFoundation\Request
;
7 use Symfony\Component\Routing\Generator\UrlGeneratorInterface
;
8 use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface
;
9 use Symfony\Component\Security\Http\Authentication\AuthenticationUtils
;
10 use Symfony\Component\Form\FormError
;
11 use Rapsys\UserBundle\Utils\Slugger
;
13 class DefaultController
extends Controller
{
14 public function loginAction(Request
$request, AuthenticationUtils
$authenticationUtils) {
16 $template = $this->container
->getParameter(($alias = $this->getAlias()).'.login.template');
18 $context = $this->container
->getParameter($alias.'.login.context');
20 //Create the form according to the FormType created previously.
21 //And give the proper parameters
22 $form = $this->createForm('Rapsys\UserBundle\Form\LoginType', null, array(
23 // To set the action use $this->generateUrl('route_identifier')
24 'action' => $this->generateUrl('rapsys_user_login'),
28 //Get the login error if there is one
29 if ($error = $authenticationUtils->getLastAuthenticationError()) {
31 $trans = $this->get('translator');
33 //Get translated error
34 $error = $trans->trans($error->getMessageKey());
36 //Add error message to mail field
37 $form->get('mail')->addError(new FormError($error));
40 //Last username entered by the user
41 if ($lastUsername = $authenticationUtils->getLastUsername()) {
42 $form->get('mail')->setData($lastUsername);
46 return $this->render($template, $context+
array('form' => $form->createView(), 'error' => $error));
49 public function registerAction(Request
$request, UserPasswordEncoderInterface
$encoder) {
51 $mailTemplate = $this->container
->getParameter(($alias = $this->getAlias()).'.register.mail_template');
53 $mailContext = $this->container
->getParameter($alias.'.register.mail_context');
55 $template = $this->container
->getParameter($alias.'.register.template');
57 $context = $this->container
->getParameter($alias.'.register.context');
59 $homeName = $this->container
->getParameter($alias.'.contact.home_name');
61 $homeArgs = $this->container
->getParameter($alias.'.contact.home_args');
63 $contactName = $this->container
->getParameter($alias.'.contact.name');
65 $contactMail = $this->container
->getParameter($alias.'.contact.mail');
66 //TODO: check if doctrine orm replacement is enough with default classes here
68 $classUser = $this->container
->getParameter($alias.'.class.user');
70 $classGroup = $this->container
->getParameter($alias.'.class.group');
72 $classTitle = $this->container
->getParameter($alias.'.class.title');
74 //Create the form according to the FormType created previously.
75 //And give the proper parameters
76 $form = $this->createForm('Rapsys\UserBundle\Form\RegisterType', null, array(
77 // To set the action use $this->generateUrl('route_identifier')
78 'class_title' => $classTitle,
79 'action' => $this->generateUrl('rapsys_user_register'),
83 if ($request->isMethod('POST')) {
84 // Refill the fields in case the form is not valid.
85 $form->handleRequest($request);
87 if ($form->isValid()) {
89 $trans = $this->get('translator');
92 $data = $form->getData();
95 $mailContext['title'] = $trans->trans($mailContext['title']);
98 $mailContext['subtitle'] = $trans->trans($mailContext['subtitle'], array('%name%' => $data['forename'].' '.$data['surname'].' ('.$data['pseudonym'].')'));
101 $mailContext['subject'] = $trans->trans($mailContext['subject'], array('%title%' => $mailContext['title']));
104 $mailContext['message'] = $trans->trans($mailContext['message'], array('%title%' => $mailContext['title']));
107 $message = \Swift_Message
::newInstance()
108 ->setSubject($mailContext['subject'])
109 ->setFrom(array($contactMail => $contactName))
110 ->setTo(array($data['mail'] => $data['forename'].' '.$data['surname']))
111 ->setBody($mailContext['message'])
116 'home' => $this->get('router')->generate($homeName, $homeArgs, UrlGeneratorInterface
::ABSOLUTE_URL
)
123 $doctrine = $this->getDoctrine();
126 $manager = $doctrine->getManager();
129 $reflection = new \
ReflectionClass($classUser);
132 $user = $reflection->newInstance();
134 $user->setMail($data['mail']);
135 $user->setPseudonym($data['pseudonym']);
136 $user->setForename($data['forename']);
137 $user->setSurname($data['surname']);
138 $user->setPassword($encoder->encodePassword($user, $data['password']));
139 $user->setActive(true);
140 $user->setTitle($data['title']);
141 //TODO: see if we can't modify group constructor to set role directly from args
142 //XXX: see vendor/symfony/symfony/src/Symfony/Component/Security/Core/Role/Role.php
143 $user->addGroup($doctrine->getRepository($classGroup)->findOneByRole('ROLE_USER'));
144 $user->setCreated(new \
DateTime('now'));
145 $user->setUpdated(new \
DateTime('now'));
148 $manager->persist($user);
155 if ($this->get('mailer')->send($message)) {
156 //Redirect to cleanup the form
157 return $this->redirectToRoute('rapsys_user_register', array('sent' => 1));
159 } catch (\Doctrine\DBAL\Exception\UniqueConstraintViolationException
$e) {
160 //Add error message mail already exists
161 $form->get('mail')->addError(new FormError($trans->trans('Account already exists: %mail%', array('%mail%' => $data['mail']))));
167 return $this->render($template, $context+
array('form' => $form->createView(), 'sent' => $request->query
->get('sent', 0)));
170 public function recoverAction(Request
$request, Slugger
$slugger) {
172 $mailTemplate = $this->container
->getParameter(($alias = $this->getAlias()).'.recover.mail_template');
174 $mailContext = $this->container
->getParameter($alias.'.recover.mail_context');
176 $template = $this->container
->getParameter($alias.'.recover.template');
178 $context = $this->container
->getParameter($alias.'.recover.context');
180 $urlName = $this->container
->getParameter($alias.'.recover.url_name');
182 $urlArgs = $this->container
->getParameter($alias.'.recover.url_args');
184 $homeName = $this->container
->getParameter($alias.'.contact.home_name');
186 $homeArgs = $this->container
->getParameter($alias.'.contact.home_args');
188 $contactName = $this->container
->getParameter($alias.'.contact.name');
190 $contactMail = $this->container
->getParameter($alias.'.contact.mail');
192 $classUser = $this->container
->getParameter($alias.'.class.user');
194 //Create the form according to the FormType created previously.
195 //And give the proper parameters
196 $form = $this->createForm('Rapsys\UserBundle\Form\RecoverType', null, array(
197 // To set the action use $this->generateUrl('route_identifier')
198 'action' => $this->generateUrl('rapsys_user_recover'),
202 if ($request->isMethod('POST')) {
203 // Refill the fields in case the form is not valid.
204 $form->handleRequest($request);
206 if ($form->isValid()) {
208 $trans = $this->get('translator');
211 $doctrine = $this->getDoctrine();
214 $data = $form->getData();
217 $mailContext['title'] = $trans->trans($mailContext['title']);
220 if ($user = $doctrine->getRepository($classUser)->findOneByMail($data['mail'])) {
222 $mailContext['subtitle'] = $trans->trans($mailContext['subtitle'], array('%name%' => $user->getForename().' '.$user->getSurname().' ('.$user->getPseudonym().')'));
225 $mailContext['subject'] = $trans->trans($mailContext['subject'], array('%title%' => $mailContext['title']));
228 $mailContext['raw'] = $trans->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
)));
231 $message = \Swift_Message
::newInstance()
232 ->setSubject($mailContext['subject'])
233 ->setFrom(array($contactMail => $contactName))
234 ->setTo(array($user->getMail() => $user->getForename().' '.$user->getSurname()))
235 ->setBody(strip_tags($mailContext['raw']))
240 'home' => $this->get('router')->generate($homeName, $homeArgs, UrlGeneratorInterface
::ABSOLUTE_URL
)
247 if ($this->get('mailer')->send($message)) {
248 //Redirect to cleanup the form
249 return $this->redirectToRoute('rapsys_user_recover', array('sent' => 1));
253 //Add error message to mail field
254 $form->get('mail')->addError(new FormError($trans->trans('Unable to find account: %mail%', array('%mail%' => $data['mail']))));
260 return $this->render($template, $context+
array('form' => $form->createView(), 'sent' => $request->query
->get('sent', 0)));
263 public function recoverMailAction(Request
$request, UserPasswordEncoderInterface
$encoder, Slugger
$slugger, $mail, $hash) {
265 $mailTemplate = $this->container
->getParameter(($alias = $this->getAlias()).'.recover_mail.mail_template');
267 $mailContext = $this->container
->getParameter($alias.'.recover_mail.mail_context');
269 $template = $this->container
->getParameter($alias.'.recover_mail.template');
271 $context = $this->container
->getParameter($alias.'.recover_mail.context');
273 $urlName = $this->container
->getParameter($alias.'.recover_mail.url_name');
275 $urlArgs = $this->container
->getParameter($alias.'.recover_mail.url_args');
277 $homeName = $this->container
->getParameter($alias.'.contact.home_name');
279 $homeArgs = $this->container
->getParameter($alias.'.contact.home_args');
281 $contactName = $this->container
->getParameter($alias.'.contact.name');
283 $contactMail = $this->container
->getParameter($alias.'.contact.mail');
285 $classUser = $this->container
->getParameter($alias.'.class.user');
287 //Create the form according to the FormType created previously.
288 //And give the proper parameters
289 $form = $this->createForm('Rapsys\UserBundle\Form\RecoverMailType', null, array(
290 // To set the action use $this->generateUrl('route_identifier')
291 'action' => $this->generateUrl('rapsys_user_recover_mail', array('mail' => $mail, 'hash' => $hash)),
296 $doctrine = $this->getDoctrine();
299 $trans = $this->get('translator');
305 if (($user = $doctrine->getRepository($classUser)->findOneByMail($slugger->unshort($mail))) && $hash == $slugger->hash($user->getPassword())) {
309 if ($request->isMethod('POST')) {
310 // Refill the fields in case the form is not valid.
311 $form->handleRequest($request);
313 if ($form->isValid()) {
315 $data = $form->getData();
318 $mailContext['title'] = $trans->trans($mailContext['title']);
321 $mailContext['subtitle'] = $trans->trans($mailContext['subtitle'], array('%name%' => $user->getForename().' '.$user->getSurname().' ('.$user->getPseudonym().')'));
324 $mailContext['subject'] = $trans->trans($mailContext['subject'], array('%title%' => $mailContext['title']));
327 $user->setPassword($encoder->encodePassword($user, $data['password']));
330 $mailContext['raw'] = $trans->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
)));
333 $manager = $doctrine->getManager();
336 $manager->persist($user);
342 $message = \Swift_Message
::newInstance()
343 ->setSubject($mailContext['subject'])
344 ->setFrom(array($contactMail => $contactName))
345 ->setTo(array($user->getMail() => $user->getForename().' '.$user->getSurname()))
346 ->setBody(strip_tags($mailContext['raw']))
351 'home' => $this->get('router')->generate($homeName, $homeArgs, UrlGeneratorInterface
::ABSOLUTE_URL
)
358 if ($this->get('mailer')->send($message)) {
359 //Redirect to cleanup the form
360 return $this->redirectToRoute('rapsys_user_recover_mail', array('mail' => $mail, 'hash' => $hash, 'sent' => 1));
367 return $this->render($template, $context+
array('form' => $form->createView(), 'sent' => $request->query
->get('sent', 0), 'notfound' => $notfound));
373 public function getAlias() {
374 return 'rapsys_user';