UserController.php 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. <?php
  2. namespace Muzich\UserBundle\Controller;
  3. use Muzich\CoreBundle\lib\Controller;
  4. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  5. use Symfony\Component\HttpFoundation\RedirectResponse;
  6. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  7. use FOS\UserBundle\Model\UserInterface;
  8. use Muzich\CoreBundle\Form\Tag\TagFavoritesForm;
  9. use Symfony\Component\Validator\Constraints\Email;
  10. use Symfony\Component\Validator\Constraints\Collection;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Muzich\UserBundle\Form\Type\RegistrationFormType;
  13. use Muzich\CoreBundle\Entity\User;
  14. use Muzich\CoreBundle\Form\User\PasswordForm;
  15. class UserController extends Controller
  16. {
  17. protected $tags_favorites = null;
  18. protected function getChangeEmailForm()
  19. {
  20. $collectionConstraint = new Collection(array(
  21. 'email' => new Email(array('message' => 'error.changeemail.email.invalid')),
  22. ));
  23. return $this->createFormBuilder(null, array(
  24. //'validation_constraint' => $collectionConstraint, UPGRADE 2.1
  25. 'constraints' => $collectionConstraint,
  26. ))
  27. ->add('email', 'text')
  28. ->getForm()
  29. ;
  30. }
  31. protected function getPreferencesForm()
  32. {
  33. return $this->createFormBuilder($this->getUser())
  34. ->add('mail_newsletter', 'checkbox', array('required' => false))
  35. ->add('mail_partner', 'checkbox', array('required' => false))
  36. ->getForm()
  37. ;
  38. }
  39. protected function getTagsFavoritesForm($user)
  40. {
  41. $ids = array();
  42. foreach ($this->getTagsFavorites() as $id => $name)
  43. {
  44. $ids[] = $id;
  45. }
  46. return $this->createForm(
  47. new TagFavoritesForm(),
  48. array('tags' => json_encode($ids))
  49. );
  50. }
  51. protected function getTagsFavorites($force = false)
  52. {
  53. if ($this->tags_favorites === null || $force)
  54. {
  55. $user = $this->getUser();
  56. $this->tags_favorites = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')
  57. ->getTagsFavorites($user->getId())
  58. ;
  59. }
  60. return $this->tags_favorites;
  61. }
  62. /**
  63. * Page de configuration de son compte
  64. *
  65. * @Template()
  66. */
  67. public function accountAction()
  68. {
  69. $user = $this->getUser();
  70. $form_password = $this->getChangePasswordForm($user);
  71. $form_tags_favorites = $this->getTagsFavoritesForm($user);
  72. $change_email_form = $this->getChangeEmailForm();
  73. return array(
  74. 'user' => $user,
  75. 'form_password' => $form_password->createView(),
  76. 'form_tags_favorites' => $form_tags_favorites->createView(),
  77. 'form_tags_favorites_name' => $form_tags_favorites->getName(),
  78. 'favorite_tags_id' => $this->getTagsFavorites(),
  79. 'change_email_form' => $change_email_form->createView(),
  80. 'avatar_form' => $this->getAvatarForm()->createView(),
  81. 'preferences_form' => $this->getPreferencesForm()->createView()
  82. );
  83. }
  84. protected function getChangePasswordForm(User $user)
  85. {
  86. return $this->createForm(new PasswordForm(), $user);
  87. }
  88. protected function getAvatarForm()
  89. {
  90. return $this->createFormBuilder($this->getUser())
  91. ->add('avatar')
  92. ->getForm()
  93. ;
  94. }
  95. public function registerAction(Request $request)
  96. {
  97. $userManager = $this->container->get('fos_user.user_manager');
  98. $user = $this->getNewUser($userManager);
  99. $form = $this->getRegistrationForm($user);
  100. $form->bindRequest($request);
  101. $errors = $this->checkRegistrationValues($form);
  102. if ($form->isValid() && !count($errors))
  103. {
  104. $response = $this->getSuccessRegistrationResponse();
  105. $userManager->updateUser($user);
  106. $this->authenticateUser($user, $response);
  107. $this->sendEmailconfirmationEmail(false);
  108. return $response;
  109. }
  110. return $this->getFailureRegistrationResponse($form, $errors);
  111. }
  112. protected function getRegistrationForm(User $user)
  113. {
  114. return $this->createForm(new RegistrationFormType(), $user);
  115. }
  116. /** @return User */
  117. protected function getNewUser()
  118. {
  119. return $this->container->get('muzich_user_manager')->getNewReadyUser();
  120. }
  121. protected function checkRegistrationValues($form)
  122. {
  123. if(!filter_var($form->getData()->getEmailCanonical(), FILTER_VALIDATE_EMAIL))
  124. {
  125. return array($this->trans('registration.email.invalid', array(), 'validators'));
  126. }
  127. $count = $this->getEntityManager()->createQuery("SELECT count(u.id) "
  128. ."FROM MuzichCoreBundle:User u "
  129. ."WHERE UPPER(u.email) = :email_canonical")
  130. ->setParameter('email_canonical', strtoupper($form->getData()->getEmailCanonical()))
  131. ->getSingleScalarResult()
  132. ;
  133. if ($count)
  134. {
  135. return array($this->trans('error.registration.email.duplicate', array(), 'validators'));
  136. }
  137. return array();
  138. }
  139. protected function getSuccessRegistrationResponse()
  140. {
  141. if (!$this->getRequest()->isXmlHttpRequest())
  142. {
  143. return new RedirectResponse($this->generateUrl('home'));
  144. }
  145. return $this->jsonResponse(array(
  146. 'status' => 'success'
  147. ));
  148. }
  149. protected function getFailureRegistrationResponse($form, $errors = array())//, $formHandler)
  150. {
  151. $parameters = array(
  152. 'form' => $form->createView(),
  153. 'error' => null,
  154. 'registration_errors' => $form->getErrors(),
  155. 'registration_errors_pers' => $errors,
  156. 'last_username' => null,
  157. 'registration_page' => true,
  158. 'presubscription_form' => $this->getPreSubscriptionForm()->createView()
  159. );
  160. if (!$this->getRequest()->isXmlHttpRequest())
  161. {
  162. return $this->render(
  163. 'MuzichIndexBundle:Index:index.html.twig',
  164. $parameters
  165. );
  166. }
  167. return $this->jsonResponse(array(
  168. 'status' => 'error',
  169. 'data' => array(
  170. 'html' => $this->render(
  171. 'MuzichUserBundle:Registration:register_form_content.html.twig',
  172. $parameters
  173. )->getContent()
  174. )
  175. ));
  176. }
  177. /**
  178. * Un bug étrange empêche la mise ne place de contraintes sur le formulaire
  179. * d'inscription. On effectue alors les vérifications ici.
  180. *
  181. * C'est sale, mais ça marche ...
  182. *
  183. * @return array of string errors
  184. */
  185. protected function checkChangePasswordInformations($form)
  186. {
  187. $errors = array();
  188. $form_values = $this->getRequest()->request->get($form->getName());
  189. $user = $form->getData();
  190. /**
  191. * Mot de passes indentiques
  192. */
  193. if ($form_values['new']['first'] != $form_values['new']['second'])
  194. {
  195. $errors[] = $this->get('translator')->trans(
  196. 'error.changepassword.new.notsame',
  197. array(),
  198. 'validators'
  199. );
  200. }
  201. return $errors;
  202. }
  203. public function changePasswordAction(Request $request)
  204. {
  205. $user = $this->getUser();
  206. $form = $this->getChangePasswordForm($user);
  207. $form->bind($request);
  208. if ($form->isValid())
  209. {
  210. $userManager = $this->container->get('fos_user.user_manager');
  211. $userManager->updateUser($form->getData());
  212. $form->getData()->setPasswordSet(true);
  213. $this->persist($form->getData());
  214. $this->flush();
  215. $this->container->get('session')->setFlash('fos_user_success', 'change_password.flash.success');
  216. return new RedirectResponse($this->generateUrl('home'));
  217. }
  218. $form_tags_favorites = $this->getTagsFavoritesForm($user);
  219. $change_email_form = $this->getChangeEmailForm();
  220. return $this->container->get('templating')->renderResponse(
  221. 'MuzichUserBundle:User:account.html.twig',
  222. array(
  223. 'form_password' => $form->createView(),
  224. 'errors_pers' => array(),
  225. 'user' => $user,
  226. 'form_tags_favorites' => $form_tags_favorites->createView(),
  227. 'form_tags_favorites_name' => $form_tags_favorites->getName(),
  228. 'favorite_tags_id' => $this->getTagsFavorites(),
  229. 'change_email_form' => $change_email_form->createView(),
  230. 'avatar_form' => $this->getAvatarForm()->createView(),
  231. 'preferences_form' => $this->getPreferencesForm()->createView()
  232. )
  233. );
  234. }
  235. /**
  236. * Page ouverte après l'inscription sur laquelle on propose de saisir ses
  237. * tags favoris.
  238. *
  239. * @Template()
  240. */
  241. public function startAction()
  242. {
  243. $user = $this->getUser();
  244. $form_tags_favorites = $this->getTagsFavoritesForm($user);
  245. return array(
  246. 'favorite_tags_id' => $this->getTagsFavorites(),
  247. 'form_tags_favorites' => $form_tags_favorites->createView(),
  248. 'form_tags_favorites_name' => $form_tags_favorites->getName(),
  249. );
  250. }
  251. /**
  252. *
  253. * @param string $redirect
  254. */
  255. public function updateTagFavoritesAction(Request $request, $redirect)
  256. {
  257. $request = $this->getRequest();
  258. $user = $this->getUser(true, array('join' => array('favorites_tags')));
  259. $form = $this->getTagsFavoritesForm($user);
  260. if ($request->getMethod() == 'POST')
  261. {
  262. $form->bind($request);
  263. if ($form->isValid())
  264. {
  265. $data = $form->getData();
  266. $user->updateTagsFavoritesById($this->getDoctrine()->getEntityManager(), $data['tags']);
  267. // On réinitialise l'eventuel session de recherche en mémoire
  268. $session = $this->get("session");
  269. $session->remove('user.element_search.params');
  270. }
  271. else
  272. {
  273. if ($request->isXmlHttpRequest())
  274. {
  275. return $this->jsonResponse(array(
  276. 'status' => 'error',
  277. 'data' => $this->render('MuzichUserBundle:User:helpbox_favorite_tags.html.twig', array(
  278. 'form' => $form->createView(),
  279. 'form_name' => 'favorites_tags_helpbox'
  280. ))->getContent()
  281. ));
  282. }
  283. return $this->container->get('templating')->renderResponse(
  284. 'MuzichUserBundle:User:start.html.twig',
  285. array(
  286. 'form' => $form->createView()
  287. )
  288. );
  289. }
  290. }
  291. if ($request->isXmlHttpRequest())
  292. {
  293. return $this->jsonResponse(array(
  294. 'status' => 'success'
  295. ));
  296. }
  297. $this->container->get('session')->setFlash('success', 'Vos tags péférés ont correctements été mis a jour.');
  298. // (Il y aura aussi une redirection vers "mon compte / tags")
  299. if ($redirect == 'home')
  300. {
  301. return $this->redirect($this->generateUrl('home'));
  302. }
  303. else
  304. {
  305. return $this->redirect($this->generateUrl('my_account'));
  306. }
  307. }
  308. protected function checkChangeEmailFrequencies($user, $new_email)
  309. {
  310. $delay = $this->container->getParameter('changeemail_security_delay');
  311. if (($last_request_datetime = $user->getEmailRequestedDatetime()))
  312. {
  313. if ((time() - $last_request_datetime) < $delay)
  314. {
  315. return false;
  316. }
  317. }
  318. return true;
  319. }
  320. /**
  321. * Procédure de demande de changement de mot de passe
  322. */
  323. public function changeEmailRequestAction()
  324. {
  325. $em = $this->getDoctrine()->getEntityManager();
  326. $user = $this->getUser();
  327. $request = $this->getRequest();
  328. $change_email_form = $this->getChangeEmailForm();
  329. $change_email_form->bind($request);
  330. if ($change_email_form->isValid())
  331. {
  332. $data = $change_email_form->getData();
  333. $email = $data['email'];
  334. if (!$this->checkChangeEmailFrequencies($user, $email))
  335. {
  336. $this->setFlash('error', 'user.changeemail.wait');
  337. return new RedirectResponse($this->generateUrl('my_account'));
  338. }
  339. /*
  340. * Optimisation: Ecrire une lib Mailer pour gérer les envois.
  341. * cf le mailer de FOSUserBundle
  342. */
  343. // On renseigne en base l'email demandé
  344. $user->setEmailRequested($email);
  345. $user->setEmailRequestedDatetime(time());
  346. //$user->generateConfirmationToken(); UPGRADE FOSUserBundle 1.3
  347. $tokenGenerator = $this->container->get('fos_user.util.token_generator');
  348. $user->setConfirmationToken($tokenGenerator->generateToken());
  349. $token = hash('sha256', $user->getConfirmationToken().$email);
  350. $url = $this->get('router')->generate('change_email_confirm', array('token' => $token), true);
  351. $rendered = $this->get('templating')->render('MuzichUserBundle:User:change_email_mail.txt.twig', array(
  352. 'user' => $user,
  353. 'confirmationUrl' => $url
  354. ));
  355. //$this->sendEmailMessage($rendered, $this->parameters['from_email']['resetting'], $user->getEmail());
  356. // Render the email, use the first line as the subject, and the rest as the body
  357. $renderedLines = explode("\n", trim($rendered));
  358. $subject = $renderedLines[0];
  359. $body = implode("\n", array_slice($renderedLines, 1));
  360. $message = \Swift_Message::newInstance()
  361. ->setSubject($subject)
  362. ->setFrom('contact@muzi.ch')
  363. ->setTo($email)
  364. ->setBody($body);
  365. $mailer = $this->get('mailer');
  366. $mailer->send($message);
  367. $this->setFlash('success', 'user.changeemail.mail_send');
  368. $em->flush();
  369. return new RedirectResponse($this->generateUrl('my_account'));
  370. }
  371. // En cas d'échec
  372. $form_password = $this->getChangePasswordForm($user);
  373. $form_tags_favorites = $this->getTagsFavoritesForm($user);
  374. return $this->container->get('templating')->renderResponse(
  375. 'MuzichUserBundle:User:account.html.twig',
  376. array(
  377. 'user' => $user,
  378. 'form_password' => $form_password->createView(),
  379. 'form_tags_favorites' => $form_tags_favorites->createView(),
  380. 'form_tags_favorites_name' => $form_tags_favorites->getName(),
  381. 'favorite_tags_id' => $this->getTagsFavorites(),
  382. 'change_email_form' => $change_email_form->createView(),
  383. 'avatar_form' => $this->getAvatarForm()->createView(),
  384. 'preferences_form' => $this->getPreferencesForm()->createView()
  385. )
  386. );
  387. }
  388. /**
  389. * Procédure de confirmation de la nouvelle adresse email.
  390. */
  391. public function changeEmailConfirmAction($token)
  392. {
  393. $em = $this->getDoctrine()->getEntityManager();
  394. $um = $this->get('muzich_user_manager');
  395. $user = $this->getUser();
  396. $token_ = hash('sha256', $user->getConfirmationToken().($email = $user->getEmailRequested()));
  397. // Le token est-il valide
  398. if ($token_ != $token)
  399. {
  400. $this->setFlash('error', 'user.changeemail.token_invalid');
  401. return new RedirectResponse($this->generateUrl('my_account'));
  402. }
  403. $user->setEmail($email);
  404. $user->setEmailRequested(null);
  405. $um->updateCanonicalFields($user);
  406. $em->flush();
  407. $this->setFlash('success', 'user.changeemail.success');
  408. return new RedirectResponse($this->generateUrl('my_account'));
  409. }
  410. /**
  411. *
  412. * @param string $town
  413. * @param string $country
  414. * @param string $token
  415. * @return Response
  416. */
  417. public function updateAddressAction($token)
  418. {
  419. if (($response = $this->mustBeConnected(true)))
  420. {
  421. return $response;
  422. }
  423. $user = $this->getUser();
  424. $errors = array();
  425. if ($user->getPersonalHash() != $token)
  426. {
  427. $errors[] = 'NotAllowed';
  428. }
  429. if (!trim($this->getRequest()->request->get('town')))
  430. {
  431. $errors[] = $this->trans('my_account.address.form.errors.notown', array(), 'userui');
  432. }
  433. if (!trim($this->getRequest()->request->get('country')))
  434. {
  435. $errors[] = $this->trans('my_account.address.form.errors.nocountry', array(), 'userui');
  436. }
  437. if (count($errors))
  438. {
  439. return $this->jsonResponse(array(
  440. 'status' => 'error',
  441. 'errors' => $errors
  442. ));
  443. }
  444. $user->setTown(trim($this->getRequest()->request->get('town')));
  445. $user->setCountry(trim($this->getRequest()->request->get('country')));
  446. $this->getDoctrine()->getEntityManager()->persist($user);
  447. $this->getDoctrine()->getEntityManager()->flush();
  448. return $this->jsonResponse(array(
  449. 'status' => 'success'
  450. ));
  451. }
  452. public function updateAvatarAction(Request $request)
  453. {
  454. $form = $this->getAvatarForm();
  455. $form->bind($request);
  456. if ($form->isValid()) {
  457. $em = $this->getEntityManager();
  458. $form->getData()->preUploadAvatar();
  459. $form->getData()->uploadAvatar();
  460. $em->persist($form->getData());
  461. $em->flush();
  462. $this->setFlash('success',
  463. $this->trans('my_account.avatar.success', array(), 'userui'));
  464. return $this->redirect($this->generateUrl('my_account'));
  465. }
  466. $this->setFlash('error',
  467. $this->trans('my_account.avatar.error', array(), 'userui'));
  468. return $this->redirect($this->generateUrl('my_account'));
  469. }
  470. public function updatePreferencesAction(Request $request)
  471. {
  472. $form = $this->getPreferencesForm();
  473. $form->bind($request);
  474. if ($form->isValid()) {
  475. $em = $this->getEntityManager();
  476. $em->persist($form->getData());
  477. $em->flush();
  478. $this->setFlash('success',
  479. $this->trans('my_account.preferences.success', array(), 'userui'));
  480. return $this->redirect($this->generateUrl('my_account'));
  481. }
  482. $this->setFlash('error',
  483. $this->trans('my_account.preferences.error', array(), 'userui'));
  484. return $this->redirect($this->generateUrl('my_account'));
  485. }
  486. public function updateHelpViewedAction($help_id, $token)
  487. {
  488. if ($this->getUser()->getPersonalHash('updateHelpAction') != $token)
  489. {
  490. return $this->jsonNotFoundResponse();
  491. }
  492. $this->getUser()->setSeeHelp($help_id, false);
  493. $this->persist($this->getUser());
  494. $this->flush();
  495. return $this->jsonResponse(array(
  496. 'status' => 'success'
  497. ));
  498. }
  499. public function subscribeOrLoginAction(Request $request)
  500. {
  501. return $this->jsonResponse(array(
  502. 'status' => 'success',
  503. 'data' => $this->render('MuzichUserBundle:Account:subscribe_or_login.html.twig', array(
  504. 'form' => $this->getRegistrationForm($this->getNewUser())->createView()
  505. ))->getContent()
  506. ));
  507. }
  508. public function changeUsernameAction(Request $request)
  509. {
  510. $user = $this->getUserRefreshed();
  511. if (!$user->isUsernameUpdatable())
  512. {
  513. return new RedirectResponse($this->generateUrl('my_account'));
  514. }
  515. $errors = array();
  516. $form = $this->getChangeUsernameForm($user);
  517. if ($request->getMethod() == 'POST')
  518. {
  519. $form->bind($request);
  520. $errors = $this->checkChangeUsernameValues($form);
  521. if ($form->isValid() && !count($errors))
  522. {
  523. $form->getData()->setUsernameUpdatable(false);
  524. $this->persist($user);
  525. $this->flush();
  526. $this->setFlash('success', 'user.change_username.success');
  527. return new RedirectResponse($this->generateUrl('my_account'));
  528. }
  529. else
  530. {
  531. $this->setFlash('error', 'user.change_username.failure');
  532. }
  533. }
  534. return $this->render('MuzichUserBundle:User:change_username.html.twig', array(
  535. 'form' => $form->createView(),
  536. 'errors' => $errors
  537. ));
  538. }
  539. protected function checkChangeUsernameValues($form)
  540. {
  541. $errors = array();
  542. $userManager = $this->container->get('fos_user.user_manager');
  543. if ($userManager->findUserByUsername($form->getData()->getUsername()))
  544. {
  545. $errors[] = $this->trans('error.change_username.duplicate', array(), 'validators');
  546. }
  547. if (strlen($form->getData()->getUsername()) < 3)
  548. {
  549. $errors[] = $this->trans(
  550. 'error.change_username.min',
  551. array('%limit%' => 3),
  552. 'validators'
  553. );
  554. }
  555. if (strlen($form->getData()->getUsername()) > 32)
  556. {
  557. $errors[] = $this->trans(
  558. 'error.change_username.max',
  559. array('%limit%' => 32),
  560. 'validators'
  561. );
  562. }
  563. return $errors;
  564. }
  565. protected function getChangeUsernameForm(User $user)
  566. {
  567. return $this->createFormBuilder($user)
  568. ->add('username', 'text')
  569. ->getForm()
  570. ;
  571. }
  572. public function sendEmailConfirmAction(Request $request, $set_send_time = true)
  573. {
  574. $user = $this->getUser();
  575. if ($user->isEmailConfirmed())
  576. {
  577. if ($request->isXmlHttpRequest())
  578. {
  579. return $this->jsonResponse(array(
  580. 'status' => 'success',
  581. 'result' => 'already_confirmed',
  582. 'message' => $this->trans('user.confirm_email.alreaydy', array(), 'flash')
  583. ));
  584. }
  585. $this->setFlash('success', 'user.confirm_email.alreaydy');
  586. return new RedirectResponse($this->generateUrl('home'));
  587. }
  588. if ((time() - $user->getEmailConfirmationSentTimestamp() < $this->getParameter('email_confirmation_email_interval')))
  589. {
  590. if ($request->isXmlHttpRequest())
  591. {
  592. return $this->jsonResponse(array(
  593. 'status' => 'error',
  594. 'result' => 'already_sent_recently',
  595. 'message' => $this->trans('user.confirm_email.sent_recently', array(), 'flash')
  596. ));
  597. }
  598. $this->setFlash('success', 'user.confirm_email.sent_recently');
  599. return new RedirectResponse($this->generateUrl('my_account'));
  600. }
  601. $this->sendEmailconfirmationEmail($set_send_time);
  602. if ($request->isXmlHttpRequest())
  603. {
  604. return $this->jsonResponse(array(
  605. 'status' => 'success',
  606. 'result' => 'sent',
  607. 'message' => $this->trans('user.confirm_email.sent', array(), 'flash')
  608. ));
  609. }
  610. $this->setFlash('success', 'user.confirm_email.sent');
  611. return new RedirectResponse($this->generateUrl('my_account'));
  612. }
  613. public function confirmEmailAction(Request $request, $token)
  614. {
  615. $user = $this->getUser();
  616. if ($token == hash('sha256', $user->getConfirmationToken().$user->getEmail()))
  617. {
  618. $user->setEmailConfirmed(true);
  619. $this->persist($user);
  620. $this->flush();
  621. $this->setFlash('success', 'user.confirm_email.confirmed');
  622. return new RedirectResponse($this->generateUrl('home'));
  623. }
  624. $this->setFlash('success', 'user.confirm_email.failtoken');
  625. return new RedirectResponse($this->generateUrl('my_account'));
  626. }
  627. public function showEmailNotConfirmedAction()
  628. {
  629. return $this->jsonResponse(array(
  630. 'status' => 'success',
  631. 'data' => $this->render('MuzichUserBundle:Account:email_not_confirmed.html.twig')->getContent()
  632. ));
  633. }
  634. public function favoriteTagsHelpboxAction()
  635. {
  636. return $this->jsonResponse(array(
  637. 'status' => 'success',
  638. 'data' => $this->render('MuzichUserBundle:User:helpbox_favorite_tags.html.twig', array(
  639. 'form' => $this->getTagsFavoritesForm($this->getUser())->createView(),
  640. 'form_name' => 'favorites_tags_helpbox'
  641. ))->getContent()
  642. ));
  643. }
  644. }