<?php
namespace App\Security\Voter;
use App\Entity\Supplier;
use App\Entity\Company;
use App\Entity\Equipment;
use App\Entity\Material;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
use App\Entity\User;
use Symfony\Component\Security\Core\Security;
use App\Repository\CompanySupplierRepository;
use App\Service\UtilService;
class SupplierVoter extends Voter
{
const VIEW ="ver";
const EDIT ="editar";
const CONFIG ="configurar";
const DELETE = "eliminar";
const VIEW_EQ = 'ver_equipos';
const MANAGE_EQ = 'equipos';
const VIEW_MA = 'ver_materiales';
const MANAGE_MA = 'materiales';
private $security;
private $csRepository;
private $util;
public function __construct(
Security $security,
CompanySupplierRepository $csRepository,
UtilService $utilService)
{
$this->security = $security;
$this->csRepository = $csRepository;
$this->util = $utilService;
}
protected function supports($attribute, $subject)
{
// if the attribute isn't one we support, return false
if (!in_array($attribute, [
self::VIEW, self::EDIT, self::CONFIG, self::DELETE, self::VIEW_EQ, self::VIEW_MA, self::MANAGE_EQ, self::MANAGE_MA])) {
return false;
}
// only vote on `Supplier, Equipment or Material` objects
if (!$subject instanceof Supplier && !$subject instanceof Equipment && !$subject instanceof Material) {
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::EDIT:
return $this->canEdit($subject, $admin);
case self::CONFIG:
return $this->canConfig($subject, $admin);
case self::DELETE:
return $this->canDelete($subject, $admin);
case self::VIEW_EQ:
return $this->canViewEq($subject, $admin);
case self::VIEW_MA:
return $this->canViewMa($subject, $admin);
case self::MANAGE_EQ:
return $this->canManageEq($subject, $admin);
case self::MANAGE_MA:
return $this->canManageMa($subject, $admin);
}
return false;
}
private function canView(Supplier $supplier, User $admin)
{
return $this->checkCompanySupplier($admin, $supplier);
}
private function canEdit(Supplier $supplier, User $admin)
{
if ( $admin->getRole() == 'ROLE_CHIEF' || $admin->getRole() == 'ROLE_SUPPLIER_CHIEF') {
return $this->checkCompanySupplier($admin, $supplier, 1);
}
return false;
}
private function canConfig(Supplier $supplier, User $admin)
{
if ( $admin->getRole() == 'ROLE_CHIEF') {
return $this->checkCompanySupplier($admin, $supplier);
}
return false;
}
private function canDelete(Supplier $supplier, User $admin)
{
return $this->checkCompanySupplier($admin, $supplier);
}
private function checkCompanySupplier(User $admin, Supplier $supplier, $type=0){
$suppliersId = $this->csRepository->findByCompany($admin->getCompany(), $type, true);
if(in_array($supplier->getId(),$suppliersId)){
return true;
}
else{
return false;
}
}
private function canViewEq(Equipment $eq, User $user)
{
$isAllowed = $this->util->getLicenseField($user->getCompany(),'marketplace');
if($eq->getId()){
$isAllowed = $user->getCompany() === $eq->getCompany();
}
return $isAllowed;
}
private function canViewMa(Material $ma, User $user)
{
$isAllowed = $this->util->getLicenseField($user->getCompany(),'marketplace');
if($ma->getId()){
$isAllowed = $user->getCompany() === $ma->getCompany();
}
return $isAllowed;
}
private function canManageEq(Equipment $eq, User $user)
{
$isAdmin = in_array($user->getRole(),['ROLE_CHIEF','ROLE_SUPPLIER_CHIEF']);
$isAllowed = $this->util->getLicenseField($user->getCompany(),'marketplace') && $isAdmin;
if($eq->getId()){
$isAllowed = $user->getCompany() === $eq->getCompany();
}
return $isAllowed;
}
private function canManageMa(Material $ma, User $user)
{
$isAdmin = in_array($user->getRole(),['ROLE_CHIEF','ROLE_SUPPLIER_CHIEF']);
$isAllowed = $this->util->getLicenseField($user->getCompany(),'marketplace') && $isAdmin;
if($ma->getId()){
$isAllowed = $user->getCompany() === $ma->getCompany();
}
return $isAllowed;
}
}