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

Commit 72042ab

Browse files
authored
Merge pull request #3 from kim-dongit/master
Flexibility changes
2 parents d82261d + a6f10e7 commit 72042ab

File tree

8 files changed

+87
-41
lines changed

8 files changed

+87
-41
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
.DS_Store
2+
.Thumbs.db
3+
4+
/.idea

src/Providers/BaseProvider.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,18 @@
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', 'user');
18+
if ($conf === 'user') {
19+
return !is_null($user->twoFactorAuth);
20+
}
21+
22+
return $conf === 'enabled';
1823
}
1924
}

src/TwoFactorAuth.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ class TwoFactorAuth extends Model
3939
*/
4040
public function user() : BelongsTo
4141
{
42-
return $this->belongsTo(\App\User::class);
42+
return $this->belongsTo(\App\User::class, 'user_id', config('twofactor-auth.models.user.primaryKey', 'id'));
4343
}
4444
}

src/TwoFactorAuthServiceProvider.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,18 @@ public function boot()
2424
{
2525
$this->loadRoutesFrom(__DIR__.'/routes.php');
2626

27-
$this->loadMigrationsFrom(__DIR__.'/database/migrations');
28-
29-
$this->loadTranslationsFrom(__DIR__.'/resources/lang', 'twofactor-auth');
30-
31-
$this->loadViewsFrom(__DIR__.'/resources/views', 'twofactor-auth');
32-
3327
$this->publishes([
3428
__DIR__.'/config/twofactor-auth.php' => config_path('twofactor-auth.php'),
3529
], 'config');
3630

31+
$this->publishMigrations();
32+
33+
$this->loadTranslationsFrom(__DIR__.'/resources/lang', 'twofactor-auth');
3734
$this->publishes([
3835
__DIR__.'/resources/lang' => resource_path('lang/vendor/twofactor-auth'),
3936
], 'lang');
4037

38+
$this->loadViewsFrom(__DIR__.'/resources/views', 'twofactor-auth');
4139
$this->publishes([
4240
__DIR__.'/resources/views' => resource_path('views/vendor/twofactor-auth'),
4341
], 'views');
@@ -62,4 +60,23 @@ public function register()
6260
return $app->make(TwoFactorAuthManager::class)->provider();
6361
});
6462
}
63+
64+
/**
65+
* Adds current timestamp prefix.
66+
*/
67+
protected function publishMigrations()
68+
{
69+
$files = [
70+
'add_mobile_to_users_table.php',
71+
'create_two_factor_auths_table.php',
72+
];
73+
74+
$paths = [];
75+
76+
foreach ($files as $file) {
77+
$paths[__DIR__ . '/database/migrations/' . $file] = database_path('migrations/'.date('Y_m_d_His').'_'.$file);
78+
}
79+
$this->publishes($paths, 'migrations');
80+
}
81+
6582
}

src/TwoFactorAuthenticable.php

Lines changed: 30 additions & 2 deletions
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
{
@@ -14,7 +15,7 @@ trait TwoFactorAuthenticable
1415
*/
1516
public function twoFactorAuth() : HasOne
1617
{
17-
return $this->hasOne(TwoFactorAuth::class);
18+
return $this->hasOne(TwoFactorAuth::class, 'user_id', $this->getKeyName());
1819
}
1920

2021
/**
@@ -25,7 +26,15 @@ 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', 'user');
30+
if ($enabled === 'user') {
31+
// respect when 2fa is not set for user, never insert
32+
$this->twoFactorAuth->update(['id' => $id]);
33+
}
34+
35+
if ($enabled === 'enabled') {
36+
$this->upsertTwoFactorAuthId($id);
37+
}
2938
}
3039

3140
/**
@@ -37,4 +46,23 @@ public function getTwoFactorAuthId() : string
3746
{
3847
return $this->twoFactorAuth->id;
3948
}
49+
50+
/**
51+
* Create or update a two-factor authentication record with the given id.
52+
*
53+
* @param string $id
54+
* @return void
55+
*/
56+
private function upsertTwoFactorAuthId(string $id) : void
57+
{
58+
DB::transaction(function () use ($id) {
59+
$attributes = ['id' => $id];
60+
61+
if (!$this->twoFactorAuth()->exists()) {
62+
$this->twoFactorAuth()->create($attributes);
63+
} else {
64+
$this->twoFactorAuth->update($attributes);
65+
}
66+
});
67+
}
4068
}

src/config/twofactor-auth.php

Lines changed: 22 additions & 12 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+
| - 'enabled': always require two-factor authentication
12+
| - 'disabled': disabled, never require two-factor authentication
13+
| - 'user': look if a row exists in the two_factor_auths table for the
14+
| user
15+
|
16+
*/
17+
18+
'enabled' => 'user',
19+
520
/*
621
|--------------------------------------------------------------------------
722
| Default Two-Factor Authentication Provider
@@ -66,22 +81,17 @@
6681

6782
/*
6883
|--------------------------------------------------------------------------
69-
| Database Settings
84+
| Model setttings
7085
|--------------------------------------------------------------------------
7186
|
72-
| Starting from Laravel 5.8, the default is to use "bigIncrements" instead
73-
| of "increments" for the "id" column on the "users" table. This setting
74-
| allows you to control what type to use for the "user_id" column on the
75-
| "two_factor_auths" table. The default is to use "unsignedBigInteger" in
76-
| order to stay in line with the changes in Laravel 5.8.
77-
|
78-
| NOTE: Modifying this setting only has an effect before you run any
79-
| migrations for this package. If you need to change the signature of
80-
| "user_id" afterwards, you will have to write your own migration for this
81-
| (see install instructions for more details).
87+
| \App\User is used, but its primary key can be specified
8288
|
8389
*/
8490

85-
'big_int' => true,
91+
'models' => [
92+
'user' => [
93+
'primaryKey' => 'id'
94+
]
95+
],
8696

8797
];
Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,7 @@ public function up()
1616
{
1717
Schema::create('two_factor_auths', function (Blueprint $table) {
1818
$table->string('id')->nullable();
19-
20-
if ($this->useBigInt()) {
21-
$table->unsignedBigInteger('user_id');
22-
} else {
23-
$table->unsignedInteger('user_id');
24-
}
25-
19+
$table->unsignedBigInteger('user_id');
2620
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
2721
$table->timestamps();
2822
});
@@ -37,15 +31,4 @@ public function down()
3731
{
3832
Schema::dropIfExists('two_factor_auths');
3933
}
40-
41-
/**
42-
* Determine if the type of the "user_id" column should be of type
43-
* "unsignedBigInteger".
44-
*
45-
* @return bool
46-
*/
47-
private function useBigInt()
48-
{
49-
return config('twofactor-auth.big_int');
50-
}
5134
}

0 commit comments

Comments
 (0)