vendor/contao/core-bundle/src/Security/Voter/BackendAccessVoter.php line 20

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of Contao.
  5.  *
  6.  * (c) Leo Feyer
  7.  *
  8.  * @license LGPL-3.0-or-later
  9.  */
  10. namespace Contao\CoreBundle\Security\Voter;
  11. use Contao\BackendUser;
  12. use Contao\PageModel;
  13. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  14. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  15. class BackendAccessVoter extends Voter
  16. {
  17.     private const PAGE_PERMISSIONS = [
  18.         'can_edit_page' => BackendUser::CAN_EDIT_PAGE,
  19.         'can_edit_page_hierarchy' => BackendUser::CAN_EDIT_PAGE_HIERARCHY,
  20.         'can_delete_page' => BackendUser::CAN_DELETE_PAGE,
  21.         'can_edit_articles' => BackendUser::CAN_EDIT_ARTICLES,
  22.         'can_edit_article_hierarchy' => BackendUser::CAN_EDIT_ARTICLE_HIERARCHY,
  23.         'can_delete_articles' => BackendUser::CAN_DELETE_ARTICLES,
  24.     ];
  25.     protected function supports($attribute$subject): bool
  26.     {
  27.         return \is_string($attribute) && === strncmp($attribute'contao_user.'12);
  28.     }
  29.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  30.     {
  31.         $user $token->getUser();
  32.         if (!$user instanceof BackendUser) {
  33.             return false;
  34.         }
  35.         $permission explode('.'$attribute3);
  36.         if ('contao_user' !== $permission[0] || !isset($permission[1])) {
  37.             return false;
  38.         }
  39.         $field $permission[1];
  40.         if (!$subject && isset($permission[2])) {
  41.             $subject $permission[2];
  42.         }
  43.         if ('can_edit_fields' === $field) {
  44.             return $this->canEditFieldsOf($subject$user);
  45.         }
  46.         if (isset(self::PAGE_PERMISSIONS[$field])) {
  47.             return $this->isAllowed($subjectself::PAGE_PERMISSIONS[$field], $user);
  48.         }
  49.         return $this->hasAccess($subject$field$user);
  50.     }
  51.     /**
  52.      * Checks the user permissions against a field in tl_user(_group).
  53.      */
  54.     private function hasAccess($subjectstring $fieldBackendUser $user): bool
  55.     {
  56.         if (!is_scalar($subject) && !\is_array($subject)) {
  57.             return false;
  58.         }
  59.         return $user->hasAccess($subject$field);
  60.     }
  61.     /**
  62.      * Checks if the user has access to a given page (tl_page.includeChmod et al.).
  63.      */
  64.     private function isAllowed($subjectint $flagBackendUser $user): bool
  65.     {
  66.         if ($subject instanceof PageModel) {
  67.             $subject->loadDetails();
  68.             $subject $subject->row();
  69.         }
  70.         if (!\is_array($subject)) {
  71.             return false;
  72.         }
  73.         return $user->isAllowed($flag$subject);
  74.     }
  75.     /**
  76.      * Checks if the user has access to any field of a table (against tl_user(_group).alexf).
  77.      */
  78.     private function canEditFieldsOf($subjectBackendUser $user): bool
  79.     {
  80.         if (!\is_string($subject)) {
  81.             return false;
  82.         }
  83.         return $user->canEditFieldsOf($subject);
  84.     }
  85. }