<?php
namespace App\Security\Voter;
use App\Entity\Order;
use App\Entity\User;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\Security;
use App\Repository\CompanySupplierRepository;
class OrderVoter extends Voter
{
const VIEW ="ver";
const CREATE ="crear";
const EDIT ="editar";
const ACCEPT ="aceptar";
const REJECT ="rechazar";
const ACCEPT_SUPPLIER ="aceptar proveedor";
const COMPLETE ="completar";
const DELETE = "eliminar";
const SEND = "enviar";
private $security;
private $csRepository;
public function __construct(Security $security, CompanySupplierRepository $csRepository)
{
$this->security = $security;
$this->csRepository = $csRepository;
}
protected function supports($attribute, $subject)
{
// if the attribute isn't one we support, return false
if (!in_array($attribute, [self::VIEW, self::CREATE, self::EDIT, self::ACCEPT, self::REJECT, self::ACCEPT_SUPPLIER, self::SEND, self::COMPLETE, self::DELETE])) {
return false;
}
// only vote on `Order` objects
if (!$subject instanceof Order) {
return false;
}
return true;
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
$admin = $token->getUser();
// if the user is anonymous, do not grant access
if (!$admin instanceof UserInterface) {
return false;
}
// ROLE_ADMIN can do anything! The power!
if ($this->security->isGranted('ROLE_ADMIN')) {
return true;
}
// ... (check conditions and return true to grant permission) ...
switch ($attribute) {
case self::VIEW:
return $this->canView($subject, $admin);
case self::CREATE:
return $this->canCreate($admin);
case self::EDIT:
return $this->canEdit($subject, $admin);
case self::ACCEPT:
return $this->canAccept($subject, $admin);
case self::REJECT:
return $this->canReject($subject, $admin);
case self::ACCEPT_SUPPLIER;
return $this->canAcceptSupplier($subject, $admin);
case self::SEND:
return $this->canSend($subject, $admin);
case self::COMPLETE:
return $this->canComplete($subject, $admin);
case self::DELETE:
return $this->canDelete($subject, $admin);
}
return false;
}
private function canView(Order $order, User $admin)
{
if ( in_array($admin->getRole(),['ROLE_CHIEF','ROLE_USER','ROLE_CLIENT']) ) {
return $admin->getCompany() === $order->getCompany() || $admin->getCompany() === $order->getSupplier()->getCompany();
}else if ($admin->getRole() == 'ROLE_SUPPLIER_CHIEF' || $admin->getRole() == 'ROLE_SUPPLIER'){
return $admin === $order->getSupplier()->getUser();
}else if ($admin->getRole() == 'ROLE_LOCAL_ADMIN' ){
return true;
}
return false;
}
private function canCreate(User $admin)
{
if ( in_array($admin->getRole(),['ROLE_CHIEF','ROLE_USER','ROLE_CLIENT','ROLE_LOCAL_ADMIN']) ) {
return true;
}
return false;
}
private function canEdit(Order $order, User $admin)
{
if ( $admin->getRole() == 'ROLE_CHIEF' || $admin->getRole() == 'ROLE_LOCAL_ADMIN') {
return $this->canDelete($order, $admin);
}
return false;
}
private function canAccept(Order $order, User $admin)
{
if ( $admin->getRole() == 'ROLE_CHIEF' || $admin->getRole() == 'ROLE_USER' || $admin->getRole() == 'ROLE_LOCAL_ADMIN' || $admin->getRole() == 'ROLE_CLIENT' ) {
return $admin->getCompany() === $order->getCompany();
}
return false;
}
private function canReject(Order $order, User $admin)
{
return $admin->getCompany() === $order->getCompany();
}
private function canAcceptSupplier(Order $order, User $admin)
{
if ( $admin->getRole() == 'ROLE_SUPPLIER_CHIEF' || $admin->getRole() == 'ROLE_SUPPLIER' || $admin->getRole() == 'ROLE_LOCAL_ADMIN' ) {
return $admin === $order->getSupplier()->getUser();
}
return false;
}
private function canSend(Order $order, User $admin)
{
if ( $admin->getRole() == 'ROLE_SUPPLIER_CHIEF' || $admin->getRole() == 'ROLE_SUPPLIER' || $admin->getRole() == 'ROLE_LOCAL_ADMIN' ) {
return $admin === $order->getSupplier()->getUser();
}
return false;
}
private function canComplete(Order $order, User $admin)
{
if ( $admin->getRole() == 'ROLE_CHIEF' || $admin->getRole() == 'ROLE_USER' || $admin->getRole() == 'ROLE_LOCAL_ADMIN' || $admin->getRole() == 'ROLE_CLIENT') {
return $admin->getCompany() === $order->getCompany();
}
return false;
}
private function canDelete(Order $order, User $admin)
{
if ( $order->getState() == 0 && ($admin->getRole() == 'ROLE_CHIEF' || $admin->getRole() == 'ROLE_LOCAL_ADMIN')) {
return $admin->getCompany() === $order->getCompany();
}
return false;
}
}