|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Imanghafoori\MasterPass; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Auth\Events\Logout; |
|
6
|
|
|
use Illuminate\Support\Facades\Auth; |
|
7
|
|
|
use Illuminate\Support\Facades\Event; |
|
8
|
|
|
use Illuminate\Support\ServiceProvider; |
|
9
|
|
|
|
|
10
|
|
|
class MasterPassServiceProvider extends ServiceProvider |
|
11
|
|
|
{ |
|
12
|
|
|
public function boot() |
|
13
|
|
|
{ |
|
14
|
|
|
$this->publishes([__DIR__.'/config/master_password.php' => config_path('master_password.php')], 'master_password'); |
|
15
|
|
|
|
|
16
|
|
|
$this->defineIsUsingMasterPass(); |
|
17
|
|
|
|
|
18
|
|
|
Event::listen(Logout::class, function () { |
|
19
|
|
|
session()->remove(config('master_password.session_key')); |
|
20
|
|
|
}); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Register any application services. |
|
25
|
|
|
* |
|
26
|
|
|
* @return void |
|
27
|
|
|
*/ |
|
28
|
|
|
public function register() |
|
29
|
|
|
{ |
|
30
|
|
|
$this->app->bind("Laravel\Passport\Bridge\UserRepository", PassportUserRepository::class); |
|
31
|
|
|
$this->registerAuthProviders(); |
|
32
|
|
|
|
|
33
|
|
|
$this->mergeConfigFrom(__DIR__.'/config/master_password.php', 'master_password'); |
|
34
|
|
|
if (config('master_password.MASTER_PASSWORD')) { |
|
35
|
|
|
$this->changeUsersDriver(); |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* If the users driver is set to eloquent or database |
|
41
|
|
|
* it changes to 'eloquent' to eloquentMasterPass and |
|
42
|
|
|
* 'database' to databaseMasterPass, |
|
43
|
|
|
* otherwise it does nothing. |
|
44
|
|
|
* |
|
45
|
|
|
* @return null |
|
46
|
|
|
*/ |
|
47
|
|
|
private function changeUsersDriver() |
|
48
|
|
|
{ |
|
49
|
|
|
foreach (config('auth.providers', []) as $providerName => $providerConfig) { |
|
50
|
|
|
$driver = $providerConfig['driver']; |
|
51
|
|
|
|
|
52
|
|
|
if (in_array($driver, ['eloquent', 'database'])) { |
|
53
|
|
|
config()->set("auth.providers.$providerName.driver", $driver.'MasterPassword'); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
private function registerAuthProviders() |
|
59
|
|
|
{ |
|
60
|
|
|
\Auth::provider('eloquentMasterPassword', function ($app, array $config) { |
|
61
|
|
|
return new MasterPassEloquentUserProvider($app['hash'], $config['model']); |
|
62
|
|
|
}); |
|
63
|
|
|
|
|
64
|
|
|
\Auth::provider('databaseMasterPassword', function ($app, array $config) { |
|
65
|
|
|
$connection = $app['db']->connection(); |
|
66
|
|
|
|
|
67
|
|
|
return new MasterPassDatabaseUserProvider($connection, $app['hash'], $config['table']); |
|
68
|
|
|
}); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
private function defineIsUsingMasterPass() |
|
72
|
|
|
{ |
|
73
|
|
|
Auth::macro('isLoggedInByMasterPass', function () { |
|
74
|
|
|
return session(config('master_password.session_key'), false); |
|
75
|
|
|
}); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|