<?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\Affair;
use App\Entity\JIT\Task;
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 TaskVoter
*
* @author NOUTCHEU Blaise
*/
class TaskVoter extends Voter {
// these strings are just invented: you can use anything
const ADD = 'ROLE_JIT_TASK_ADD';
const VIEW = 'ROLE_JIT_TASK_VIEW';
const CLONE = 'ROLE_JIT_TASK_CLONE';
const EDIT = 'ROLE_JIT_TASK_EDIT';
const DELETE = 'ROLE_JIT_TASK_DELETE';
private $security;
public function __construct(Security $security) {
$this->security = $security;
}
protected function supports(string $attribute, $subject) {
// only a teacher can add task on his subject
if (in_array($attribute, [
self::ADD,
self::VIEW,
self::CLONE,
self::EDIT,
self::DELETE,
])) {
return true;
}
if (!in_array($attribute, [
// self::VIEW,
])) {
return false;
}
// only vote on `Task` objects
if (!$subject instanceof Task) {
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_TASK_MANAGE can do anything on task! The power!
if ($this->security->isGranted('ROLE_MANAGER')) {
return true;
}
switch ($attribute) {
case self::ADD:
return $this->canAdd($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);
}
throw new LogicException('This code should not be reached!');
}
private function canAdd(Affair $affair, User $user) {
return true;
}
private function canView(Task $task, User $user) {
return true;
}
private function canClone(Task $task, User $user) {
if($task->getAffair() !== null){
return $this->canAdd($task->getAffair(),$user) && $this->canView($task, $user);
}else{
return false;
}
}
private function canEdit(Task $task, User $user) {
//Uniquement le chef d'agence peut modifier une taskne
// if ($user->getMyAgencies()->contains($task->getAgency())) {
// return true;
// }
return $this->canView($task, $user);
}
private function canDelete(Task $task, User $user) {
//Uniquement le chef d'agence peut modifier une taskne
// if ($user->getMyAgencies()->contains($task->getAgency())) {
// return true;
// }
return $this->canView($task, $user);
}
}