<?php
/**
* @license SILK SOFTWARE HOUSE SP Z O O
*/
namespace App\Security;
use App\Entity\LineType;
use App\Entity\LineTypeGroup;
use App\Entity\User;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
/**
* Class LineTypeVoter.
*/
class LineTypeVoter extends Voter
{
const SHOW = 'show';
const NEW = 'new';
const EDIT = 'edit';
const DELETE = 'delete';
/**
* Supports.
*
* @param string $attribute
* @param object $subject
*
* @return bool
*/
protected function supports($attribute, $subject)
{
// if the attribute isn't one we support, return false
if (!in_array($attribute, [self::SHOW, self::NEW, self::EDIT, self::DELETE])) {
return false;
}
return $subject instanceof LineType;
}
/**
* Vote on attribute.
*
* @param string $attribute
* @param LineType $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;
}
if (!$subject->getId() && self::NEW === $attribute) {
return true;
}
return $subject->getLineTypeGroup() && $user->getActiveCustomer()
&& $subject->getLineTypeGroup()->getCustomer()
&& $subject->getLineTypeGroup()->getCustomer()->getId() === $user->getActiveCustomer()->getId();
}
}