|
@@ -8,9 +8,37 @@ use Symfony\Component\HttpFoundation\RedirectResponse;
|
8
|
8
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
|
9
|
9
|
use FOS\UserBundle\Model\UserInterface;
|
10
|
10
|
use Muzich\CoreBundle\Form\Tag\TagFavoritesForm;
|
|
11
|
+use Symfony\Component\Validator\Constraints\Email;
|
|
12
|
+use Symfony\Component\Validator\Constraints\Collection;
|
11
|
13
|
|
12
|
14
|
class UserController extends Controller
|
13
|
15
|
{
|
|
16
|
+
|
|
17
|
+ protected function getChangeEmailForm()
|
|
18
|
+ {
|
|
19
|
+ $collectionConstraint = new Collection(array(
|
|
20
|
+ 'email' => new Email(array('message' => 'error.changeemail.email.invalid')),
|
|
21
|
+ ));
|
|
22
|
+
|
|
23
|
+ return $this->createFormBuilder(null, array(
|
|
24
|
+ 'validation_constraint' => $collectionConstraint,
|
|
25
|
+ ))
|
|
26
|
+ ->add('email', 'text')
|
|
27
|
+ ->getForm()
|
|
28
|
+ ;
|
|
29
|
+ }
|
|
30
|
+
|
|
31
|
+ protected function getTagsFavoritesForm($user)
|
|
32
|
+ {
|
|
33
|
+ return $this->createForm(
|
|
34
|
+ new TagFavoritesForm(),
|
|
35
|
+ array('tags' => $this->getDoctrine()->getRepository('MuzichCoreBundle:User')
|
|
36
|
+ ->getTagIdsFavorites($user->getId())
|
|
37
|
+ ),
|
|
38
|
+ array('tags' => $this->getTagsArray())
|
|
39
|
+ );
|
|
40
|
+ }
|
|
41
|
+
|
14
|
42
|
/**
|
15
|
43
|
* Page de configuration de son compte
|
16
|
44
|
*
|
|
@@ -19,22 +47,16 @@ class UserController extends Controller
|
19
|
47
|
public function accountAction()
|
20
|
48
|
{
|
21
|
49
|
$user = $this->getUser();
|
22
|
|
-
|
23
|
50
|
$form_password = $this->container->get('fos_user.change_password.form');
|
|
51
|
+ $form_tags_favorites = $this->getTagsFavoritesForm($user);
|
|
52
|
+ $change_email_form = $this->getChangeEmailForm();
|
24
|
53
|
|
25
|
|
- $form_tags_favorites = $this->createForm(
|
26
|
|
- new TagFavoritesForm(),
|
27
|
|
- array('tags' => $this->getDoctrine()->getRepository('MuzichCoreBundle:User')
|
28
|
|
- ->getTagIdsFavorites($user->getId())
|
29
|
|
- ),
|
30
|
|
- array('tags' => $this->getTagsArray())
|
|
54
|
+ return array(
|
|
55
|
+ 'user' => $user,
|
|
56
|
+ 'form_password' => $form_password->createView(),
|
|
57
|
+ 'form_tags_favorites' => $form_tags_favorites->createView(),
|
|
58
|
+ 'change_email_form' => $change_email_form->createView()
|
31
|
59
|
);
|
32
|
|
-
|
33
|
|
- return array(
|
34
|
|
- 'user' => $user,
|
35
|
|
- 'form_password' => $form_password->createView(),
|
36
|
|
- 'form_tags_favorites' => $form_tags_favorites->createView()
|
37
|
|
- );
|
38
|
60
|
}
|
39
|
61
|
|
40
|
62
|
/**
|
|
@@ -295,5 +317,115 @@ class UserController extends Controller
|
295
|
317
|
return $this->redirect($this->generateUrl('my_account'));
|
296
|
318
|
}
|
297
|
319
|
}
|
|
320
|
+
|
|
321
|
+ protected function checkChangeEmailFrequencies($user, $new_email)
|
|
322
|
+ {
|
|
323
|
+ $delay = $this->container->getParameter('changeemail_security_delay');
|
|
324
|
+ if (($last_request_datetime = $user->getEmailRequestedDatetime()))
|
|
325
|
+ {
|
|
326
|
+ if ((time() - $last_request_datetime) < $delay)
|
|
327
|
+ {
|
|
328
|
+ return false;
|
|
329
|
+ }
|
|
330
|
+ }
|
|
331
|
+ return true;
|
|
332
|
+ }
|
|
333
|
+
|
|
334
|
+
|
|
335
|
+ /**
|
|
336
|
+ * Procédure de demande de changement de mot de passe
|
|
337
|
+ */
|
|
338
|
+ public function changeEmailRequestAction()
|
|
339
|
+ {
|
|
340
|
+ $em = $this->getDoctrine()->getEntityManager();
|
|
341
|
+ $user = $this->getUser();
|
|
342
|
+ $request = $this->getRequest();
|
|
343
|
+ $change_email_form = $this->getChangeEmailForm();
|
|
344
|
+
|
|
345
|
+ $change_email_form->bindRequest($request);
|
|
346
|
+ if ($change_email_form->isValid())
|
|
347
|
+ {
|
|
348
|
+ $data = $change_email_form->getData();
|
|
349
|
+ $email = $data['email'];
|
|
350
|
+
|
|
351
|
+ if (!$this->checkChangeEmailFrequencies($user, $email))
|
|
352
|
+ {
|
|
353
|
+ $this->setFlash('error', 'user.changeemail.wait');
|
|
354
|
+ return new RedirectResponse($this->generateUrl('my_account'));
|
|
355
|
+ }
|
|
356
|
+
|
|
357
|
+ // On renseigne en base l'email demandé
|
|
358
|
+ $user->setEmailRequested($email);
|
|
359
|
+ $user->setEmailRequestedDatetime(time());
|
|
360
|
+ $user->generateConfirmationToken();
|
|
361
|
+ $token = hash('sha256', $user->getConfirmationToken().$email);
|
|
362
|
+ $url = $this->get('router')->generate('change_email_confirm', array('token' => $token), true);
|
|
363
|
+ $rendered = $this->get('templating')->render('MuzichUserBundle:User:change_email_mail.txt.twig', array(
|
|
364
|
+ 'user' => $user,
|
|
365
|
+ 'confirmationUrl' => $url
|
|
366
|
+ ));
|
|
367
|
+
|
|
368
|
+ //$this->sendEmailMessage($rendered, $this->parameters['from_email']['resetting'], $user->getEmail());
|
|
369
|
+
|
|
370
|
+ // Render the email, use the first line as the subject, and the rest as the body
|
|
371
|
+ $renderedLines = explode("\n", trim($rendered));
|
|
372
|
+ $subject = $renderedLines[0];
|
|
373
|
+ $body = implode("\n", array_slice($renderedLines, 1));
|
|
374
|
+
|
|
375
|
+ $message = \Swift_Message::newInstance()
|
|
376
|
+ ->setSubject($subject)
|
|
377
|
+ ->setFrom('noreply@muzi.ch')
|
|
378
|
+ ->setTo($email)
|
|
379
|
+ ->setBody($body);
|
|
380
|
+
|
|
381
|
+ $mailer = $this->get('mailer');
|
|
382
|
+ $mailer->send($message);
|
|
383
|
+
|
|
384
|
+ $this->setFlash('info', 'user.changeemail.mail_send');
|
|
385
|
+ $em->flush();
|
|
386
|
+ }
|
|
387
|
+
|
|
388
|
+ // En cas d'échec
|
|
389
|
+ $form_password = $this->container->get('fos_user.change_password.form');
|
|
390
|
+ $form_tags_favorites = $this->getTagsFavoritesForm($user);
|
|
391
|
+
|
|
392
|
+ return $this->container->get('templating')->renderResponse(
|
|
393
|
+ 'MuzichUserBundle:User:account.html.twig',
|
|
394
|
+ array(
|
|
395
|
+ 'user' => $user,
|
|
396
|
+ 'form_password' => $form_password->createView(),
|
|
397
|
+ 'form_tags_favorites' => $form_tags_favorites->createView(),
|
|
398
|
+ 'change_email_form' => $change_email_form->createView()
|
|
399
|
+ )
|
|
400
|
+ );
|
|
401
|
+ }
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
|
|
405
|
+ /**
|
|
406
|
+ * Procédure de confirmation de la nouvelle adresse email.
|
|
407
|
+ */
|
|
408
|
+ public function changeEmailConfirmAction($token)
|
|
409
|
+ {
|
|
410
|
+ $em = $this->getDoctrine()->getEntityManager();
|
|
411
|
+ $um = $this->get('muzich_user_manager');
|
|
412
|
+ $user = $this->getUser();
|
|
413
|
+ $token_ = hash('sha256', $user->getConfirmationToken().($email = $user->getEmailRequested()));
|
|
414
|
+
|
|
415
|
+ // Le token est-il valide
|
|
416
|
+ if ($token_ != $token)
|
|
417
|
+ {
|
|
418
|
+ $this->setFlash('error', 'user.changeemail.token_invalid');
|
|
419
|
+ return new RedirectResponse($this->generateUrl('my_account'));
|
|
420
|
+ }
|
|
421
|
+
|
|
422
|
+ $user->setEmail($email);
|
|
423
|
+ $user->setEmailRequested(null);
|
|
424
|
+ $um->updateCanonicalFields($user);
|
|
425
|
+ $em->flush();
|
|
426
|
+
|
|
427
|
+ $this->setFlash('success', 'user.changeemail.success');
|
|
428
|
+ return new RedirectResponse($this->generateUrl('my_account'));
|
|
429
|
+ }
|
298
|
430
|
|
299
|
431
|
}
|