mirror of
https://github.com/quentin-g-dev/afup.git
synced 2026-03-26 02:02:15 +01:00
61 lines
1.4 KiB
PHP
61 lines
1.4 KiB
PHP
<?php
|
|
|
|
|
|
namespace AppBundle\Security;
|
|
|
|
use AppBundle\Event\Model\GithubUser;
|
|
use AppBundle\Event\Model\Repository\SpeakerRepository;
|
|
use AppBundle\Event\Model\Speaker;
|
|
use AppBundle\Event\Model\Talk;
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
|
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
|
|
|
|
class TalkVoter extends Voter
|
|
{
|
|
/**
|
|
* @var SpeakerRepository
|
|
*/
|
|
private $speakerRepository;
|
|
|
|
public function __construct(SpeakerRepository $speakerRepository)
|
|
{
|
|
$this->speakerRepository = $speakerRepository;
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
protected function supports($attribute, $subject)
|
|
{
|
|
if ($attribute !== 'edit' || !$subject instanceof Talk) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
|
|
{
|
|
$user = $token->getUser();
|
|
if (!$user instanceof GithubUser) {
|
|
return false;
|
|
}
|
|
|
|
$speakers = $this->speakerRepository->getSpeakersByTalk($subject);
|
|
|
|
// All speakers associated to a talk can edit the talk
|
|
foreach ($speakers as $speaker) {
|
|
/**
|
|
* @var $speaker Speaker
|
|
*/
|
|
if ($speaker->getUser() === $user->getId()) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
}
|