Skip to content

Beautiful Open-Source Form Builder

Add console command to disable two-factor authentication for users (#992)

Chirag Chhatralaebd66acedfe5
diff --git a/api/app/Console/Commands/DisableTwoFactor.php b/api/app/Console/Commands/DisableTwoFactor.php
new file mode 100644
index 000000000..7b6a6fe8f
--- /dev/null
+++ b/api/app/Console/Commands/DisableTwoFactor.php
@@ -0,0 +1,80 @@
+<?php
+
+namespace App\Console\Commands;
+
+use App\Models\User;
+use Illuminate\Console\Command;
+use Illuminate\Support\Facades\Log;
+
+class DisableTwoFactor extends Command
+{
+ /**
+ * The name and signature of the console command.
+ *
+ * @var string
+ */
+ protected $signature = 'user:disable-two-factor {user_email} {reason}
+ {--force : Skip confirmation prompt}
+ {--allow-admin : Allow disabling 2FA for admin users (requires confirmation)}';
+
+ /**
+ * The console command description.
+ *
+ * @var string
+ */
+ protected $description = 'Disable Two-Factor Authentication for a User';
+
+ /**
+ * Execute the console command.
+ *
+ * @return int
+ */
+ public function handle()
+ {
+ $user = User::whereEmail(strtolower($this->argument('user_email')))->first();
+ if (!$user) {
+ $this->error("User not found.");
+ return Command::FAILURE;
+ }
+
+ if ($user->admin) {
+ if (!$this->option('allow-admin')) {
+ $this->error('You cannot disable 2FA for an admin. Use --allow-admin flag if intended.');
+ return Command::FAILURE;
+ }
+
+ if ($this->option('force')) {
+ $this->error('Cannot use --force with --allow-admin for safety reasons.');
+ return Command::FAILURE;
+ }
+
+ if (!$this->confirm('WARNING: This user is an admin. Are you sure you want to disable their 2FA?', false)) {
+ $this->info('Operation cancelled.');
+ return Command::SUCCESS;
+ }
+ }
+
+ if (!$user->hasTwoFactorEnabled()) {
+ $this->error("Two-factor authentication is not enabled.");
+ return Command::FAILURE;
+ }
+
+ if (!$this->option('force') && !$this->confirm('Are you sure you want to disable two-factor authentication for user ' . $this->argument('user_email') . '?', true)) {
+ $this->info('Operation cancelled.');
+ return Command::SUCCESS;
+ }
+
+ $user->disableTwoFactorAuth();
+
+ Log::channel('slack_admin')->warning('Via Command: Disable Two-Factor Authentication ', [
+ 'user_id' => $user->id,
+ 'user_email' => $user->email,
+ 'reason' => $this->argument('reason'),
+ 'admin_override' => $this->option('allow-admin'),
+ ]);
+
+ $this->info("Two-factor authentication has been disabled successfully.");
+
+ return Command::SUCCESS;
+ }
+}
diff --git a/docs/configuration/disable-2fa.mdx b/docs/configuration/disable-2fa.mdx
new file mode 100644
index 000000000..b845fa495
--- /dev/null
+++ b/docs/configuration/disable-2fa.mdx
@@ -0,0 +1,66 @@
+---
+title: Disable Two-Factor Authentication
+description: Administrative command to disable two-factor authentication for users
+---
+
+The `user:disable-two-factor` command allows administrators to disable two-factor authentication (2FA) for users who have lost access to their authenticator app or recovery codes.
+
+<Warning>
+ This command should only be used in exceptional circumstances. All usage is logged for security auditing purposes.
+</Warning>
+
+## Command Signature
+
+```bash
+php artisan user:disable-two-factor {user_email} {reason} [--force] [--allow-admin]
+```
+
+| Parameter/Option | Description |
+| ---------------- | ------------------------------------------------------------------------------ |
+| `user_email` | The email address of the user (case-insensitive) |
+| `reason` | Reason for disabling 2FA (required for audit logging) |
+| `--force` | Skip confirmation prompt (non-admin users only) |
+| `--allow-admin` | Allow disabling 2FA for admin users (always requires interactive confirmation) |
+
+## Usage Examples
+
+```bash
+# Standard usage (with confirmation prompt)
+php artisan user:disable-two-factor [email protected] "User lost access to authenticator app"
+
+# Non-interactive mode
+php artisan user:disable-two-factor [email protected] "Automated recovery" --force
+
+# For admin users (requires confirmation, cannot use --force)
+php artisan user:disable-two-factor [email protected] "Emergency access" --allow-admin
+```
+
+## Running in Docker
+
+<Tabs>
+<Tab title="Docker Compose">
+```bash
+# Non-interactive
+docker compose exec api php artisan user:disable-two-factor [email protected] "Lost authenticator" --force
+
+# Interactive (for admin users)
+docker compose exec -it api php artisan user:disable-two-factor [email protected] "Emergency" --allow-admin
+```
+</Tab>
+
+<Tab title="Docker (standalone)">
+```bash
+docker exec <container_name> php artisan user:disable-two-factor [email protected] "Lost authenticator" --force
+```
+</Tab>
+</Tabs>
+
+<Note>
+ Use `-it` flags for interactive commands (without `--force`) to allow the confirmation prompt.
+</Note>
+
+## Security Notes
+
+- All operations are logged to the Slack admin channel with user ID, email, reason, and admin override status
+- Admin users require `--allow-admin` flag and cannot bypass confirmation with `--force`
+- Users who can access their authenticator should disable 2FA through their account settings instead
diff --git a/docs/docs.json b/docs/docs.json
index fcf71cc36..484444d83 100644
--- a/docs/docs.json
+++ b/docs/docs.json
@@ -38,7 +38,8 @@
"configuration/email-setup",
"configuration/custom-domain",
"configuration/subdomain-redirect",
- "configuration/oidc-sso"
+ "configuration/oidc-sso",
+ "configuration/disable-2fa"
]
}
]