Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * This file is part of the FOSUserBundle package.
- *
- * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace UserBundle\Controller;
- use FOS\UserBundle\FOSUserEvents;
- use FOS\UserBundle\Event\FormEvent;
- use FOS\UserBundle\Event\FilterUserResponseEvent;
- use FOS\UserBundle\Event\GetResponseUserEvent;
- use FOS\UserBundle\Model\UserInterface;
- use Symfony\Bundle\FrameworkBundle\Controller\Controller;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\HttpFoundation\RedirectResponse;
- use Symfony\Component\Security\Core\Exception\AccessDeniedException;
- /**
- * Controller managing the password change
- *
- * @author Thibault Duplessis <thibault.duplessis@gmail.com>
- * @author Christophe Coevoet <stof@notk.org>
- */
- class ChangePasswordController extends Controller
- {
- /**
- * Change user password
- */
- public function changePasswordAction(Request $request)
- {
- $user = $this->getUser();
- if (!is_object($user) || !$user instanceof UserInterface) {
- throw new AccessDeniedException('This user does not have access to this section.');
- }
- /** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
- $dispatcher = $this->get('event_dispatcher');
- $event = new GetResponseUserEvent($user, $request);
- $dispatcher->dispatch(FOSUserEvents::CHANGE_PASSWORD_INITIALIZE, $event);
- if (null !== $event->getResponse()) {
- return $event->getResponse();
- }
- /** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
- $formFactory = $this->get('fos_user.change_password.form.factory');
- $form = $formFactory->createForm();
- $form->setData($user);
- $form->handleRequest($request);
- if ($form->isValid()) {
- /** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
- $userManager = $this->get('fos_user.user_manager');
- $event = new FormEvent($form, $request);
- $dispatcher->dispatch(FOSUserEvents::CHANGE_PASSWORD_SUCCESS, $event);
- $userManager->updateUser($user);
- if (null === $response = $event->getResponse()) {
- $url = $this->generateUrl('fos_user_profile_show');
- $response = new RedirectResponse($url);
- }
- $dispatcher->dispatch(FOSUserEvents::CHANGE_PASSWORD_COMPLETED, new FilterUserResponseEvent($user, $request, $response));
- return $response;
- }
- return $this->render('FOSUserBundle:ChangePassword:changePassword.html.twig', array(
- 'form' => $form->createView(),
- 'user' => $user,
- 'bodyClass' => 'account',
- ));
- }
- }
- ---------------------------------------------------------------
- <?php
- namespace UserBundle\Controller;
- use Symfony\Component\HttpFoundation\RedirectResponse;
- use FOS\UserBundle\Controller\RegistrationController as BaseController;
- use Symfony\Component\HttpFoundation\Request;
- use FOS\UserBundle\Event\GetResponseUserEvent;
- use FOS\UserBundle\FOSUserEvents;
- use FOS\UserBundle\Event\FormEvent;
- use FOS\UserBundle\Event\FilterUserResponseEvent;
- use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
- use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
- use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
- class RegistrationController extends BaseController
- {
- public function registerAction(Request $request)
- {
- /** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
- $formFactory = $this->get('fos_user.registration.form.factory');
- /** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
- $userManager = $this->get('fos_user.user_manager');
- /** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
- $dispatcher = $this->get('event_dispatcher');
- $cities = array();
- $loginError = $this->get('security.authentication_utils')->getLastAuthenticationError();
- $user = $userManager->createUser();
- $event = new GetResponseUserEvent($user, $request);
- $dispatcher->dispatch(FOSUserEvents::REGISTRATION_INITIALIZE, $event);
- if (null !== $event->getResponse()) {
- return $event->getResponse();
- }
- $form = $formFactory->createForm();
- $form->setData($user);
- $form->handleRequest($request);
- if ($form->isValid()) {
- $em = $this->getDoctrine()->getManager();
- $user->setEnabled(true);
- $em->persist($user);
- $em->flush();
- return $this->redirect($this->generateUrl('fos_user_registration_confirmed'));
- } else {
- var_dump((string)$form->getErrors());
- }
- return $this->render('FOSUserBundle:Registration:register.html.twig', array(
- 'form' => $form->createView(),
- ));
- }
- /**
- * Tell the user his account is now confirmed.
- */
- public function confirmedAction()
- {
- return $this->render('FOSUserBundle:Registration:confirmed.html.twig', array(
- ));
- }
- }
- --------------------------------------------------------------------
- <?php
- /*
- * This file is part of the FOSUserBundle package.
- *
- * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace UserBundle\Controller;
- use FOS\UserBundle\FOSUserEvents;
- use FOS\UserBundle\Event\FormEvent;
- use FOS\UserBundle\Event\GetResponseUserEvent;
- use FOS\UserBundle\Event\FilterUserResponseEvent;
- use FOS\UserBundle\Model\UserInterface;
- use Symfony\Bundle\FrameworkBundle\Controller\Controller;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\HttpFoundation\RedirectResponse;
- use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
- /**
- * Controller managing the resetting of the password
- *
- * @author Thibault Duplessis <thibault.duplessis@gmail.com>
- * @author Christophe Coevoet <stof@notk.org>
- */
- class ResettingController extends Controller
- {
- /**
- * Request reset user password: show form
- */
- public function requestAction()
- {
- return $this->render(
- 'FOSUserBundle:Resetting:request.html.twig',[]
- );
- }
- /**
- * Request reset user password: submit form and send email
- */
- public function sendEmailAction(Request $request)
- {
- $username = $request->request->get('username');
- /** @var $user UserInterface */
- $user = $this->get('fos_user.user_manager')->findUserByUsernameOrEmail($username);
- if (null === $user) {
- return $this->render('FOSUserBundle:Resetting:request.html.twig', array(
- 'invalid_username' => $username
- ));
- }
- if ($user->isPasswordRequestNonExpired($this->container->getParameter('fos_user.resetting.token_ttl'))) {
- return $this->render('FOSUserBundle:Resetting:passwordAlreadyRequested.html.twig');
- }
- if (null === $user->getConfirmationToken()) {
- /** @var $tokenGenerator \FOS\UserBundle\Util\TokenGeneratorInterface */
- $tokenGenerator = $this->get('fos_user.util.token_generator');
- $user->setConfirmationToken($tokenGenerator->generateToken());
- }
- $this->get('fos_user.mailer')->sendResettingEmailMessage($user);
- $user->setPasswordRequestedAt(new \DateTime());
- $this->get('fos_user.user_manager')->updateUser($user);
- return new RedirectResponse($this->generateUrl('fos_user_resetting_check_email',
- array('email' => $this->getObfuscatedEmail($user))
- ));
- }
- /**
- * Tell the user to check his email provider
- */
- public function checkEmailAction(Request $request)
- {
- $email = $request->query->get('email');
- if (empty($email)) {
- // the user does not come from the sendEmail action
- return new RedirectResponse($this->generateUrl('fos_user_resetting_request'));
- }
- return $this->render('FOSUserBundle:Resetting:checkEmail.html.twig', array(
- 'email' => $email,
- 'breadcrumb' => [
- 'title' => 'Espace utilisateur',
- 'subtitle' => "L'email vient d'être envoyé"
- ],
- ));
- }
- /**
- * Reset user password
- */
- public function resetAction(Request $request, $token)
- {
- /** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
- $formFactory = $this->get('fos_user.resetting.form.factory');
- /** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
- $userManager = $this->get('fos_user.user_manager');
- /** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
- $dispatcher = $this->get('event_dispatcher');
- $user = $userManager->findUserByConfirmationToken($token);
- if (null === $user) {
- throw new NotFoundHttpException(sprintf('The user with "confirmation token" does not exist for value "%s"', $token));
- }
- $event = new GetResponseUserEvent($user, $request);
- $dispatcher->dispatch(FOSUserEvents::RESETTING_RESET_INITIALIZE, $event);
- if (null !== $event->getResponse()) {
- return $event->getResponse();
- }
- $form = $formFactory->createForm();
- $form->setData($user);
- $form->handleRequest($request);
- if ($form->isValid()) {
- $event = new FormEvent($form, $request);
- $dispatcher->dispatch(FOSUserEvents::RESETTING_RESET_SUCCESS, $event);
- $userManager->updateUser($user);
- if (null === $response = $event->getResponse()) {
- $url = $this->generateUrl('fos_user_profile_show');
- $response = new RedirectResponse($url);
- }
- $dispatcher->dispatch(FOSUserEvents::RESETTING_RESET_COMPLETED, new FilterUserResponseEvent($user, $request, $response));
- return $response;
- }
- return $this->render('FOSUserBundle:Resetting:reset.html.twig', array(
- 'token' => $token,
- 'form' => $form->createView(),
- ));
- }
- /**
- * Get the truncated email displayed when requesting the resetting.
- *
- * The default implementation only keeps the part following @ in the address.
- *
- * @param \FOS\UserBundle\Model\UserInterface $user
- *
- * @return string
- */
- protected function getObfuscatedEmail(UserInterface $user)
- {
- $email = $user->getEmail();
- if (false !== $pos = strpos($email, '@')) {
- $email = '...' . substr($email, $pos);
- }
- return $email;
- }
- }
- --------------------------------------------------------
- <?php
- /*
- * This file is part of the FOSUserBundle package.
- *
- * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace UserBundle\Controller;
- use Symfony\Bundle\FrameworkBundle\Controller\Controller;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\Security\Core\Security;
- use Symfony\Component\Security\Core\SecurityContextInterface;
- use Symfony\Component\Security\Core\Exception\AuthenticationException;
- class SecurityController extends Controller
- {
- public function loginAction(Request $request)
- {
- /** @var $session \Symfony\Component\HttpFoundation\Session\Session */
- $session = $request->getSession();
- if (class_exists('\Symfony\Component\Security\Core\Security')) {
- $authErrorKey = Security::AUTHENTICATION_ERROR;
- $lastUsernameKey = Security::LAST_USERNAME;
- } else {
- // BC for SF < 2.6
- $authErrorKey = SecurityContextInterface::AUTHENTICATION_ERROR;
- $lastUsernameKey = SecurityContextInterface::LAST_USERNAME;
- }
- // get the error if any (works with forward and redirect -- see below)
- if ($request->attributes->has($authErrorKey)) {
- $error = $request->attributes->get($authErrorKey);
- } elseif (null !== $session && $session->has($authErrorKey)) {
- $error = $session->get($authErrorKey);
- $session->remove($authErrorKey);
- } else {
- $error = null;
- }
- if (!$error instanceof AuthenticationException) {
- $error = null; // The value does not come from the security component.
- }
- // last username entered by the user
- $lastUsername = (null === $session) ? '' : $session->get($lastUsernameKey);
- if ($this->has('security.csrf.token_manager')) {
- $csrfToken = $this->get('security.csrf.token_manager')->getToken('authenticate')->getValue();
- } else {
- // BC for SF < 2.4
- $csrfToken = $this->has('form.csrf_provider')
- ? $this->get('form.csrf_provider')->generateCsrfToken('authenticate')
- : null;
- }
- return $this->renderLogin(array(
- 'breadcrumb' => [
- 'title' => 'Espace utilisateur',
- 'subtitle' => 'Se connecter'
- ],
- 'last_username' => $lastUsername,
- 'error' => $error,
- 'csrf_token' => $csrfToken,
- ));
- }
- /**
- * Renders the login template with the given parameters. Overwrite this function in
- * an extended controller to provide additional data for the login template.
- *
- * @param array $data
- *
- * @return \Symfony\Component\HttpFoundation\Response
- */
- protected function renderLogin(array $data)
- {
- return $this->render('FOSUserBundle:Security:login.html.twig', $data);
- }
- public function checkAction()
- {
- throw new \RuntimeException('You must configure the check path to be handled by the firewall using form_login in your security firewall configuration.');
- }
- public function logoutAction()
- {
- throw new \RuntimeException('You must activate the logout in your security firewall configuration.');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement