Skip to content

Commit

Permalink
Merged branch '4.4' into 4.5
Browse files Browse the repository at this point in the history
  • Loading branch information
alongosz committed Jun 20, 2023
2 parents e32afe5 + 13abf69 commit 519c552
Show file tree
Hide file tree
Showing 18 changed files with 606 additions and 117 deletions.
12 changes: 12 additions & 0 deletions src/contracts/Persistence/User/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,18 @@ public function loadRoleAssignment($roleAssignmentId);
*/
public function loadRoleAssignmentsByRoleId($roleId);

/**
* Loads Role's assignments based on provided $offset and $limit arguments.
*
* @return \Ibexa\Contracts\Core\Persistence\User\RoleAssignment[]
*/
public function loadRoleAssignmentsByRoleIdWithOffsetAndLimit(int $roleId, int $offset, ?int $limit): array;

/**
* Counts Role's assignments taking into consideration related and existing user and user group objects.
*/
public function countRoleAssignments(int $roleId): int;

/**
* Loads roles assignments to a user/group.
*
Expand Down
10 changes: 10 additions & 0 deletions src/contracts/Repository/Decorator/RoleServiceDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,16 @@ public function getRoleAssignments(Role $role): iterable
return $this->innerService->getRoleAssignments($role);
}

public function loadRoleAssignments(Role $role, int $offset = 0, ?int $limit = null): iterable
{
return $this->innerService->loadRoleAssignments($role, $offset, $limit);
}

public function countRoleAssignments(Role $role): int
{
return $this->innerService->countRoleAssignments($role);
}

public function getRoleAssignmentsForUser(
User $user,
bool $inherited = false
Expand Down
24 changes: 24 additions & 0 deletions src/contracts/Repository/RoleService.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,30 @@ public function loadRoleAssignment(int $roleAssignmentId): RoleAssignment;
*/
public function getRoleAssignments(Role $role): iterable;

/**
* Returns the assigned users and user groups to this role with $offset and $limit arguments.
*
* @return \Ibexa\Contracts\Core\Repository\Values\User\RoleAssignment[]
*
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\BadStateException
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to read a role
*/
public function loadRoleAssignments(
Role $role,
int $offset = 0,
?int $limit = null
): iterable;

/**
* Returns the number of users and user groups assigned to this role.
*
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\BadStateException
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to read a role
*/
public function countRoleAssignments(Role $role): int;

