2021-10-15 14:28:04 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Console\Commands\Maintenance;
|
|
|
|
|
2021-12-02 13:15:53 +01:00
|
|
|
use App\Models\TwoFAccount;
|
2021-10-15 14:28:04 +02:00
|
|
|
use Illuminate\Console\Command;
|
|
|
|
use Illuminate\Support\Facades\Schema;
|
|
|
|
|
2021-11-30 17:39:33 +01:00
|
|
|
/**
|
|
|
|
* @codeCoverageIgnore
|
|
|
|
*/
|
2021-10-15 14:28:04 +02:00
|
|
|
class FixUnsplittedAccounts extends Command
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The name and signature of the console command.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $signature = '2fauth:fix-unsplitted-accounts';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The console command description.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $description = 'Try to fix accounts that haven\t been splitted during SplitTwofaccountsUriInMultipleColumns migration';
|
|
|
|
|
2023-09-20 14:55:28 +02:00
|
|
|
/**
|
|
|
|
* Indicates whether the command should be shown in the Artisan command list.
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $hidden = true;
|
|
|
|
|
2021-10-15 14:28:04 +02:00
|
|
|
/**
|
|
|
|
* Create a new command instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the console command.
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
2022-11-22 15:15:52 +01:00
|
|
|
if (! Schema::hasColumn('twofaccounts', 'legacy_uri')) {
|
2021-10-15 14:28:04 +02:00
|
|
|
$this->comment('2fauth:fix-unsplitted-accounts is useful only after SplitTwofaccountsUriInMultipleColumns migration ran');
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2021-10-15 14:28:04 +02:00
|
|
|
return;
|
2022-11-22 15:15:52 +01:00
|
|
|
} else {
|
|
|
|
$this->line('Fetching accounts...');
|
2021-10-15 14:28:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$twofaccounts = TwoFAccount::where('otp_type', '')
|
2023-12-20 16:55:58 +01:00
|
|
|
->where('secret', '')
|
|
|
|
->where('algorithm', '')
|
|
|
|
->where('digits', 0)
|
|
|
|
->whereNull('period')
|
|
|
|
->whereNull('counter')
|
|
|
|
->get();
|
2021-10-15 14:28:04 +02:00
|
|
|
|
|
|
|
$this->line(sprintf('%d inconsistent accounts found', $twofaccounts->count()));
|
|
|
|
|
|
|
|
if ($twofaccounts->count() == 0) {
|
|
|
|
$this->info('Nothing to fix');
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2021-10-15 14:28:04 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->line('Try to fix them...');
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2021-10-15 14:28:04 +02:00
|
|
|
foreach ($twofaccounts as $twofaccount) {
|
|
|
|
if ($twofaccount->legacy_uri === __('errors.indecipherable')) {
|
|
|
|
$this->error(sprintf('Account #%d cannot be deciphered', $twofaccount->id));
|
2022-11-22 15:15:52 +01:00
|
|
|
} else {
|
2021-10-15 14:28:04 +02:00
|
|
|
try {
|
|
|
|
// Get a consistent account
|
2022-07-28 22:01:33 +02:00
|
|
|
$twofaccount->fillWithURI($twofaccount->legacy_uri, false, true);
|
2021-10-15 14:28:04 +02:00
|
|
|
$twofaccount->save();
|
2022-07-28 22:01:33 +02:00
|
|
|
|
2021-10-15 14:28:04 +02:00
|
|
|
$this->info(sprintf('Account #%d fixed', $twofaccount->id));
|
2022-11-22 15:15:52 +01:00
|
|
|
} catch (\Exception $ex) {
|
2021-10-15 14:28:04 +02:00
|
|
|
$this->error(sprintf('Error while updating account #%d', $twofaccount->id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->line('Task completed');
|
|
|
|
}
|
2022-11-22 15:15:52 +01:00
|
|
|
}
|