Skip to content

Commit

Permalink
IBX-5663: Paginated role assignments to improve performance in the Ro…
Browse files Browse the repository at this point in the history
…les module (#2102)

* IBX-5663: Paginated role assignments to improve performance in the Roles module

* IBX-5663: Updated list action API method to `loadRoles`

* IBX-5663: Removed temporary `dump`

* IBX-5663: Applied review remarks
  • Loading branch information
barw4 authored Jun 19, 2023
1 parent 262c772 commit 4836b25
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 37 deletions.
40 changes: 21 additions & 19 deletions src/bundle/Controller/RoleAssignmentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
use EzSystems\EzPlatformAdminUi\Form\Factory\FormFactory;
use EzSystems\EzPlatformAdminUi\Form\SubmitHandler;
use EzSystems\EzPlatformAdminUi\Notification\TranslatableNotificationHandlerInterface;
use Pagerfanta\Adapter\ArrayAdapter;
use Ibexa\AdminUi\Pagination\Pagerfanta\RoleAssignmentsSearchAdapter;
use Pagerfanta\Pagerfanta;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -60,31 +60,33 @@ public function __construct(
}

/**
* @param \eZ\Publish\API\Repository\Values\User\Role $role
* @param string $routeName
* @param int $assignmentPage
*
* @return \Symfony\Component\HttpFoundation\Response
* @throws \eZ\Publish\API\Repository\Exceptions\BadStateException
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
*/
public function listAction(Role $role, string $routeName, int $assignmentPage = 1): Response
{
public function listAction(
Role $role,
string $routeName,
int $assignmentsCount,
int $assignmentPage = 1
): Response {
$pagerfanta = new Pagerfanta(
new RoleAssignmentsSearchAdapter(
$this->roleService,
$role,
$assignmentsCount
)
);
$pagerfanta->setMaxPerPage($this->configResolver->getParameter('pagination.role_assignment_limit'));
$pagerfanta->setCurrentPage($assignmentPage);

// If user has no permission to content/read than he should see empty table.
try {
$assignments = $this->roleService->getRoleAssignments($role);
/** @var \eZ\Publish\API\Repository\Values\User\RoleAssignment[] $assignments */
$assignments = $pagerfanta->getCurrentPageResults();
} catch (UnauthorizedException $e) {
$assignments = [];
}

$pagerfanta = new Pagerfanta(
new ArrayAdapter($assignments)
);

$pagerfanta->setMaxPerPage($this->configResolver->getParameter('pagination.role_assignment_limit'));
$pagerfanta->setCurrentPage(min($assignmentPage, $pagerfanta->getNbPages()));

/** @var \eZ\Publish\API\Repository\Values\User\RoleAssignment[] $assignments */
$assignments = $pagerfanta->getCurrentPageResults();

$deleteRoleAssignmentsForm = $this->formFactory->deleteRoleAssignments(
new RoleAssignmentsDeleteData($role, $this->getRoleAssignmentsNumbers($assignments))
);
Expand Down
21 changes: 5 additions & 16 deletions src/bundle/Controller/RoleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,6 @@ public function __construct(
$this->configResolver = $configResolver;
}

/**
* @param \Symfony\Component\HttpFoundation\Request $request
*
* @return \Symfony\Component\HttpFoundation\Response
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
*/
public function listAction(Request $request): Response
{
$page = $request->query->get('page') ?? 1;
Expand Down Expand Up @@ -117,12 +110,8 @@ public function listAction(Request $request): Response
}

/**
* @param \Symfony\Component\HttpFoundation\Request $request
* @param \eZ\Publish\API\Repository\Values\User\Role $role
* @param int $policyPage
* @param int $assignmentPage
*
* @return \Symfony\Component\HttpFoundation\Response
* @throws \eZ\Publish\API\Repository\Exceptions\BadStateException
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
*/
public function viewAction(Request $request, Role $role, int $policyPage = 1, int $assignmentPage = 1): Response
{
Expand All @@ -132,14 +121,14 @@ public function viewAction(Request $request, Role $role, int $policyPage = 1, in

// If user has no permission to content/read than he should see empty table.
try {
$assignments = $this->roleService->getRoleAssignments($role);
$assignmentsCount = $this->roleService->countRoleAssignments($role);
} catch (UnauthorizedException $e) {
$assignments = [];
$assignmentsCount = 0;
}

return $this->render('@ezdesign/user/role/index.html.twig', [
'role' => $role,
'assignments' => $assignments,
'assignments_count' => $assignmentsCount,
'delete_form' => $deleteForm->createView(),
'route_name' => $request->get('_route'),
'policy_page' => $policyPage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
</li>
<li role="presentation" class="nav-item">
<a href="#users-and-groups" class="nav-link" role="tab" data-toggle="tab">
{{ 'role_assignment.view.list.title.count'|trans({'%count%': assignments|length})|desc('Assignments (%count%)') }}
{{ 'role_assignment.view.list.title.count'|trans({'%count%': assignments_count})|desc('Assignments (%count%)') }}
</a>
</li>
</ul>
Expand All @@ -51,7 +51,8 @@
{{ render(controller('EzSystems\\EzPlatformAdminUiBundle\\Controller\\RoleAssignmentController::listAction', {
roleId: role.id,
assignmentPage: assignment_page,
routeName: route_name
routeName: route_name,
assignmentsCount: assignments_count
})) }}
</div>
</div>
Expand Down
52 changes: 52 additions & 0 deletions src/lib/Pagination/Pagerfanta/RoleAssignmentsSearchAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?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\AdminUi\Pagination\Pagerfanta;

use eZ\Publish\API\Repository\RoleService;
use eZ\Publish\API\Repository\Values\User\Role;
use Pagerfanta\Adapter\AdapterInterface;

final class RoleAssignmentsSearchAdapter implements AdapterInterface
{
/** @var \eZ\Publish\API\Repository\RoleService */
private $roleService;

/** @var \eZ\Publish\API\Repository\Values\User\Role */
private $role;

/** @var int|null */
private $assignmentsCount;

public function __construct(RoleService $roleService, Role $role, ?int $assignmentsCount = null)
{
$this->roleService = $roleService;
$this->role = $role;
$this->assignmentsCount = $assignmentsCount;
}

/**
* @throws \eZ\Publish\API\Repository\Exceptions\BadStateException
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
*/
public function getNbResults(): int
{
return $this->assignmentsCount ?: $this->roleService->countRoleAssignments($this->role);
}

/**
* @throws \eZ\Publish\API\Repository\Exceptions\BadStateException
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
*/
public function getSlice($offset, $length): iterable
{
return $this->roleService->loadRoleAssignments($this->role, $offset, $length);
}
}

0 comments on commit 4836b25

Please sign in to comment.