diff --git a/app/Console/Commands/Maintenance/FixUnsplittedAccounts.php b/app/Console/Commands/Maintenance/FixUnsplittedAccounts.php new file mode 100644 index 00000000..ab2f1950 --- /dev/null +++ b/app/Console/Commands/Maintenance/FixUnsplittedAccounts.php @@ -0,0 +1,96 @@ +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'); + } +} \ No newline at end of file