src/Security/Voter/PlanVoter.php line 25

Open in your IDE?
  1. <?php
  2. /*
  3.  * Eventfix - UserVoter.php
  4.  * --------------------------------------------------------------------------
  5.  * Created by: mhs
  6.  * Created on: 23.5.2023
  7.  * --------------------------------------------------------------------------
  8.  * Copyright (c) 2023 | Michael Hack Software e.K. | www.mh-s.de
  9.  */
  10. namespace App\Security\Voter;
  11. use App\Entity\Plan;
  12. use App\Entity\User;
  13. use App\Entity\UserProfile;
  14. use App\Repository\EventRepository;
  15. use App\Repository\NetworkRepository;
  16. use App\Repository\UserProfileInterfaceRepository;
  17. use App\Repository\UserProfileRepository;
  18. use App\Repository\UserProfileWebsiteRepository;
  19. use App\Service\Settings;
  20. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  21. use Symfony\Component\Security\Core\Security;
  22. class PlanVoter extends \Core\Base\Voter{
  23.     const EVENT     'event';
  24.     const INTERFACE = 'interface';
  25.     const NETWORK   'network';
  26.     const PROFILE   'profile';
  27.     const WEBSITE   'website';
  28.     const ATTRIBUTES = [self::EVENTself::INTERFACE, self::NETWORKself::PROFILEself::WEBSITE];
  29.     private Settings $settings;
  30.     private EventRepository                $events;
  31.     private UserProfileInterfaceRepository $interfaces;
  32.     private NetworkRepository              $networks;
  33.     private UserProfileRepository          $profiles;
  34.     private UserProfileWebsiteRepository   $websites;
  35.     public function __construct(
  36.         Settings $settings,
  37.         EventRepository $eventsUserProfileInterfaceRepository $interfacesNetworkRepository $networksUserProfileRepository $profiles,
  38.         UserProfileWebsiteRepository $websitesSecurity $security
  39.     ) {
  40.         $this->settings  $settings;
  41.         $this->events     $events;
  42.         $this->interfaces $interfaces;
  43.         $this->networks   $networks;
  44.         $this->profiles   $profiles;
  45.         $this->websites   $websites;
  46.         parent::__construct($security);
  47.     }
  48.     // ---------------------------------------------------------------------------------------------
  49.     /**
  50.      * @inheritDoc
  51.      */
  52.     protected function supports(string $attribute$subject) {
  53.         if (!in_array($attributeself::ATTRIBUTES)) return false;
  54.         return $subject instanceof UserProfile;
  55.     }
  56.     /**
  57.      * @inheritDoc
  58.      */
  59.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token) {
  60.         $profile $subject/* @var $profile UserProfile */
  61.         $user    $profile->getUser();
  62.         // Sind die Pläne aktiv?
  63.         if (!$this->settings->getInt('plan.active')) return true;
  64.         // Administratoren
  65.         if ($user->isAdmin()) return true;
  66.         // Plan ermitteln
  67.         if (!$up $user->getActivePlan()) {
  68.             return false;
  69.         }
  70.         $plan $up->getPlan();
  71.         // Limits nach Plan ermitteln
  72.         $limit 0;
  73.         $count 0;
  74.         switch ($attribute) {
  75.             case self::EVENT:
  76.                 if ($plan->getEventMax() == Plan::UNLIMITED) return true;
  77.                 $limit $plan->getEventMax();
  78.                 $count $this->events->byUser($user)->count();
  79.                 break;
  80.             case self::INTERFACE:
  81.                 if ($plan->getInterfaceMax() == Plan::UNLIMITED) return true;
  82.                 $limit $plan->getInterfaceMax();
  83.                 $count $this->interfaces->byUser($user)->count();
  84.                 break;
  85.             case self::NETWORK:
  86.                 if ($plan->getNetworkMax() == Plan::UNLIMITED) return true;
  87.                 if ($plan->getNetworkPrice() > 0)              return true;
  88.                 $limit $plan->getNetworkMax();
  89.                 $count $this->networks->byUser($user)->count();
  90.                 break;
  91.             case self::PROFILE:
  92.                 if ($plan->getProfileMax() == Plan::UNLIMITED) return true;
  93.                 $limit $plan->getProfileMax();
  94.                 $count $this->profiles->byUser($user)->count();
  95.                 break;
  96.             case self::WEBSITE:
  97.                 if ($plan->getWebsiteMax() == Plan::UNLIMITED) return true;
  98.                 $limit $plan->getWebsiteMax();
  99.                 $count max(0$this->websites->byUser($user)->count() - 1); // TODO Workaround - momentan max 1 Webseite möglich
  100.                 break;
  101.         }
  102.         // Limits überprüfen
  103.         if ($count $limit) {
  104.             return true;
  105.         }
  106.         return false;
  107.     }
  108. }