mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-25 19:28:51 +02:00
Automatically upgrade permission version schema in the background when needed on a request.
This commit is contained in:
parent
0e56f11b6b
commit
7aa88a8dbd
6 changed files with 130 additions and 5 deletions
|
@ -0,0 +1,73 @@
|
|||
<?php
|
||||
/*
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published
|
||||
* by the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace App\EventSubscriber\UserSystem;
|
||||
|
||||
use App\Entity\UserSystem\User;
|
||||
use App\Services\UserSystem\PermissionSchemaUpdater;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
|
||||
use Symfony\Component\HttpKernel\Event\RequestEvent;
|
||||
use Symfony\Component\HttpKernel\KernelEvents;
|
||||
use Symfony\Component\Security\Core\Security;
|
||||
|
||||
/**
|
||||
* The purpose of this event subscriber is to check if the permission schema of the current user is up to date and upgrade it automatically if needed.
|
||||
*/
|
||||
class UpgradePermissionsSchemaSubscriber implements EventSubscriberInterface
|
||||
{
|
||||
private Security $security;
|
||||
private PermissionSchemaUpdater $permissionSchemaUpdater;
|
||||
private EntityManagerInterface $entityManager;
|
||||
private FlashBagInterface $flashBag;
|
||||
|
||||
public function __construct(Security $security, PermissionSchemaUpdater $permissionSchemaUpdater, EntityManagerInterface $entityManager, FlashBagInterface $flashBag)
|
||||
{
|
||||
$this->security = $security;
|
||||
$this->permissionSchemaUpdater = $permissionSchemaUpdater;
|
||||
$this->entityManager = $entityManager;
|
||||
$this->flashBag = $flashBag;
|
||||
}
|
||||
|
||||
public function onRequest(RequestEvent $event): void
|
||||
{
|
||||
if (!$event->isMainRequest()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$user = $this->security->getUser();
|
||||
if (null === $user) {
|
||||
//Retrieve anonymous user
|
||||
$user = $this->entityManager->getRepository(User::class)->getAnonymousUser();
|
||||
}
|
||||
|
||||
if ($this->permissionSchemaUpdater->isSchemaUpdateNeeded($user)) {
|
||||
$this->permissionSchemaUpdater->userUpgradeSchemaRecursively($user);
|
||||
$this->entityManager->flush();
|
||||
$this->flashBag->add('notice', 'user.permissions_schema_updated');
|
||||
}
|
||||
}
|
||||
|
||||
public static function getSubscribedEvents(): array
|
||||
{
|
||||
return [KernelEvents::REQUEST => 'onRequest'];
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue