<?php
namespace App\Controller;
use App\Kernel;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security as SecurityGranted;
use Symfony\Component\Security\Core\Security;
use App\Controller\UsersController;
/**
* Require ROLE_ADMIN for *every* controller method in this class.
*
* @SecurityGranted("is_granted('ROLE_SUPER') or is_granted('ROLE_ADMIN')")
***/
class BackendController extends AbstractController
{
/**
* BackendController constructor.
*/
private $environment;
private $usersController;
public function __construct(Kernel $kernel, UsersController $usersController)
{
$this->environment = $kernel->getEnvironment();
$this->usersController = $usersController;
}
/**
* @Route("/administration", name="administration_index")
*/
public function index(Request $request, Security $security): Response
{
/*
* README: in Vhost was created alias: Alias /backend_dev [siteroot]/templates/administration/ExtJsAdministration
* This was for dev environment of administration sencha app, you can ignore for production
*/
$user = $security->getUser();
if($user)
{
$user->setUserpicture($this->usersController->retrieveUserImage($user->id));
}
$twig_params = [
'ExtJsManifest'=>'/backend/bootstrap.json',
'base_path'=>$request->getBaseUrl(),
'backend_path'=>$request->getBaseUrl().'/backend/',
'last_username' => '',
'error' => '',
'current_user'=> json_encode($user)
];
if($this->environment=='prod')
{
return $this->render('administration/administration.production.html.twig', $twig_params);
}
else{
$twig_params['ExtJsManifest'] = '/backend_dev/bootstrap.json';
$twig_params['base_path'] = $request->getBaseUrl();
$twig_params['backend_path'] = $request->getBaseUrl().'/backend/';
return $this->render('administration/administration.html.twig', $twig_params);
}
}
/**
* @Route("/administration-check-session", name="administration-check-session")
*/
public function checkBackendSession(Request $request, Security $security): Response
{
$response = new Response();
//verifichiamo che l'utente sia collegato
$user = $security->getUser();
if(!$user)
{
throw new \Exception('Session expired.');
}
$response->setContent(json_encode(['session'=>true]));
$response->headers->set('Content-Type', 'application/json');
return $response;
}
}