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 //FIXME: we need to change the $this->container->getParameter($alias.'.xyz') to $this->container->getParameter($alias)['xyz']
15 public function loginAction(Request
$request, AuthenticationUtils
$authenticationUtils) {
17 $template = $this->container
->getParameter(($alias = $this->getAlias()).'.login.template');
19 $context = $this->container
->getParameter($alias.'.login.context');
21 //Create the form according to the FormType created previously.
22 //And give the proper parameters
23 $form = $this->createForm('Rapsys\UserBundle\Form\LoginType', null, array(
24 // To set the action use $this->generateUrl('route_identifier')
25 'action' => $this->generateUrl('rapsys_user_login'),
29 //Get the login error if there is one
30 if ($error = $authenticationUtils->getLastAuthenticationError()) {
32 $trans = $this->get('translator');
34 //Get translated error
35 $error = $trans->trans($error->getMessageKey());
37 //Add error message to mail field
38 $form->get('mail')->addError(new FormError($error));
41 //Last username entered by the user
42 if ($lastUsername = $authenticationUtils->getLastUsername()) {
43 $form->get('mail')->setData($lastUsername);
47 return $this->render($template, $context+
array('form' => $form->createView(), 'error' => $error));
50 public function registerAction(Request
$request, UserPasswordEncoderInterface
$encoder) {
52 $mailTemplate = $this->container
->getParameter(($alias = $this->getAlias()).'.register.mail_template');
54 $mailContext = $this->container
->getParameter($alias.'.register.mail_context');
56 $template = $this->container
->getParameter($alias.'.register.template');
58 $context = $this->container
->getParameter($alias.'.register.context');
60 $homeName = $this->container
->getParameter($alias.'.contact.home_name');
62 $homeArgs = $this->container
->getParameter($alias.'.contact.home_args');
64 $contactName = $this->container
->getParameter($alias.'.contact.name');
66 $contactMail = $this->container
->getParameter($alias.'.contact.mail');
67 //TODO: check if doctrine orm replacement is enough with default classes here
69 $classUser = $this->container
->getParameter($alias.'.class.user');
71 $classGroup = $this->container
->getParameter($alias.'.class.group');
73 $classTitle = $this->container
->getParameter($alias.'.class.title');
75 //Create the form according to the FormType created previously.
76 //And give the proper parameters
77 $form = $this->createForm('Rapsys\UserBundle\Form\RegisterType', null, array(
78 // To set the action use $this->generateUrl('route_identifier')
79 'class_title' => $classTitle,
80 'action' => $this->generateUrl('rapsys_user_register'),
84 if ($request->isMethod('POST')) {
85 // Refill the fields in case the form is not valid.
86 $form->handleRequest($request);
88 if ($form->isValid()) {
90 $trans = $this->get('translator');
93 $data = $form->getData();
96 $mailContext['title'] = $trans->trans($mailContext['title']);
99 $mailContext['subtitle'] = $trans->trans($mailContext['subtitle'], array('%name%' => $data['forename'].' '.$data['surname'].' ('.$data['pseudonym'].')'));
102 $mailContext['subject'] = $trans->trans($mailContext['subject'], array('%title%' => $mailContext['title']));
105 $mailContext['message'] = $trans->trans($mailContext['message'], array('%title%' => $mailContext['title']));
108 $message = \Swift_Message
::newInstance()
109 ->setSubject($mailContext['subject'])
110 ->setFrom(array($contactMail => $contactName))
111 ->setTo(array($data['mail'] => $data['forename'].' '.$data['surname']))
112 ->setBody($mailContext['message'])
117 'home' => $this->get('router')->generate($homeName, $homeArgs, UrlGeneratorInterface
::ABSOLUTE_URL
)
124 $doctrine = $this->getDoctrine();
127 $manager = $doctrine->getManager();
130 $reflection = new \
ReflectionClass($classUser);
133 $user = $reflection->newInstance();
135 $user->setMail($data['mail']);
136 $user->setPseudonym($data['pseudonym']);
137 $user->setForename($data['forename']);
138 $user->setSurname($data['surname']);
139 $user->setPassword($encoder->encodePassword($user, $data['password']));
140 $user->setActive(true);
141 $user->setTitle($data['title']);
142 //TODO: see if we can't modify group constructor to set role directly from args
143 //XXX: see vendor/symfony/symfony/src/Symfony/Component/Security/Core/Role/Role.php
144 $user->addGroup($doctrine->getRepository($classGroup)->findOneByRole('ROLE_USER'));
145 $user->setCreated(new \
DateTime('now'));
146 $user->setUpdated(new \
DateTime('now'));
149 $manager->persist($user);
156 if ($this->get('mailer')->send($message)) {
157 //Redirect to cleanup the form
158 return $this->redirectToRoute('rapsys_user_register', array('sent' => 1));
160 } catch (\Doctrine\DBAL\Exception\UniqueConstraintViolationException
$e) {
161 //Add error message mail already exists
162 $form->get('mail')->addError(new FormError($trans->trans('Account already exists: %mail%', array('%mail%' => $data['mail']))));
168 return $this->render($template, $context+
array('form' => $form->createView(), 'sent' => $request->query
->get('sent', 0)));
171 public function recoverAction(Request
$request, Slugger
$slugger) {
173 $mailTemplate = $this->container
->getParameter(($alias = $this->getAlias()).'.recover.mail_template');
175 $mailContext = $this->container
->getParameter($alias.'.recover.mail_context');
177 $template = $this->container
->getParameter($alias.'.recover.template');
179 $context = $this->container
->getParameter($alias.'.recover.context');
181 $urlName = $this->container
->getParameter($alias.'.recover.url_name');
183 $urlArgs = $this->container
->getParameter($alias.'.recover.url_args');
185 $homeName = $this->container
->getParameter($alias.'.contact.home_name');
187 $homeArgs = $this->container
->getParameter($alias.'.contact.home_args');
189 $contactName = $this->container
->getParameter($alias.'.contact.name');
191 $contactMail = $this->container
->getParameter($alias.'.contact.mail');
193 $classUser = $this->container
->getParameter($alias.'.class.user');
195 //Create the form according to the FormType created previously.
196 //And give the proper parameters
197 $form = $this->createForm('Rapsys\UserBundle\Form\RecoverType', null, array(
198 // To set the action use $this->generateUrl('route_identifier')
199 'action' => $this->generateUrl('rapsys_user_recover'),
203 if ($request->isMethod('POST')) {
204 // Refill the fields in case the form is not valid.
205 $form->handleRequest($request);
207 if ($form->isValid()) {
209 $trans = $this->get('translator');
212 $doctrine = $this->getDoctrine();
215 $data = $form->getData();
218 $mailContext['title'] = $trans->trans($mailContext['title']);
221 if ($user = $doctrine->getRepository($classUser)->findOneByMail($data['mail'])) {
223 $mailContext['subtitle'] = $trans->trans($mailContext['subtitle'], array('%name%' => $user->getForename().' '.$user->getSurname().' ('.$user->getPseudonym().')'));
226 $mailContext['subject'] = $trans->trans($mailContext['subject'], array('%title%' => $mailContext['title']));
229 $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
)));
232 $message = \Swift_Message
::newInstance()
233 ->setSubject($mailContext['subject'])
234 ->setFrom(array($contactMail => $contactName))
235 ->setTo(array($user->getMail() => $user->getForename().' '.$user->getSurname()))
236 ->setBody(strip_tags($mailContext['raw']))
241 'home' => $this->get('router')->generate($homeName, $homeArgs, UrlGeneratorInterface
::ABSOLUTE_URL
)
248 if ($this->get('mailer')->send($message)) {
249 //Redirect to cleanup the form
250 return $this->redirectToRoute('rapsys_user_recover', array('sent' => 1));
254 //Add error message to mail field
255 $form->get('mail')->addError(new FormError($trans->trans('Unable to find account: %mail%', array('%mail%' => $data['mail']))));
261 return $this->render($template, $context+
array('form' => $form->createView(), 'sent' => $request->query
->get('sent', 0)));
264 public function recoverMailAction(Request
$request, UserPasswordEncoderInterface
$encoder, Slugger
$slugger, $mail, $hash) {
266 $mailTemplate = $this->container
->getParameter(($alias = $this->getAlias()).'.recover_mail.mail_template');
268 $mailContext = $this->container
->getParameter($alias.'.recover_mail.mail_context');
270 $template = $this->container
->getParameter($alias.'.recover_mail.template');
272 $context = $this->container
->getParameter($alias.'.recover_mail.context');
274 $urlName = $this->container
->getParameter($alias.'.recover_mail.url_name');
276 $urlArgs = $this->container
->getParameter($alias.'.recover_mail.url_args');
278 $homeName = $this->container
->getParameter($alias.'.contact.home_name');
280 $homeArgs = $this->container
->getParameter($alias.'.contact.home_args');
282 $contactName = $this->container
->getParameter($alias.'.contact.name');
284 $contactMail = $this->container
->getParameter($alias.'.contact.mail');
286 $classUser = $this->container
->getParameter($alias.'.class.user');
288 //Create the form according to the FormType created previously.
289 //And give the proper parameters
290 $form = $this->createForm('Rapsys\UserBundle\Form\RecoverMailType', null, array(
291 // To set the action use $this->generateUrl('route_identifier')
292 'action' => $this->generateUrl('rapsys_user_recover_mail', array('mail' => $mail, 'hash' => $hash)),
297 $doctrine = $this->getDoctrine();
300 $trans = $this->get('translator');
306 if (($user = $doctrine->getRepository($classUser)->findOneByMail($slugger->unshort($mail))) && $hash == $slugger->hash($user->getPassword())) {
310 if ($request->isMethod('POST')) {
311 // Refill the fields in case the form is not valid.
312 $form->handleRequest($request);
314 if ($form->isValid()) {
316 $data = $form->getData();
319 $mailContext['title'] = $trans->trans($mailContext['title']);
322 $mailContext['subtitle'] = $trans->trans($mailContext['subtitle'], array('%name%' => $user->getForename().' '.$user->getSurname().' ('.$user->getPseudonym().')'));
325 $mailContext['subject'] = $trans->trans($mailContext['subject'], array('%title%' => $mailContext['title']));
328 $user->setPassword($encoder->encodePassword($user, $data['password']));
331 $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
)));
334 $manager = $doctrine->getManager();
337 $manager->persist($user);
343 $message = \Swift_Message
::newInstance()
344 ->setSubject($mailContext['subject'])
345 ->setFrom(array($contactMail => $contactName))
346 ->setTo(array($user->getMail() => $user->getForename().' '.$user->getSurname()))
347 ->setBody(strip_tags($mailContext['raw']))
352 'home' => $this->get('router')->generate($homeName, $homeArgs, UrlGeneratorInterface
::ABSOLUTE_URL
)
359 if ($this->get('mailer')->send($message)) {
360 //Redirect to cleanup the form
361 return $this->redirectToRoute('rapsys_user_recover_mail', array('mail' => $mail, 'hash' => $hash, 'sent' => 1));
368 return $this->render($template, $context+
array('form' => $form->createView(), 'sent' => $request->query
->get('sent', 0), 'notfound' => $notfound));
374 public function getAlias() {
375 return 'rapsys_user';