<?php
namespace App\V4\Voters;
use App\Security\User;
use App\V4\Model\Contact\Contact;
use App\V4\Model\Security\UserInfo;
use LogicException;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
class ContactVoter extends Voter
{
const CONTACT_MANAGE_LIST = 'contact_manage_list';
const CONTACT_SHOW_LIST = 'contact_show_list';
const CONTACT_ADD_EDIT = 'contact_add_edit';
const CONTACT_MANAGE_FIELDS = 'contact_manage_fields';
const CONTACT_STATE_ADD_EDIT = 'contact_state_add_edit';
const CONTACT_MOVE = 'contact_move';
/**
* @var Security
*/
private $security;
/**
* @param Security $security
*/
public function __construct(Security $security)
{
$this->security = $security;
}
/**
* @param $attribute
* @param $subject
*
* @return bool
*/
protected function supports($attribute, $subject): bool
{
if ($subject instanceof Contact && in_array($attribute, [
self::CONTACT_MOVE,
], true)) {
return true;
}
return in_array($attribute, [
self::CONTACT_MANAGE_LIST,
self::CONTACT_SHOW_LIST,
self::CONTACT_ADD_EDIT,
self::CONTACT_MANAGE_FIELDS,
self::CONTACT_STATE_ADD_EDIT,
], true);
}
/**
* @param $attribute
* @param $subject
* @param TokenInterface $token
*
* @return bool
*
* @throws LogicException
*/
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof User) {
return false;
}
switch ($attribute) {
case self::CONTACT_MANAGE_LIST:
return $this->canContactManageList();
case self::CONTACT_SHOW_LIST:
return $this->canContactShowList();
case self::CONTACT_ADD_EDIT:
return $this->canContactAddEdit();
case self::CONTACT_MANAGE_FIELDS:
return $this->canContactManageFields();
case self::CONTACT_STATE_ADD_EDIT:
return $this->canContactStateAddEdit();
case self::CONTACT_MOVE:
return $this->canContactMove($subject, $user);
}
throw new LogicException('This should never happen');
}
/**
* @return bool
*/
private function canContactManageList(): bool
{
return $this->canAccessContact();
}
/**
* @return bool
*/
private function canContactShowList(): bool
{
return $this->canAccessContact();
}
/**
* @return bool
*/
private function canContactAddEdit(): bool
{
return $this->canAccessContact();
}
/**
* @return bool
*/
private function canContactManageFields(): bool
{
return $this->canAccessContact();
}
/**
* @return bool
*/
private function canContactStateAddEdit(): bool
{
return $this->canAccessContact();
}
/**
* @param Contact $subject
* @param UserInfo $user
*
* @return bool
*/
private function canContactMove(Contact $subject, User $user): bool
{
if ($subject->getCustomerId() !== $user->getCustomerId()) {
return false;
}
return $this->canAccessContact();
}
/**
* @TODO: Si le client n'a pas le droit "contact show", il peut tout de même voir les contacts rattachés aux prospects qu'il gère.
*
* @return bool
*/
private function canAccessContact(): bool
{
return true;
}
}