src/Twig/Extension/UserExtension.php line 50

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Enterprise License (PEL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PEL
  13.  */
  14. namespace App\Twig\Extension;
  15. use Twig\Extension\AbstractExtension;
  16. use Twig\TwigFunction;
  17. use Pimcore\Model\DataObject;
  18. use Pimcore\Bundle\EcommerceFrameworkBundle\Factory;
  19. use Symfony\Component\Security\Core\User\UserInterface;
  20. use App\Model\Utility;
  21. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  22. use FrontendPermissionToolkitBundle\Service;
  23. class UserExtension extends AbstractExtension
  24. {
  25.    
  26.     /**
  27.      * @var Factory
  28.      */
  29.     protected $service;
  30.     public function __construct(EventDispatcherInterface $eventDispatcher)
  31.     {
  32.         $this->service = new Service($eventDispatcher);
  33.     }
  34.     public function getFunctions()
  35.     {
  36.         return [
  37.             new TwigFunction('allow_user', [$this'allowUser']),                     
  38.         ];
  39.     }
  40.     public function allowUser($key){
  41.         $allow false;
  42.         
  43.         if(\Pimcore::getContainer()->get('security.token_storage')->getToken()){
  44.             $userObject \Pimcore::getContainer()->get('security.token_storage')->getToken()->getUser();        
  45.             if($userObject instanceof \Pimcore\Model\DataObject){
  46.                 if($this->service->isAllowed($userObject$key)) {
  47.                     $allow true;
  48.                 }
  49.             }
  50.         }
  51.         return $allow;
  52.     }   
  53. }