This repository was archived by the owner on Apr 19, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +45
-3
lines changed Expand file tree Collapse file tree 3 files changed +45
-3
lines changed Original file line number Diff line number Diff line change 77abstract 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}
Original file line number Diff line number Diff line change 44
55use Illuminate \Database \Eloquent \Relations \HasOne ;
66use MichaelDzjap \TwoFactorAuth \TwoFactorAuth ;
7+ use Illuminate \Support \Facades \DB ;
78
89trait 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}
Original file line number Diff line number Diff line change 22
33return [
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
You can’t perform that action at this time.
0 commit comments