Lines 100.00% 4 / 4
Functions and Methods 100.00% 2 / 2
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
TimelineEventVoter 100.00% 4 / 4 100.00% 2 / 2 4 100.00% 1 / 1
 supports 100.00% 1 / 1 100.00% 1 / 1 2
 voteOnAttribute 100.00% 3 / 3 100.00% 1 / 1 2
1<?php
2
3declare(strict_types=1);
4
5namespace App\Security\Voter;
6
7use App\Entity\TimelineEvent;
8use App\Entity\User;
9use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
10use Symfony\Component\Security\Core\Authorization\Voter\Vote;
11use Symfony\Component\Security\Core\Authorization\Voter\Voter;
12
13/**
14 * Only the owner sees (as a single event), changes and deletes an event; others see it on the
15 * timeline with the stats history.
16 *
17 * @extends Voter<string, TimelineEvent>
18 */
19final class TimelineEventVoter extends Voter
20{
21    public const string EDIT = 'TIMELINE_EVENT_EDIT';
22
23    #[\Override]
24    protected function supports(string $attribute, mixed $subject): bool
25    {
26        return self::EDIT === $attribute && $subject instanceof TimelineEvent;
27    }
28
29    #[\Override]
30    protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token, ?Vote $vote = null): bool
31    {
32        $user = $token->getUser();
33
34        return $user instanceof User
35            && $subject->owner?->id === $user->id;
36    }
37}