src/Security/Voter/UserVoter.php line 48

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 function in_array;
  43. class UserVoter extends ExtendedVoter
  44. {
  45.     /**
  46.      * Determines if the attribute and subject are supported by this voter.
  47.      *
  48.      * @param string $attribute An attribute
  49.      * @param mixed  $subject   The subject to secure, e.g. an object the user wants to access or any other PHP type
  50.      *
  51.      * @return bool True if the attribute and subject are supported, false otherwise
  52.      */
  53.     protected function supports($attribute$subject)
  54.     {
  55.         if (is_a($subjectUser::class, true)) {
  56.             return in_array($attributearray_merge(
  57.                 $this->resolver->listOperationsForPermission('users'),
  58.                 $this->resolver->listOperationsForPermission('self')),
  59.                             false
  60.             );
  61.         }
  62.         return false;
  63.     }
  64.     /**
  65.      * Similar to voteOnAttribute, but checking for the anonymous user is already done.
  66.      * The current user (or the anonymous user) is passed by $user.
  67.      *
  68.      * @param string $attribute
  69.      */
  70.     protected function voteOnUser($attribute$subjectUser $user): bool
  71.     {
  72.         //Check if the checked user is the user itself
  73.         if (($subject instanceof User) && $subject->getID() === $user->getID() &&
  74.             $this->resolver->isValidOperation('self'$attribute)) {
  75.             //Then we also need to check the self permission
  76.             $tmp $this->resolver->inherit($user'self'$attribute) ?? false;
  77.             //But if the self value is not allowed then use just the user value:
  78.             if ($tmp) {
  79.                 return $tmp;
  80.             }
  81.         }
  82.         //Else just check users permission:
  83.         if ($this->resolver->isValidOperation('users'$attribute)) {
  84.             return $this->resolver->inherit($user'users'$attribute) ?? false;
  85.         }
  86.         return false;
  87.     }
  88. }