2024-04-01 18:03:43 +02:00
|
|
|
<?php
|
2024-04-02 07:47:24 +02:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
2024-04-01 18:03:43 +02:00
|
|
|
/*
|
|
|
|
* MigratePreferences.php
|
|
|
|
* Copyright (c) 2024 james@firefly-iii.org.
|
|
|
|
*
|
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
|
|
|
*
|
|
|
|
* 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 FireflyIII\Console\Commands\Correction;
|
|
|
|
|
|
|
|
use FireflyIII\Models\Preference;
|
|
|
|
use FireflyIII\User;
|
|
|
|
use Illuminate\Console\Command;
|
|
|
|
use Symfony\Component\Console\Command\Command as CommandAlias;
|
|
|
|
|
|
|
|
class MigratePreferences extends Command
|
|
|
|
{
|
|
|
|
protected $description = 'Give Firefly III preferences a user group ID so they can be made administration specific.';
|
|
|
|
|
2024-04-02 15:40:33 +02:00
|
|
|
protected $signature = 'firefly-iii:migrate-preferences';
|
|
|
|
|
2024-04-01 18:03:43 +02:00
|
|
|
/**
|
|
|
|
* Execute the console command.
|
|
|
|
*/
|
|
|
|
public function handle(): int
|
|
|
|
{
|
2024-04-01 18:18:48 +02:00
|
|
|
$items = config('firefly.admin_specific_prefs');
|
2024-04-01 18:03:43 +02:00
|
|
|
$users = User::get();
|
2024-04-02 07:47:24 +02:00
|
|
|
|
2024-04-01 18:03:43 +02:00
|
|
|
/** @var User $user */
|
2024-04-02 07:47:24 +02:00
|
|
|
foreach ($users as $user) {
|
2024-04-01 18:03:43 +02:00
|
|
|
$count = 0;
|
2024-04-02 07:47:24 +02:00
|
|
|
foreach ($items as $item) {
|
2024-04-01 18:03:43 +02:00
|
|
|
$preference = Preference::where('name', $item)->where('user_id', $user->id)->first();
|
2024-04-02 07:47:24 +02:00
|
|
|
if (null === $preference) {
|
2024-04-01 18:03:43 +02:00
|
|
|
continue;
|
|
|
|
}
|
2024-04-02 07:47:24 +02:00
|
|
|
if (null !== $preference->user_group_id) {
|
2024-04-01 18:03:43 +02:00
|
|
|
$preference->user_group_id = $user->user_group_id;
|
|
|
|
$preference->save();
|
2024-04-02 07:47:24 +02:00
|
|
|
++$count;
|
2024-04-01 18:03:43 +02:00
|
|
|
}
|
|
|
|
}
|
2024-04-02 07:47:24 +02:00
|
|
|
if ($count > 0) {
|
2024-04-01 18:03:43 +02:00
|
|
|
$this->info(sprintf('Migrated %d preference(s) for user #%d ("%s").', $count, $user->id, $user->email));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return CommandAlias::SUCCESS;
|
|
|
|
}
|
|
|
|
}
|