/**
* Returns UserRoleAssignments assigned to the given User, excluding the ones the current user is not allowed to read.
*
Expand Down
67 changes: 59 additions & 8 deletions src/lib/Persistence/Cache/UserHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class UserHandler extends AbstractInMemoryPersistenceHandler implements UserHand
private const BY_IDENTIFIER_SUFFIX = 'by_identifier_suffix';
private const LOCATION_PATH_IDENTIFIER = 'location_path';
private const ROLE_ASSIGNMENT_WITH_BY_ROLE_SUFFIX_IDENTIFIER = 'role_assignment_with_by_role_suffix';
private const ROLE_ASSIGNMENT_WITH_BY_ROLE_SUFFIX_OFFSET_LIMIT_IDENTIFIER = 'role_assignment_with_by_role_offset_limit_suffix';
private const ROLE_ASSIGNMENT_WITH_BY_GROUP_INHERITED_SUFFIX_IDENTIFIER = 'role_assignment_with_by_group_inherited_suffix';
private const ROLE_ASSIGNMENT_WITH_BY_GROUP_SUFFIX_IDENTIFIER = 'role_assignment_with_by_group_suffix';
private const USER_WITH_ACCOUNT_KEY_SUFFIX_IDENTIFIER = 'user_with_account_key_suffix';
Expand Down Expand Up @@ -495,6 +496,43 @@ function () use ($roleId) {
);
}

public function loadRoleAssignmentsByRoleIdWithOffsetAndLimit(int $roleId, int $offset, ?int $limit): array
{
return $this->getListCacheValue(
$this->cacheIdentifierGenerator->generateKey(
self::ROLE_ASSIGNMENT_WITH_BY_ROLE_SUFFIX_OFFSET_LIMIT_IDENTIFIER,
[$roleId, $offset, $limit],
true
),
function () use ($roleId, $offset, $limit): array {
return $this->persistenceHandler
->userHandler()
->loadRoleAssignmentsByRoleIdWithOffsetAndLimit($roleId, $offset, $limit);
},
$this->getRoleAssignmentTags,
$this->getRoleAssignmentKeys,
function () use ($roleId): array {
return [
$this->cacheIdentifierGenerator->generateTag(
self::ROLE_ASSIGNMENT_ROLE_LIST_IDENTIFIER,
[$roleId]
),
$this->cacheIdentifierGenerator->generateTag(self::ROLE_IDENTIFIER, [$roleId]),
];
},
[$roleId, $offset, $limit]
);
}

public function countRoleAssignments(int $roleId): int
{
$this->logger->logCall(__METHOD__, ['roleId' => $roleId]);

return $this->persistenceHandler
->userHandler()
->countRoleAssignments($roleId);
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -698,19 +736,32 @@ public function unassignRole($contentId, $roleId)
return $return;
}

/**
* {@inheritdoc}
*/
public function removeRoleAssignment($roleAssignmentId)
public function removeRoleAssignment($roleAssignmentId): void
{
$this->logger->logCall(__METHOD__, ['assignment' => $roleAssignmentId]);
$return = $this->persistenceHandler->userHandler()->removeRoleAssignment($roleAssignmentId);
$roleAssignment = $this->persistenceHandler->userHandler()->loadRoleAssignment($roleAssignmentId);

$this->logger->logCall(
__METHOD__,
[
'assignment' => $roleAssignmentId,
'contentId' => $roleAssignment->contentId,
'roleId' => $roleAssignment->roleId,
]
);

$this->persistenceHandler->userHandler()->removeRoleAssignment($roleAssignmentId);

$this->cache->invalidateTags([
$this->cacheIdentifierGenerator->generateTag(self::ROLE_ASSIGNMENT_IDENTIFIER, [$roleAssignmentId]),
$this->cacheIdentifierGenerator->generateTag(
self::ROLE_ASSIGNMENT_GROUP_LIST_IDENTIFIER,
[$roleAssignment->contentId]
),
$this->cacheIdentifierGenerator->generateTag(
self::ROLE_ASSIGNMENT_ROLE_LIST_IDENTIFIER,
[$roleAssignment->roleId]
),
]);

return $return;
}
}

Expand Down
19 changes: 19 additions & 0 deletions src/lib/Persistence/Legacy/User/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,25 @@ public function loadRoleAssignmentsByRoleId($roleId)
return $this->mapper->mapRoleAssignments($data);
}

/**
* @return \Ibexa\Contracts\Core\Persistence\User\RoleAssignment[]
*/
public function loadRoleAssignmentsByRoleIdWithOffsetAndLimit(int $roleId, int $offset, ?int $limit): array
{
$data = $this->roleGateway->loadRoleAssignmentsByRoleIdWithOffsetAndLimit($roleId, $offset, $limit);

if (empty($data)) {
return [];
}

return $this->mapper->mapRoleAssignments($data);
}

public function countRoleAssignments(int $roleId): int
{
return $this->roleGateway->countRoleAssignments($roleId);
}

/**
* Loads roles assignments to a user/group.
*
Expand Down
4 changes: 0 additions & 4 deletions src/lib/Persistence/Legacy/User/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,6 @@ public function mapPolicies(array $data)
/**
* Map role data to a role.
*
* @param array $data
*
* @return \Ibexa\Contracts\Core\Persistence\User\Role
*/
public function mapRole(array $data)
Expand All @@ -134,8 +132,6 @@ public function mapRole(array $data)
/**
* Map data for a set of roles.
*
* @param array $data
*
* @return \Ibexa\Contracts\Core\Persistence\User\Role[]
*/
public function mapRoles(array $data)
Expand Down
15 changes: 14 additions & 1 deletion src/lib/Persistence/Legacy/User/Role/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ abstract class Gateway
public const POLICY_LIMITATION_TABLE = 'ezpolicy_limitation';
public const POLICY_LIMITATION_VALUE_TABLE = 'ezpolicy_limitation_value';
public const USER_ROLE_TABLE = 'ezuser_role';

