src/Controller/BackendController.php line 38

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Kernel;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security as SecurityGranted;
  9. use Symfony\Component\Security\Core\Security;
  10. use App\Controller\UsersController;
  11. /**
  12.   * Require ROLE_ADMIN for *every* controller method in this class.
  13.   *
  14.   *  @SecurityGranted("is_granted('ROLE_SUPER') or is_granted('ROLE_ADMIN')")
  15. ***/
  16. class BackendController extends AbstractController
  17. {
  18.     /**
  19.      * BackendController constructor.
  20.      */
  21.     private $environment;
  22.     private $usersController;
  23.     public function __construct(Kernel $kernelUsersController $usersController)
  24.     {
  25.         $this->environment $kernel->getEnvironment();
  26.         $this->usersController $usersController;
  27.     }
  28.     /**
  29.      * @Route("/administration", name="administration_index")
  30.      */
  31.     public function index(Request $requestSecurity $security): Response
  32.     {
  33.         /*
  34.          *  README: in Vhost was created alias: Alias /backend_dev [siteroot]/templates/administration/ExtJsAdministration
  35.          *  This was for dev environment of administration sencha app, you can ignore for production
  36.          */
  37.         $user $security->getUser();
  38.         if($user)
  39.         {
  40.             $user->setUserpicture($this->usersController->retrieveUserImage($user->id));
  41.         }
  42.         $twig_params = [
  43.             'ExtJsManifest'=>'/backend/bootstrap.json',
  44.             'base_path'=>$request->getBaseUrl(),
  45.             'backend_path'=>$request->getBaseUrl().'/backend/',
  46.             'last_username' => '',
  47.             'error' => '',
  48.             'current_user'=> json_encode($user)
  49.         ];
  50.         if($this->environment=='prod')
  51.         {
  52.             return $this->render('administration/administration.production.html.twig'$twig_params);
  53.         }
  54.         else{
  55.            $twig_params['ExtJsManifest'] = '/backend_dev/bootstrap.json';
  56.            $twig_params['base_path'] = $request->getBaseUrl();
  57.            $twig_params['backend_path'] = $request->getBaseUrl().'/backend/';
  58.            return $this->render('administration/administration.html.twig'$twig_params);
  59.         }
  60.     }
  61.     /**
  62.      * @Route("/administration-check-session", name="administration-check-session")
  63.      */
  64.     public function checkBackendSession(Request $requestSecurity $security): Response
  65.     {
  66.         $response = new Response();
  67.         //verifichiamo che l'utente sia collegato
  68.         $user $security->getUser();
  69.         if(!$user)
  70.         {
  71.             throw new \Exception('Session expired.');
  72.         }
  73.         $response->setContent(json_encode(['session'=>true]));
  74.         $response->headers->set('Content-Type''application/json');
  75.         return $response;
  76.     }
  77. }