Skip to content

Beautiful Open-Source Form Builder

Add two-factor authentication management features in AdminController (#986)

Chirag Chhatralac0868d122f69
diff --git a/api/app/Http/Controllers/Admin/AdminController.php b/api/app/Http/Controllers/Admin/AdminController.php
index 4635b590c..6bddc97bf 100644
--- a/api/app/Http/Controllers/Admin/AdminController.php
+++ b/api/app/Http/Controllers/Admin/AdminController.php
@@ -63,6 +63,9 @@ class AdminController extends Controller
$user->makeVisible('meta');
+ // Get two-factor authentication status
+ $user->two_factor_enabled = $user->hasTwoFactorEnabled();
+
$workspaces = $user->workspaces()
->withCount('forms')
->get()
@@ -310,6 +313,44 @@ class AdminController extends Controller
]);
}
+ public function disableTwoFactorAuthentication(Request $request)
+ {
+ $request->validate([
+ 'user_id' => 'required',
+ 'reason' => 'required'
+ ]);
+
+ $user = User::findOrFail($request->get('user_id'));
+
+ if ($user->admin) {
+ return $this->error([
+ 'message' => 'You cannot disable 2FA for an admin.'
+ ]);
+ }
+
+ if (!$user->hasTwoFactorEnabled()) {
+ return $this->error([
+ 'message' => "Two-factor authentication is not enabled."
+ ]);
+ }
+
+ // Disable 2FA
+ $user->disableTwoFactorAuth();
+
+ self::log('Disable Two-Factor Authentication ', [
+ 'user_id' => $user->id,
+ 'user_email' => $user->email,
+ 'reason' => $request->get('reason')
+ ]);
+
+ $user->two_factor_enabled = false;
+
+ return $this->success([
+ 'message' => "Two-factor authentication has been disabled successfully.",
+ 'user' => $user->makeVisible('meta')
+ ]);
+ }
+
public static function log($message, $data = [])
{
$moderator = request()->user();
diff --git a/api/routes/api.php b/api/routes/api.php
index 9c872a741..c14ca1476 100644
--- a/api/routes/api.php
+++ b/api/routes/api.php
@@ -283,6 +283,11 @@ Route::group(['middleware' => 'auth.multi'], function () {
[\App\Http\Controllers\Admin\AdminController::class, 'unblockUser']
);
+ Route::post(
+ 'disable-two-factor-authentication',
+ [\App\Http\Controllers\Admin\AdminController::class, 'disableTwoFactorAuthentication']
+ );
+
Route::group(['prefix' => 'billing'], function () {
Route::get('{user}/email', [\App\Http\Controllers\Admin\BillingController::class, 'getEmail']);
Route::patch('/email', [\App\Http\Controllers\Admin\BillingController::class, 'updateEmail']);
diff --git a/api/tests/Feature/Admin/TwoFactorDisableTest.php b/api/tests/Feature/Admin/TwoFactorDisableTest.php
new file mode 100644
index 000000000..ac6d2b0fe
--- /dev/null
+++ b/api/tests/Feature/Admin/TwoFactorDisableTest.php
@@ -0,0 +1,118 @@
+<?php
+
+namespace Tests\Feature\Admin;
+
+use App\Models\User;
+use Illuminate\Support\Facades\Config;
+
+function setupUsersFor2FA()
+{
+ Config::set('opnform.moderator_emails', ['[email protected]']);
+ $moderator = User::factory()->create([
+ 'email' => '[email protected]',
+ ]);
+
+ $user = User::factory()->create();
+
+ return [$moderator, $user];
+}
+
+function enableTwoFactorFor(User $user): void
+{
+ $secret = $user->createTwoFactorAuth();
+ $code = $secret->makeCode();
+ $user->confirmTwoFactorAuth($code);
+}
+
+it('can disable 2FA for a user', function () {
+ [$moderator, $user] = setupUsersFor2FA();
+ enableTwoFactorFor($user);
+
+ expect($user->fresh()->hasTwoFactorEnabled())->toBeTrue();
+
+ $this->actingAs($moderator)
+ ->postJson('/moderator/disable-two-factor-authentication', [
+ 'user_id' => $user->id,
+ 'reason' => 'User lost access to authenticator app',
+ ])
+ ->assertSuccessful()
+ ->assertJson([
+ 'message' => 'Two-factor authentication has been disabled successfully.',
+ ])
+ ->assertJsonPath('user.two_factor_enabled', false);
+
+ expect($user->fresh()->hasTwoFactorEnabled())->toBeFalse();
+});
+
+it('does not allow a non-moderator to disable 2FA', function () {
+ $nonModerator = User::factory()->create();
+ $user = User::factory()->create();
+ enableTwoFactorFor($user);
+
+ $this->actingAs($nonModerator)
+ ->postJson('/moderator/disable-two-factor-authentication', [
+ 'user_id' => $user->id,
+ 'reason' => 'Attempting to disable 2FA',
+ ])
+ ->assertForbidden();
+
+ expect($user->fresh()->hasTwoFactorEnabled())->toBeTrue();
+});
+
+it('cannot disable 2FA for an admin user', function () {
+ [$moderator, $user] = setupUsersFor2FA();
+ // Make user an admin by adding their email to admin_emails config
+ Config::set('opnform.admin_emails', [$user->email]);
+ enableTwoFactorFor($user);
+
+ $this->actingAs($moderator)
+ ->postJson('/moderator/disable-two-factor-authentication', [
+ 'user_id' => $user->id,
+ 'reason' => 'Attempting to disable admin 2FA',
+ ])
+ ->assertStatus(400)
+ ->assertJson([
+ 'message' => 'You cannot disable 2FA for an admin.',
+ ]);
+
+ expect($user->fresh()->hasTwoFactorEnabled())->toBeTrue();
+});
+
+it('returns error when 2FA is not enabled', function () {
+ [$moderator, $user] = setupUsersFor2FA();
+
+ expect($user->hasTwoFactorEnabled())->toBeFalse();
+
+ $this->actingAs($moderator)
+ ->postJson('/moderator/disable-two-factor-authentication', [
+ 'user_id' => $user->id,
+ 'reason' => 'Test reason',
+ ])
+ ->assertStatus(400)
+ ->assertJson([
+ 'message' => 'Two-factor authentication is not enabled.',
+ ]);
+});
+
+it('requires reason to disable 2FA', function () {
+ [$moderator, $user] = setupUsersFor2FA();
+ enableTwoFactorFor($user);
+
+ $this->actingAs($moderator)
+ ->postJson('/moderator/disable-two-factor-authentication', [
+ 'user_id' => $user->id,
+ ])
+ ->assertStatus(422)
+ ->assertJsonValidationErrors(['reason']);
+});
+
+it('requires user_id to disable 2FA', function () {
+ [$moderator, $user] = setupUsersFor2FA();
+
+ $this->actingAs($moderator)
+ ->postJson('/moderator/disable-two-factor-authentication', [
+ 'reason' => 'Test reason',
+ ])
+ ->assertStatus(422)
+ ->assertJsonValidationErrors(['user_id']);
+});
diff --git a/client/components/pages/admin/TwoFactorDisable.vue b/client/components/pages/admin/TwoFactorDisable.vue
new file mode 100644
index 000000000..42b1b783b
--- /dev/null
+++ b/client/components/pages/admin/TwoFactorDisable.vue
@@ -0,0 +1,68 @@
+<template>
+ <AdminCard
+ v-if="props.user.two_factor_enabled"
+ title="Disable Two-Factor Authentication"
+ icon="i-heroicons-shield-exclamation-20-solid"
+ >
+ <div class="space-y-6 flex flex-col justify-between">
+ <UAlert
+ icon="i-heroicons-exclamation-triangle"
+ color="error"
+ variant="subtle"
+ >
+ <template #description>
+ <div>
+ Disabling two-factor authentication is a critical security action.
+ Make sure you have a solid reason and strong proof of account ownership (such as verified identity or written consent) before proceeding.
+ The user will be required to re-enable 2FA on their next login.
+ </div>
+ </template>
+ </UAlert>
+
+ <VForm @submit.prevent="submit">
+ <TextAreaInput
+ label="Reason"
+ name="reason"
+ :form="form"
+ :required="true"
+ help="Reason will be sent to slack for internal use only."
+ />
+ <div class="flex space-x-2 mt-4">
+ <UButton
+ block
+ :loading="form.busy"
+ type="submit"
+ class="grow"
+ :label="'Disable Two-Factor Authentication'"
+ />
+ </div>
+ </VForm>
+ </div>
+ </AdminCard>
+</template>
+
+<script setup>
+const props = defineProps({
+ user: { type: Object, required: true }
+})
+const emit = defineEmits(['user-updated'])
+
+const alert = useAlert()
+
+const form = useForm({
+ user_id: props.user.id,
+ reason: ''
+})
+
+async function submit() {
+ try {
+ let response
+ response = await form.post('/moderator/disable-two-factor-authentication')
+ alert.success(response.message)
+ emit('user-updated', response.user)
+ form.reset()
+ } catch (error) {
+ alert.error(error.data?.message || 'An error occurred.')
+ }
+}
+</script>
\ No newline at end of file
diff --git a/client/pages/admin.vue b/client/pages/admin.vue
index dd5140d27..a4b79340d 100644
--- a/client/pages/admin.vue
+++ b/client/pages/admin.vue
@@ -120,6 +120,10 @@
:user="userInfo"
@user-updated="onUserUpdate"
/>
+ <TwoFactorDisable
+ :user="userInfo"
+ @user-updated="onUserUpdate"
+ />
<ExtendTrial
:user="userInfo"
/>