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\Company\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.     const APPROVE_EVENT self::RESOURCE_NAME '_' IntegrationType::APPROVE_EVENT;
  23.     const ALLOWED_ORGANIZATION_INTEGRATION_WITHOUT_CONFIG = [
  24.         self::APPROVE_EVENT,
  25.     ];
  26.     /**
  27.      * Return resource name
  28.      *
  29.      * @return string
  30.      */
  31.     function getResourceName(): string
  32.     {
  33.         return self::RESOURCE_NAME;
  34.     }
  35.     /**
  36.      * Return allowed attributes for current user
  37.      *
  38.      * @return array|string[]
  39.      */
  40.     function getResourceAttributes(): array
  41.     {
  42.         return [
  43.             self::ZAPIER,
  44.             self::ZOOM,
  45.             self::WHEREBY,
  46.             self::FB_PIXEL,
  47.             self::KOPANO_MEET,
  48.             self::GA,
  49.             self::SIMPLYPAY,
  50.             self::SMTP,
  51.             self::SMS,
  52.             self::FLEXIBLE_DURATION,
  53.             self::MULTIPLE_MEETING,
  54.             self::MULTIPLE_EMAIL,
  55.             self::EVENT_TYPE_TAG,
  56.             self::ROUTING_FORM,
  57.             self::APPROVE_EVENT,
  58.         ];
  59.     }
  60.     /**
  61.      * Return allowed attributes for current user
  62.      *
  63.      * @param UserInterface $user
  64.      * @param $subject
  65.      * @return array|string[]
  66.      */
  67.     function getAllowedAttributes(UserInterface $user$subject): array
  68.     {
  69.         $allowedAttributes = [];
  70.         foreach ($user->getIntegrations() as $integration) {
  71.             if ($integration->isEnabled()) {
  72.                 $allowedAttributes[] = self::RESOURCE_NAME '_' $integration->getType();
  73.             }
  74.         }
  75.         $organization $user->getDefaultOrganization();
  76.         if ($organization) {
  77.             foreach ($organization->getIntegrations() as $organizationIntegration) {
  78.                 if ($organizationIntegration->isEnabled()) {
  79.                     $attribute self::RESOURCE_NAME '_' $organizationIntegration->getType();
  80.                     $allowOrganizationIntegrationAttribute false;
  81.                     if ($organizationIntegration->isConfigured()) {
  82.                         $organizationIntegrationConfig $organizationIntegration->getConfig();
  83.                         if (isset($organizationIntegrationConfig['overwritePersonal']) && $organizationIntegrationConfig['overwritePersonal']) {
  84.                             $allowOrganizationIntegrationAttribute true;
  85.                         }
  86.                     } elseif (in_array($attributeself::ALLOWED_ORGANIZATION_INTEGRATION_WITHOUT_CONFIG)) {
  87.                         $allowOrganizationIntegrationAttribute true;
  88.                     }
  89.                     if ($allowOrganizationIntegrationAttribute && !in_array($attribute$allowedAttributes)) {
  90.                         $allowedAttributes[] = $attribute;
  91.                     }
  92.                 }
  93.             }
  94.         }
  95.         return $allowedAttributes;
  96.     }
  97.     /**
  98.      * Determines if the attribute and subject are supported by this voter.
  99.      *
  100.      * @param string $attribute An attribute
  101.      * @param mixed $subject The subject to secure, e.g. an object the user wants to access or any other PHP type
  102.      *
  103.      * @return bool True if the attribute and subject are supported, false otherwise
  104.      */
  105.     protected function supports($attribute$subject): bool
  106.     {
  107.         if (!in_array($attribute$this->getResourceAttributes())) {
  108.             return false;
  109.         }
  110.         return true;
  111.     }
  112.     /**
  113.      * @param string $attribute
  114.      * @param UserInterface $user
  115.      * @return bool
  116.      */
  117.     public static function staticVoteOnAttribute(string $attributeUserInterface $user): bool
  118.     {
  119.         $instance = new self();
  120.         if (!in_array($attribute$instance->getResourceAttributes())) {
  121.             return false;
  122.         }
  123.         return in_array($attribute$instance->getAllowedAttributes($usernull));
  124.     }
  125. }