<?php
/**
* @license SILK SOFTWARE HOUSE SP Z O O
*/
namespace App\Security;
use App\Entity\Customer;
use App\Entity\License;
use App\Entity\Measurement;
use App\Entity\User;
use App\Model\View\MeasurementView;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
/**
* Class MeasurementViewVoter.
*/
class MeasurementViewVoter extends Voter
{
const SHOW = 'show';
/**
* Supports.
*
* @param string $attribute
* @param License $subject
*
* @return bool
*/
protected function supports($attribute, $subject)
{
// if the attribute isn't one we support, return false
if (!in_array($attribute, [self::SHOW])) {
return false;
}
if (!$subject instanceof MeasurementView) {
return false;
}
return true;
}
/**
* Vote on attribute.
*
* @param string $attribute
* @param MeasurementView $subject
* @param TokenInterface $token
*
* @return bool
*/
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
$user = $token->getUser();
if (!$user instanceof User) {
// the user must be logged in; if not, deny access
return false;
}
if ($user->hasRole('ROLE_ADMIN')) {
return true;
}
return $user->getActiveCustomer() && $user->getActiveCustomer()->getId() === $subject->getCustomer()->getId();
}
}