UserController.php 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  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. // Ce serais mieux d'appeler notre user manager et d'utiliser notre createUser
  120. // avec ce code.
  121. $userManager = $this->container->get('fos_user.user_manager');
  122. $user = $userManager->createUser();
  123. $user->setUsername($this->generateUsername());
  124. $user->setPlainPassword($this->generatePassword(32));
  125. $user->setEnabled(true);
  126. $user->setCguAccepted(true);
  127. $user->setEmailConfirmed(false);
  128. $user->setUsernameUpdatable(true);
  129. $user->setPasswordSet(false);
  130. return $user;
  131. }
  132. protected function generateUsername()
  133. {
  134. $qb = $this->getEntityManager()->createQueryBuilder();
  135. $qb->select('count(id)');
  136. $qb->from('MuzichCoreBundle:User','id');
  137. $count = $qb->getQuery()->getSingleScalarResult();
  138. return 'User'.$count;
  139. }
  140. protected function generatePassword($length = 8)
  141. {
  142. $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  143. $count = mb_strlen($chars);
  144. for ($i = 0, $result = ''; $i < $length; $i++) {
  145. $index = rand(0, $count - 1);
  146. $result .= mb_substr($chars, $index, 1);
  147. }
  148. return $result;
  149. }
  150. protected function checkRegistrationValues($form)
  151. {
  152. if(!filter_var($form->getData()->getEmailCanonical(), FILTER_VALIDATE_EMAIL))
  153. {
  154. return array($this->trans('registration.email.invalid', array(), 'validators'));
  155. }
  156. $count = $this->getEntityManager()->createQuery("SELECT count(u.id) "
  157. ."FROM MuzichCoreBundle:User u "
  158. ."WHERE UPPER(u.email) = :email_canonical")
  159. ->setParameter('email_canonical', strtoupper($form->getData()->getEmailCanonical()))
  160. ->getSingleScalarResult()
  161. ;
  162. if ($count)
  163. {
  164. return array($this->trans('error.registration.email.duplicate', array(), 'validators'));
  165. }
  166. return array();
  167. }
  168. protected function getSuccessRegistrationResponse()
  169. {
  170. if (!$this->getRequest()->isXmlHttpRequest())
  171. {
  172. return new RedirectResponse($this->generateUrl('home'));
  173. }
  174. return $this->jsonResponse(array(
  175. 'status' => 'success'
  176. ));
  177. }
  178. protected function getFailureRegistrationResponse($form, $errors = array())//, $formHandler)
  179. {
  180. $parameters = array(
  181. 'form' => $form->createView(),
  182. 'error' => null,
  183. 'registration_errors' => $form->getErrors(),
  184. 'registration_errors_pers' => $errors,
  185. 'last_username' => null,
  186. 'registration_page' => true,
  187. 'presubscription_form' => $this->getPreSubscriptionForm()->createView()
  188. );
  189. if (!$this->getRequest()->isXmlHttpRequest())
  190. {
  191. return $this->render(
  192. 'MuzichIndexBundle:Index:index.html.twig',
  193. $parameters
  194. );
  195. }
  196. return $this->jsonResponse(array(
  197. 'status' => 'error',
  198. 'data' => array(
  199. 'html' => $this->render(
  200. 'MuzichUserBundle:Registration:register_form_content.html.twig',
  201. $parameters
  202. )->getContent()
  203. )
  204. ));
  205. }
  206. /**
  207. * Un bug étrange empêche la mise ne place de contraintes sur le formulaire
  208. * d'inscription. On effectue alors les vérifications ici.
  209. *
  210. * C'est sale, mais ça marche ...
  211. *
  212. * @return array of string errors
  213. */
  214. protected function checkChangePasswordInformations($form)
  215. {
  216. $errors = array();
  217. $form_values = $this->getRequest()->request->get($form->getName());
  218. $user = $form->getData();
  219. /**
  220. * Mot de passes indentiques
  221. */
  222. if ($form_values['new']['first'] != $form_values['new']['second'])
  223. {
  224. $errors[] = $this->get('translator')->trans(
  225. 'error.changepassword.new.notsame',
  226. array(),
  227. 'validators'
  228. );
  229. }
  230. return $errors;
  231. }
  232. public function changePasswordAction(Request $request)
  233. {
  234. $user = $this->getUser();
  235. $form = $this->getChangePasswordForm($user);
  236. $form->bind($request);
  237. if ($form->isValid())
  238. {
  239. $userManager = $this->container->get('fos_user.user_manager');
  240. $userManager->updateUser($form->getData());
  241. $form->getData()->setPasswordSet(true);
  242. $this->persist($form->getData());
  243. $this->flush();
  244. $this->container->get('session')->setFlash('fos_user_success', 'change_password.flash.success');
  245. return new RedirectResponse($this->generateUrl('home'));
  246. }
  247. $form_tags_favorites = $this->getTagsFavoritesForm($user);
  248. $change_email_form = $this->getChangeEmailForm();
  249. return $this->container->get('templating')->renderResponse(
  250. 'MuzichUserBundle:User:account.html.twig',
  251. array(
  252. 'form_password' => $form->createView(),
  253. 'errors_pers' => array(),
  254. 'user' => $user,
  255. 'form_tags_favorites' => $form_tags_favorites->createView(),
  256. 'form_tags_favorites_name' => $form_tags_favorites->getName(),
  257. 'favorite_tags_id' => $this->getTagsFavorites(),
  258. 'change_email_form' => $change_email_form->createView(),
  259. 'avatar_form' => $this->getAvatarForm()->createView(),
  260. 'preferences_form' => $this->getPreferencesForm()->createView()
  261. )
  262. );
  263. }
  264. /**
  265. * Page ouverte après l'inscription sur laquelle on propose de saisir ses
  266. * tags favoris.
  267. *
  268. * @Template()
  269. */
  270. public function startAction()
  271. {
  272. $user = $this->getUser();
  273. $form_tags_favorites = $this->getTagsFavoritesForm($user);
  274. return array(
  275. 'favorite_tags_id' => $this->getTagsFavorites(),
  276. 'form_tags_favorites' => $form_tags_favorites->createView(),
  277. 'form_tags_favorites_name' => $form_tags_favorites->getName(),
  278. );
  279. }
  280. /**
  281. *
  282. * @param string $redirect
  283. */
  284. public function updateTagFavoritesAction(Request $request, $redirect)
  285. {
  286. $request = $this->getRequest();
  287. $user = $this->getUser(true, array('join' => array('favorites_tags')));
  288. $form = $this->getTagsFavoritesForm($user);
  289. if ($request->getMethod() == 'POST')
  290. {
  291. $form->bind($request);
  292. if ($form->isValid())
  293. {
  294. $data = $form->getData();
  295. $user->updateTagsFavoritesById($this->getDoctrine()->getEntityManager(), $data['tags']);
  296. // On réinitialise l'eventuel session de recherche en mémoire
  297. $session = $this->get("session");
  298. $session->remove('user.element_search.params');
  299. }
  300. else
  301. {
  302. if ($request->isXmlHttpRequest())
  303. {
  304. return $this->jsonResponse(array(
  305. 'status' => 'error',
  306. 'data' => $this->render('MuzichUserBundle:User:helpbox_favorite_tags.html.twig', array(
  307. 'form' => $form->createView(),
  308. 'form_name' => 'favorites_tags_helpbox'
  309. ))->getContent()
  310. ));
  311. }
  312. return $this->container->get('templating')->renderResponse(
  313. 'MuzichUserBundle:User:start.html.twig',
  314. array(
  315. 'form' => $form->createView()
  316. )
  317. );
  318. }
  319. }
  320. if ($request->isXmlHttpRequest())
  321. {
  322. return $this->jsonResponse(array(
  323. 'status' => 'success'
  324. ));
  325. }
  326. $this->container->get('session')->setFlash('success', 'Vos tags péférés ont correctements été mis a jour.');
  327. // (Il y aura aussi une redirection vers "mon compte / tags")
  328. if ($redirect == 'home')
  329. {
  330. return $this->redirect($this->generateUrl('home'));
  331. }
  332. else
  333. {
  334. return $this->redirect($this->generateUrl('my_account'));
  335. }
  336. }
  337. protected function checkChangeEmailFrequencies($user, $new_email)
  338. {
  339. $delay = $this->container->getParameter('changeemail_security_delay');
  340. if (($last_request_datetime = $user->getEmailRequestedDatetime()))
  341. {
  342. if ((time() - $last_request_datetime) < $delay)
  343. {
  344. return false;
  345. }
  346. }
  347. return true;
  348. }
  349. /**
  350. * Procédure de demande de changement de mot de passe
  351. */
  352. public function changeEmailRequestAction()
  353. {
  354. $em = $this->getDoctrine()->getEntityManager();
  355. $user = $this->getUser();
  356. $request = $this->getRequest();
  357. $change_email_form = $this->getChangeEmailForm();
  358. $change_email_form->bind($request);
  359. if ($change_email_form->isValid())
  360. {
  361. $data = $change_email_form->getData();
  362. $email = $data['email'];
  363. if (!$this->checkChangeEmailFrequencies($user, $email))
  364. {
  365. $this->setFlash('error', 'user.changeemail.wait');
  366. return new RedirectResponse($this->generateUrl('my_account'));
  367. }
  368. /*
  369. * Optimisation: Ecrire une lib Mailer pour gérer les envois.
  370. * cf le mailer de FOSUserBundle
  371. */
  372. // On renseigne en base l'email demandé
  373. $user->setEmailRequested($email);
  374. $user->setEmailRequestedDatetime(time());
  375. //$user->generateConfirmationToken(); UPGRADE FOSUserBundle 1.3
  376. $tokenGenerator = $this->container->get('fos_user.util.token_generator');
  377. $user->setConfirmationToken($tokenGenerator->generateToken());
  378. $token = hash('sha256', $user->getConfirmationToken().$email);
  379. $url = $this->get('router')->generate('change_email_confirm', array('token' => $token), true);
  380. $rendered = $this->get('templating')->render('MuzichUserBundle:User:change_email_mail.txt.twig', array(
  381. 'user' => $user,
  382. 'confirmationUrl' => $url
  383. ));
  384. //$this->sendEmailMessage($rendered, $this->parameters['from_email']['resetting'], $user->getEmail());
  385. // Render the email, use the first line as the subject, and the rest as the body
  386. $renderedLines = explode("\n", trim($rendered));
  387. $subject = $renderedLines[0];
  388. $body = implode("\n", array_slice($renderedLines, 1));
  389. $message = \Swift_Message::newInstance()
  390. ->setSubject($subject)
  391. ->setFrom('contact@muzi.ch')
  392. ->setTo($email)
  393. ->setBody($body);
  394. $mailer = $this->get('mailer');
  395. $mailer->send($message);
  396. $this->setFlash('success', 'user.changeemail.mail_send');
  397. $em->flush();
  398. return new RedirectResponse($this->generateUrl('my_account'));
  399. }
  400. // En cas d'échec
  401. $form_password = $this->getChangePasswordForm($user);
  402. $form_tags_favorites = $this->getTagsFavoritesForm($user);
  403. return $this->container->get('templating')->renderResponse(
  404. 'MuzichUserBundle:User:account.html.twig',
  405. array(
  406. 'user' => $user,
  407. 'form_password' => $form_password->createView(),
  408. 'form_tags_favorites' => $form_tags_favorites->createView(),
  409. 'form_tags_favorites_name' => $form_tags_favorites->getName(),
  410. 'favorite_tags_id' => $this->getTagsFavorites(),
  411. 'change_email_form' => $change_email_form->createView(),
  412. 'avatar_form' => $this->getAvatarForm()->createView(),
  413. 'preferences_form' => $this->getPreferencesForm()->createView()
  414. )
  415. );
  416. }
  417. /**
  418. * Procédure de confirmation de la nouvelle adresse email.
  419. */
  420. public function changeEmailConfirmAction($token)
  421. {
  422. $em = $this->getDoctrine()->getEntityManager();
  423. $um = $this->get('muzich_user_manager');
  424. $user = $this->getUser();
  425. $token_ = hash('sha256', $user->getConfirmationToken().($email = $user->getEmailRequested()));
  426. // Le token est-il valide
  427. if ($token_ != $token)
  428. {
  429. $this->setFlash('error', 'user.changeemail.token_invalid');
  430. return new RedirectResponse($this->generateUrl('my_account'));
  431. }
  432. $user->setEmail($email);
  433. $user->setEmailRequested(null);
  434. $um->updateCanonicalFields($user);
  435. $em->flush();
  436. $this->setFlash('success', 'user.changeemail.success');
  437. return new RedirectResponse($this->generateUrl('my_account'));
  438. }
  439. /**
  440. *
  441. * @param string $town
  442. * @param string $country
  443. * @param string $token
  444. * @return Response
  445. */
  446. public function updateAddressAction($token)
  447. {
  448. if (($response = $this->mustBeConnected(true)))
  449. {
  450. return $response;
  451. }
  452. $user = $this->getUser();
  453. $errors = array();
  454. if ($user->getPersonalHash() != $token)
  455. {
  456. $errors[] = 'NotAllowed';
  457. }
  458. if (!trim($this->getRequest()->request->get('town')))
  459. {
  460. $errors[] = $this->trans('my_account.address.form.errors.notown', array(), 'userui');
  461. }
  462. if (!trim($this->getRequest()->request->get('country')))
  463. {
  464. $errors[] = $this->trans('my_account.address.form.errors.nocountry', array(), 'userui');
  465. }
  466. if (count($errors))
  467. {
  468. return $this->jsonResponse(array(
  469. 'status' => 'error',
  470. 'errors' => $errors
  471. ));
  472. }
  473. $user->setTown(trim($this->getRequest()->request->get('town')));
  474. $user->setCountry(trim($this->getRequest()->request->get('country')));
  475. $this->getDoctrine()->getEntityManager()->persist($user);
  476. $this->getDoctrine()->getEntityManager()->flush();
  477. return $this->jsonResponse(array(
  478. 'status' => 'success'
  479. ));
  480. }
  481. public function updateAvatarAction(Request $request)
  482. {
  483. $form = $this->getAvatarForm();
  484. $form->bind($request);
  485. if ($form->isValid()) {
  486. $em = $this->getEntityManager();
  487. $form->getData()->preUploadAvatar();
  488. $form->getData()->uploadAvatar();
  489. $em->persist($form->getData());
  490. $em->flush();
  491. $this->setFlash('success',
  492. $this->trans('my_account.avatar.success', array(), 'userui'));
  493. return $this->redirect($this->generateUrl('my_account'));
  494. }
  495. $this->setFlash('error',
  496. $this->trans('my_account.avatar.error', array(), 'userui'));
  497. return $this->redirect($this->generateUrl('my_account'));
  498. }
  499. public function updatePreferencesAction(Request $request)
  500. {
  501. $form = $this->getPreferencesForm();
  502. $form->bind($request);
  503. if ($form->isValid()) {
  504. $em = $this->getEntityManager();
  505. $em->persist($form->getData());
  506. $em->flush();
  507. $this->setFlash('success',
  508. $this->trans('my_account.preferences.success', array(), 'userui'));
  509. return $this->redirect($this->generateUrl('my_account'));
  510. }
  511. $this->setFlash('error',
  512. $this->trans('my_account.preferences.error', array(), 'userui'));
  513. return $this->redirect($this->generateUrl('my_account'));
  514. }
  515. public function updateHelpViewedAction($help_id, $token)
  516. {
  517. if ($this->getUser()->getPersonalHash('updateHelpAction') != $token)
  518. {
  519. return $this->jsonNotFoundResponse();
  520. }
  521. $this->getUser()->setSeeHelp($help_id, false);
  522. $this->persist($this->getUser());
  523. $this->flush();
  524. return $this->jsonResponse(array(
  525. 'status' => 'success'
  526. ));
  527. }
  528. public function subscribeOrLoginAction(Request $request)
  529. {
  530. return $this->jsonResponse(array(
  531. 'status' => 'success',
  532. 'data' => $this->render('MuzichUserBundle:Account:subscribe_or_login.html.twig', array(
  533. 'form' => $this->getRegistrationForm($this->getNewUser())->createView()
  534. ))->getContent()
  535. ));
  536. }
  537. public function changeUsernameAction(Request $request)
  538. {
  539. $user = $this->getUserRefreshed();
  540. if (!$user->isUsernameUpdatable())
  541. {
  542. return new RedirectResponse($this->generateUrl('my_account'));
  543. }
  544. $errors = array();
  545. $form = $this->getChangeUsernameForm($user);
  546. if ($request->getMethod() == 'POST')
  547. {
  548. $form->bind($request);
  549. $errors = $this->checkChangeUsernameValues($form);
  550. if ($form->isValid() && !count($errors))
  551. {
  552. $form->getData()->setUsernameUpdatable(false);
  553. $this->persist($user);
  554. $this->flush();
  555. $this->setFlash('success', 'user.change_username.success');
  556. return new RedirectResponse($this->generateUrl('my_account'));
  557. }
  558. else
  559. {
  560. $this->setFlash('error', 'user.change_username.failure');
  561. }
  562. }
  563. return $this->render('MuzichUserBundle:User:change_username.html.twig', array(
  564. 'form' => $form->createView(),
  565. 'errors' => $errors
  566. ));
  567. }
  568. protected function checkChangeUsernameValues($form)
  569. {
  570. $errors = array();
  571. $userManager = $this->container->get('fos_user.user_manager');
  572. if ($userManager->findUserByUsername($form->getData()->getUsername()))
  573. {
  574. $errors[] = $this->trans('error.change_username.duplicate', array(), 'validators');
  575. }
  576. if (strlen($form->getData()->getUsername()) < 3)
  577. {
  578. $errors[] = $this->trans(
  579. 'error.change_username.min',
  580. array('%limit%' => 3),
  581. 'validators'
  582. );
  583. }
  584. if (strlen($form->getData()->getUsername()) > 32)
  585. {
  586. $errors[] = $this->trans(
  587. 'error.change_username.max',
  588. array('%limit%' => 32),
  589. 'validators'
  590. );
  591. }
  592. return $errors;
  593. }
  594. protected function getChangeUsernameForm(User $user)
  595. {
  596. return $this->createFormBuilder($user)
  597. ->add('username', 'text')
  598. ->getForm()
  599. ;
  600. }
  601. public function sendEmailConfirmAction(Request $request, $set_send_time = true)
  602. {
  603. $user = $this->getUser();
  604. if ($user->isEmailConfirmed())
  605. {
  606. if ($request->isXmlHttpRequest())
  607. {
  608. return $this->jsonResponse(array(
  609. 'status' => 'success',
  610. 'result' => 'already_confirmed',
  611. 'message' => $this->trans('user.confirm_email.alreaydy', array(), 'flash')
  612. ));
  613. }
  614. $this->setFlash('success', 'user.confirm_email.alreaydy');
  615. return new RedirectResponse($this->generateUrl('home'));
  616. }
  617. if ((time() - $user->getEmailConfirmationSentTimestamp() < $this->getParameter('email_confirmation_email_interval')))
  618. {
  619. if ($request->isXmlHttpRequest())
  620. {
  621. return $this->jsonResponse(array(
  622. 'status' => 'error',
  623. 'result' => 'already_sent_recently',
  624. 'message' => $this->trans('user.confirm_email.sent_recently', array(), 'flash')
  625. ));
  626. }
  627. $this->setFlash('success', 'user.confirm_email.sent_recently');
  628. return new RedirectResponse($this->generateUrl('my_account'));
  629. }
  630. $this->sendEmailconfirmationEmail($set_send_time);
  631. if ($request->isXmlHttpRequest())
  632. {
  633. return $this->jsonResponse(array(
  634. 'status' => 'success',
  635. 'result' => 'sent',
  636. 'message' => $this->trans('user.confirm_email.sent', array(), 'flash')
  637. ));
  638. }
  639. $this->setFlash('success', 'user.confirm_email.sent');
  640. return new RedirectResponse($this->generateUrl('my_account'));
  641. }
  642. public function confirmEmailAction(Request $request, $token)
  643. {
  644. $user = $this->getUser();
  645. if ($token == hash('sha256', $user->getConfirmationToken().$user->getEmail()))
  646. {
  647. $user->setEmailConfirmed(true);
  648. $this->persist($user);
  649. $this->flush();
  650. $this->setFlash('success', 'user.confirm_email.confirmed');
  651. return new RedirectResponse($this->generateUrl('home'));
  652. }
  653. $this->setFlash('success', 'user.confirm_email.failtoken');
  654. return new RedirectResponse($this->generateUrl('my_account'));
  655. }
  656. public function showEmailNotConfirmedAction()
  657. {
  658. return $this->jsonResponse(array(
  659. 'status' => 'success',
  660. 'data' => $this->render('MuzichUserBundle:Account:email_not_confirmed.html.twig')->getContent()
  661. ));
  662. }
  663. public function favoriteTagsHelpboxAction()
  664. {
  665. return $this->jsonResponse(array(
  666. 'status' => 'success',
  667. 'data' => $this->render('MuzichUserBundle:User:helpbox_favorite_tags.html.twig', array(
  668. 'form' => $this->getTagsFavoritesForm($this->getUser())->createView(),
  669. 'form_name' => 'favorites_tags_helpbox'
  670. ))->getContent()
  671. ));
  672. }
  673. }