Skip to content
This repository was archived by the owner on Apr 19, 2025. It is now read-only.

Commit c9cb9f1

Browse files
committed
Added option to always enable the two-factor authentication (for all users) and to disable it.
1 parent 5c16267 commit c9cb9f1

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

src/Providers/BaseProvider.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@
77
abstract class BaseProvider
88
{
99
/**
10-
* Check if two-factor authentication is enabled for a user.
10+
* Check if two-factor authentication is enabled, dependent on the "enabled" config option.
1111
*
1212
* @param \App\User $user
1313
* @return bool
1414
*/
1515
public function enabled(User $user)
1616
{
17-
return !is_null($user->twoFactorAuth);
17+
$conf = config('twofactor-auth.enabled', 'per_user');
18+
if ($conf === 'per_user') {
19+
return !is_null($user->twoFactorAuth);
20+
}
21+
return (bool) $conf;
1822
}
1923
}

src/TwoFactorAuthenticable.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Database\Eloquent\Relations\HasOne;
66
use MichaelDzjap\TwoFactorAuth\TwoFactorAuth;
7+
use Illuminate\Support\Facades\DB;
78

89
trait TwoFactorAuthenticable
910
{
@@ -25,7 +26,14 @@ public function twoFactorAuth() : HasOne
2526
*/
2627
public function setTwoFactorAuthId(string $id) : void
2728
{
28-
$this->twoFactorAuth->update(['id' => $id]);
29+
$enabled = config('twofactor-auth.enabled', 'per_user');
30+
if ($enabled === 'per_user') {
31+
// respect when 2fa is not set for user, never insert
32+
$this->twoFactorAuth->update(['id' => $id]);
33+
}
34+
elseif ($enabled) {
35+
$this->upsertTwoFactorAuthId($id);
36+
}
2937
}
3038

3139
/**
@@ -37,4 +45,19 @@ public function getTwoFactorAuthId() : string
3745
{
3846
return $this->twoFactorAuth->id;
3947
}
48+
49+
/**
50+
* @param string $id
51+
*/
52+
private function upsertTwoFactorAuthId(string $id) : void
53+
{
54+
DB::transaction(function () use ($id) {
55+
$attributes = ['id' => $id];
56+
if (!$this->twoFactorAuth()->exists()) {
57+
$this->twoFactorAuth()->create($attributes);
58+
} else {
59+
$this->twoFactorAuth->update($attributes);
60+
}
61+
});
62+
}
4063
}

src/config/twofactor-auth.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,21 @@
22

33
return [
44

5+
/*
6+
|--------------------------------------------------------------------------
7+
| Enabled
8+
|--------------------------------------------------------------------------
9+
|
10+
| Options:
11+
| - true: always require two-factor authentication
12+
| - false: never require two-factor authentication
13+
| - 'per_user': look if a row exists in the two_factor_auths table for the
14+
| user
15+
|
16+
*/
17+
18+
'enabled' => 'per_user',
19+
520
/*
621
|--------------------------------------------------------------------------
722
| Default Two-Factor Authentication Provider

0 commit comments

Comments
 (0)