src/Security/Voter/ExtendedVoter.php line 82

Open in your IDE?
  1. <?php
  2. /**
  3.  * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
  4.  *
  5.  * Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU Affero General Public License as published
  9.  * by the Free Software Foundation, either version 3 of the License, or
  10.  * (at your option) any later version.
  11.  *
  12.  * This program is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU Affero General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU Affero General Public License
  18.  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  19.  */
  20. declare(strict_types=1);
  21. /**
  22.  * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
  23.  *
  24.  * Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
  25.  *
  26.  * This program is free software; you can redistribute it and/or
  27.  * modify it under the terms of the GNU General Public License
  28.  * as published by the Free Software Foundation; either version 2
  29.  * of the License, or (at your option) any later version.
  30.  *
  31.  * This program is distributed in the hope that it will be useful,
  32.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  33.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  34.  * GNU General Public License for more details.
  35.  *
  36.  * You should have received a copy of the GNU General Public License
  37.  * along with this program; if not, write to the Free Software
  38.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
  39.  */
  40. namespace App\Security\Voter;
  41. use App\Entity\UserSystem\User;
  42. use App\Repository\UserRepository;
  43. use App\Services\PermissionResolver;
  44. use Doctrine\ORM\EntityManagerInterface;
  45. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  46. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  47. /**
  48.  * The purpose of this class is, to use the anonymous user from DB in the case, that nobody is logged in.
  49.  */
  50. abstract class ExtendedVoter extends Voter
  51. {
  52.     protected $entityManager;
  53.     /**
  54.      * @var PermissionResolver
  55.      */
  56.     protected $resolver;
  57.     public function __construct(PermissionResolver $resolverEntityManagerInterface $entityManager)
  58.     {
  59.         $this->resolver $resolver;
  60.         $this->entityManager $entityManager;
  61.     }
  62.     final protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  63.     {
  64.         $user $token->getUser();
  65.         //An allowed user is not allowed to do anything...
  66.         if ($user instanceof User && $user->isDisabled()) {
  67.             return false;
  68.         }
  69.         // if the user is anonymous, we use the anonymous user.
  70.         if (!$user instanceof User) {
  71.             /** @var UserRepository $repo */
  72.             $repo $this->entityManager->getRepository(User::class);
  73.             $user $repo->getAnonymousUser();
  74.             if (null === $user) {
  75.                 return false;
  76.             }
  77.         }
  78.         return $this->voteOnUser($attribute$subject$user);
  79.     }
  80.     /**
  81.      * Similar to voteOnAttribute, but checking for the anonymous user is already done.
  82.      * The current user (or the anonymous user) is passed by $user.
  83.      *
  84.      * @param string $attribute
  85.      */
  86.     abstract protected function voteOnUser($attribute$subjectUser $user): bool;
  87. }