65 lines
2.2 KiB
PHP
65 lines
2.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Controller\Admin\AccessCode;
|
|
|
|
use App\Controller\Traits\ConfirmedActionTrait;
|
|
use App\Htmx\HxRedirectResponse;
|
|
use App\Repository\UserRepository;
|
|
use App\Security\Role;
|
|
use App\Service\AccessCodeNotifier;
|
|
use App\Service\AuditLogger;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\Routing\Attribute\Route;
|
|
use Symfony\Component\Security\Http\Attribute\IsGranted;
|
|
|
|
/**
|
|
* Tells the whole staff that the codes changed. Confirmed first, because a mail to everybody
|
|
* cannot be taken back -- and the confirmation says how many people that is.
|
|
*/
|
|
#[IsGranted(Role::ADMIN)]
|
|
final class NotifyController extends AbstractController
|
|
{
|
|
use ConfirmedActionTrait;
|
|
|
|
private const CSRF_TOKEN_ID = 'notify-access-codes';
|
|
|
|
public function __construct(
|
|
private readonly AccessCodeNotifier $notifier,
|
|
private readonly UserRepository $users,
|
|
private readonly AuditLogger $audit,
|
|
) {
|
|
}
|
|
|
|
#[Route('/admin/access-codes/notify', name: 'app_admin_access_code_notify', methods: ['GET', 'POST'])]
|
|
public function index(Request $request): Response
|
|
{
|
|
if (!$request->isMethod(Request::METHOD_POST)) {
|
|
return $this->render('admin/_modal_access_code_notify.html.twig', [
|
|
'recipients' => $this->users->countActive(),
|
|
'csrf_token_id' => self::CSRF_TOKEN_ID,
|
|
]);
|
|
}
|
|
|
|
$this->denyUnlessCsrfValid($request, self::CSRF_TOKEN_ID);
|
|
|
|
$report = $this->notifier->codesUpdated();
|
|
$this->audit->accessCodesAnnounced($report->sent, $report->failed);
|
|
|
|
if (0 === $report->failed) {
|
|
$this->addFlash('success', \sprintf('Benachrichtigung an %d Mitarbeitende versendet.', $report->sent));
|
|
} else {
|
|
$this->addFlash('warning', \sprintf(
|
|
'Benachrichtigung an %d Mitarbeitende versendet, %d konnten nicht erreicht werden.',
|
|
$report->sent,
|
|
$report->failed,
|
|
));
|
|
}
|
|
|
|
return new HxRedirectResponse($this->generateUrl('app_admin_access_code'));
|
|
}
|
|
}
|