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

Open in your IDE?
  1. <?php
  2. namespace Harmonizely\Service\Panel\AccessVoters;
  3. use Harmonizely\Model\Member;
  4. use Harmonizely\Model\Organization;
  5. use Harmonizely\Model\UserInterface;
  6. use Harmonizely\Service\Subscription\Voter\SubscriptionVoter;
  7. class OrganizationVoter extends AbstractVoter
  8. {
  9.     /**
  10.      * Resource name
  11.      */
  12.     const RESOURCE_NAME 'organization';
  13.     /**
  14.      * View events
  15.      */
  16.     const ORGANIZATION_REPORT_VIEW 'organization_report_view';
  17.     /**
  18.      * View organization
  19.      */
  20.     const ORGANIZATION_VIEW 'organization_view';
  21.     /**
  22.      * Delete organization
  23.      */
  24.     const ORGANIZATION_DELETE 'organization_delete';
  25.     /**
  26.      * Organization integration
  27.      */
  28.     const ORGANIZATION_INTEGRATION 'organization_integration';
  29.     const ORGANIZATION_EDIT 'organization_edit';
  30.     const ORGANIZATION_BILLING 'organization_billing';
  31.     const ORGANIZATION_MANAGE_USER 'organization_manage_user';
  32.     const ORGANIZATION_MANAGE_OWNER 'organization_manage_owner';
  33.     const ORGANIZATION_MANAGE_ROUND_ROBIN 'organization_manage_round_robin';
  34.     const ORGANIZATION_MANAGE_TRANSLATION 'organization_manage_translation';
  35.     const ORGANIZATION_MANAGE_APPEARANCE 'organization_manage_appearance';
  36.     const ORGANIZATION_MANAGE_WIDGET 'organization_manage_widget';
  37.     const ORGANIZATION_LEAVE 'organization_leave';
  38.     const ORGANIZATION_CALENDAR_SETTINGS 'organization_calendar_settings';
  39.     const ORGANIZATION_MANAGE_ROLE 'organization_manage_role';
  40.     const ORGANIZATION_INTERNAL_EVENT_VIEW 'organization_internal_event_view';
  41.     const ORGANIZATION_ALLOW_LOGIN_AS_USER 'organization_allow_login_as_user';
  42.     public static array $organizationRoleActionMock = [
  43.         Member::ROLE_OWNER => [
  44.             OrganizationVoter::ORGANIZATION_LEAVE => ['fixed' => true'enable' => true],
  45.             OrganizationVoter::ORGANIZATION_VIEW => ['fixed' => true'enable' => true],
  46.             OrganizationVoter::ORGANIZATION_REPORT_VIEW => ['fixed' => true'enable' => true],
  47.             OrganizationVoter::ORGANIZATION_CALENDAR_SETTINGS => ['fixed' => true'enable' => true],
  48.             OrganizationVoter::ORGANIZATION_INTERNAL_EVENT_VIEW => ['fixed' => true'enable' => true],
  49.             OrganizationVoter::ORGANIZATION_MANAGE_ROUND_ROBIN => ['fixed' => true'enable' => true],
  50.             OrganizationVoter::ORGANIZATION_INTEGRATION => ['fixed' => true'enable' => true],
  51.             OrganizationVoter::ORGANIZATION_EDIT => ['fixed' => true'enable' => true],
  52.             OrganizationVoter::ORGANIZATION_MANAGE_USER => ['fixed' => true'enable' => true],
  53.             OrganizationVoter::ORGANIZATION_MANAGE_ROLE => ['fixed' => true'enable' => true],
  54.             OrganizationVoter::ORGANIZATION_MANAGE_TRANSLATION => ['fixed' => true'enable' => true],
  55.             OrganizationVoter::ORGANIZATION_MANAGE_APPEARANCE => ['fixed' => true'enable' => true],
  56.             OrganizationVoter::ORGANIZATION_MANAGE_WIDGET => ['fixed' => true'enable' => true],
  57.             OrganizationVoter::ORGANIZATION_DELETE => ['fixed' => true'enable' => true],
  58.             OrganizationVoter::ORGANIZATION_BILLING => ['fixed' => true'enable' => true],
  59.             OrganizationVoter::ORGANIZATION_MANAGE_OWNER => ['fixed' => true'enable' => true],
  60.             OrganizationVoter::ORGANIZATION_ALLOW_LOGIN_AS_USER => ['fixed' => true'enable' => true],
  61.         ],
  62.         Member::ROLE_MANAGER => [
  63.             OrganizationVoter::ORGANIZATION_LEAVE => ['fixed' => true'enable' => true],
  64.             OrganizationVoter::ORGANIZATION_VIEW => ['fixed' => false'enable' => true],
  65.             OrganizationVoter::ORGANIZATION_REPORT_VIEW => ['fixed' => false'enable' => true],
  66.             OrganizationVoter::ORGANIZATION_CALENDAR_SETTINGS => ['fixed' => false'enable' => true],
  67.             OrganizationVoter::ORGANIZATION_INTERNAL_EVENT_VIEW => ['fixed' => false'enable' => true],
  68.             OrganizationVoter::ORGANIZATION_MANAGE_ROUND_ROBIN => ['fixed' => false'enable' => false],
  69.             OrganizationVoter::ORGANIZATION_INTEGRATION => ['fixed' => false'enable' => false],
  70.             OrganizationVoter::ORGANIZATION_EDIT => ['fixed' => false'enable' => false],
  71.             OrganizationVoter::ORGANIZATION_MANAGE_USER => ['fixed' => false'enable' => true],
  72.             OrganizationVoter::ORGANIZATION_MANAGE_ROLE => ['fixed' => false'enable' => false],
  73.             OrganizationVoter::ORGANIZATION_MANAGE_TRANSLATION => ['fixed' => false'enable' => true],
  74.             OrganizationVoter::ORGANIZATION_MANAGE_APPEARANCE => ['fixed' => false'enable' => true],
  75.             OrganizationVoter::ORGANIZATION_MANAGE_WIDGET => ['fixed' => false'enable' => true],
  76.             OrganizationVoter::ORGANIZATION_DELETE => ['fixed' => true'enable' => false],
  77.             OrganizationVoter::ORGANIZATION_BILLING => ['fixed' => true'enable' => false],
  78.             OrganizationVoter::ORGANIZATION_MANAGE_OWNER => ['fixed' => true'enable' => false],
  79.             OrganizationVoter::ORGANIZATION_ALLOW_LOGIN_AS_USER => ['fixed' => false'enable' => false],
  80.         ],
  81.         Member::ROLE_MEMBER => [
  82.             OrganizationVoter::ORGANIZATION_LEAVE => ['fixed' => true'enable' => true],
  83.             OrganizationVoter::ORGANIZATION_VIEW => ['fixed' => false'enable' => false],
  84.             OrganizationVoter::ORGANIZATION_REPORT_VIEW => ['fixed' => false'enable' => false],
  85.             OrganizationVoter::ORGANIZATION_CALENDAR_SETTINGS => ['fixed' => false'enable' => true],
  86.             OrganizationVoter::ORGANIZATION_INTERNAL_EVENT_VIEW => ['fixed' => false'enable' => false],
  87.             OrganizationVoter::ORGANIZATION_MANAGE_ROUND_ROBIN => ['fixed' => false'enable' => false],
  88.             OrganizationVoter::ORGANIZATION_INTEGRATION => ['fixed' => true'enable' => false],
  89.             OrganizationVoter::ORGANIZATION_EDIT => ['fixed' => true'enable' => false],
  90.             OrganizationVoter::ORGANIZATION_MANAGE_USER => ['fixed' => true'enable' => false],
  91.             OrganizationVoter::ORGANIZATION_MANAGE_ROLE => ['fixed' => true'enable' => false],
  92.             OrganizationVoter::ORGANIZATION_MANAGE_TRANSLATION => ['fixed' => true'enable' => false],
  93.             OrganizationVoter::ORGANIZATION_MANAGE_APPEARANCE => ['fixed' => true'enable' => false],
  94.             OrganizationVoter::ORGANIZATION_MANAGE_WIDGET => ['fixed' => true'enable' => false],
  95.             OrganizationVoter::ORGANIZATION_DELETE => ['fixed' => true'enable' => false],
  96.             OrganizationVoter::ORGANIZATION_BILLING => ['fixed' => true'enable' => false],
  97.             OrganizationVoter::ORGANIZATION_MANAGE_OWNER => ['fixed' => true'enable' => false],
  98.             OrganizationVoter::ORGANIZATION_ALLOW_LOGIN_AS_USER => ['fixed' => true'enable' => false],
  99.         ],
  100.     ];
  101.     /**
  102.      * Return resource name
  103.      *
  104.      * @return string
  105.      */
  106.     function getResourceName(): string
  107.     {
  108.         return self::RESOURCE_NAME;
  109.     }
  110.     /**
  111.      * Return allowed attributes for current user
  112.      *
  113.      * @return array|string[]
  114.      */
  115.     function getResourceAttributes(): array
  116.     {
  117.         return [
  118.             self::ORGANIZATION_LEAVE,
  119.             self::ORGANIZATION_VIEW,
  120.             self::ORGANIZATION_REPORT_VIEW,
  121.             self::ORGANIZATION_CALENDAR_SETTINGS,
  122.             self::ORGANIZATION_INTERNAL_EVENT_VIEW,
  123.             self::ORGANIZATION_MANAGE_ROUND_ROBIN,
  124.             self::ORGANIZATION_INTEGRATION,
  125.             self::ORGANIZATION_EDIT,
  126.             self::ORGANIZATION_MANAGE_USER,
  127.             self::ORGANIZATION_MANAGE_ROLE,
  128.             self::ORGANIZATION_MANAGE_TRANSLATION,
  129.             self::ORGANIZATION_MANAGE_APPEARANCE,
  130.             self::ORGANIZATION_MANAGE_WIDGET,
  131.             self::ORGANIZATION_DELETE,
  132.             self::ORGANIZATION_BILLING,
  133.             self::ORGANIZATION_MANAGE_OWNER,
  134.             self::ORGANIZATION_ALLOW_LOGIN_AS_USER,
  135.         ];
  136.     }
  137.     /**
  138.      * Return allowed attributes for current user
  139.      *
  140.      * @param UserInterface $user
  141.      * @param mixed $subject
  142.      * @return array|string[]
  143.      */
  144.     function getAllowedAttributes(UserInterface $user$subject): array
  145.     {
  146.         $allowedAttributes = [];
  147.         $organization $user->getDefaultOrganization();
  148.         if ($organization instanceof Organization && (!$subject or $subject === $organization)) {
  149.             if (SubscriptionVoter::staticVoteOnAttribute(SubscriptionVoter::ORGANIZATION$user)) {
  150.                 $member $user->getMember();
  151.                 if ($member) {
  152.                     foreach ($organization->getRoles() as $roleEntity) {
  153.                         if ($roleEntity->getName() === $member->getRole()) {
  154.                             foreach ($roleEntity->getPermissions() as $permission) {
  155.                                 if ($permission->isEnable()) {
  156.                                     $allowedAttributes[] = $permission->getPermission()->getAction();
  157.                                 }
  158.                             }
  159.                         }
  160.                     }
  161.                 }
  162.             } else {
  163.                 if ($organization->isOrganizationOwner($user)) {
  164.                     $allowedAttributes = [
  165.                         self::ORGANIZATION_VIEW,
  166.                         self::ORGANIZATION_BILLING,
  167.                         self::ORGANIZATION_MANAGE_USER,
  168.                         self::ORGANIZATION_MANAGE_OWNER,
  169.                         self::ORGANIZATION_CALENDAR_SETTINGS,
  170.                     ];
  171.                 }
  172.             }
  173.             if ($user->isOnlySsoUser()) {
  174.                 $allowedAttributes array_diff($allowedAttributes, [self::ORGANIZATION_LEAVE]);
  175.             }
  176.         }
  177.         return $allowedAttributes;
  178.     }
  179.     /**
  180.      * Determines if the attribute and subject are supported by this voter.
  181.      *
  182.      * @param string $attribute An attribute
  183.      * @param mixed $subject The subject to secure, e.g. an object the user wants to access or any other PHP type
  184.      *
  185.      * @return bool True if the attribute and subject are supported, false otherwise
  186.      */
  187.     protected function supports($attribute$subject): bool
  188.     {
  189.         if ($subject !== null && !($subject instanceof Organization)) {
  190.             return false;
  191.         }
  192.         if (!in_array($attribute$this->getResourceAttributes())) {
  193.             return false;
  194.         }
  195.         return true;
  196.     }
  197.     /**
  198.      * @param string $attribute
  199.      * @param UserInterface $user
  200.      * @return bool
  201.      */
  202.     public static function staticVoteOnAttribute(string $attributeUserInterface $user): bool
  203.     {
  204.         $instance = new self();
  205.         if (!in_array($attribute$instance->getResourceAttributes())) {
  206.             return false;
  207.         }
  208.         return in_array($attribute$instance->getAllowedAttributes($usernull));
  209.     }
  210. }