public const ROLE_SEQ = 'ezrole_id_seq';
public const POLICY_SEQ = 'ezpolicy_id_seq';
public const POLICY_LIMITATION_SEQ = 'ezpolicy_limitation_id_seq';
Expand Down Expand Up @@ -101,6 +100,20 @@ abstract public function loadRoleAssignmentsByGroupId(
*/
abstract public function loadRoleAssignmentsByRoleId(int $roleId): array;

/**
* Load a Role assignments for given Role ID with provided $offset and $limit arguments.
*/
abstract public function loadRoleAssignmentsByRoleIdWithOffsetAndLimit(
int $roleId,
int $offset,
?int $limit
): array;

/**
* Count Role's assignments taking into consideration related and existing user and user group objects.
*/
abstract public function countRoleAssignments(int $roleId): int;

/**
* Return User Policies data associated with User.
*
Expand Down
67 changes: 67 additions & 0 deletions src/lib/Persistence/Legacy/User/Role/Gateway/DoctrineDatabase.php
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\Core\Persistence\Legacy\Content\Gateway as ContentGateway;
use Ibexa\Core\Persistence\Legacy\User\Role\Gateway;

/**
Expand Down Expand Up @@ -342,6 +343,72 @@ public function loadRoleAssignmentsByRoleId(int $roleId): array
return $statement->fetchAll(FetchMode::ASSOCIATIVE);
}

/**
* @throws \Doctrine\DBAL\Driver\Exception
* @throws \Doctrine\DBAL\Exception
*/
public function loadRoleAssignmentsByRoleIdWithOffsetAndLimit(int $roleId, int $offset, ?int $limit): array
{
$query = $this
->buildLoadRoleAssignmentsQuery(
[
'user_role.id',
'user_role.contentobject_id',
'user_role.limit_identifier',
'user_role.limit_value',
'user_role.role_id',
],
$roleId
)
->setFirstResult($offset);

if ($limit !== null) {
$query->setMaxResults($limit);
}

return $query
->execute()
->fetchAllAssociative();
}

/**
* @throws \Doctrine\DBAL\Driver\Exception
* @throws \Doctrine\DBAL\Exception
*/
public function countRoleAssignments(int $roleId): int
{
$query = $this->buildLoadRoleAssignmentsQuery(
[$this->connection->getDatabasePlatform()->getCountExpression('user_role.id')],
$roleId
);

return (int)$query->execute()->fetchOne();
}

/**
* @param array<string> $columns
*/
private function buildLoadRoleAssignmentsQuery(array $columns, int $roleId): QueryBuilder
{
$query = $this->connection->createQueryBuilder();
$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'
)->where(
$query->expr()->eq(
'role_id',
$query->createPositionalParameter($roleId, ParameterType::INTEGER)
)
);

return $query;
}

public function loadPoliciesByUserId(int $userId): array
{
$groupIds = $this->fetchUserGroups($userId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,24 @@ public function loadRoleAssignmentsByRoleId(int $roleId): array
}
}

public function loadRoleAssignmentsByRoleIdWithOffsetAndLimit(int $roleId, int $offset, ?int $limit): array
{
try {
return $this->innerGateway->loadRoleAssignmentsByRoleIdWithOffsetAndLimit($roleId, $offset, $limit);
} catch (DBALException | PDOException $e) {
throw DatabaseException::wrap($e);
}
}

public function countRoleAssignments(int $roleId): int
{
try {
return $this->innerGateway->countRoleAssignments($roleId);
} catch (DBALException | PDOException $e) {
throw DatabaseException::wrap($e);
}
}

public function loadPoliciesByUserId(int $userId): array
{
try {
Expand Down
Loading

0 comments on commit 519c552

Please sign in to comment.