<?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\Service;
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 ServiceVoter
*
* @author NOUTCHEU Blaise
*/
class ServiceVoter extends Voter {
// these strings are just invented: you can use anything
// const LIST = 'ROLE_JIT_SERVICE_INDEX';
const LIST = 'ROLE_JIT_SERVICE_INDEX';
const ADD = 'ROLE_JIT_SERVICE_ADD';
const ADDSS = 'ROLE_JIT_SERVICE_ADDSS';
const VIEW = 'ROLE_JIT_SERVICE_VIEW';
const CLONE = 'ROLE_JIT_SERVICE_CLONE';
const EDIT = 'ROLE_JIT_SERVICE_EDIT';
const DELETE = 'ROLE_JIT_SERVICE_DELETE';
private $security;
public function __construct(Security $security) {
$this->security = $security;
}
protected function supports(string $attribute, $subject) {
// only a teacher can add service on his subject
if (in_array($attribute, [
self::LIST,
self::ADD,
self::VIEW,
self::CLONE,
self::EDIT,
self::DELETE,
])) {
return true;
}
if (!in_array($attribute, [
// self::VIEW,
])) {
return false;
}
// only vote on `Service` objects
if (!$subject instanceof Service) {
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_SERVICE_MANAGE can do anything on service! The power!
if ($this->security->isGranted('ROLE_MANAGER')) {
return true;
}
switch ($attribute) {
case self::LIST:
return $this->canList($user);
case self::ADD:
return $this->canAdd($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);
}
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 canView(Service $service, User $user) {
return true;
}
private function canClone(Service $service, User $user) {
return $this->canAdd($user) && $this->canView($service, $user);
}
private function canEdit(Service $service, User $user) {
//Uniquement le chef d'agence peut modifier une servicene
// if ($user->getMyAgencies()->contains($service->getService())) {
// return true;
// }
return $this->canView($service, $user);
}
private function canDelete(Service $service, User $user) {
//Uniquement le chef d'agence peut modifier une servicene
// if ($user->getMyAgencies()->contains($service->getService())) {
// return true;
// }
return $this->canView($service, $user);
}
}