<?php declare(strict_types=1);
namespace App\Security\Voter;
use App\Entity\MachinePurposeDictionary;
use App\Entity\User;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class MachinePurposeDictionaryVoter extends Voter
{
public const VIEW = 'view';
public const EDIT = 'edit';
public const DELETE = 'delete';
/**
* @param string $attribute
* @param mixed $subject
*
* @return bool
*/
protected function supports($attribute, $subject): bool
{
$supports = true;
if (!\in_array($attribute, [self::VIEW, self::EDIT, self::DELETE], true)) {
$supports = false;
}
if (!$subject instanceof MachinePurposeDictionary) {
$supports = false;
}
return $supports;
}
/**
* @param string $attribute
* @param mixed $subject
* @param TokenInterface $token
*
* @return bool
*/
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
$hasVotePassed = false;
if (!$user instanceof User) {
return $hasVotePassed;
}
/** @var MachinePurposeDictionary $entity */
$entity = $subject;
switch ($attribute) {
case self::VIEW:
$hasVotePassed = $this->canView($entity);
break;
case self::EDIT:
$hasVotePassed = $this->canEdit($entity);
break;
case self::DELETE:
$hasVotePassed = $this->canDelete($entity);
break;
}
return $hasVotePassed;
}
/**
* @param MachinePurposeDictionary $entity
*
* @return bool
*/
private function canView(MachinePurposeDictionary $entity): bool
{
// TODO Wykonać dostępy na podstawie roli użytkownika
// Admin i Projekt widzi i może robić wszystko
// Default/użytkownik może widzieć element i detale
return true;
}
/**
* @param MachinePurposeDictionary $toolFamily
*
* @return bool
*/
private function canEdit(MachinePurposeDictionary $toolFamily): bool
{
// TODO Wykonać dostępy na podstawie roli użytkownika
// Admin i Projekt widzi i może robić wszystko
// Default/użytkownik może widzieć element i detale
return true;
}
/**
* @param MachinePurposeDictionary $toolFamily
*
* @return bool
*/
private function canArchive(MachinePurposeDictionary $toolFamily): bool
{
// TODO Wykonać dostępy na podstawie roli użytkownika
// Admin i Projekt widzi i może robić wszystko
// Default/użytkownik może widzieć element i detale
return true;
}
/**
* @param MachinePurposeDictionary $toolFamily
*
* @return bool
*/
private function canDelete(MachinePurposeDictionary $toolFamily): bool
{
// TODO Wykonać dostępy na podstawie roli użytkownika
// Admin i Projekt widzi i może robić wszystko
// Default/użytkownik może widzieć element i detale
return true;
}
}