src/Security/Voter/BackendCustomerVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\User;
  4. use App\Utils\Commons;
  5. use LogicException;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. use Symfony\Component\Security\Core\Security;
  9. use Symfony\Component\Security\Core\User\UserInterface;
  10. class BackendCustomerVoter extends Voter
  11. {
  12.     const BACKEND_CUSTOMER_VIEW 'BACKEND_CUSTOMER_VIEW';
  13.     const BACKEND_CUSTOMER_SHOW 'BACKEND_CUSTOMER_SHOW';
  14.     const BACKEND_CUSTOMER_EDIT 'BACKEND_CUSTOMER_EDIT';
  15.     const BACKEND_CUSTOMER_INACTIVE 'BACKEND_CUSTOMER_INACTIVE';
  16.     const BACKEND_CUSTOMER_DELETE 'BACKEND_CUSTOMER_DELETE';
  17.     const BACKEND_CUSTOMER_BILLING_STATUS 'BACKEND_CUSTOMER_BILLING_STATUS';
  18.     const BACKEND_CUSTOMER_USERS_VIEW 'BACKEND_CUSTOMER_USERS_VIEW';
  19.     const BACKEND_CUSTOMER_TEAM_CHANGE 'BACKEND_CUSTOMER_TEAM_CHANGE';
  20.     const BACKEND_CUSTOMER_INVOICES_VIEW 'BACKEND_CUSTOMER_INVOICES_VIEW';
  21.     const BACKEND_CUSTOMER_TRANSACTIONS_VIEW 'BACKEND_CUSTOMER_TRANSACTIONS_VIEW';
  22.     const BACKEND_CUSTOMER_PLAN_CHANGES_VIEW 'BACKEND_CUSTOMER_PLAN_CHANGES_VIEW';
  23.     const BACKEND_CUSTOMER_KEYS_VIEW 'BACKEND_CUSTOMER_KEYS_VIEW';
  24.     const BACKEND_CUSTOMER_TRANSACTION_CHARGE 'BACKEND_CUSTOMER_TRANSACTION_CHARGE';
  25.     const BACKEND_CUSTOMER_TRANSACTION_CANCEL 'BACKEND_CUSTOMER_TRANSACTION_CANCEL';
  26.     const BACKEND_CUSTOMER_TRANSACTION_REMOVE_AUTH 'BACKEND_CUSTOMER_TRANSACTION_REMOVE_AUTH';
  27.     const BACKEND_CUSTOMER_PLAN_CHANGE_EDIT 'BACKEND_CUSTOMER_PLAN_CHANGE_EDIT';
  28.     /**
  29.      * @var Security
  30.      */
  31.     private $security;
  32.     /**
  33.      * @var Commons
  34.      */
  35.     private $commons;
  36.     /**
  37.      * @var array
  38.      */
  39.     private $permissions;
  40.     /**
  41.      * SupportVoter constructor.
  42.      * @param Security $security
  43.      * @param Commons $commons
  44.      */
  45.     public function __construct(Security $securityCommons $commons)
  46.     {
  47.         $this->security $security;
  48.         $this->commons $commons;
  49.         $this->permissions = [];
  50.     }
  51.     /**
  52.      * @param string $attribute
  53.      * @param mixed $subject
  54.      * @return bool
  55.      */
  56.     protected function supports($attribute$subject): bool
  57.     {
  58.         if (!in_array($attribute,
  59.             [
  60.                 self::BACKEND_CUSTOMER_VIEWself::BACKEND_CUSTOMER_SHOWself::BACKEND_CUSTOMER_EDIT,
  61.                 self::BACKEND_CUSTOMER_INACTIVEself::BACKEND_CUSTOMER_DELETEself::BACKEND_CUSTOMER_BILLING_STATUS,
  62.                 self::BACKEND_CUSTOMER_USERS_VIEWself::BACKEND_CUSTOMER_TRANSACTIONS_VIEWself::BACKEND_CUSTOMER_INVOICES_VIEW,
  63.                 self::BACKEND_CUSTOMER_KEYS_VIEWself::BACKEND_CUSTOMER_PLAN_CHANGES_VIEWself::BACKEND_CUSTOMER_TEAM_CHANGE,
  64.                 self::BACKEND_CUSTOMER_TRANSACTION_CANCELself::BACKEND_CUSTOMER_TRANSACTION_CHARGEself::BACKEND_CUSTOMER_TRANSACTION_REMOVE_AUTH,
  65.                 self::BACKEND_CUSTOMER_PLAN_CHANGE_EDIT
  66.             ]
  67.         )) {
  68.             return false;
  69.         }
  70.         return true;
  71.     }
  72.     /**
  73.      * @param string $attribute
  74.      * @param mixed $subject
  75.      * @param TokenInterface $token
  76.      * @return bool
  77.      */
  78.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  79.     {
  80.         /** @var User $user */
  81.         $user $token->getUser();
  82.         // if the user is anonymous, do not grant access
  83.         if (!$user instanceof UserInterface) {
  84.             return false;
  85.         }
  86.         $this->permissions $this->commons->getBackendPermissions($user);
  87.         if ($this->hasSuperUser() === true) {
  88.             return true;
  89.         }
  90.         switch ($attribute) {
  91.             case self::BACKEND_CUSTOMER_VIEW:
  92.                 return $this->canView();
  93.             case self::BACKEND_CUSTOMER_SHOW:
  94.                 return $this->canShow();
  95.             case self::BACKEND_CUSTOMER_INACTIVE:
  96.                 return $this->canInactive();
  97.             case self::BACKEND_CUSTOMER_EDIT:
  98.                 return $this->canEdit();
  99.             case self::BACKEND_CUSTOMER_DELETE:
  100.                 return $this->canDelete();
  101.             case self::BACKEND_CUSTOMER_TEAM_CHANGE:
  102.                 return $this->canTeamChange();
  103.             case self::BACKEND_CUSTOMER_BILLING_STATUS:
  104.                 return $this->canBillingAllow();
  105.             case self::BACKEND_CUSTOMER_USERS_VIEW:
  106.                 return $this->canViewUsers();
  107.             case self::BACKEND_CUSTOMER_TRANSACTIONS_VIEW:
  108.                 return $this->canViewTransactions();
  109.             case self::BACKEND_CUSTOMER_INVOICES_VIEW:
  110.                 return $this->canViewInvoices();
  111.             case self::BACKEND_CUSTOMER_KEYS_VIEW:
  112.                 return $this->canViewKeys();
  113.             case self::BACKEND_CUSTOMER_PLAN_CHANGES_VIEW:
  114.                 return $this->canViewPlanChanges();
  115.             case self::BACKEND_CUSTOMER_PLAN_CHANGE_EDIT:
  116.                 return $this->canEditPlanChange();
  117.             case self::BACKEND_CUSTOMER_TRANSACTION_CHARGE:
  118.                 return $this->canTransactionCharge();
  119.             case self::BACKEND_CUSTOMER_TRANSACTION_CANCEL:
  120.                 return $this->canTransactionCancel();
  121.             case self::BACKEND_CUSTOMER_TRANSACTION_REMOVE_AUTH:
  122.                 return $this->canRemoveAuth();
  123.         }
  124.         throw new LogicException('Invalid attribute: ' $attribute);
  125.     }
  126.     /**
  127.      * Return True if have Super View Permission else return false
  128.      * @return bool
  129.      */
  130.     private function hasSuperUser(): bool
  131.     {
  132.         if (array_key_exists('superUser'$this->permissions)) {
  133.             if (in_array('Yes'$this->permissions['superUser'])) {
  134.                 return true;
  135.             }
  136.         }
  137.         return false;
  138.     }
  139.     /**
  140.      * Return True if have View Permission else return false
  141.      * @return bool
  142.      */
  143.     private function canView(): bool
  144.     {
  145.         if (array_key_exists('customer'$this->permissions)) {
  146.             if (in_array('View'$this->permissions['customer'])) {
  147.                 return true;
  148.             }
  149.         }
  150.         return false;
  151.     }
  152.     /**
  153.      * Return True if have Show Permission else return false
  154.      * @return bool
  155.      */
  156.     private function canShow(): bool
  157.     {
  158.         if (array_key_exists('customer'$this->permissions)) {
  159.             if (in_array('Show'$this->permissions['customer'])) {
  160.                 return true;
  161.             }
  162.         }
  163.         return false;
  164.     }
  165.     /**
  166.      * Return True if have Edit Permission else return false
  167.      * @return bool
  168.      */
  169.     private function canEdit(): bool
  170.     {
  171.         if (array_key_exists('customer'$this->permissions)) {
  172.             if (in_array('Edit'$this->permissions['customer'])) {
  173.                 return true;
  174.             }
  175.         }
  176.         return false;
  177.     }
  178.     /**
  179.      * Return True if have Change Status Permission else return false
  180.      * @return bool
  181.      */
  182.     private function canInactive(): bool
  183.     {
  184.         if (array_key_exists('customer'$this->permissions)) {
  185.             if (in_array('Inactive'$this->permissions['customer'])) {
  186.                 return true;
  187.             }
  188.         }
  189.         return false;
  190.     }
  191.     /**
  192.      * Return True if have Delete Permission else return false
  193.      * @return bool
  194.      */
  195.     private function canDelete(): bool
  196.     {
  197.         if (array_key_exists('customer'$this->permissions)) {
  198.             if (in_array('Delete'$this->permissions['customer'])) {
  199.                 return true;
  200.             }
  201.         }
  202.         return false;
  203.     }
  204.     /**
  205.      * Return True if have TeamChange Permission else return false
  206.      * @return bool
  207.      */
  208.     private function canTeamChange(): bool
  209.     {
  210.         if (array_key_exists('customer'$this->permissions)) {
  211.             if (in_array('TeamChange'$this->permissions['customer'])) {
  212.                 return true;
  213.             }
  214.         }
  215.         return false;
  216.     }
  217.     /**
  218.      * Return True if have ViewUsers Permission else return false
  219.      * @return bool
  220.      */
  221.     private function canViewUsers(): bool
  222.     {
  223.         if (array_key_exists('customerAbo'$this->permissions)) {
  224.             if (in_array('ViewUsers'$this->permissions['customerAbo'])) {
  225.                 return true;
  226.             }
  227.         }
  228.         return false;
  229.     }
  230.     /**
  231.      * Return True if have ViewTransactions Permission else return false
  232.      * @return bool
  233.      */
  234.     private function canViewTransactions(): bool
  235.     {
  236.         if (array_key_exists('customerAbo'$this->permissions)) {
  237.             if (in_array('ViewTransactions'$this->permissions['customerAbo'])) {
  238.                 return true;
  239.             }
  240.         }
  241.         return false;
  242.     }
  243.     /**
  244.      * Return True if have ViewInvoices Permission else return false
  245.      * @return bool
  246.      */
  247.     private function canViewInvoices(): bool
  248.     {
  249.         if (array_key_exists('customerAbo'$this->permissions)) {
  250.             if (in_array('ViewInvoices'$this->permissions['customerAbo'])) {
  251.                 return true;
  252.             }
  253.         }
  254.         return false;
  255.     }
  256.     /**
  257.      * Return True if have ViewKeys Permission else return false
  258.      * @return bool
  259.      */
  260.     private function canViewKeys(): bool
  261.     {
  262.         if (array_key_exists('customerAbo'$this->permissions)) {
  263.             if (in_array('ViewKeys'$this->permissions['customerAbo'])) {
  264.                 return true;
  265.             }
  266.         }
  267.         return false;
  268.     }
  269.     /**
  270.      * Return True if have ViewPlanChanges Permission else return false
  271.      * @return bool
  272.      */
  273.     private function canViewPlanChanges(): bool
  274.     {
  275.         if (array_key_exists('customerAbo'$this->permissions)) {
  276.             if (in_array('ViewPlanChanges'$this->permissions['customerAbo'])) {
  277.                 return true;
  278.             }
  279.         }
  280.         return false;
  281.     }
  282.     /**
  283.      * Return True if have BillingAllow Permission else return false
  284.      * @return bool
  285.      */
  286.     private function canBillingAllow(): bool
  287.     {
  288.         if (array_key_exists('customerInvoices'$this->permissions)) {
  289.             if (in_array('BillingAllow'$this->permissions['customerInvoices'])) {
  290.                 return true;
  291.             }
  292.         }
  293.         return false;
  294.     }
  295.     /**
  296.      * Return True if have Charge Permission else return false
  297.      * @return bool
  298.      */
  299.     private function canTransactionCharge(): bool
  300.     {
  301.         if (array_key_exists('customerTransactions'$this->permissions)) {
  302.             if (in_array('Charge'$this->permissions['customerTransactions'])) {
  303.                 return true;
  304.             }
  305.         }
  306.         return false;
  307.     }
  308.     /**
  309.      * Return True if have Cancellation Permission else return false
  310.      * @return bool
  311.      */
  312.     private function canTransactionCancel(): bool
  313.     {
  314.         if (array_key_exists('customerTransactions'$this->permissions)) {
  315.             if (in_array('Cancellation'$this->permissions['customerTransactions'])) {
  316.                 return true;
  317.             }
  318.         }
  319.         return false;
  320.     }
  321.     /**
  322.      * Return True if have RemoveAuth Permission else return false
  323.      * @return bool
  324.      */
  325.     private function canRemoveAuth(): bool
  326.     {
  327.         if (array_key_exists('customerTransactions'$this->permissions)) {
  328.             if (in_array('RemoveAuth'$this->permissions['customerTransactions'])) {
  329.                 return true;
  330.             }
  331.         }
  332.         return false;
  333.     }
  334.     /**
  335.      * Return True if have Edit Permission else return false
  336.      * @return bool
  337.      */
  338.     private function canEditPlanChange(): bool
  339.     {
  340.         if (array_key_exists('customerPlanChanges'$this->permissions)) {
  341.             if (in_array('Edit'$this->permissions['customerPlanChanges'])) {
  342.                 return true;
  343.             }
  344.         }
  345.         return false;
  346.     }
  347. }