Guest User

Untitled

a guest
Aug 15th, 2018
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.82 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * (c) Thibault Duplessis <thibault.duplessis@gmail.com>
  5. *
  6. * This source file is subject to the MIT license that is bundled
  7. * with this source code in the file LICENSE.
  8. */
  9.  
  10. namespace Bundle\DoctrineUserBundle\Controller;
  11.  
  12. use Symfony\Bundle\FrameworkBundle\Controller\Controller as Controller;
  13. use Bundle\DoctrineUserBundle\Model\User;
  14. use Bundle\DoctrineUserBundle\Form\ChangePassword;
  15. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  16.  
  17. /**
  18. * RESTful controller managing user CRUD
  19. */
  20. class UserController extends Controller
  21. {
  22. /**
  23. * Show all users
  24. **/
  25. public function listAction()
  26. {
  27. $users = $this['doctrine_user.user_repository']->findAll();
  28.  
  29. return $this->render('DoctrineUserBundle:User:list.'.$this->getRenderer(), array('users' => $users));
  30. }
  31.  
  32. /**
  33. * Show one user
  34. */
  35. public function showAction($username)
  36. {
  37. $user = $this->findUser($username);
  38.  
  39. return $this->render('DoctrineUserBundle:User:show.'.$this->getRenderer(), array('user' => $user));
  40. }
  41.  
  42. /**
  43. * Edit one user, show the edit form
  44. */
  45. public function editAction($username)
  46. {
  47. $user = $this->findUser($username);
  48. $form = $this->createForm($user);
  49.  
  50. return $this->render('DoctrineUserBundle:User:edit.'.$this->getRenderer(), array(
  51. 'form' => $form,
  52. 'username' => $username
  53. ));
  54. }
  55.  
  56. /**
  57. * Update a user
  58. */
  59. public function updateAction($username)
  60. {
  61. $user = $this->findUser($username);
  62. $form = $this->createForm($user);
  63.  
  64. $request = $this['request'];
  65. if ($data = $request->request->get($form->getName())) {
  66. $form->bind($data);
  67. if ($form->isValid()) {
  68. $this->saveUser($user);
  69. $this['session']->setFlash('doctrine_user_user_update/success', true);
  70. if ($request->isXmlHttpRequest()) {
  71. return $this->render('DoctrineUserBundle:User:show.'.$this->getRenderer(), array('user' => $user));
  72. }
  73. $userUrl = $this->generateUrl('doctrine_user_user_show', array('username' => $user->getUsername()));
  74. return $this->redirect($userUrl);
  75. }
  76. }
  77.  
  78. return $this->render('DoctrineUserBundle:User:edit.'.$this->getRenderer(), array(
  79. 'form' => $form,
  80. 'username' => $username
  81. ));
  82. }
  83.  
  84. /**
  85. * Show the new form
  86. */
  87. public function newAction()
  88. {
  89. $form = $this->createForm();
  90.  
  91. return $this->render('DoctrineUserBundle:User:new.'.$this->getRenderer(), array(
  92. 'form' => $form
  93. ));
  94. }
  95.  
  96. /**
  97. * Create a user and send a confirmation email
  98. */
  99. public function createAction()
  100. {
  101. $form = $this->createForm();
  102. $request = $this['request'];
  103. $form->bind($request->request->get($form->getName()));
  104.  
  105. if ($form->isValid()) {
  106. $user = $form->getData();
  107. if ($this->container->getParameter('doctrine_user.confirmation_email.enabled')) {
  108. $user->setIsActive(false);
  109. $this->saveUser($user);
  110. $this['session']->set('doctrine_user_send_confirmation_email/email', $user->getEmail());
  111. $route = 'doctrine_user_user_send_confirmation_email';
  112. } else {
  113. $user->setIsActive(true);
  114. $this->saveUser($user);
  115. $this['doctrine_user.auth']->login($user);
  116. $route = 'doctrine_user_user_confirmed';
  117. }
  118.  
  119. $this['session']->setFlash('doctrine_user_user_create/success', true);
  120. if ($request->isXmlHttpRequest()) {
  121. if ($route == 'doctrine_user_user_confirmed') {
  122. return $this->render('DoctrineUserBundle:User:confirmed.'.$this->getRenderer());
  123. }
  124. return $this->render('DoctrineUserBundle:User:checkConfirmationEmail.'.$this->getRenderer(), array(
  125. 'user' => $user,
  126. ));
  127. }
  128.  
  129. $url = $this->generateUrl($route);
  130. return $this->redirect($url);
  131. }
  132.  
  133. return $this->render('DoctrineUserBundle:User:new.'.$this->getRenderer(), array(
  134. 'form' => $form
  135. ));
  136. }
  137.  
  138. /**
  139. * Send the confirmation email containing a link to the confirmation page,
  140. * then redirect the check email page
  141. */
  142. public function sendConfirmationEmailAction()
  143. {
  144. if (!$this->container->getParameter('doctrine_user.confirmation_email.enabled')) {
  145. throw new NotFoundHttpException('Email confirmation is disabled');
  146. }
  147.  
  148. $email = $this['session']->get('doctrine_user_send_confirmation_email/email');
  149. if (!$email) {
  150. throw new NotFoundHttpException(sprintf('The email "%s" does not exist', $email));
  151. }
  152.  
  153. $user = $this['doctrine_user.user_repository']->findOneByEmail($email);
  154. if (!$user) {
  155. throw new NotFoundHttpException(sprintf('The email "%s" does not exist', $email));
  156. }
  157.  
  158. $message = $this->getConfirmationEmailMessage($user);
  159. $this['mailer']->send($message);
  160.  
  161. return $this->redirect($this->generateUrl('doctrine_user_user_check_confirmation_email'));
  162. }
  163.  
  164. protected function getConfirmationEmailMessage(User $user)
  165. {
  166. $template = $this->container->getParameter('doctrine_user.confirmation_email.template');
  167. // Render the email, use the first line as the subject, and the rest as the body
  168. $rendered = $this->renderView($template.'.'.$this->getRenderer(), array(
  169. 'user' => $user,
  170. 'confirmationUrl' => $this->generateUrl('doctrine_user_user_confirm', array('token' => $user->getConfirmationToken()), true)
  171. ));
  172. $renderedLines = explode("\n", $rendered);
  173. $subject = $renderedLines[0];
  174. $body = implode("\n", array_slice($renderedLines, 1));
  175.  
  176. $fromEmail = $this->container->getParameter('doctrine_user.confirmation_email.from_email');
  177. return \Swift_Message::newInstance()
  178. ->setSubject($subject)
  179. ->setFrom($fromEmail)
  180. ->setTo($user->getEmail())
  181. ->setBody($body);
  182. }
  183.  
  184. /**
  185. * Tell the user to check his email provider
  186. */
  187. public function checkConfirmationEmailAction()
  188. {
  189. $email = $this['session']->get('doctrine_user_send_confirmation_email/email');
  190. if (!$email) {
  191. throw new NotFoundHttpException(sprintf('The email "%s" does not exist', $email));
  192. }
  193.  
  194. $user = $this['doctrine_user.user_repository']->findOneByEmail($email);
  195. if (!$user) {
  196. throw new NotFoundHttpException(sprintf('The user "%s" does not exist', $email));
  197. }
  198.  
  199. return $this->render('DoctrineUserBundle:User:checkConfirmationEmail.'.$this->getRenderer(), array(
  200. 'user' => $user,
  201. ));
  202. }
  203.  
  204. /**
  205. * Receive the confirmation token from user email provider, login the user
  206. */
  207. public function confirmAction($token)
  208. {
  209. $user = $this['doctrine_user.user_repository']->findOneByConfirmationToken($token);
  210. if (!$user) {
  211. throw new NotFoundHttpException(sprintf('No user to confirm with token "%s"', $token));
  212. }
  213.  
  214. $user->setConfirmationToken(null);
  215. $user->setIsActive(true);
  216.  
  217. $this->saveUser($user);
  218.  
  219. $this['doctrine_user.auth']->login($user);
  220.  
  221. return $this->redirect($this->generateUrl('doctrine_user_user_confirmed'));
  222. }
  223.  
  224. /**
  225. * Tell the user his account is now confirmed
  226. */
  227. public function confirmedAction()
  228. {
  229. $user = $this['doctrine_user.auth']->getUser();
  230. if (!$user) {
  231. throw new NotFoundHttpException(sprintf('No user confirmed'));
  232. }
  233.  
  234. return $this->render('DoctrineUserBundle:User:confirmed.'.$this->getRenderer());
  235. }
  236.  
  237. /**
  238. * Delete one user
  239. */
  240. public function deleteAction($username)
  241. {
  242. $user = $this->findUser($username);
  243. if (!$user) {
  244. throw new NotFoundHttpException(sprintf('Must be logged in to change your password'));
  245. }
  246.  
  247. $objectManager = $this['doctrine_user.user_repository']->getObjectManager();
  248. $objectManager->remove($user);
  249. $objectManager->flush();
  250. $this['session']->setFlash('doctrine_user_user_delete/success', true);
  251.  
  252. return $this->redirect($this->generateUrl('doctrine_user_user_list'));
  253. }
  254.  
  255. /**
  256. * Change user password: show form
  257. */
  258. public function changePasswordAction()
  259. {
  260. $user = $this['doctrine_user.auth']->getUser();
  261. if (!$user) {
  262. throw new NotFoundHttpException(sprintf('Must be logged in to change your password'));
  263. }
  264.  
  265. $form = $this->createChangePasswordForm($user);
  266.  
  267. return $this->render('DoctrineUserBundle:User:changePassword.'.$this->getRenderer(), array(
  268. 'form' => $form
  269. ));
  270. }
  271.  
  272. /**
  273. * Change user password: submit form
  274. */
  275. public function changePasswordUpdateAction()
  276. {
  277. $user = $this['doctrine_user.auth']->getUser();
  278. if (!$user) {
  279. throw new NotFoundHttpException(sprintf('Must be logged in to change your password'));
  280. }
  281.  
  282. $form = $this->createChangePasswordForm($user);
  283. $form->bind($this['request']->request->get($form->getName()));
  284. if($form->isValid()) {
  285. $user->setPassword($form->getNewPassword());
  286. $this['doctrine_user.user_repository']->getObjectManager()->flush();
  287. $userUrl = $this->generateUrl('doctrine_user_user_show', array('username' => $user->getUsername()));
  288. return $this->redirect($userUrl);
  289. }
  290.  
  291. return $this->render('DoctrineUserBundle:User:changePassword.'.$this->getRenderer(), array(
  292. 'form' => $form
  293. ));
  294. }
  295.  
  296. /**
  297. * Find a user by its username
  298. *
  299. * @param string $username
  300. * @throw NotFoundException if user does not exist
  301. * @return User
  302. */
  303. protected function findUser($username)
  304. {
  305. if (empty($username)) {
  306. throw new NotFoundHttpException(sprintf('The user "%s" does not exist', $username));
  307. }
  308. $user = $this['doctrine_user.user_repository']->findOneByUsername($username);
  309. if (!$user) {
  310. throw new NotFoundHttpException(sprintf('The user "%s" does not exist', $username));
  311. }
  312.  
  313. return $user;
  314. }
  315.  
  316. /**
  317. * Save a user in database
  318. *
  319. * @param User $user
  320. * @return null
  321. **/
  322. public function saveUser(User $user)
  323. {
  324. $objectManager = $this['doctrine_user.user_repository']->getObjectManager();
  325. $objectManager->persist($user);
  326. $objectManager->flush();
  327. }
  328.  
  329. /**
  330. * Create a UserForm instance and returns it
  331. *
  332. * @param User $object
  333. * @return Bundle\DoctrineUserBundle\Form\UserForm
  334. */
  335. protected function createForm($object = null)
  336. {
  337. $form = $this['doctrine_user.user_form'];
  338. if (null === $object) {
  339. $userClass = $this['doctrine_user.user_repository']->getObjectClass();
  340. $object = new $userClass();
  341. }
  342.  
  343. $form->setData($object);
  344.  
  345. return $form;
  346. }
  347.  
  348. protected function createChangePasswordForm(User $user)
  349. {
  350. $form = $this['doctrine_user.change_password_form.class'];
  351. $form->setData($user);
  352.  
  353. return $form;
  354. }
  355.  
  356. protected function getRenderer()
  357. {
  358. return $this->container->getParameter('doctrine_user.template.renderer');
  359. }
  360. }
Add Comment
Please, Sign In to add comment