Skip to content
Open
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
6 changes: 6 additions & 0 deletions src/bundle/Core/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -390,3 +390,9 @@ services:
decoration_priority: 500
arguments:
$inner: '@.inner'

Ibexa\Core\Persistence\Cache\EventSubscriber\RoleAssignmentCacheInvalidationListener:
autowire: true
autoconfigure: true
arguments:
$cache: '@ibexa.cache_pool'
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?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\Core\Persistence\Cache\EventSubscriber;

use Ibexa\Contracts\Core\Repository\Events\Trash\RecoverEvent;
use Ibexa\Contracts\Core\Repository\Events\Trash\TrashEvent;
use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException as APINotFoundException;
use Ibexa\Contracts\Core\Repository\RoleService;
use Ibexa\Contracts\Core\Repository\UserService;
use Ibexa\Contracts\Core\Repository\Values\Content\Location;
use Ibexa\Core\Persistence\Cache\Identifier\CacheIdentifierGeneratorInterface;
use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

final class RoleAssignmentCacheInvalidationListener implements EventSubscriberInterface
{
private const ROLE_ASSIGNMENT_ROLE_LIST_IDENTIFIER = 'role_assignment_role_list';

private TagAwareAdapterInterface $cache;

private CacheIdentifierGeneratorInterface $identifierGenerator;

private RoleService $roleService;

private UserService $userService;

public function __construct(
TagAwareAdapterInterface $cache,
CacheIdentifierGeneratorInterface $identifierGenerator,
RoleService $roleService,
UserService $userService
) {
$this->cache = $cache;
$this->identifierGenerator = $identifierGenerator;
$this->roleService = $roleService;
$this->userService = $userService;
}

public static function getSubscribedEvents(): array
{
return [
TrashEvent::class => 'onTrashContent',
RecoverEvent::class => 'onRecoverContent',
];
}

public function onRecoverContent(RecoverEvent $event): void
{
$this->clearCache($event->getTrashItem());
}

public function onTrashContent(TrashEvent $event): void
{
$item = $event->getTrashItem();

if ($item !== null) {
$this->clearCache($item);
}
}

private function clearCache(Location $item): void
{
$tags = $this->buildCacheTags($item);

if (!empty($tags)) {
$this->cache->invalidateTags($tags);
}
}

/**
* @return array<string>
*/
private function buildCacheTags(Location $item): array
{
$contentId = $item->getContentId();

try {
$userGroup = $this->userService->loadUserGroup($contentId);
$roleAssignments = $this->roleService->getRoleAssignmentsForUserGroup($userGroup);
} catch (APINotFoundException $e) {
try {
$user = $this->userService->loadUser($contentId);
$roleAssignments = $this->roleService->getRoleAssignmentsForUser($user, true);
} catch (APINotFoundException $e) {
return [];
}
}

$tags = [];
foreach ($roleAssignments as $roleAssignment) {
$tags[] = $this->identifierGenerator->generateTag(
self::ROLE_ASSIGNMENT_ROLE_LIST_IDENTIFIER,
[$roleAssignment->getRole()->id]
);
}

return $tags;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Ibexa\Contracts\Core\Persistence\User\Policy;
use Ibexa\Contracts\Core\Persistence\User\Role;
use Ibexa\Contracts\Core\Persistence\User\RoleUpdateStruct;
use Ibexa\Contracts\Core\Repository\Values\Content\ContentInfo;
use Ibexa\Core\Persistence\Legacy\Content\Gateway as ContentGateway;
use Ibexa\Core\Persistence\Legacy\User\Role\Gateway;

Expand Down Expand Up @@ -393,16 +394,20 @@ public function countRoleAssignments(int $roleId): int
private function buildLoadRoleAssignmentsQuery(array $columns, int $roleId): QueryBuilder
{
$query = $this->connection->createQueryBuilder();
$expr = $query->expr();
$query
->select(...$columns)
->from(self::USER_ROLE_TABLE, 'user_role')
->innerJoin(
'user_role',
ContentGateway::CONTENT_ITEM_TABLE,
'content_object',
'user_role.contentobject_id = content_object.id'
(string) $expr->and(
$expr->eq('user_role.contentobject_id', 'content_object.id'),
$expr->eq('content_object.status', ContentInfo::STATUS_PUBLISHED)
)
)->where(
$query->expr()->eq(
$expr->eq(
'role_id',
$query->createPositionalParameter($roleId, ParameterType::INTEGER)
)
Expand Down
2 changes: 2 additions & 0 deletions tests/lib/Persistence/Legacy/User/_fixtures/roles.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,11 @@
'ezcontentobject' => [
[
'id' => '11',
'status' => '1',
],
[
'id' => '42',
'status' => '1',
],
],
];
Loading