src/Security/DocumentVoter.php line 11
<?phpnamespace App\Security;use App\Entity\Document;use App\Entity\DocumentPermission;use App\Entity\User;use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;use Symfony\Component\Security\Core\Authorization\Voter\Voter;class DocumentVoter extends Voter {const VIEW = 'view';const EDIT = 'edit';protected function supports(string $attribute, $subject): bool{// if the attribute isn't one we support, return falseif (!in_array($attribute, [self::VIEW, self::EDIT])) {return false;}// only vote on "Document" objectsif(!$subject instanceof Document) {return false;}return true;}protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool{$user = $token->getUser();if(!$user instanceof User) {return false;}/** @var Document $document */$document = $subject;switch ($attribute) {case self::VIEW:return $this->canView($document, $user);case self::EDIT:return $this->canEdit($document, $user);default:throw new \LogicException('This code should not be reached');}}private function canView(Document $document, User $user) {if($this->canEdit($document, $user))return true;return false;}private function canEdit(Document $document, User $user) {/** @var DocumentPermission $permission */foreach($document->getDocumentPermissions() as $permission) {if($user === $permission->getUser())return true;}return $user === $document->getOwner();}}