src/Service/SuperAdmin/Panel/AccessVoters/OrganizationVoter.php line 9

Open in your IDE?
  1. <?php
  2. namespace Harmonizely\Service\SuperAdmin\Panel\AccessVoters;
  3. use Harmonizely\Entity\Company\SuperAdmin\UserEntity;
  4. use Harmonizely\Model\Organization;
  5. use Harmonizely\Types\Company\SuperAdmin\UserRole;
  6. class OrganizationVoter extends AbstractVoter
  7. {
  8.     /**
  9.      * Resource name
  10.      */
  11.     const RESOURCE_NAME 'organization';
  12.     /**
  13.      * View customers
  14.      */
  15.     const VIEW 'super_admin_organization_view';
  16.     /**
  17.      * Enable user
  18.      */
  19.     const CHANGE_LIMIT 'super_admin_organization_change_limit';
  20.     /**
  21.      * Disabled user
  22.      */
  23.     const CHANGE_LOGIN_AS_USER 'super_admin_organization_change_login_as_user';
  24.     /**
  25.      * Return resource name
  26.      *
  27.      * @return string
  28.      */
  29.     function getResourceName(): string
  30.     {
  31.         return self::RESOURCE_NAME;
  32.     }
  33.     /**
  34.      * Return allowed attributes for current user
  35.      *
  36.      * @return array|string[]
  37.      */
  38.     function getResourceAttributes(): array
  39.     {
  40.         return [
  41.             self::VIEW,
  42.             self::CHANGE_LIMIT,
  43.             self::CHANGE_LOGIN_AS_USER,
  44.         ];
  45.     }
  46.     /**
  47.      * Return allowed attributes for current user
  48.      *
  49.      * @param UserEntity $user
  50.      * @return array|string[]
  51.      */
  52.     function getAllowedAttributes(UserEntity $user): array
  53.     {
  54.         switch ($user->getRole()) {
  55.             case UserRole::ROLE_ADMIN:
  56.             case UserRole::ROLE_SYSTEM_USER:
  57.                 return $this->getResourceAttributes();
  58.             case UserRole::ROLE_SUPPORT:
  59.                 return [self::VIEWself::CHANGE_LIMIT];
  60.             default:
  61.                 return [self::VIEW];
  62.         }
  63.     }
  64.     /**
  65.      * Determines if the attribute and subject are supported by this voter.
  66.      *
  67.      * @param string $attribute An attribute
  68.      * @param mixed $subject The subject to secure, e.g. an object the user wants to access or any other PHP type
  69.      *
  70.      * @return bool True if the attribute and subject are supported, false otherwise
  71.      */
  72.     protected function supports($attribute$subject): bool
  73.     {
  74.         if ($subject !== null && !($subject instanceof Organization)) {
  75.             return false;
  76.         }
  77.         if (!in_array($attribute$this->getResourceAttributes())) {
  78.             return false;
  79.         }
  80.         if (!in_array($attribute, [self::VIEW]) && $subject === null) {
  81.             return false;
  82.         }
  83.         return true;
  84.     }
  85. }