Passed
Push — master ( a89971...f48d5e )
by Iman
02:16
created

defineIsUsingMasterPass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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