<?php
namespace App\Security\Voter;
use App\Entity\Gauge\GaugeReading;
use App\Entity\User;
use App\Repository\Gauge\GaugeReadingRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
class GaugeReadingVoter extends Voter
{
// these strings are just invented: you can use anything
const EDIT = 'edit';
const DELETE = 'delete';
/**
* @var Security
*/
private Security $security;
/**
* @var EntityManagerInterface
*/
private EntityManagerInterface $entityManager;
public function __construct(Security $security, EntityManagerInterface $entityManager)
{
$this->security = $security;
$this->entityManager = $entityManager;
}
protected function supports(string $attribute, $subject): bool
{
// if the attribute isn't one we support, return false
if (!in_array($attribute, [self::EDIT, self::DELETE], true)) {
return false;
}
// only vote on `GaugeReading` objects
if (!$subject instanceof GaugeReading) {
return false;
}
return true;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$loggedUser = $token->getUser();
if (!$loggedUser instanceof User) {
// the user must be logged in; if not, deny access
return false;
}
// you know $subject is a Post object, thanks to `supports()`
/** @var GaugeReading $targetGauge */
$targetGaugeReading = $subject;
switch ($attribute) {
case self::EDIT:
return $this->canEdit($targetGaugeReading, $loggedUser);
case self::DELETE:
return $this->canDelete($targetGaugeReading, $loggedUser);
}
throw new \LogicException('This code should not be reached!');
}
/**
* @param GaugeReading $targetGaugeReading
* @param User $loggedUser
* @return bool
*/
private function canEdit(GaugeReading $targetGaugeReading, User $loggedUser): bool
{
if (!$this->security->isGranted('view', $targetGaugeReading->getGauge())) {
return false;
}
if ($this->security->isGranted('ROLE_CAN_EDIT_ALL_GAUGE_READINGS')) {
return true;
}
if ($this->isLast($targetGaugeReading) && $this->security->isGranted('ROLE_CAN_EDIT_LAST_GAUGE_READINGS')) {
return true;
}
return false;
}
/**
* @param GaugeReading $targetGaugeReading
* @param User $loggedUser
* @return bool
*/
private function canDelete(GaugeReading $targetGaugeReading, User $loggedUser): bool
{
if (!$this->security->isGranted('view', $targetGaugeReading->getGauge())) {
return false;
}
if ($this->security->isGranted('ROLE_CAN_DELETE_ALL_GAUGE_READINGS')) {
return true;
}
if ($this->isLast($targetGaugeReading) && $this->security->isGranted('ROLE_CAN_DELETE_LAST_GAUGE_READINGS')) {
return true;
}
return false;
}
/**
* @param GaugeReading $targetGaugeReading
* @return bool
*/
private function isLast(GaugeReading $targetGaugeReading): bool
{
/** @var GaugeReadingRepository $grRepository */
$grRepository = $this->entityManager->getRepository(GaugeReading::class);
$lastGr = $grRepository->getLastForGauge($targetGaugeReading->getGauge());
if ($lastGr !== null && $lastGr->getId() === $targetGaugeReading->getId()) {
return true;
}
return false;
}
}