mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-07-15 13:26:13 +00:00
90 lines
3.1 KiB
PHP
90 lines
3.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/*
|
|
* Chill is a software for social workers
|
|
*
|
|
* For the full copyright and license information, please view
|
|
* the LICENSE file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Chill\MainBundle\Security\Authorization;
|
|
|
|
use Chill\MainBundle\Entity\Notification;
|
|
use Chill\MainBundle\Entity\NotificationComment;
|
|
use Chill\MainBundle\Entity\User;
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
|
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
|
|
use UnexpectedValueException;
|
|
|
|
final class NotificationVoter extends Voter
|
|
{
|
|
/**
|
|
* Allow to add a comment on a notification.
|
|
*
|
|
* May apply on both @see{NotificationComment::class} and @see{Notification::class}.
|
|
*/
|
|
public const COMMENT_ADD = 'CHILL_MAIN_NOTIFICATION_COMMENT_ADD';
|
|
|
|
public const COMMENT_EDIT = 'CHILL_MAIN_NOTIFICATION_COMMENT_EDIT';
|
|
|
|
public const NOTIFICATION_SEE = 'CHILL_MAIN_NOTIFICATION_SEE';
|
|
|
|
public const NOTIFICATION_TOGGLE_READ_STATUS = 'CHILL_MAIIN_NOTIFICATION_TOGGLE_READ_STATUS';
|
|
|
|
public const NOTIFICATION_UPDATE = 'CHILL_MAIN_NOTIFICATION_UPDATE';
|
|
|
|
protected function supports($attribute, $subject): bool
|
|
{
|
|
return $subject instanceof Notification || $subject instanceof NotificationComment;
|
|
}
|
|
|
|
/**
|
|
* @param string $attribute
|
|
* @param mixed $subject
|
|
*/
|
|
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
|
|
{
|
|
$user = $token->getUser();
|
|
|
|
if (!$user instanceof User) {
|
|
return false;
|
|
}
|
|
|
|
if ($subject instanceof Notification) {
|
|
switch ($attribute) {
|
|
case self::COMMENT_ADD:
|
|
return false === $subject->isSystem() && (
|
|
$subject->getAddressees()->contains($user) || $subject->getSender() === $user
|
|
);
|
|
|
|
case self::NOTIFICATION_SEE:
|
|
case self::NOTIFICATION_TOGGLE_READ_STATUS:
|
|
return $subject->getSender() === $user || $subject->getAddressees()->contains($user);
|
|
|
|
case self::NOTIFICATION_UPDATE:
|
|
return $subject->getSender() === $user && false === $subject->isSystem();
|
|
|
|
default:
|
|
throw new UnexpectedValueException("this subject {$attribute} is not implemented");
|
|
}
|
|
} elseif ($subject instanceof NotificationComment) {
|
|
switch ($attribute) {
|
|
case self::COMMENT_ADD:
|
|
return false === $subject->getNotification()->isSystem() && (
|
|
$subject->getNotification()->getAddressees()->contains($user) || $subject->getNotification()->getSender() === $user
|
|
);
|
|
|
|
case self::COMMENT_EDIT:
|
|
return $subject->getCreatedBy() === $user && false === $subject->getNotification()->isSystem();
|
|
|
|
default:
|
|
throw new UnexpectedValueException("this subject {$attribute} is not implemented");
|
|
}
|
|
}
|
|
|
|
throw new UnexpectedValueException();
|
|
}
|
|
}
|