src/Controller/Admin/AbstractAdminController.php line 60

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Admin;
  3. use App\Entity\Client;
  4. use App\Repository\ClientRepository;
  5. use App\Security\NotClientContextException;
  6. use Doctrine\ORM\EntityManager;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Exception;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\RequestStack;
  12. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  13. use Symfony\Component\Security\Core\Security;
  14. use Twig\Environment;
  15. abstract class AbstractAdminController extends AbstractController {
  16.   protected $clientrepo;
  17.   
  18.   /**
  19.    * @var Client
  20.    */
  21.   protected $client;
  22.   
  23.   /**
  24.    * @var EntityManager
  25.    */
  26.   protected $em;
  27.   
  28.   /**
  29.    * @var SessionInterface
  30.    */
  31.   protected $session;
  32.   
  33.   /**
  34.    * @var Environment
  35.    */
  36.   protected $twig;
  37.   
  38.   /**
  39.    * @var Request
  40.    */
  41.   protected $request;
  42.   
  43.   /**
  44.    * @var Security
  45.    */
  46.   protected $security;
  47.   
  48.   public function __construct(ClientRepository $clientrepoEntityManagerInterface $emRequestStack $stack,  Environment $twigSecurity $security) {
  49.     $this->clientrepo $clientrepo;
  50.     $this->em $em;
  51.     $this->session $stack->getSession();
  52.     $this->request $stack->getMainRequest();
  53.     $this->twig $twig;
  54.     $this->security $security;
  55.     
  56.     // Load client from user or from session for admins
  57.     if($security->isGranted("ROLE_ADMIN") && $this->session->has('MANAGE_CLIENT') ) {
  58.       $this->loadClient($this->session->get('MANAGE_CLIENT'));
  59.     } else if($security->isGranted("ROLE_CLIENT") && $security->getUser()->getClient()) {
  60.       $this->loadClient($security->getUser()->getClient()->getId());
  61.     } else {
  62.       $this->twig->addGlobal("client"null);
  63.     }
  64.   }
  65.   
  66.   protected function isClientContext() {
  67.     return ($this->client != null);
  68.   }
  69.   protected function getClient() {
  70.     if(!$this->client) throw new NotClientContextException();
  71.     return $this->client;
  72.   }
  73.   
  74.   protected function ensureClient(Client $expectedClient) {
  75.     // IF EXPECTED CLIENT IS PROVIDED ENSURE THE GOOD ONE IS LOGGED
  76.     if(!$this->client || $expectedClient->getId() != $this->client->getId()) throw new NotClientContextException($expectedClient);
  77.     return $this->client;
  78.   }
  79.   
  80.   protected function loadClient($clientid) {
  81.     $this->client $this->clientrepo->find($clientid);
  82.     if(!$this->client) throw $this->createAccessDeniedException();
  83.     $this->twig->addGlobal("client"$this->client);
  84.   }
  85.   
  86.   protected function generateUrlWihClient($route, array $parameters = []) {
  87.     if(!$this->client) throw new Exception("Method getClient should be called first");
  88.     if($this->isGranted("ROLE_ADMIN")) $parameters['client'] = $this->client->getId();
  89.     return $this->generateUrl($route$parameters);
  90.   }
  91. }