src/Security/Voter/PlanPricingVoter.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\DeviceGroup;
  4. use App\Entity\Plan;
  5. use App\Entity\User;
  6. use App\Entity\Workspace;
  7. use App\Repository\DeviceGroupRepository;
  8. use App\Repository\UserRepository;
  9. use App\Utils\Commons;
  10. use Symfony\Component\Security\Core\Security;
  11. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  12. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  13. class PlanPricingVoter extends Voter
  14. {
  15.     /**
  16.      * @var UserRepository
  17.      */
  18.     private $userRepository;
  19.     /**
  20.      * @var DeviceGroupRepository
  21.      */
  22.     private $deviceGroupRepository;
  23.     /**
  24.      * @var Commons
  25.      */
  26.     private $commons;
  27.     /**
  28.      * @var array
  29.      */
  30.     private $permissions;
  31.     private $security;
  32.     /**
  33.      * Constructor.
  34.      * @param UserRepository $userRepository
  35.      * @param Security $security
  36.      */
  37.     public function __construct(
  38.         Commons $commons,
  39.         UserRepository $userRepository,
  40.         DeviceGroupRepository $deviceGroupRepository,
  41.         Security $security
  42.     )
  43.     {
  44.         $this->permissions = [];
  45.         $this->commons $commons;
  46.         $this->security $security;
  47.         $this->userRepository $userRepository;
  48.         $this->deviceGroupRepository $deviceGroupRepository;
  49.     }
  50.     // these strings are just invented: you can use anything
  51.     const VIEW_PLAN_USER_GROUP 'VIEW_PLAN_USER_GROUP';
  52.     const VIEW_PLAN_SANDBOX_MODE 'VIEW_PLAN_SANDBOX_MODE';
  53.     const VIEW_PLAN_DATA_EXPORT 'VIEW_PLAN_DATA_EXPORT';
  54.     const VIEW_PLAN_2FA_AUTH 'VIEW_PLAN_2FA_AUTH';
  55.     const VIEW_PLAN_INDIVIDUAL_DEVICE_FIELDS 'VIEW_PLAN_INDIVIDUAL_DEVICE_FIELDS';
  56.     const VIEW_PLAN_SCHEDULED_ROLLOUTS 'VIEW_PLAN_SCHEDULED_ROLLOUTS';
  57.     const VIEW_PLAN_BLOCKCHAIN_DEVICE_LOG 'VIEW_PLAN_BLOCKCHAIN_DEVICE_LOG';
  58.     const VIEW_PLAN_NAMED_USERS 'VIEW_PLAN_NAMED_USERS';
  59.     const VIEW_PLAN_MAPBOX 'VIEW_PLAN_MAPBOX';
  60.     protected function supports($attribute$subject)
  61.     {
  62.         // if the attribute isn't one we support, return false
  63.         if (!in_array($attribute, [
  64.                 self::VIEW_PLAN_USER_GROUP,
  65.                 self::VIEW_PLAN_SANDBOX_MODE,
  66.                 self::VIEW_PLAN_DATA_EXPORT,
  67.                 self::VIEW_PLAN_2FA_AUTH,
  68.                 self::VIEW_PLAN_INDIVIDUAL_DEVICE_FIELDS,
  69.                 self::VIEW_PLAN_SCHEDULED_ROLLOUTS,
  70.                 self::VIEW_PLAN_BLOCKCHAIN_DEVICE_LOG,
  71.                 self::VIEW_PLAN_NAMED_USERS,
  72.                 self::VIEW_PLAN_MAPBOX,
  73.         ])) {
  74.             return false;
  75.         }
  76.         // only vote on `User` objects
  77. //        if (!$subject instanceof User) {
  78. //            return false;
  79. //        }
  80.         return true;
  81.     }
  82.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  83.     {
  84.         if ($this->security->isGranted('ROLE_BACKEND_USER')) {
  85.             return true;
  86.         }
  87.         $user $token->getUser();
  88.         $this->permissions $this->commons->getUserPermissions($user);
  89.         if (!$user instanceof User) {
  90.             // the user must be logged in; if not, deny access
  91.             return false;
  92.         }
  93.         $plan $token->getUser()->getWorkSpace()->getPlan();
  94.         switch ($attribute) {
  95.             case self::VIEW_PLAN_USER_GROUP:
  96.                 return $this->canViewUserGroup($plan);
  97.             case self::VIEW_PLAN_SANDBOX_MODE:
  98.                 return $this->canViewSandBoxMode($plan);
  99.             case self::VIEW_PLAN_DATA_EXPORT:
  100.                 return $this->canViewDataExport($plan);
  101.             case self::VIEW_PLAN_2FA_AUTH:
  102.                 return $this->canView2FAAuth($plan);
  103.             case self::VIEW_PLAN_INDIVIDUAL_DEVICE_FIELDS:
  104.                 return $this->canViewIndividualDeviceField($plan);
  105.             case self::VIEW_PLAN_SCHEDULED_ROLLOUTS:
  106.                 return $this->canViewScheduledRollouts($plan);
  107.             case self::VIEW_PLAN_BLOCKCHAIN_DEVICE_LOG:
  108.                 return $this->canViewBlockchainDeviceLog($plan);
  109.             case self::VIEW_PLAN_NAMED_USERS:
  110.                 return $this->canViewNamedUsers($user->getWorkspace(), $user);
  111.             case self::VIEW_PLAN_MAPBOX:
  112.                 return $this->canViewMapbox($plan);
  113. ;
  114.         }
  115.         throw new \LogicException('This code should not be reached!');
  116.     }
  117.     private function canViewUserGroup(Plan $plan)
  118.     {
  119.         return $plan->getUserGroups();
  120.     }
  121.     private function canViewSandBoxMode(Plan $plan)
  122.     {
  123.         return $plan->getSandboxMode();
  124.     }
  125.     private function canViewDataExport(Plan $plan)
  126.     {
  127.         return $plan->getDataExport();
  128.     }
  129.     private function canView2FAAuth(Plan $plan)
  130.     {
  131.         return $plan->getAuth2fa();
  132.     }
  133.     private function canViewIndividualDeviceField(Plan $plan)
  134.     {
  135.         return $plan->getIndividualDeviceFields();
  136.     }
  137.     private function canViewScheduledRollouts(Plan $plan)
  138.     {
  139.         return $plan->getScheduledRollouts();
  140.     }
  141.     private function canViewBlockchainDeviceLog(Plan $plan)
  142.     {
  143.         return $plan->getBlockchainDeviceLog();
  144.     }
  145.     private function canViewNamedUsers(Workspace $workspaceUser $user)
  146.     {
  147.         $users $this->userRepository->findAllUsersByRole($user->getId(), $workspace->getId())->getQuery()->getArrayResult();
  148.         $flag false;
  149.         if(count($users) < $workspace->getPlan()->getNamedUsers()){
  150.             $flag true;
  151.         }
  152.         return $flag;
  153.     }
  154.     private function canViewMapbox(Plan $plan)
  155.     {
  156. //        if (!$plan->getMapBox()) {
  157. //            return false;
  158. //        }
  159. //
  160. //        if ($this->security->isGranted('ROLE_ADMIN')) {
  161. //            return true;
  162. //        }
  163. //
  164. //        if (array_key_exists('deviceGroup', $this->permissions)) {
  165. //            if (in_array('Edit', $this->permissions['deviceGroup'])) {
  166. //                return true;
  167. //            }
  168. //        }
  169.         return $plan->getMapBox();
  170.     }
  171.     /**
  172.      * @param User|null $user
  173.      * @param $subject
  174.      * @return bool
  175.      */
  176.     private function isOwner(User $user$subject): bool
  177.     {
  178.         if (!is_object($subject)) {
  179.             $subject $this->deviceGroupRepository->findOneBy(["id" => intval($subject)]);
  180.         }
  181.         if (!$subject instanceof DeviceGroup) {
  182.             return false;
  183.         }
  184.         return $user->getWorkspace()->getId() === $subject->getWorkspace()->getId();
  185.     }
  186.     /** Return True if have Edit Permission else return false
  187.      * @return bool
  188.      */
  189.     private function canEdit(): bool
  190.     {
  191.     }
  192. }