<?php
namespace App\Security\Voter\Purchase;
use App\Entity\Purchase\PurchaseReception;
use App\Entity\Security\User;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
class PurchaseReceptionVoter extends Voter
{
private $session;
public function __construct(SessionInterface $session)
{
$this->session = $session;
}
protected function supports($attribute, $subject)
{
// replace with your own logic
// https://symfony.com/doc/current/security/voters.html
if (!in_array($attribute, [
'ROLE_PURCHASE_RECEPTION_CREATE',
'ROLE_PURCHASE_RECEPTION_VIEW',
'ROLE_PURCHASE_RECEPTION_EDIT',
'ROLE_PURCHASE_RECEPTION_DELETE',
'ROLE_PURCHASE_RECEPTION_ENABLE',
'ROLE_PURCHASE_RECEPTION_DISABLE',
'ROLE_PURCHASE_RECEPTION_ARCHIVE',
'ROLE_PURCHASE_RECEPTION_NOT_ARCHIVE',
'ROLE_PURCHASE_RECEPTION_DUPLICATE',
'ROLE_PURCHASE_RECEPTION_VALIDATE',
'ROLE_PURCHASE_RECEPTION_REJECT',
'ROLE_PURCHASE_RECEPTION_REVERSE'])) {
return false;
}
// only vote on `Activity` objects
if (!$subject instanceof PurchaseReception) {
return false;
}
return true;
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
$user = $token->getUser();
// if the user is anonymous, do not grant access
if (!$user instanceof UserInterface) {
return false;
}
// ... (check conditions and return true to grant permission) ...
switch ($attribute) {
case 'ROLE_PURCHASE_RECEPTION_CREATE':
return $this->canCreate();
break;
case 'ROLE_PURCHASE_RECEPTION_VIEW':
return $this->canView();
break;
case 'ROLE_PURCHASE_RECEPTION_EDIT':
return $this->canEdit($subject, $user);
break;
case 'ROLE_PURCHASE_RECEPTION_DELETE':
return $this->canDelete($subject, $user);
break;
case 'ROLE_PURCHASE_RECEPTION_ENABLE':
return $this->canEnable($subject, $user);
break;
case 'ROLE_PURCHASE_RECEPTION_DISABLE':
return $this->canDisable($subject, $user);
break;
case 'ROLE_PURCHASE_RECEPTION_ARCHIVE':
return $this->canArchive($subject, $user);
break;
case 'ROLE_PURCHASE_RECEPTION_NOT_ARCHIVE':
return $this->canNotArchive($subject, $user);
break;
case 'ROLE_PURCHASE_RECEPTION_VALIDATE':
return $this->canValidate($subject, $user);
break;
case 'ROLE_PURCHASE_RECEPTION_DUPLICATE':
return $this->canDuplicate($subject, $user);
break;
case 'ROLE_PURCHASE_RECEPTION_REJECT':
return $this->canReject($subject, $user);
break;
case 'ROLE_PURCHASE_RECEPTION_REVERSE':
return $this->canReverse($subject, $user);
break;
}
return false;
}
private function canCreate()
{
if (in_array('ROLE_PURCHASE_RECEPTION_CREATE', $this->session->get('privileges'))) {
return true;
}
return false;
}
private function canView()
{
if (in_array('ROLE_PURCHASE_RECEPTION_VIEW', $this->session->get('privileges'))) {
return true;
}
return false;
}
private function canEdit(PurchaseReception $purchaseReception, User $user)
{
if (in_array('ROLE_PURCHASE_RECEPTION_EDIT', $this->session->get('privileges'))) {
return true;
}
return false;
}
private function canDelete(PurchaseReception $purchaseReception, User $user)
{
if (in_array('ROLE_PURCHASE_RECEPTION_DELETE', $this->session->get('privileges'))) {
return true;
}
return false;
}
private function canEnable(PurchaseReception $purchaseReception, User $user)
{
if (in_array('ROLE_PURCHASE_RECEPTION_ENABLE', $this->session->get('privileges'))) {
return true;
}
return false;
}
private function canDisable(PurchaseReception $purchaseReception, User $user)
{
if (in_array('ROLE_PURCHASE_RECEPTION_DISABLE', $this->session->get('privileges'))) {
return true;
}
return false;
}
private function canArchive(PurchaseReception $purchaseReception, User $user)
{
if (in_array('ROLE_PURCHASE_RECEPTION_ARCHIVE', $this->session->get('privileges'))) {
return true;
}
return false;
}
private function canNotArchive(PurchaseReception $purchaseReception, User $user)
{
if (in_array('ROLE_PURCHASE_RECEPTION_NOT_ARCHIVE', $this->session->get('privileges'))) {
return true;
}
return false;
}
private function canValidate(PurchaseReception $purchaseReception, User $user)
{
if (in_array('ROLE_PURCHASE_RECEPTION_VALIDATE', $this->session->get('privileges'))) {
return true;
}
return false;
}
private function canDuplicate(PurchaseReception $purchaseReception, User $user)
{
if (in_array('ROLE_PURCHASE_RECEPTION_DUPLICATE', $this->session->get('privileges'))) {
return true;
}
return false;
}
private function canReject(PurchaseReception $purchaseReception, User $user)
{
if (in_array('ROLE_PURCHASE_RECEPTION_REJECT', $this->session->get('privileges'))) {
return true;
}
return false;
}
private function canReverse(PurchaseReception $purchaseReception, User $user)
{
if (in_array('ROLE_PURCHASE_RECEPTION_REVERSE', $this->session->get('privileges'))) {
return true;
}
return false;
}
}