mirror of
https://github.com/Bubka/2FAuth.git
synced 2025-06-24 22:12:06 +02:00
Add Artisan command to fix inconsistent accounts after migration
This commit is contained in:
parent
7c0613ac77
commit
9e899aab53
96
app/Console/Commands/Maintenance/FixUnsplittedAccounts.php
Normal file
96
app/Console/Commands/Maintenance/FixUnsplittedAccounts.php
Normal file
@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands\Maintenance;
|
||||
|
||||
use App\TwoFAccount;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
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';
|
||||
|
||||
/**
|
||||
* Create a new command instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
|
||||
if (!Schema::hasColumn('twofaccounts', 'legacy_uri')) {
|
||||
$this->comment('2fauth:fix-unsplitted-accounts is useful only after SplitTwofaccountsUriInMultipleColumns migration ran');
|
||||
return;
|
||||
}
|
||||
else $this->line('Fetching accounts...');
|
||||
|
||||
$twofaccounts = TwoFAccount::where('otp_type', '')
|
||||
->where('secret', '')
|
||||
->where('algorithm', '')
|
||||
->where('digits', 0)
|
||||
->whereNull('period')
|
||||
->whereNull('counter')
|
||||
->get();
|
||||
|
||||
$this->line(sprintf('%d inconsistent accounts found', $twofaccounts->count()));
|
||||
|
||||
if ($twofaccounts->count() == 0) {
|
||||
$this->info('Nothing to fix');
|
||||
return;
|
||||
}
|
||||
|
||||
$this->line('Try to fix them...');
|
||||
$twofaccountService = resolve('App\Services\TwoFAccountService');
|
||||
|
||||
foreach ($twofaccounts as $twofaccount) {
|
||||
if ($twofaccount->legacy_uri === __('errors.indecipherable')) {
|
||||
$this->error(sprintf('Account #%d cannot be deciphered', $twofaccount->id));
|
||||
}
|
||||
else {
|
||||
try {
|
||||
// Get a consistent account
|
||||
$tempAccount = $twofaccountService->createFromUri($twofaccount->legacy_uri, false);
|
||||
|
||||
$twofaccount->otp_type = $tempAccount->otp_type;
|
||||
$twofaccount->secret = $tempAccount->secret;
|
||||
$twofaccount->algorithm = $tempAccount->algorithm;
|
||||
$twofaccount->digits = $tempAccount->digits;
|
||||
$twofaccount->period = $tempAccount->period;
|
||||
$twofaccount->counter = $tempAccount->counter;
|
||||
|
||||
$twofaccount->save();
|
||||
$this->info(sprintf('Account #%d fixed', $twofaccount->id));
|
||||
}
|
||||
catch (\Exception $ex) {
|
||||
$this->error(sprintf('Error while updating account #%d', $twofaccount->id));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->line('Task completed');
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user