vendor/pimcore/pimcore/bundles/AdminBundle/Controller/AdminController.php line 29

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 Pimcore\Bundle\AdminBundle\Controller;
  15. use Pimcore\Bundle\AdminBundle\HttpFoundation\JsonResponse;
  16. use Pimcore\Bundle\AdminBundle\Security\User\TokenStorageUserResolver;
  17. use Pimcore\Bundle\AdminBundle\Security\User\User as UserProxy;
  18. use Pimcore\Controller\Controller;
  19. use Pimcore\Model\User;
  20. use Symfony\Component\HttpFoundation\Request;
  21. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  22. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  23. use Symfony\Component\Serializer\Encoder\DecoderInterface;
  24. use Symfony\Component\Serializer\SerializerInterface;
  25. use Symfony\Component\Translation\Exception\InvalidArgumentException;
  26. abstract class AdminController extends Controller implements AdminControllerInterface
  27. {
  28.     /**
  29.      * @inheritDoc
  30.      */
  31.     public function needsSessionDoubleAuthenticationCheck()
  32.     {
  33.         return true;
  34.     }
  35.     /**
  36.      * @inheritDoc
  37.      */
  38.     public function needsStorageDoubleAuthenticationCheck()
  39.     {
  40.         return true;
  41.     }
  42.     /**
  43.      * Get user from user proxy object which is registered on security component
  44.      *
  45.      * @param bool $proxyUser Return the proxy user (UserInterface) instead of the pimcore model
  46.      *
  47.      * @return UserProxy|User
  48.      */
  49.     protected function getAdminUser($proxyUser false)
  50.     {
  51.         $resolver $this->get(TokenStorageUserResolver::class);
  52.         if ($proxyUser) {
  53.             return $resolver->getUserProxy();
  54.         } else {
  55.             return $resolver->getUser();
  56.         }
  57.     }
  58.     /**
  59.      * Check user permission
  60.      *
  61.      * @param string $permission
  62.      *
  63.      * @throws AccessDeniedHttpException
  64.      */
  65.     protected function checkPermission($permission)
  66.     {
  67.         if (!$this->getAdminUser() || !$this->getAdminUser()->isAllowed($permission)) {
  68.             $this->get('monolog.logger.security')->error(
  69.                 'User {user} attempted to access {permission}, but has no permission to do so',
  70.                 [
  71.                     'user' => $this->getAdminUser()->getName(),
  72.                     'permission' => $permission,
  73.                 ]
  74.             );
  75.             throw $this->createAccessDeniedHttpException();
  76.         }
  77.     }
  78.     /**
  79.      * @param string $message
  80.      * @param \Throwable|null $previous
  81.      * @param int $code
  82.      * @param array $headers
  83.      *
  84.      * @return AccessDeniedHttpException
  85.      */
  86.     protected function createAccessDeniedHttpException(string $message 'Access Denied.', \Throwable $previous nullint $code 0, array $headers = []): AccessDeniedHttpException
  87.     {
  88.         // $headers parameter not supported by Symfony 3.4
  89.         return new AccessDeniedHttpException($message$previous$code);
  90.     }
  91.     /**
  92.      * @param string[] $permissions
  93.      */
  94.     protected function checkPermissionsHasOneOf(array $permissions)
  95.     {
  96.         $allowed false;
  97.         $permission null;
  98.         foreach ($permissions as $permission) {
  99.             if ($this->getAdminUser()->isAllowed($permission)) {
  100.                 $allowed true;
  101.                 break;
  102.             }
  103.         }
  104.         if (!$this->getAdminUser() || !$allowed) {
  105.             $this->get('monolog.logger.security')->error(
  106.                 'User {user} attempted to access {permission}, but has no permission to do so',
  107.                 [
  108.                     'user' => $this->getAdminUser()->getName(),
  109.                     'permission' => $permission,
  110.                 ]
  111.             );
  112.             throw new AccessDeniedHttpException('Attempt to access ' $permission ', but has no permission to do so.');
  113.         }
  114.     }
  115.     /**
  116.      * Check permission against all controller actions. Can optionally exclude a list of actions.
  117.      *
  118.      * @param FilterControllerEvent $event
  119.      * @param string $permission
  120.      * @param array $unrestrictedActions
  121.      */
  122.     protected function checkActionPermission(FilterControllerEvent $eventstring $permission, array $unrestrictedActions = [])
  123.     {
  124.         $actionName null;
  125.         $controller $event->getController();
  126.         if (is_array($controller) && count($controller) === && is_string($controller[1])) {
  127.             $actionName $controller[1];
  128.         }
  129.         if (null === $actionName || !in_array($actionName$unrestrictedActions)) {
  130.             $this->checkPermission($permission);
  131.         }
  132.     }
  133.     /**
  134.      * Encodes data into JSON string
  135.      *
  136.      * @param mixed $data    The data to be encoded
  137.      * @param array $context Context to pass to serializer when using serializer component
  138.      * @param int $options   Options passed to json_encode
  139.      * @param bool $useAdminSerializer
  140.      *
  141.      * @return string
  142.      */
  143.     protected function encodeJson($data, array $context = [], $options JsonResponse::DEFAULT_ENCODING_OPTIONSbool $useAdminSerializer true)
  144.     {
  145.         /** @var SerializerInterface $serializer */
  146.         $serializer null;
  147.         if ($useAdminSerializer) {
  148.             $serializer $this->container->get('pimcore_admin.serializer');
  149.         } else {
  150.             $serializer $this->container->get('serializer');
  151.         }
  152.         return $serializer->serialize($data'json'array_merge([
  153.             'json_encode_options' => $options,
  154.         ], $context));
  155.     }
  156.     /**
  157.      * Decodes a JSON string into an array/object
  158.      *
  159.      * @param mixed $json       The data to be decoded
  160.      * @param bool $associative Whether to decode into associative array or object
  161.      * @param array $context    Context to pass to serializer when using serializer component
  162.      * @param bool $useAdminSerializer
  163.      *
  164.      * @return mixed
  165.      */
  166.     protected function decodeJson($json$associative true, array $context = [], bool $useAdminSerializer true)
  167.     {
  168.         /** @var SerializerInterface|DecoderInterface $serializer */
  169.         $serializer null;
  170.         if ($useAdminSerializer) {
  171.             $serializer $this->container->get('pimcore_admin.serializer');
  172.         } else {
  173.             $serializer $this->container->get('serializer');
  174.         }
  175.         if ($associative) {
  176.             $context['json_decode_associative'] = true;
  177.         }
  178.         return $serializer->decode($json'json'$context);
  179.     }
  180.     /**
  181.      * Returns a JsonResponse that uses the admin serializer
  182.      *
  183.      * @param mixed $data    The response data
  184.      * @param int $status    The status code to use for the Response
  185.      * @param array $headers Array of extra headers to add
  186.      * @param array $context Context to pass to serializer when using serializer component
  187.      * @param bool $useAdminSerializer
  188.      *
  189.      * @return JsonResponse
  190.      */
  191.     protected function adminJson($data$status 200$headers = [], $context = [], bool $useAdminSerializer true)
  192.     {
  193.         $json $this->encodeJson($data$contextJsonResponse::DEFAULT_ENCODING_OPTIONS$useAdminSerializer);
  194.         return new JsonResponse($json$status$headerstrue);
  195.     }
  196.     /**
  197.      * Translates the given message.
  198.      *
  199.      * @param string $id The message id (may also be an object that can be cast to string)
  200.      * @param array $parameters An array of parameters for the message
  201.      * @param string|null $domain The domain for the message or null to use the default
  202.      * @param string|null $locale The locale or null to use the default
  203.      *
  204.      * @return string The translated string
  205.      *
  206.      * @throws InvalidArgumentException If the locale contains invalid characters
  207.      */
  208.     public function trans($id, array $parameters = [], $domain 'admin'$locale null)
  209.     {
  210.         $translator $this->get('translator');
  211.         return $translator->trans($id$parameters$domain$locale);
  212.     }
  213.     /**
  214.      * @param Request $request
  215.      */
  216.     public function checkCsrfToken(Request $request)
  217.     {
  218.         $csrfCheck $this->container->get('Pimcore\Bundle\AdminBundle\EventListener\CsrfProtectionListener');
  219.         $csrfCheck->checkCsrfToken($request);
  220.     }
  221. }