2023-01-08 00:15:35 +01:00
< ? php
2023-06-11 18:59:07 +02:00
declare ( strict_types = 1 );
2023-01-08 00:15:35 +01:00
/*
* 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\Command\User ;
2023-06-11 14:55:06 +02:00
use Symfony\Component\Console\Attribute\AsCommand ;
2023-01-08 00:15:35 +01:00
use App\Entity\UserSystem\Group ;
use App\Entity\UserSystem\PermissionData ;
use App\Entity\UserSystem\User ;
2023-01-08 20:14:23 +01:00
use App\Services\LogSystem\EventCommentHelper ;
2023-01-08 00:15:35 +01:00
use App\Services\UserSystem\PermissionSchemaUpdater ;
use Doctrine\ORM\EntityManagerInterface ;
use Symfony\Component\Console\Command\Command ;
use Symfony\Component\Console\Input\InputInterface ;
use Symfony\Component\Console\Output\OutputInterface ;
use Symfony\Component\Console\Style\SymfonyStyle ;
2023-06-11 14:55:06 +02:00
#[AsCommand('partdb:users:upgrade-permissions-schema', '(Manually) upgrades the permissions schema of all users to the latest version.')]
2023-01-08 00:15:35 +01:00
final class UpgradePermissionsSchemaCommand extends Command
{
2023-06-11 14:15:46 +02:00
public function __construct ( private readonly PermissionSchemaUpdater $permissionSchemaUpdater , private readonly EntityManagerInterface $em , private readonly EventCommentHelper $eventCommentHelper )
2023-01-08 00:15:35 +01:00
{
2025-08-03 21:59:51 +02:00
parent :: __construct ();
2023-01-08 00:15:35 +01:00
}
protected function execute ( InputInterface $input , OutputInterface $output ) : int
{
$io = new SymfonyStyle ( $input , $output );
$io -> info ( 'Target schema version number: ' . PermissionData :: CURRENT_SCHEMA_VERSION );
//Retrieve all users and groups
$users = $this -> em -> getRepository ( User :: class ) -> findAll ();
$groups = $this -> em -> getRepository ( Group :: class ) -> findAll ();
//Check which users and groups need an update
$groups_to_upgrade = [];
$users_to_upgrade = [];
foreach ( $groups as $group ) {
if ( $this -> permissionSchemaUpdater -> isSchemaUpdateNeeded ( $group )) {
$groups_to_upgrade [] = $group ;
}
}
foreach ( $users as $user ) {
if ( $this -> permissionSchemaUpdater -> isSchemaUpdateNeeded ( $user )) {
$users_to_upgrade [] = $user ;
}
}
$io -> info ( 'Found ' . count ( $groups_to_upgrade ) . ' groups and ' . count ( $users_to_upgrade ) . ' users that need an update.' );
2023-06-11 14:15:46 +02:00
if ( $groups_to_upgrade === [] && $users_to_upgrade === []) {
2023-01-08 00:15:35 +01:00
$io -> success ( 'All users and group permissions schemas are up-to-date. No update needed.' );
2023-06-11 14:55:06 +02:00
return Command :: SUCCESS ;
2023-01-08 00:15:35 +01:00
}
//List all users and groups that need an update
$io -> section ( 'Groups that need an update:' );
2023-06-11 14:15:46 +02:00
$io -> listing ( array_map ( static fn ( Group $group ) : string => $group -> getName () . ' (ID: ' . $group -> getID () . ', Current version: ' . $group -> getPermissions () -> getSchemaVersion () . ')' , $groups_to_upgrade ));
2023-01-08 00:15:35 +01:00
$io -> section ( 'Users that need an update:' );
2023-06-11 14:15:46 +02:00
$io -> listing ( array_map ( static fn ( User $user ) : string => $user -> getUsername () . ' (ID: ' . $user -> getID () . ', Current version: ' . $user -> getPermissions () -> getSchemaVersion () . ')' , $users_to_upgrade ));
2023-01-08 00:15:35 +01:00
if ( ! $io -> confirm ( 'Continue with the update?' , false )) {
$io -> warning ( 'Update aborted.' );
2023-06-11 14:55:06 +02:00
return Command :: SUCCESS ;
2023-01-08 00:15:35 +01:00
}
//Update all users and groups
foreach ( $groups_to_upgrade as $group ) {
$io -> writeln ( 'Updating group ' . $group -> getName () . ' (ID: ' . $group -> getID () . ') to schema version ' . PermissionData :: CURRENT_SCHEMA_VERSION . '...' , OutputInterface :: VERBOSITY_VERBOSE );
$this -> permissionSchemaUpdater -> upgradeSchema ( $group );
}
foreach ( $users_to_upgrade as $user ) {
$io -> writeln ( 'Updating user ' . $user -> getUsername () . ' (ID: ' . $user -> getID () . ') to schema version ' . PermissionData :: CURRENT_SCHEMA_VERSION . '...' , OutputInterface :: VERBOSITY_VERBOSE );
$this -> permissionSchemaUpdater -> upgradeSchema ( $user );
}
2023-01-08 20:14:23 +01:00
$this -> eventCommentHelper -> setMessage ( 'Manual permissions schema update via CLI' );
2023-01-08 00:15:35 +01:00
//Write changes to database
$this -> em -> flush ();
$io -> success ( 'All users and groups have been updated to the latest permissions schema version.' );
return Command :: SUCCESS ;
}
}