src/AppBundle/Controller/SecureController.php line 45

Open in your IDE?
  1. <?php
  2. namespace AppBundle\Controller;
  3. use AppBundle\Form\LoginFormType;
  4. use AppBundle\Model\DataObject\User;
  5. use Pimcore\Controller\Configuration\TemplatePhp;
  6. use Pimcore\Controller\FrontendController;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  11. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  12. class SecureController extends FrontendController
  13. {
  14.     public function loginAction(
  15.         Request $request,
  16.         AuthenticationUtils $authenticationUtils
  17.     ) {
  18.         // get the login error if there is one
  19.         $error $authenticationUtils->getLastAuthenticationError();
  20.         // last username entered by the user
  21.         $lastUsername $authenticationUtils->getLastUsername();
  22.         $formData = [
  23.             '_username'    => $lastUsername,
  24.             '_target_path' => '/' $request->getLocale()
  25.         ];
  26.         $form $this->createForm(LoginFormType::class, $formData, [
  27.             'action' => $this->generateUrl('demo_login'),
  28.         ]);
  29.         return [
  30.             'hideLeftNav'     => true,
  31.             'showBreadcrumbs' => false,
  32.             'form'            => $form->createView(),
  33.             'error'           => $error,
  34.             'availableUsers'  => $this->loadAvailableUsers()
  35.         ];
  36.     }
  37.     public function palloginAction(
  38.         Request $request,
  39.         AuthenticationUtils $authenticationUtils
  40.     ) {
  41.         // get the login error if there is one
  42.         $error $authenticationUtils->getLastAuthenticationError();
  43.         // last username entered by the user
  44.         $lastUsername $authenticationUtils->getLastUsername();
  45.         $formData = [
  46.             '_username'    => $lastUsername,
  47.             '_target_path' => '/' $request->getLocale()
  48.         ];
  49.         $form $this->createForm(LoginFormType::class, $formData, [
  50.             'action' => $this->generateUrl('my_login'),
  51.         ]);
  52.         return [
  53.             'hideLeftNav'     => true,
  54.             'showBreadcrumbs' => false,
  55.             'form'            => $form->createView(),
  56.             'error'           => $error,
  57.             'availableUsers'  => $this->loadAvailableUsers()
  58.         ];
  59.     }
  60.     /**
  61.      * This is only for DEMO purposes - show a list of available users. Obviously you do NOT want
  62.      * this in your real application.
  63.      *
  64.      * @return array
  65.      */
  66.     private function loadAvailableUsers()
  67.     {
  68.         /** @var User[] $users */
  69.         $users User::getList();
  70.         $result = [];
  71.         foreach ($users as $user) {
  72.             $result[] = [
  73.                 'username' => $user->getUsername(),
  74.                 'roles'    => $user->getRoles(),
  75.                 'password' => 'doe'
  76.             ];
  77.         }
  78.         return $result;
  79.     }
  80.     /**
  81.      * Sample route which can only be seen by logged in users.
  82.      *
  83.      * @Route("/{_locale}/secure/user", name="demo_secure_user")
  84.      * @TemplatePhp("Secure/secure.html.php")
  85.      * @Security("has_role('ROLE_USER')")
  86.      */
  87.     public function secureUserAction()
  88.     {
  89.         return [
  90.             'showBreadcrumbs' => false,
  91.         ];
  92.     }
  93.     /**
  94.      * Sample route which can only be seen by logged in admin users.
  95.      *
  96.      * @Route("/{_locale}/secure/admin", name="demo_secure_admin")
  97.      * @TemplatePhp("Secure/secure.html.php")
  98.      */
  99.     public function secureAdminAction()
  100.     {
  101.         // there are multiple ways to control authorization (= what a user is allowed to do):
  102.         //
  103.         // * access_control in your security.yml (see Symfony Security docs)
  104.         // * @Security annotation (see secureUserAction)
  105.         // * isGranted() or denyAccessUnlessGranted() calls in your controller (see Symfony\Bundle\FrameworkBundle\Controller\Controller)
  106.         //
  107.         // this is the same as adding a @Security("has_role('ROLE_ADMIN')") annotation, but gives you more control when
  108.         // to check and what to do
  109.         $this->denyAccessUnlessGranted('ROLE_ADMIN');
  110.         // another possibility
  111.         if (!$this->isGranted('ROLE_ADMIN')) {
  112.             throw new AccessDeniedHttpException('No no');
  113.         }
  114.         return [
  115.             'admin' => true
  116.         ];
  117.     }
  118. }