src/Service/Panel/AccessVoters/EnabledIntegrationVoter.php line 8

Open in your IDE?
  1. <?php
  2. namespace Harmonizely\Service\Panel\AccessVoters;
  3. use Harmonizely\Model\UserInterface;
  4. use Harmonizely\Types\IntegrationType;
  5. class EnabledIntegrationVoter extends AbstractVoter
  6. {
  7.     const RESOURCE_NAME 'enabled_integration';
  8.     const ZAPIER self::RESOURCE_NAME '_' IntegrationType::TYPE_ZAPIER;
  9.     const ZOOM self::RESOURCE_NAME '_' IntegrationType::TYPE_ZOOM;
  10.     const WHEREBY self::RESOURCE_NAME '_' IntegrationType::TYPE_WHEREBY;
  11.     const FB_PIXEL self::RESOURCE_NAME '_' IntegrationType::TYPE_FB_PIXEL;
  12.     const KOPANO_MEET self::RESOURCE_NAME '_' IntegrationType::TYPE_KOPANO_MEET;
  13.     const GA self::RESOURCE_NAME '_' IntegrationType::TYPE_GA;
  14.     const SIMPLYPAY self::RESOURCE_NAME '_' IntegrationType::TYPE_SIMPLYPAY;
  15.     const SMTP self::RESOURCE_NAME '_' IntegrationType::TYPE_SMTP;
  16.     const SMS self::RESOURCE_NAME '_' IntegrationType::TYPE_SMS;
  17.     const FLEXIBLE_DURATION self::RESOURCE_NAME '_' IntegrationType::TYPE_FLEXIBLE_DURATION;
  18.     const MULTIPLE_MEETING self::RESOURCE_NAME '_' IntegrationType::TYPE_MULTIPLE_MEETING;
  19.     const MULTIPLE_EMAIL self::RESOURCE_NAME '_' IntegrationType::TYPE_MULTIPLE_EMAIL;
  20.     const EVENT_TYPE_TAG self::RESOURCE_NAME '_' IntegrationType::EVENT_TYPE_TAG;
  21.     const ROUTING_FORM self::RESOURCE_NAME '_' IntegrationType::ROUTING_FORM;
  22.     /**
  23.      * Return resource name
  24.      *
  25.      * @return string
  26.      */
  27.     function getResourceName(): string
  28.     {
  29.         return self::RESOURCE_NAME;
  30.     }
  31.     /**
  32.      * Return allowed attributes for current user
  33.      *
  34.      * @return array|string[]
  35.      */
  36.     function getResourceAttributes(): array
  37.     {
  38.         return [
  39.             self::ZAPIER,
  40.             self::ZOOM,
  41.             self::WHEREBY,
  42.             self::FB_PIXEL,
  43.             self::KOPANO_MEET,
  44.             self::GA,
  45.             self::SIMPLYPAY,
  46.             self::SMTP,
  47.             self::SMS,
  48.             self::FLEXIBLE_DURATION,
  49.             self::MULTIPLE_MEETING,
  50.             self::MULTIPLE_EMAIL,
  51.             self::EVENT_TYPE_TAG,
  52.             self::ROUTING_FORM
  53.         ];
  54.     }
  55.     /**
  56.      * Return allowed attributes for current user
  57.      *
  58.      * @param UserInterface $user
  59.      * @param $subject
  60.      * @return array|string[]
  61.      */
  62.     function getAllowedAttributes(UserInterface $user$subject): array
  63.     {
  64.         $allowedAttributes = [];
  65.         foreach ($user->getIntegrations() as $integration) {
  66.             if ($integration->isEnabled()) {
  67.                 $allowedAttributes[] = self::RESOURCE_NAME '_' $integration->getType();
  68.             }
  69.         }
  70.         return $allowedAttributes;
  71.     }
  72.     /**
  73.      * Determines if the attribute and subject are supported by this voter.
  74.      *
  75.      * @param string $attribute An attribute
  76.      * @param mixed $subject The subject to secure, e.g. an object the user wants to access or any other PHP type
  77.      *
  78.      * @return bool True if the attribute and subject are supported, false otherwise
  79.      */
  80.     protected function supports($attribute$subject): bool
  81.     {
  82.         if (!in_array($attribute$this->getResourceAttributes())) {
  83.             return false;
  84.         }
  85.         return true;
  86.     }
  87.     /**
  88.      * @param string $attribute
  89.      * @param UserInterface $user
  90.      * @return bool
  91.      */
  92.     public static function staticVoteOnAttribute(string $attributeUserInterface $user): bool
  93.     {
  94.         $instance = new self();
  95.         if (!in_array($attribute$instance->getResourceAttributes())) {
  96.             return false;
  97.         }
  98.         return in_array($attribute$instance->getAllowedAttributes($usernull));
  99.     }
  100. }