src/Service/Subscription/Voter/SubscriptionVoter.php line 8

Open in your IDE?
  1. <?php
  2. namespace Harmonizely\Service\Subscription\Voter;
  3. use Harmonizely\Model\UserInterface;
  4. use Harmonizely\Service\Panel\AccessVoters\AbstractVoter;
  5. class SubscriptionVoter extends AbstractVoter
  6. {
  7.     const RESOURCE_NAME 'subscription';
  8.     const CUSTOM_DOMAIN 'custom_domain';
  9.     const CHANGE_TEMPLATE 'change_template';
  10.     const WHITE_LABEL 'white_label';
  11.     const ORGANIZATION 'organization';
  12.     const SMS 'sms';
  13.     const SMTP 'smtp';
  14.     const FLEXIBLE_DURATION 'flexible_duration';
  15.     const SBPAY_PRO 'sbpay_pro';
  16.     const MULTIPLE_MEETING 'multiple_meeting';
  17.     const MULTIPLE_EMAIL 'multiple_email';
  18.     const EVENT_TYPE_TAG 'event_type_tag';
  19.     const API 'api';
  20.     const USER_RELATION 'user_relation';
  21.     const ROUTING_FORM 'routing_form';
  22.     const POLL 'poll';
  23.     const LOGIN_AS_USER 'login_as_user';
  24.     const OKTA 'okta';
  25.     const REMINDERS_AND_FOLLOW_UPS 'reminders_and_follow_ups';
  26.     const INVITEE 'invitee';
  27.     const ANALYTICS 'analytics';
  28.     const ZAPIER 'zapier';
  29.     const RECURRING 'recurring';
  30.     const WIDGET 'widget';
  31.     const QUESTION 'question';
  32.     const CANCELLATION_POLICY 'cancellation_policy';
  33.     const CONFIRMATION_REDIRECT 'confirmation_redirect';
  34.     const GROUP_BOOKING 'group_booking';
  35.     const SBPAY 'sbpay';
  36.     const AFFILIATE 'affiliate';
  37.     const APPROVE_EVENT 'approve_event';
  38.     const SINGLE_USE_LINK 'single_use_link';
  39.     /**
  40.      * @return string
  41.      */
  42.     function getResourceName(): string
  43.     {
  44.         return self::RESOURCE_NAME;
  45.     }
  46.     /**
  47.      * @return string[]
  48.      */
  49.     function getResourceAttributes(): array
  50.     {
  51.         return [
  52.             self::CUSTOM_DOMAIN,
  53.             self::WHITE_LABEL,
  54.             self::CHANGE_TEMPLATE,
  55.             self::ORGANIZATION,
  56.             self::SMS,
  57.             self::SMTP,
  58.             self::FLEXIBLE_DURATION,
  59.             self::SBPAY_PRO,
  60.             self::MULTIPLE_MEETING,
  61.             self::MULTIPLE_EMAIL,
  62.             self::EVENT_TYPE_TAG,
  63.             self::API,
  64.             self::USER_RELATION,
  65.             self::ROUTING_FORM,
  66.             self::POLL,
  67.             self::LOGIN_AS_USER,
  68.             self::OKTA,
  69.             self::REMINDERS_AND_FOLLOW_UPS,
  70.             self::SBPAY,
  71.             self::INVITEE,
  72.             self::GROUP_BOOKING,
  73.             self::CANCELLATION_POLICY,
  74.             self::QUESTION,
  75.             self::CONFIRMATION_REDIRECT,
  76.             self::ANALYTICS,
  77.             self::ZAPIER,
  78.             self::RECURRING,
  79.             self::WIDGET,
  80.             self::AFFILIATE,
  81.             self::APPROVE_EVENT,
  82.             self::SINGLE_USE_LINK
  83.         ];
  84.     }
  85.     /**
  86.      * @param UserInterface $user
  87.      * @param $subject
  88.      * @return array|string[]
  89.      */
  90.     function getAllowedAttributes(UserInterface $user$subject): array
  91.     {
  92.         $subscriptionAttributes $user->getSubscriptionAttributes();
  93.         if (empty($subscriptionAttributes) || $this->hasOnlyLegacyAttributes($subscriptionAttributes)) {
  94.             return $this->getOldAllowedAttributes($user);
  95.         }
  96.         return $subscriptionAttributes;
  97.     }
  98.     /**
  99.      * @param string $attribute
  100.      * @param mixed $subject
  101.      * @return bool
  102.      */
  103.     protected function supports($attribute$subject): bool
  104.     {
  105.         if (!in_array($attribute$this->getResourceAttributes())) {
  106.             return false;
  107.         }
  108.         return true;
  109.     }
  110.     /**
  111.      * @param string $attribute
  112.      * @param UserInterface $user
  113.      * @return bool
  114.      */
  115.     public static function staticVoteOnAttribute(string $attributeUserInterface $user): bool
  116.     {
  117.         $instance = new self();
  118.         if (!in_array($attribute$instance->getResourceAttributes())) {
  119.             return false;
  120.         }
  121.         return in_array($attribute$instance->getAllowedAttributes($usernull));
  122.     }
  123.     /**
  124.      * Old subscriptions have only these 7 legacy attributes and must fall through to getOldAllowedAttributes().
  125.      *
  126.      * @param array $attributes
  127.      * @return bool
  128.      */
  129.     private function hasOnlyLegacyAttributes(array $attributes): bool
  130.     {
  131.         $legacyAttributes = [
  132.             'branding',
  133.             'round-robin-meetings',
  134.             'organization-management',
  135.             'okta-sso-authentication-for-your-team',
  136.             'sms-text-messages',
  137.             'smtp-server',
  138.             'admin-app',
  139.         ];
  140.         return count($attributes) === count($legacyAttributes) && empty(array_diff($attributes$legacyAttributes));
  141.     }
  142.     /**
  143.      * @param UserInterface $user
  144.      * @return array|string[]
  145.      */
  146.     private function getOldAllowedAttributes(UserInterface $user): array
  147.     {
  148.         $allowedAttributes $this->getResourceAttributes();
  149.         if ($user->hasPaidSubscription(true)) {
  150.             return $allowedAttributes;
  151.         }
  152.         if ($user->hasPaidSubscription()) {
  153.             return array_values(array_diff($allowedAttributes, [
  154.                 self::CHANGE_TEMPLATE,
  155.                 self::AFFILIATE
  156.             ]));
  157.         }
  158.         if ($user->hasCurrentSubscription()) {
  159.             return array_values(array_diff($allowedAttributes, [
  160.                 self::CUSTOM_DOMAIN,
  161.                 self::WHITE_LABEL,
  162.                 self::CHANGE_TEMPLATE,
  163.                 self::ORGANIZATION,
  164.                 self::SMS,
  165.                 self::SMTP,
  166.                 self::FLEXIBLE_DURATION,
  167.                 self::SBPAY_PRO,
  168.                 self::MULTIPLE_MEETING,
  169.                 self::MULTIPLE_EMAIL,
  170.                 self::EVENT_TYPE_TAG,
  171.                 self::USER_RELATION,
  172.                 self::LOGIN_AS_USER,
  173.                 self::ROUTING_FORM,
  174.                 self::AFFILIATE,
  175.                 self::APPROVE_EVENT,
  176.                 self::SINGLE_USE_LINK
  177.             ]));
  178.         }
  179.         return [];
  180.     }
  181. }