Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/bundle/DependencyInjection/IbexaMessengerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use Ibexa\Bundle\Messenger\Middleware\DeduplicateMiddleware;
use Ibexa\Bundle\Messenger\Middleware\SudoMiddleware;
use Ibexa\Bundle\Messenger\Middleware\UserPermissionMiddleware;
use Ibexa\Bundle\Messenger\Serializer\Normalizer\LockKeyNormalizer;
use Ibexa\Contracts\Messenger\Transport\MessageProviderInterface;
use LogicException;
Expand Down Expand Up @@ -121,6 +122,7 @@ private function registerMessengerConfiguration(

$middleware = [
['id' => SudoMiddleware::class],
['id' => UserPermissionMiddleware::class],
];

if ($mergedConfig['deduplication_lock_storage']['enabled'] === true) {
Expand Down
8 changes: 8 additions & 0 deletions src/bundle/Middleware/SudoMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ public function handle(Envelope $envelope, StackInterface $stack): Envelope
{
$stamp = $envelope->last(SudoStamp::class);
if ($stamp === null) {
// todo add this deprecation in 5.0.x release
trigger_deprecation(
'ibexa/messenger',
'5.0.x',
'Since 6.0.0, %s will not be attached automatically to all messages. You will need to add the stamp explicitly when dispatching the message.',
SudoStamp::class,
);

$envelope = $envelope->with(new SudoStamp());

$envelope = $this->repository->sudo(
Expand Down
43 changes: 43 additions & 0 deletions src/bundle/Middleware/UserPermissionMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Bundle\Messenger\Middleware;

use Ibexa\Bundle\Messenger\Stamp\UserPermissionStamp;
use Ibexa\Contracts\Core\Repository\PermissionResolver;
use Ibexa\Core\Repository\Values\User\UserReference;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
use Symfony\Component\Messenger\Middleware\StackInterface;

final class UserPermissionMiddleware implements MiddlewareInterface
{
public function __construct(
private PermissionResolver $permissionResolver
) {
}

public function handle(Envelope $envelope, StackInterface $stack): Envelope
{
$stamp = $envelope->last(UserPermissionStamp::class);
if ($stamp === null) {
return $stack->next()->handle($envelope, $stack);
}

$previousUserReference = $this->permissionResolver->getCurrentUserReference();
$this->permissionResolver->setCurrentUserReference(
new UserReference($stamp->userId)
);

try {
return $stack->next()->handle($envelope, $stack);
} finally {
$this->permissionResolver->setCurrentUserReference($previousUserReference);
}
}
}
2 changes: 2 additions & 0 deletions src/bundle/Resources/config/services/buses.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ services:

Ibexa\Bundle\Messenger\Middleware\SudoMiddleware: ~

Ibexa\Bundle\Messenger\Middleware\UserPermissionMiddleware: ~

Ibexa\Bundle\Messenger\Middleware\DeduplicateMiddleware:
arguments:
- '@ibexa.messenger.lock_factory'
Expand Down
19 changes: 19 additions & 0 deletions src/bundle/Stamp/UserPermissionStamp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Bundle\Messenger\Stamp;

use Symfony\Component\Messenger\Stamp\StampInterface;

final readonly class UserPermissionStamp implements StampInterface
{
public function __construct(
public int $userId
) {
}
}
Loading