<?php
/*
* Eventfix - UserVoter.php
* --------------------------------------------------------------------------
* Created by: mhs
* Created on: 23.5.2023
* --------------------------------------------------------------------------
* Copyright (c) 2023 | Michael Hack Software e.K. | www.mh-s.de
*/
namespace App\Security\Voter;
use App\Entity\Plan;
use App\Entity\User;
use App\Entity\UserProfile;
use App\Repository\EventRepository;
use App\Repository\NetworkRepository;
use App\Repository\UserProfileInterfaceRepository;
use App\Repository\UserProfileRepository;
use App\Repository\UserProfileWebsiteRepository;
use App\Service\Settings;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Security;
class PlanVoter extends \Core\Base\Voter{
const EVENT = 'event';
const INTERFACE = 'interface';
const NETWORK = 'network';
const PROFILE = 'profile';
const WEBSITE = 'website';
const ATTRIBUTES = [self::EVENT, self::INTERFACE, self::NETWORK, self::PROFILE, self::WEBSITE];
private Settings $settings;
private EventRepository $events;
private UserProfileInterfaceRepository $interfaces;
private NetworkRepository $networks;
private UserProfileRepository $profiles;
private UserProfileWebsiteRepository $websites;
public function __construct(
Settings $settings,
EventRepository $events, UserProfileInterfaceRepository $interfaces, NetworkRepository $networks, UserProfileRepository $profiles,
UserProfileWebsiteRepository $websites, Security $security
) {
$this->settings = $settings;
$this->events = $events;
$this->interfaces = $interfaces;
$this->networks = $networks;
$this->profiles = $profiles;
$this->websites = $websites;
parent::__construct($security);
}
// ---------------------------------------------------------------------------------------------
/**
* @inheritDoc
*/
protected function supports(string $attribute, $subject) {
if (!in_array($attribute, self::ATTRIBUTES)) return false;
return $subject instanceof UserProfile;
}
/**
* @inheritDoc
*/
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token) {
$profile = $subject; /* @var $profile UserProfile */
$user = $profile->getUser();
// Sind die Pläne aktiv?
if (!$this->settings->getInt('plan.active')) return true;
// Administratoren
if ($user->isAdmin()) return true;
// Plan ermitteln
if (!$up = $user->getActivePlan()) {
return false;
}
$plan = $up->getPlan();
// Limits nach Plan ermitteln
$limit = 0;
$count = 0;
switch ($attribute) {
case self::EVENT:
if ($plan->getEventMax() == Plan::UNLIMITED) return true;
$limit = $plan->getEventMax();
$count = $this->events->byUser($user)->count();
break;
case self::INTERFACE:
if ($plan->getInterfaceMax() == Plan::UNLIMITED) return true;
$limit = $plan->getInterfaceMax();
$count = $this->interfaces->byUser($user)->count();
break;
case self::NETWORK:
if ($plan->getNetworkMax() == Plan::UNLIMITED) return true;
if ($plan->getNetworkPrice() > 0) return true;
$limit = $plan->getNetworkMax();
$count = $this->networks->byUser($user)->count();
break;
case self::PROFILE:
if ($plan->getProfileMax() == Plan::UNLIMITED) return true;
$limit = $plan->getProfileMax();
$count = $this->profiles->byUser($user)->count();
break;
case self::WEBSITE:
if ($plan->getWebsiteMax() == Plan::UNLIMITED) return true;
$limit = $plan->getWebsiteMax();
$count = max(0, $this->websites->byUser($user)->count() - 1); // TODO Workaround - momentan max 1 Webseite möglich
break;
}
// Limits überprüfen
if ($count < $limit) {
return true;
}
return false;
}
}