<?php
/**
* @license SILK SOFTWARE HOUSE SP Z O O
*/
namespace App\Security;
use App\Entity\Customer;
use App\Entity\User;
use App\Entity\UserSetting;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use LogicException;
/**
* Class CustomerVoter.
*/
class UserSettingVoter 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;
}
if (!$subject instanceof UserSetting) {
return false;
}
return true;
}
/**
* Vote on attribute.
*
* @param string $attribute
* @param UserSetting $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;
}
return $subject->getUser() && $subject->getUser()->getId() === $user->getId();
}
}