src/Security/Voter/StructureVoter.php line 58

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\Attachments\AttachmentType;
  42. use App\Entity\Devices\Device;
  43. use App\Entity\Parts\Category;
  44. use App\Entity\Parts\Footprint;
  45. use App\Entity\Parts\Manufacturer;
  46. use App\Entity\Parts\MeasurementUnit;
  47. use App\Entity\Parts\Storelocation;
  48. use App\Entity\Parts\Supplier;
  49. use App\Entity\PriceInformations\Currency;
  50. use App\Entity\UserSystem\User;
  51. use function get_class;
  52. use function is_object;
  53. class StructureVoter extends ExtendedVoter
  54. {
  55.     protected const OBJ_PERM_MAP = [
  56.         AttachmentType::class => 'attachment_types',
  57.         Category::class => 'categories',
  58.         Device::class => 'devices',
  59.         Footprint::class => 'footprints',
  60.         Manufacturer::class => 'manufacturers',
  61.         Storelocation::class => 'storelocations',
  62.         Supplier::class => 'suppliers',
  63.         Currency::class => 'currencies',
  64.         MeasurementUnit::class => 'measurement_units',
  65.     ];
  66.     /**
  67.      * Determines if the attribute and subject are supported by this voter.
  68.      *
  69.      * @param string $attribute An attribute
  70.      * @param mixed  $subject   The subject to secure, e.g. an object the user wants to access or any other PHP type
  71.      *
  72.      * @return bool True if the attribute and subject are supported, false otherwise
  73.      */
  74.     protected function supports($attribute$subject)
  75.     {
  76.         if (is_object($subject) || is_string($subject)) {
  77.             $permission_name $this->instanceToPermissionName($subject);
  78.             //If permission name is null, then the subject is not supported
  79.             return (null !== $permission_name) && $this->resolver->isValidOperation($permission_name$attribute);
  80.         }
  81.         return false;
  82.     }
  83.     /**
  84.      * Maps a instance type to the permission name.
  85.      *
  86.      * @param object|string $subject The subject for which the permission name should be generated
  87.      *
  88.      * @return string|null the name of the permission for the subject's type or null, if the subject is not supported
  89.      */
  90.     protected function instanceToPermissionName($subject): ?string
  91.     {
  92.         if (!is_string($subject)) {
  93.             $class_name get_class($subject);
  94.         } else {
  95.             $class_name $subject;
  96.         }
  97.         //If it is existing in index, we can skip the loop
  98.         if (isset(static::OBJ_PERM_MAP[$class_name])) {
  99.             return static::OBJ_PERM_MAP[$class_name];
  100.         }
  101.         foreach (static::OBJ_PERM_MAP as $class => $ret) {
  102.             if (is_a($class_name$classtrue)) {
  103.                 return $ret;
  104.             }
  105.         }
  106.         return null;
  107.     }
  108.     /**
  109.      * Similar to voteOnAttribute, but checking for the anonymous user is already done.
  110.      * The current user (or the anonymous user) is passed by $user.
  111.      *
  112.      * @param string $attribute
  113.      */
  114.     protected function voteOnUser($attribute$subjectUser $user): bool
  115.     {
  116.         $permission_name $this->instanceToPermissionName($subject);
  117.         //Just resolve the permission
  118.         return $this->resolver->inherit($user$permission_name$attribute) ?? false;
  119.     }
  120. }