71 lines
2.1 KiB
PHP
71 lines
2.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Dashboard\Widget;
|
|
|
|
use App\Dashboard\Contract\DashboardWidgetProviderInterface;
|
|
use App\Entity\User;
|
|
use App\Model\DashboardWidget;
|
|
use App\Model\DashboardWidgetEntry;
|
|
use App\Repository\UserRepository;
|
|
use App\Security\Role;
|
|
use App\Service\RoleApprovalUrlGenerator;
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
|
|
|
/**
|
|
* The accounts waiting on an administrator to act.
|
|
*
|
|
* A nomination grants nothing until it is approved. RoleNominationHandler emails the
|
|
* administrators the moment one appears, but that fires once, on the login that produced it —
|
|
* this widget is the standing list, and the only thing that still shows a nomination somebody
|
|
* has left sitting.
|
|
*/
|
|
class PendingRoleApprovalsWidgetProvider implements DashboardWidgetProviderInterface
|
|
{
|
|
private const LIMIT = 5;
|
|
|
|
public function __construct(
|
|
private readonly UserRepository $userRepository,
|
|
private readonly UrlGeneratorInterface $urlGenerator,
|
|
private readonly RoleApprovalUrlGenerator $approvalUrlGenerator,
|
|
) {
|
|
}
|
|
|
|
public function getRequiredRole(): string
|
|
{
|
|
return Role::ADMIN;
|
|
}
|
|
|
|
public function getPriority(): int
|
|
{
|
|
return 90;
|
|
}
|
|
|
|
public function build(): ?DashboardWidget
|
|
{
|
|
return new DashboardWidget(
|
|
'Offene Rollenfreigaben',
|
|
array_map(fn (User $user): DashboardWidgetEntry => new DashboardWidgetEntry(
|
|
$this->label($user),
|
|
$this->approvalUrlGenerator->forUser($user),
|
|
'user',
|
|
), $this->userRepository->findWithPendingRoles(self::LIMIT)),
|
|
'Keine offenen Rollenfreigaben.',
|
|
$this->urlGenerator->generate('app_admin_user'),
|
|
'Alle Benutzer',
|
|
);
|
|
}
|
|
|
|
private function label(User $user): string
|
|
{
|
|
$nominated = Role::nominatedFrom($user->getRoles());
|
|
|
|
if ([] === $nominated) {
|
|
return $user->getDisplayName();
|
|
}
|
|
|
|
return sprintf('%s: %s', $user->getDisplayName(), implode(', ', $nominated));
|
|
}
|
|
}
|