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

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