<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
namespace App\Security\Voter;
use App\Entity\JIT\Charge;
use App\Entity\JIT\Incident;
use App\Entity\JIT\Invoice;
use App\Entity\JIT\Tenancy;
use App\Entity\Security\User;
use LogicException;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
/**
* Description of InvoiceVoter
*
* @author NOUTCHEU Blaise
*/
class InvoiceVoter extends Voter {
// these strings are just invented: you can use anything
// const LIST = 'ROLE_JIT_INVOICE_INDEX';
const LIST = 'ROLE_JIT_INVOICE_INDEX';
const ALL = 'ROLE_JIT_INVOICE_ALL';
const LUNPAY = 'ROLE_JIT_INVOICE_LUNPAY';
const LVALIDATE = 'ROLE_JIT_INVOICE_LVALIDATE';
const LPAY = 'ROLE_JIT_INVOICE_LPAY';
const ADD = 'ROLE_JIT_INVOICE_ADD';
const ADDBI = 'ROLE_JIT_INVOICE_ADDBI';
const VIEW = 'ROLE_JIT_INVOICE_VIEW';
const CLONE = 'ROLE_JIT_INVOICE_CLONE';
const EDIT = 'ROLE_JIT_INVOICE_EDIT';
const DELETE = 'ROLE_JIT_INVOICE_DELETE';
const PAY = 'ROLE_JIT_INVOICE_PAY';
const VALIDATE = 'ROLE_JIT_INVOICE_VALIDATE';
const GENERATE = 'ROLE_JIT_INVOICE_GENERATE';
const GENERATE2 = 'ROLE_JIT_INVOICE_GENERATE2';
private $security;
public function __construct(Security $security) {
$this->security = $security;
}
protected function supports(string $attribute, $subject) {
// only a teacher can add invoice on his subject
if (in_array($attribute, [
self::LIST,
self::ALL,
self::LUNPAY,
self::LVALIDATE,
self::LPAY,
self::ADD,
self::ADDBI,
self::VIEW,
self::CLONE,
self::EDIT,
self::PAY,
self::VALIDATE,
self::GENERATE,
self::GENERATE2,
self::DELETE,
])) {
return true;
}
if (!in_array($attribute, [
// self::VIEW,
])) {
return false;
}
// only vote on `Invoice` objects
if (!$subject instanceof Invoice) {
return false;
}
return true;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token) {
$user = $token->getUser();
if (!$user instanceof User) {
// the user must be logged in; if not, deny access
return false;
}
// ROLE_JIT_INVOICE_MANAGE can do anything on invoice! The power!
if ($this->security->isGranted('ROLE_MANAGER')) {
return true;
}
switch ($attribute) {
case self::ALL:
case self::LIST:
case self::LUNPAY:
return true;
case self::LVALIDATE:
case self::LPAY:
return $this->canList($user);
case self::ADD:
return $this->canAdd($user);
case self::ADDBI:
return $this->canAddBI($subject, $user);
case self::VIEW:
return $this->canView($subject, $user);
case self::CLONE:
return $this->canClone($subject, $user);
case self::EDIT:
return $this->canEdit($subject, $user);
case self::DELETE:
return $this->canDelete($subject, $user);
case self::PAY:
return $this->canPay($subject, $user);
case self::VALIDATE:
return $this->canValidate($subject, $user);
case self::GENERATE:
return $this->canGenerate($subject, $user);
case self::GENERATE2:
return $this->canGenerate2($subject, $user);
}
throw new LogicException('This code should not be reached!');
}
private function canList(User $user) {
return true;
}
private function canAdd(User $user) {
return true;
}
private function canAddBI(Incident $incident, User $user) {
return true;
}
private function canView(Invoice $invoice, User $user) {
return true;
}
private function canClone(Invoice $invoice, User $user) {
return $this->canAdd($user) && $this->canView($invoice, $user);
}
private function canEdit(Invoice $invoice, User $user) {
//Uniquement le chef d'agence peut modifier une invoicene
// if ($user->getMyAgencies()->contains($invoice->getInvoice())) {
// return true;
// }
return $this->canView($invoice, $user);
}
private function canDelete(Invoice $invoice, User $user) {
//Uniquement le chef d'agence peut modifier une invoicene
// if ($user->getMyAgencies()->contains($invoice->getInvoice())) {
// return true;
// }
return $this->canView($invoice, $user);
}
private function canPay(Invoice $invoice, User $user) {
return $this->canView($invoice, $user);
}
private function canValidate(Invoice $invoice, User $user) {
return $this->canView($invoice, $user);
}
private function canGenerate(Tenancy $tenancy, User $user) {
// return $this->canView($tenancy, $user);
return true;
}
private function canGenerate2(Charge $charge, User $user) {
// return $this->canView($invoice, $user);
return true;
}
}