UserController.php 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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. class UserController extends Controller
  13. {
  14. protected $tags_favorites = null;
  15. protected function getChangeEmailForm()
  16. {
  17. $collectionConstraint = new Collection(array(
  18. 'email' => new Email(array('message' => 'error.changeemail.email.invalid')),
  19. ));
  20. return $this->createFormBuilder(null, array(
  21. //'validation_constraint' => $collectionConstraint, UPGRADE 2.1
  22. 'constraints' => $collectionConstraint,
  23. ))
  24. ->add('email', 'text')
  25. ->getForm()
  26. ;
  27. }
  28. protected function getPreferencesForm()
  29. {
  30. /**
  31. * Bug lors des tests: L'user n'est pas 'lié' a celui en base par doctrine.
  32. * Docrine le voit si on faire une requete directe.
  33. */
  34. if ($this->container->getParameter('env') == 'test')
  35. {
  36. $user = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')->findOneById(
  37. $this->container->get('security.context')->getToken()->getUser()->getId(),
  38. array()
  39. )->getSingleResult();
  40. }
  41. else
  42. {
  43. $user = $this->getUser();
  44. }
  45. return $this->createFormBuilder($user)
  46. ->add('mail_newsletter', 'checkbox', array('required' => false))
  47. ->add('mail_partner', 'checkbox', array('required' => false))
  48. ->getForm()
  49. ;
  50. }
  51. protected function getTagsFavoritesForm($user)
  52. {
  53. $ids = array();
  54. foreach ($this->getTagsFavorites() as $id => $name)
  55. {
  56. $ids[] = $id;
  57. }
  58. return $this->createForm(
  59. new TagFavoritesForm(),
  60. array('tags' => json_encode($ids))
  61. );
  62. }
  63. protected function getTagsFavorites($force = false)
  64. {
  65. if ($this->tags_favorites === null || $force)
  66. {
  67. $user = $this->getUser();
  68. /**
  69. * Bug lors des tests: L'user n'est pas 'lié' a celui en base par doctrine.
  70. * Docrine le voit si on faire une requete directe.
  71. */
  72. if ($this->container->getParameter('env') == 'test')
  73. {
  74. $user = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')->findOneById(
  75. $this->container->get('security.context')->getToken()->getUser()->getId(),
  76. array()
  77. )->getSingleResult();
  78. }
  79. $this->tags_favorites = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')
  80. ->getTagsFavorites($user->getId())
  81. ;
  82. }
  83. return $this->tags_favorites;
  84. }
  85. /**
  86. * Page de configuration de son compte
  87. *
  88. * @Template()
  89. */
  90. public function accountAction()
  91. {
  92. $user = $this->getUser();
  93. $form_password = $this->container->get('fos_user.change_password.form');
  94. $form_tags_favorites = $this->getTagsFavoritesForm($user);
  95. $change_email_form = $this->getChangeEmailForm();
  96. return array(
  97. 'user' => $user,
  98. 'form_password' => $form_password->createView(),
  99. 'form_tags_favorites' => $form_tags_favorites->createView(),
  100. 'form_tags_favorites_name' => $form_tags_favorites->getName(),
  101. 'favorite_tags_id' => $this->getTagsFavorites(),
  102. 'change_email_form' => $change_email_form->createView(),
  103. 'avatar_form' => $this->getAvatarForm()->createView(),
  104. 'preferences_form' => $this->getPreferencesForm()->createView()
  105. );
  106. }
  107. protected function getAvatarForm()
  108. {
  109. return $this->createFormBuilder($this->getUser())
  110. ->add('avatar')
  111. ->getForm()
  112. ;
  113. }
  114. public function registerAction()
  115. {
  116. $form = $this->container->get('fos_user.registration.form');
  117. $formHandler = $this->container->get('fos_user.registration.form.handler');
  118. $confirmationEnabled = $this->container->getParameter('fos_user.registration.confirmation.enabled');
  119. $process = $formHandler->process($confirmationEnabled);
  120. if ($process)
  121. {
  122. $user = $form->getData();
  123. $authUser = false;
  124. if ($confirmationEnabled) {
  125. $this->container->get('session')->set('fos_user_send_confirmation_email/email', $user->getEmail());
  126. $route = 'fos_user_registration_check_email';
  127. } else {
  128. $authUser = true;
  129. $route = 'start';
  130. }
  131. $this->setFlash('fos_user_success', 'registration.flash.user_created');
  132. $url = $this->container->get('router')->generate($route);
  133. $response = new RedirectResponse($url);
  134. if ($authUser) {
  135. $this->authenticateUser($user, $response);
  136. }
  137. $formHandler->getToken()->addUseCount();
  138. $em = $this->getDoctrine()->getEntityManager();
  139. $em->persist($formHandler->getToken());
  140. $em->flush();
  141. return $response;
  142. }
  143. return $this->container->get('templating')->renderResponse(
  144. 'MuzichIndexBundle:Index:index.html.twig',
  145. array(
  146. 'form' => $form->createView(),
  147. 'error' => null,
  148. 'registration_errors' => $form->getErrors(),
  149. 'registration_errors_pers' => $formHandler->getErrors(),
  150. 'last_username' => null,
  151. 'registration_page' => true,
  152. 'presubscription_form' => $this->getPreSubscriptionForm()->createView()
  153. )
  154. );
  155. }
  156. /**
  157. * Un bug étrange empêche la mise ne place de contraintes sur le formulaire
  158. * d'inscription. On effectue alors les vérifications ici.
  159. *
  160. * C'est sale, mais ça marche ...
  161. *
  162. * @return array of string errors
  163. */
  164. protected function checkChangePasswordInformations($form)
  165. {
  166. $errors = array();
  167. $form_values = $this->getRequest()->request->get($form->getName());
  168. $user = $form->getData();
  169. /**
  170. * Mot de passes indentiques
  171. */
  172. if ($form_values['new']['first'] != $form_values['new']['second'])
  173. {
  174. $errors[] = $this->get('translator')->trans(
  175. 'error.changepassword.new.notsame',
  176. array(),
  177. 'validators'
  178. );
  179. }
  180. return $errors;
  181. }
  182. public function changePasswordAction()
  183. {
  184. $user = $this->getUser();
  185. /**
  186. * Bug lors des tests: L'user n'est pas 'lié' a celui en base par doctrine.
  187. * Docrine le voit si on faire une requete directe.
  188. */
  189. if ($this->container->getParameter('env') == 'test')
  190. {
  191. $user = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')->findOneById(
  192. $this->container->get('security.context')->getToken()->getUser()->getId(),
  193. array()
  194. )->getSingleResult();
  195. }
  196. if (!is_object($user) || !$user instanceof UserInterface) {
  197. throw new AccessDeniedException('This user does not have access to this section.');
  198. }
  199. $form = $this->container->get('fos_user.change_password.form');
  200. $formHandler = $this->container->get('fos_user.change_password.form.handler');
  201. $process = $formHandler->process($user);
  202. if (count(($errors = $this->checkChangePasswordInformations($form))) < 1 && $process)
  203. {
  204. $this->container->get('session')->setFlash('fos_user_success', 'change_password.flash.success');
  205. return new RedirectResponse($this->generateUrl('my_account'));
  206. }
  207. else
  208. {
  209. $form_tags_favorites = $this->getTagsFavoritesForm($user);
  210. $change_email_form = $this->getChangeEmailForm();
  211. return $this->container->get('templating')->renderResponse(
  212. 'MuzichUserBundle:User:account.html.twig',
  213. array(
  214. 'form_password' => $form->createView(),
  215. 'errors_pers' => $errors,
  216. 'user' => $user,
  217. 'form_tags_favorites' => $form_tags_favorites->createView(),
  218. 'form_tags_favorites_name' => $form_tags_favorites->getName(),
  219. 'favorite_tags_id' => $this->getTagsFavorites(),
  220. 'change_email_form' => $change_email_form->createView(),
  221. 'avatar_form' => $this->getAvatarForm()->createView()
  222. )
  223. );
  224. }
  225. }
  226. /**
  227. * Page ouverte après l'inscription sur laquelle on propose de saisir ses
  228. * tags favoris.
  229. *
  230. * @Template()
  231. */
  232. public function startAction()
  233. {
  234. $user = $this->getUser();
  235. $form_tags_favorites = $this->getTagsFavoritesForm($user);
  236. return array(
  237. 'favorite_tags_id' => $this->getTagsFavorites(),
  238. 'form_tags_favorites' => $form_tags_favorites->createView(),
  239. 'form_tags_favorites_name' => $form_tags_favorites->getName(),
  240. );
  241. }
  242. /**
  243. *
  244. * @param string $redirect
  245. */
  246. public function updateTagFavoritesAction($redirect)
  247. {
  248. $request = $this->getRequest();
  249. $user = $this->getUser(true, array('join' => array('favorites_tags')));
  250. /**
  251. * Bug lors des tests: L'user n'est pas 'lié' a celui en base par doctrine.
  252. * Docrine le voit si on faire une requete directe.
  253. */
  254. if ($this->container->getParameter('env') == 'test')
  255. {
  256. $user = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')->findOneById(
  257. $this->container->get('security.context')->getToken()->getUser()->getId(),
  258. array()
  259. )->getSingleResult();
  260. }
  261. $form = $this->getTagsFavoritesForm($user);
  262. if ($request->getMethod() == 'POST')
  263. {
  264. $form->bind($request);
  265. if ($form->isValid())
  266. {
  267. $data = $form->getData();
  268. $user->updateTagsFavoritesById($this->getDoctrine()->getEntityManager(), $data['tags']);
  269. // On réinitialise l'eventuel session de recherche en mémoire
  270. $session = $this->get("session");
  271. $session->remove('user.element_search.params');
  272. $this->container->get('session')->setFlash('success', 'Vos tags péférés ont correctements été mis a jour.');
  273. }
  274. else
  275. {
  276. return $this->container->get('templating')->renderResponse(
  277. 'MuzichUserBundle:User:start.html.twig',
  278. array(
  279. 'form' => $form->createView()
  280. )
  281. );
  282. }
  283. }
  284. // (Il y aura aussi une redirection vers "mon compte / tags")
  285. if ($redirect == 'home')
  286. {
  287. return $this->redirect($this->generateUrl('home'));
  288. }
  289. else
  290. {
  291. return $this->redirect($this->generateUrl('my_account'));
  292. }
  293. }
  294. protected function checkChangeEmailFrequencies($user, $new_email)
  295. {
  296. $delay = $this->container->getParameter('changeemail_security_delay');
  297. if (($last_request_datetime = $user->getEmailRequestedDatetime()))
  298. {
  299. if ((time() - $last_request_datetime) < $delay)
  300. {
  301. return false;
  302. }
  303. }
  304. return true;
  305. }
  306. /**
  307. * Procédure de demande de changement de mot de passe
  308. */
  309. public function changeEmailRequestAction()
  310. {
  311. $em = $this->getDoctrine()->getEntityManager();
  312. $user = $this->getUser();
  313. /**
  314. * Bug lors des tests: L'user n'est pas 'lié' a celui en base par doctrine.
  315. * Docrine le voit si on faire une requete directe.
  316. */
  317. if ($this->container->getParameter('env') == 'test')
  318. {
  319. $user = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')->findOneById(
  320. $this->container->get('security.context')->getToken()->getUser()->getId(),
  321. array()
  322. )->getSingleResult();
  323. }
  324. $request = $this->getRequest();
  325. $change_email_form = $this->getChangeEmailForm();
  326. $change_email_form->bind($request);
  327. if ($change_email_form->isValid())
  328. {
  329. $data = $change_email_form->getData();
  330. $email = $data['email'];
  331. if (!$this->checkChangeEmailFrequencies($user, $email))
  332. {
  333. $this->setFlash('error', 'user.changeemail.wait');
  334. return new RedirectResponse($this->generateUrl('my_account'));
  335. }
  336. /*
  337. * Optimisation: Ecrire une lib Mailer pour gérer les envois.
  338. * cf le mailer de FOSUserBundle
  339. */
  340. // On renseigne en base l'email demandé
  341. $user->setEmailRequested($email);
  342. $user->setEmailRequestedDatetime(time());
  343. //$user->generateConfirmationToken(); UPGRADE FOSUserBundle 1.3
  344. $tokenGenerator = $this->container->get('fos_user.util.token_generator');
  345. $user->setConfirmationToken($tokenGenerator->generateToken());
  346. $token = hash('sha256', $user->getConfirmationToken().$email);
  347. $url = $this->get('router')->generate('change_email_confirm', array('token' => $token), true);
  348. $rendered = $this->get('templating')->render('MuzichUserBundle:User:change_email_mail.txt.twig', array(
  349. 'user' => $user,
  350. 'confirmationUrl' => $url
  351. ));
  352. //$this->sendEmailMessage($rendered, $this->parameters['from_email']['resetting'], $user->getEmail());
  353. // Render the email, use the first line as the subject, and the rest as the body
  354. $renderedLines = explode("\n", trim($rendered));
  355. $subject = $renderedLines[0];
  356. $body = implode("\n", array_slice($renderedLines, 1));
  357. $message = \Swift_Message::newInstance()
  358. ->setSubject($subject)
  359. ->setFrom('contact@muzi.ch')
  360. ->setTo($email)
  361. ->setBody($body);
  362. $mailer = $this->get('mailer');
  363. $mailer->send($message);
  364. $this->setFlash('success', 'user.changeemail.mail_send');
  365. $em->flush();
  366. return new RedirectResponse($this->generateUrl('my_account'));
  367. }
  368. // En cas d'échec
  369. $form_password = $this->container->get('fos_user.change_password.form');
  370. $form_tags_favorites = $this->getTagsFavoritesForm($user);
  371. return $this->container->get('templating')->renderResponse(
  372. 'MuzichUserBundle:User:account.html.twig',
  373. array(
  374. 'user' => $user,
  375. 'form_password' => $form_password->createView(),
  376. 'form_tags_favorites' => $form_tags_favorites->createView(),
  377. 'form_tags_favorites_name' => $form_tags_favorites->getName(),
  378. 'favorite_tags_id' => $this->getTagsFavorites(),
  379. 'change_email_form' => $change_email_form->createView(),
  380. 'avatar_form' => $this->getAvatarForm()->createView()
  381. )
  382. );
  383. }
  384. /**
  385. * Procédure de confirmation de la nouvelle adresse email.
  386. */
  387. public function changeEmailConfirmAction($token)
  388. {
  389. $em = $this->getDoctrine()->getEntityManager();
  390. $um = $this->get('muzich_user_manager');
  391. $user = $this->getUser();
  392. /**
  393. * Bug lors des tests: L'user n'est pas 'lié' a celui en base par doctrine.
  394. * Docrine le voit si on faire une requete directe.
  395. */
  396. if ($this->container->getParameter('env') == 'test')
  397. {
  398. $user = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')->findOneById(
  399. $this->container->get('security.context')->getToken()->getUser()->getId(),
  400. array()
  401. )->getSingleResult();
  402. }
  403. $token_ = hash('sha256', $user->getConfirmationToken().($email = $user->getEmailRequested()));
  404. // Le token est-il valide
  405. if ($token_ != $token)
  406. {
  407. $this->setFlash('error', 'user.changeemail.token_invalid');
  408. return new RedirectResponse($this->generateUrl('my_account'));
  409. }
  410. $user->setEmail($email);
  411. $user->setEmailRequested(null);
  412. $um->updateCanonicalFields($user);
  413. $em->flush();
  414. $this->setFlash('success', 'user.changeemail.success');
  415. return new RedirectResponse($this->generateUrl('my_account'));
  416. }
  417. /**
  418. *
  419. * @param string $town
  420. * @param string $country
  421. * @param string $token
  422. * @return Response
  423. */
  424. public function updateAddressAction($token)
  425. {
  426. if (($response = $this->mustBeConnected(true)))
  427. {
  428. return $response;
  429. }
  430. /**
  431. * Bug lors des tests: L'user n'est pas 'lié' a celui en base par doctrine.
  432. * Docrine le voit si on faire une requete directe.
  433. */
  434. $user = $this->getUser();
  435. if ($this->container->getParameter('env') == 'test')
  436. {
  437. $user = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')->findOneById(
  438. $this->container->get('security.context')->getToken()->getUser()->getId(),
  439. array()
  440. )->getSingleResult();
  441. }
  442. $errors = array();
  443. if ($user->getPersonalHash() != $token)
  444. {
  445. $errors[] = 'NotAllowed';
  446. }
  447. if (!trim($this->getRequest()->request->get('town')))
  448. {
  449. $errors[] = $this->trans('my_account.address.form.errors.notown', array(), 'userui');
  450. }
  451. if (!trim($this->getRequest()->request->get('country')))
  452. {
  453. $errors[] = $this->trans('my_account.address.form.errors.nocountry', array(), 'userui');
  454. }
  455. if (count($errors))
  456. {
  457. return $this->jsonResponse(array(
  458. 'status' => 'error',
  459. 'errors' => $errors
  460. ));
  461. }
  462. $user->setTown(trim($this->getRequest()->request->get('town')));
  463. $user->setCountry(trim($this->getRequest()->request->get('country')));
  464. $this->getDoctrine()->getEntityManager()->persist($user);
  465. $this->getDoctrine()->getEntityManager()->flush();
  466. return $this->jsonResponse(array(
  467. 'status' => 'success'
  468. ));
  469. }
  470. public function updateAvatarAction(Request $request)
  471. {
  472. $form = $this->getAvatarForm();
  473. $form->bind($request);
  474. if ($form->isValid()) {
  475. $em = $this->getEntityManager();
  476. $form->getData()->preUploadAvatar();
  477. $form->getData()->uploadAvatar();
  478. $em->persist($form->getData());
  479. $em->flush();
  480. $this->setFlash('success',
  481. $this->trans('my_account.avatar.success', array(), 'userui'));
  482. return $this->redirect($this->generateUrl('my_account'));
  483. }
  484. $this->setFlash('error',
  485. $this->trans('my_account.avatar.error', array(), 'userui'));
  486. return $this->redirect($this->generateUrl('my_account'));
  487. }
  488. public function updatePreferencesAction(Request $request)
  489. {
  490. $form = $this->getPreferencesForm();
  491. $form->bind($request);
  492. if ($form->isValid()) {
  493. $em = $this->getEntityManager();
  494. $em->persist($form->getData());
  495. $em->flush();
  496. $this->setFlash('success',
  497. $this->trans('my_account.preferences.success', array(), 'userui'));
  498. return $this->redirect($this->generateUrl('my_account'));
  499. }
  500. $this->setFlash('error',
  501. $this->trans('my_account.preferences.error', array(), 'userui'));
  502. return $this->redirect($this->generateUrl('my_account'));
  503. }
  504. public function updateHelpViewedAction($help_id, $token)
  505. {
  506. if ($this->getUser()->getPersonalHash('updateHelpAction') != $token)
  507. {
  508. return $this->jsonNotFoundResponse();
  509. }
  510. $this->getUser()->setSeeHelp($help_id, false);
  511. $this->persist($this->getUser());
  512. $this->flush();
  513. return $this->jsonResponse(array(
  514. 'status' => 'success'
  515. ));
  516. }
  517. }