Advertisement
Guest User

Untitled

a guest
May 12th, 2017
605
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.60 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. * This file is part of the FOSUserBundle package.
  5. *
  6. * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11.  
  12. namespace UserBundle\Controller;
  13.  
  14. use FOS\UserBundle\FOSUserEvents;
  15. use FOS\UserBundle\Event\FormEvent;
  16. use FOS\UserBundle\Event\FilterUserResponseEvent;
  17. use FOS\UserBundle\Event\GetResponseUserEvent;
  18. use FOS\UserBundle\Model\UserInterface;
  19. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  20. use Symfony\Component\HttpFoundation\Request;
  21. use Symfony\Component\HttpFoundation\RedirectResponse;
  22. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  23.  
  24. /**
  25. * Controller managing the password change
  26. *
  27. * @author Thibault Duplessis <thibault.duplessis@gmail.com>
  28. * @author Christophe Coevoet <stof@notk.org>
  29. */
  30. class ChangePasswordController extends Controller
  31. {
  32. /**
  33. * Change user password
  34. */
  35. public function changePasswordAction(Request $request)
  36. {
  37. $user = $this->getUser();
  38. if (!is_object($user) || !$user instanceof UserInterface) {
  39. throw new AccessDeniedException('This user does not have access to this section.');
  40. }
  41.  
  42. /** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
  43. $dispatcher = $this->get('event_dispatcher');
  44.  
  45. $event = new GetResponseUserEvent($user, $request);
  46. $dispatcher->dispatch(FOSUserEvents::CHANGE_PASSWORD_INITIALIZE, $event);
  47.  
  48. if (null !== $event->getResponse()) {
  49. return $event->getResponse();
  50. }
  51.  
  52. /** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
  53. $formFactory = $this->get('fos_user.change_password.form.factory');
  54.  
  55. $form = $formFactory->createForm();
  56. $form->setData($user);
  57.  
  58. $form->handleRequest($request);
  59.  
  60. if ($form->isValid()) {
  61. /** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
  62. $userManager = $this->get('fos_user.user_manager');
  63.  
  64. $event = new FormEvent($form, $request);
  65. $dispatcher->dispatch(FOSUserEvents::CHANGE_PASSWORD_SUCCESS, $event);
  66.  
  67. $userManager->updateUser($user);
  68.  
  69. if (null === $response = $event->getResponse()) {
  70. $url = $this->generateUrl('fos_user_profile_show');
  71. $response = new RedirectResponse($url);
  72. }
  73.  
  74. $dispatcher->dispatch(FOSUserEvents::CHANGE_PASSWORD_COMPLETED, new FilterUserResponseEvent($user, $request, $response));
  75.  
  76. return $response;
  77. }
  78.  
  79. return $this->render('FOSUserBundle:ChangePassword:changePassword.html.twig', array(
  80. 'form' => $form->createView(),
  81. 'user' => $user,
  82. 'bodyClass' => 'account',
  83. ));
  84. }
  85. }
  86.  
  87. ---------------------------------------------------------------
  88.  
  89. <?php
  90. namespace UserBundle\Controller;
  91.  
  92. use Symfony\Component\HttpFoundation\RedirectResponse;
  93. use FOS\UserBundle\Controller\RegistrationController as BaseController;
  94. use Symfony\Component\HttpFoundation\Request;
  95. use FOS\UserBundle\Event\GetResponseUserEvent;
  96. use FOS\UserBundle\FOSUserEvents;
  97. use FOS\UserBundle\Event\FormEvent;
  98. use FOS\UserBundle\Event\FilterUserResponseEvent;
  99.  
  100. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  101. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  102. use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
  103.  
  104. class RegistrationController extends BaseController
  105. {
  106. public function registerAction(Request $request)
  107. {
  108.  
  109. /** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
  110. $formFactory = $this->get('fos_user.registration.form.factory');
  111. /** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
  112. $userManager = $this->get('fos_user.user_manager');
  113. /** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
  114. $dispatcher = $this->get('event_dispatcher');
  115. $cities = array();
  116. $loginError = $this->get('security.authentication_utils')->getLastAuthenticationError();
  117.  
  118. $user = $userManager->createUser();
  119. $event = new GetResponseUserEvent($user, $request);
  120. $dispatcher->dispatch(FOSUserEvents::REGISTRATION_INITIALIZE, $event);
  121.  
  122. if (null !== $event->getResponse()) {
  123. return $event->getResponse();
  124. }
  125.  
  126. $form = $formFactory->createForm();
  127.  
  128. $form->setData($user);
  129. $form->handleRequest($request);
  130. if ($form->isValid()) {
  131. $em = $this->getDoctrine()->getManager();
  132. $user->setEnabled(true);
  133. $em->persist($user);
  134. $em->flush();
  135.  
  136. return $this->redirect($this->generateUrl('fos_user_registration_confirmed'));
  137. } else {
  138. var_dump((string)$form->getErrors());
  139.  
  140. }
  141.  
  142. return $this->render('FOSUserBundle:Registration:register.html.twig', array(
  143. 'form' => $form->createView(),
  144. ));
  145. }
  146.  
  147. /**
  148. * Tell the user his account is now confirmed.
  149. */
  150. public function confirmedAction()
  151. {
  152.  
  153. return $this->render('FOSUserBundle:Registration:confirmed.html.twig', array(
  154. ));
  155. }
  156. }
  157.  
  158.  
  159. --------------------------------------------------------------------
  160.  
  161. <?php
  162.  
  163. /*
  164. * This file is part of the FOSUserBundle package.
  165. *
  166. * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
  167. *
  168. * For the full copyright and license information, please view the LICENSE
  169. * file that was distributed with this source code.
  170. */
  171.  
  172. namespace UserBundle\Controller;
  173.  
  174. use FOS\UserBundle\FOSUserEvents;
  175. use FOS\UserBundle\Event\FormEvent;
  176. use FOS\UserBundle\Event\GetResponseUserEvent;
  177. use FOS\UserBundle\Event\FilterUserResponseEvent;
  178. use FOS\UserBundle\Model\UserInterface;
  179. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  180. use Symfony\Component\HttpFoundation\Request;
  181. use Symfony\Component\HttpFoundation\RedirectResponse;
  182. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  183.  
  184. /**
  185. * Controller managing the resetting of the password
  186. *
  187. * @author Thibault Duplessis <thibault.duplessis@gmail.com>
  188. * @author Christophe Coevoet <stof@notk.org>
  189. */
  190. class ResettingController extends Controller
  191. {
  192. /**
  193. * Request reset user password: show form
  194. */
  195. public function requestAction()
  196. {
  197. return $this->render(
  198. 'FOSUserBundle:Resetting:request.html.twig',[]
  199. );
  200. }
  201.  
  202. /**
  203. * Request reset user password: submit form and send email
  204. */
  205. public function sendEmailAction(Request $request)
  206. {
  207. $username = $request->request->get('username');
  208.  
  209. /** @var $user UserInterface */
  210. $user = $this->get('fos_user.user_manager')->findUserByUsernameOrEmail($username);
  211.  
  212. if (null === $user) {
  213. return $this->render('FOSUserBundle:Resetting:request.html.twig', array(
  214. 'invalid_username' => $username
  215. ));
  216. }
  217.  
  218. if ($user->isPasswordRequestNonExpired($this->container->getParameter('fos_user.resetting.token_ttl'))) {
  219. return $this->render('FOSUserBundle:Resetting:passwordAlreadyRequested.html.twig');
  220. }
  221.  
  222. if (null === $user->getConfirmationToken()) {
  223. /** @var $tokenGenerator \FOS\UserBundle\Util\TokenGeneratorInterface */
  224. $tokenGenerator = $this->get('fos_user.util.token_generator');
  225. $user->setConfirmationToken($tokenGenerator->generateToken());
  226. }
  227.  
  228. $this->get('fos_user.mailer')->sendResettingEmailMessage($user);
  229. $user->setPasswordRequestedAt(new \DateTime());
  230. $this->get('fos_user.user_manager')->updateUser($user);
  231.  
  232. return new RedirectResponse($this->generateUrl('fos_user_resetting_check_email',
  233. array('email' => $this->getObfuscatedEmail($user))
  234. ));
  235. }
  236.  
  237. /**
  238. * Tell the user to check his email provider
  239. */
  240. public function checkEmailAction(Request $request)
  241. {
  242. $email = $request->query->get('email');
  243.  
  244. if (empty($email)) {
  245. // the user does not come from the sendEmail action
  246. return new RedirectResponse($this->generateUrl('fos_user_resetting_request'));
  247. }
  248.  
  249. return $this->render('FOSUserBundle:Resetting:checkEmail.html.twig', array(
  250. 'email' => $email,
  251. 'breadcrumb' => [
  252. 'title' => 'Espace utilisateur',
  253. 'subtitle' => "L'email vient d'être envoyé"
  254. ],
  255. ));
  256. }
  257.  
  258. /**
  259. * Reset user password
  260. */
  261. public function resetAction(Request $request, $token)
  262. {
  263. /** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
  264. $formFactory = $this->get('fos_user.resetting.form.factory');
  265. /** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
  266. $userManager = $this->get('fos_user.user_manager');
  267. /** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
  268. $dispatcher = $this->get('event_dispatcher');
  269.  
  270. $user = $userManager->findUserByConfirmationToken($token);
  271.  
  272. if (null === $user) {
  273. throw new NotFoundHttpException(sprintf('The user with "confirmation token" does not exist for value "%s"', $token));
  274. }
  275.  
  276. $event = new GetResponseUserEvent($user, $request);
  277. $dispatcher->dispatch(FOSUserEvents::RESETTING_RESET_INITIALIZE, $event);
  278.  
  279. if (null !== $event->getResponse()) {
  280. return $event->getResponse();
  281. }
  282.  
  283. $form = $formFactory->createForm();
  284. $form->setData($user);
  285.  
  286. $form->handleRequest($request);
  287.  
  288. if ($form->isValid()) {
  289. $event = new FormEvent($form, $request);
  290. $dispatcher->dispatch(FOSUserEvents::RESETTING_RESET_SUCCESS, $event);
  291.  
  292. $userManager->updateUser($user);
  293.  
  294. if (null === $response = $event->getResponse()) {
  295. $url = $this->generateUrl('fos_user_profile_show');
  296. $response = new RedirectResponse($url);
  297. }
  298.  
  299. $dispatcher->dispatch(FOSUserEvents::RESETTING_RESET_COMPLETED, new FilterUserResponseEvent($user, $request, $response));
  300.  
  301. return $response;
  302. }
  303.  
  304. return $this->render('FOSUserBundle:Resetting:reset.html.twig', array(
  305. 'token' => $token,
  306. 'form' => $form->createView(),
  307. ));
  308. }
  309.  
  310. /**
  311. * Get the truncated email displayed when requesting the resetting.
  312. *
  313. * The default implementation only keeps the part following @ in the address.
  314. *
  315. * @param \FOS\UserBundle\Model\UserInterface $user
  316. *
  317. * @return string
  318. */
  319. protected function getObfuscatedEmail(UserInterface $user)
  320. {
  321. $email = $user->getEmail();
  322. if (false !== $pos = strpos($email, '@')) {
  323. $email = '...' . substr($email, $pos);
  324. }
  325.  
  326. return $email;
  327. }
  328. }
  329.  
  330.  
  331. --------------------------------------------------------
  332.  
  333. <?php
  334.  
  335. /*
  336. * This file is part of the FOSUserBundle package.
  337. *
  338. * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
  339. *
  340. * For the full copyright and license information, please view the LICENSE
  341. * file that was distributed with this source code.
  342. */
  343.  
  344. namespace UserBundle\Controller;
  345.  
  346. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  347. use Symfony\Component\HttpFoundation\Request;
  348. use Symfony\Component\Security\Core\Security;
  349. use Symfony\Component\Security\Core\SecurityContextInterface;
  350. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  351.  
  352. class SecurityController extends Controller
  353. {
  354. public function loginAction(Request $request)
  355. {
  356. /** @var $session \Symfony\Component\HttpFoundation\Session\Session */
  357. $session = $request->getSession();
  358.  
  359. if (class_exists('\Symfony\Component\Security\Core\Security')) {
  360. $authErrorKey = Security::AUTHENTICATION_ERROR;
  361. $lastUsernameKey = Security::LAST_USERNAME;
  362. } else {
  363. // BC for SF < 2.6
  364. $authErrorKey = SecurityContextInterface::AUTHENTICATION_ERROR;
  365. $lastUsernameKey = SecurityContextInterface::LAST_USERNAME;
  366. }
  367.  
  368. // get the error if any (works with forward and redirect -- see below)
  369. if ($request->attributes->has($authErrorKey)) {
  370. $error = $request->attributes->get($authErrorKey);
  371. } elseif (null !== $session && $session->has($authErrorKey)) {
  372. $error = $session->get($authErrorKey);
  373. $session->remove($authErrorKey);
  374. } else {
  375. $error = null;
  376. }
  377.  
  378. if (!$error instanceof AuthenticationException) {
  379. $error = null; // The value does not come from the security component.
  380. }
  381.  
  382.  
  383. // last username entered by the user
  384. $lastUsername = (null === $session) ? '' : $session->get($lastUsernameKey);
  385.  
  386. if ($this->has('security.csrf.token_manager')) {
  387. $csrfToken = $this->get('security.csrf.token_manager')->getToken('authenticate')->getValue();
  388. } else {
  389. // BC for SF < 2.4
  390. $csrfToken = $this->has('form.csrf_provider')
  391. ? $this->get('form.csrf_provider')->generateCsrfToken('authenticate')
  392. : null;
  393. }
  394.  
  395. return $this->renderLogin(array(
  396. 'breadcrumb' => [
  397. 'title' => 'Espace utilisateur',
  398. 'subtitle' => 'Se connecter'
  399. ],
  400. 'last_username' => $lastUsername,
  401. 'error' => $error,
  402. 'csrf_token' => $csrfToken,
  403. ));
  404. }
  405.  
  406. /**
  407. * Renders the login template with the given parameters. Overwrite this function in
  408. * an extended controller to provide additional data for the login template.
  409. *
  410. * @param array $data
  411. *
  412. * @return \Symfony\Component\HttpFoundation\Response
  413. */
  414. protected function renderLogin(array $data)
  415. {
  416. return $this->render('FOSUserBundle:Security:login.html.twig', $data);
  417. }
  418.  
  419. public function checkAction()
  420. {
  421. throw new \RuntimeException('You must configure the check path to be handled by the firewall using form_login in your security firewall configuration.');
  422. }
  423.  
  424. public function logoutAction()
  425. {
  426. throw new \RuntimeException('You must activate the logout in your security firewall configuration.');
  427. }
  428. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement