parent
8eded5234f
commit
da69428831
@ -0,0 +1,30 @@ |
||||
<?php |
||||
|
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
namespace Chamilo\CoreBundle\Entity\Listener; |
||||
|
||||
use Chamilo\CoreBundle\Entity\Message; |
||||
use Chamilo\CoreBundle\Entity\MessageRelUser; |
||||
use Doctrine\ORM\Event\PostLoadEventArgs; |
||||
|
||||
class MessageListener |
||||
{ |
||||
public function postLoad(Message $message, PostLoadEventArgs $args): void |
||||
{ |
||||
$om = $args->getObjectManager(); |
||||
$messageRelUserRepo = $om->getRepository(MessageRelUser::class); |
||||
|
||||
$softDeleteable = $om->getFilters()->enable('softdeleteable'); |
||||
|
||||
$softDeleteable->disableForEntity(MessageRelUser::class); |
||||
|
||||
$message->setReceiversFromArray( |
||||
$messageRelUserRepo->findBy(['message' => $message]) |
||||
); |
||||
|
||||
$softDeleteable->enableForEntity(Message::class); |
||||
} |
||||
} |
@ -0,0 +1,26 @@ |
||||
<?php |
||||
|
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
namespace Chamilo\CoreBundle\Migrations\Schema\V200; |
||||
|
||||
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo; |
||||
use Doctrine\DBAL\Schema\Schema; |
||||
|
||||
final class Version20240918002830 extends AbstractMigrationChamilo |
||||
{ |
||||
public function getDescription(): string |
||||
{ |
||||
return 'Make soft-deleteable message_rel_user'; |
||||
} |
||||
|
||||
/** |
||||
* @inheritDoc |
||||
*/ |
||||
public function up(Schema $schema): void |
||||
{ |
||||
$this->addSql("ALTER TABLE message_rel_user ADD deleted_at DATETIME DEFAULT NULL COMMENT '(DC2Type:datetime)'"); |
||||
} |
||||
} |
@ -0,0 +1,56 @@ |
||||
<?php |
||||
|
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
namespace Chamilo\CoreBundle\Security\Authorization\Voter; |
||||
|
||||
use Chamilo\CoreBundle\Entity\MessageRelUser; |
||||
use Chamilo\CoreBundle\Entity\User; |
||||
use Symfony\Bundle\SecurityBundle\Security; |
||||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
||||
use Symfony\Component\Security\Core\Authorization\Voter\Voter; |
||||
use Symfony\Component\Security\Core\User\UserInterface; |
||||
|
||||
class MessageRelUserVoter extends Voter |
||||
{ |
||||
public const DELETE = 'DELETE'; |
||||
public const VIEW = 'VIEW'; |
||||
public const EDIT = 'EDIT'; |
||||
|
||||
public function __construct( |
||||
private readonly Security $security |
||||
) { |
||||
} |
||||
|
||||
protected function supports(string $attribute, mixed $subject): bool |
||||
{ |
||||
return in_array($attribute, [self::DELETE, self::VIEW, self::EDIT]) |
||||
&& $subject instanceof MessageRelUser; |
||||
} |
||||
|
||||
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool |
||||
{ |
||||
$user = $token->getUser(); |
||||
|
||||
if (!$user instanceof UserInterface) { |
||||
return false; |
||||
} |
||||
|
||||
if ($this->security->isGranted('ROLE_ADMIN')) { |
||||
return true; |
||||
} |
||||
|
||||
assert($user instanceof User); |
||||
assert($subject instanceof MessageRelUser); |
||||
|
||||
$message = $subject->getMessage(); |
||||
$isReceiver = $message->hasUserReceiver($user); |
||||
|
||||
return match ($attribute) { |
||||
self::VIEW, self::EDIT, self::DELETE => $isReceiver, |
||||
default => false, |
||||
}; |
||||
} |
||||
} |
Loading…
Reference in new issue