Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace AppBundle\Controller;
- use AppBundle\Form\Model\ExpiredCredentialsModel;
- use AppBundle\Form\Model\ExpiredCredentials;
- use AppBundle\Form\Type\ExpiredCredentialsType;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Bundle\FrameworkBundle\Controller\Controller;
- use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
- use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
- use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
- use AppBundle\Form\AccountType;
- use AppBundle\Entity\Identity;
- use Symfony\Component\Security\Core\Exception\BadCredentialsException;
- use Symfony\Component\Security\Core\Exception\CredentialsExpiredException;
- /**
- * Identity controller.
- *
- * @Route("/account")
- */
- class AccountController extends Controller
- {
- /**
- * Displays own account page.
- *
- * @Route("/", name="account")
- * @Method("GET")
- * @Template()
- */
- public function indexAction()
- {
- $em = $this->getDoctrine()->getManager();
- $entities = $em->getRepository('AppBundle:Identity')->findAll();
- return array(
- 'entities' => $entities,
- );
- }
- /**
- * Displays account login page.
- *
- * @Route("/login", name="account_login")
- */
- public function loginAction(Request $request)
- {
- $authenticationUtils = $this->get('security.authentication_utils');
- // last username entered by the user
- $lastUsername = $authenticationUtils->getLastUsername();
- // get the login error if there is one
- $error = $authenticationUtils->getLastAuthenticationError();
- // check if credentials have expired
- if ($error instanceof CredentialsExpiredException) {
- return $this->redirect($this->generateUrl('account_renew_expired_credentials'));
- $form = $this->createExpiredCredentialsForm(new ExpiredCredentials($lastUsername));
- return $this->render(
- 'AppBundle:Account:expiredPassword.html.twig',
- array(
- 'form' => $form->createView(),
- // last username entered by the user
- 'last_username' => $lastUsername,
- 'error' => $error,
- )
- );
- } else {
- return $this->render(
- 'AppBundle:Account:login.html.twig',
- array(
- // last username entered by the user
- 'last_username' => $lastUsername,
- 'error' => $error,
- )
- );
- }
- }
- /**
- * @Route("/authenticate", name="account_authenticate")
- */
- public function authenticateAction()
- {
- // this controller will not be executed,
- // as the route is handled by the Security system
- }
- /**
- * @Route("/logout", name="account_logout")
- */
- public function logoutAction()
- {
- // this controller will not be executed,
- // as the route is handled by the Security system
- }
- /**
- * Displays password recovery site.
- *
- * @Route("/lost-password", name="account_lost_password")
- * @Method("GET")
- */
- public function lostPasswordAction()
- {
- return $this->render(
- 'AppBundle:Account:lostPassword.html.twig'
- );
- }
- /**
- * @Route("/renew-credentials", name="account_renew_expired_credentials")
- */
- public function renewExpiredCredentialsAction(Request $request)
- {
- $credentialsStorage = new ExpiredCredentialsModel();
- $form = $this->createExpiredCredentialsForm($credentialsStorage);
- $form->handleRequest($request);
- if ($form->isValid()) {
- try {
- $this->get('projectname.expired_credentials_handler')->updatePassword($request, $credentialsStorage);
- $this->addFlash('success', 'account.flash.credentialsUpdated');
- return $this->redirect($this->generateUrl('homepage'));
- } catch (BadCredentialsException $e) {
- $this->addFlash('notice', 'account.flash.invalidCredentials');
- return $this->redirect($this->generateUrl('account_renew_expired_credentials'));
- }
- }
- return $this->render(
- 'AppBundle:Account:expiredPassword.html.twig',
- array(
- 'form' => $form->createView(),
- )
- );
- }
- /**
- * Creates a form to create a Identity entity.
- *
- * @param ExpiredCredentialsModel $data
- * @return ExpiredCredentialsType The form
- */
- private function createExpiredCredentialsForm(ExpiredCredentialsModel $data = null)
- {
- $form = $this->createForm(new ExpiredCredentialsType(), $data, array(
- 'action' => $this->generateUrl('account_renew_expired_credentials'),
- 'method' => 'POST',
- ));
- $form->add('submit', 'submit', array(
- 'label' => 'layout.button.renew',
- 'attr' => array(
- 'class' => 'btn-warning',
- ),
- ));
- return $form;
- }
- /**
- * Creates a form to register an identity.
- *
- * @param Identity $entity The entity
- *
- * @return \Symfony\Component\Form\Form The form
- */
- private function createRegisterForm(Identity $entity)
- {
- $form = $this->createForm(
- new AccountType\IdentityType(),
- $entity,
- array(
- 'action' => $this->generateUrl('account_register_processing', array('id' => $entity->getId())),
- 'method' => 'POST',
- 'attr' => [
- 'class' => 'form-user-offline',
- ],
- )
- );
- $form->add(
- 'submit',
- 'submit',
- [
- 'label' => 'form.btn.register',
- ]
- );
- return $form;
- }
- /**
- * Displays registration form.
- *
- * @Route("/register", name="account_register")
- * @Method("GET")
- */
- public function registerAction()
- {
- $identity = new Identity();
- $form = $this->createRegisterForm($identity);
- return $this->render(
- 'AppBundle:Account:register.html.twig',
- [
- 'entity' => $identity,
- 'form' => $form->createView(),
- ]
- );
- }
- /**
- * Processes registration form.
- *
- * @Route("/register", name="account_register_processing")
- * @Method("POST")
- * @Param Request $request
- */
- public function registerProcessingAction(Request $request)
- {
- $identity = new Identity();
- $form = $this->createRegisterForm($identity);
- $form->handleRequest($request);
- if ($form->isValid()) {
- $em = $this->getDoctrine()->getManager();
- $identity->setRandomPassword();
- $identity->setHidePlainPassword(false);
- $em->persist($identity);
- $em->flush();
- $this->get('projectname.security.authentication_manager')->makeOnline($request, $identity, 'protected_area');
- /**
- * FIXME: Marking credentials expired
- *
- * I could not figure out how to mark credentials right away after making the user online
- * so instead I am marking them expired on the "thank you" page in case the user has been just created.
- *
- * My security system fails if for any reason the redirect fails.
- */
- return $this->redirect($this->generateUrl('account_register_thank_you'));
- }
- return $this->render(
- 'AppBundle:Account:register.html.twig',
- [
- 'entity' => $identity,
- 'form' => $form->createView(),
- ]
- );
- }
- /**
- * @Route("/thank-you", name="account_register_thank_you")
- * @Method("GET")
- */
- public function thankYouAction()
- {
- //
- $currentUser = $this->getUser();
- if ($currentUser->isJustCreated()) {
- $currentUser->setCredentialsExpired(true);
- $em = $this->getDoctrine()->getManager();
- $em->persist($currentUser);
- $em->flush();
- }
- return $this->render(
- 'AppBundle:Account:thankYou.html.twig'
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement