Completed
Push — master ( e28708...370080 )
by Marceau
01:48
created

ImpersonateServiceProvider::registerAuthDriver()   B

Complexity

Conditions 4
Paths 1

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 12
nc 1
nop 0
1
<?php
2
3
namespace Lab404\Impersonate;
4
5
use Illuminate\Auth\AuthManager;
6
use Illuminate\Foundation\Application;
7
use Illuminate\Support\Facades\Blade;
8
use Lab404\Impersonate\Guard\SessionGuard;
9
use Lab404\Impersonate\Middleware\ProtectFromImpersonation;
10
use Lab404\Impersonate\Services\ImpersonateManager;
11
12
/**
13
 * Class ServiceProvider
14
 *
15
 * @package Lab404\Impersonate
16
 */
17
class ImpersonateServiceProvider extends \Illuminate\Support\ServiceProvider
18
{
19
    /**
20
     * @var bool
21
     */
22
    protected $defer = false;
23
24
    /**
25
     * @var string
26
     */
27
    protected $configName = 'laravel-impersonate';
28
29
    /**
30
     * Register the service provider.
31
     *
32
     * @return void
33
     */
34
    public function register()
35
    {
36
        $this->mergeConfig();
37
38
        $this->app->bind(ImpersonateManager::class, ImpersonateManager::class);
39
40
        $this->app->singleton(ImpersonateManager::class, function ($app) {
41
            return new ImpersonateManager($app);
42
        });
43
44
        $this->app->alias(ImpersonateManager::class, 'impersonate');
45
46
        $this->registerRoutesMacro();
47
        $this->registerBladeDirectives();
48
        $this->registerMiddleware();
49
        $this->registerAuthDriver();
50
    }
51
52
    /**
53
     * Bootstrap the application events.
54
     *
55
     * @return void
56
     */
57
    public function boot()
58
    {
59
        $this->publishConfig();
60
    }
61
62
    /**
63
     * Register plugin blade directives.
64
     *
65
     * @param   void
66
     * @return  void
67
     */
68
    protected function registerBladeDirectives()
69
    {
70
        Blade::directive('impersonating', function () {
71
            return '<?php if (app()["auth"]->check() && app()["auth"]->user()->isImpersonated()): ?>';
72
        });
73
74
        Blade::directive('endImpersonating', function () {
75
            return '<?php endif; ?>';
76
        });
77
78
        Blade::directive('canImpersonate', function () {
79
            return '<?php if (app()["auth"]->check() && app()["auth"]->user()->canImpersonate()): ?>';
80
        });
81
82
        Blade::directive('endCanImpersonate', function () {
83
            return '<?php endif; ?>';
84
        });
85
86
        Blade::directive('canBeImpersonated', function($expression) {
87
            $user = trim($expression);
88
            return "<?php if (app()['auth']->check() && app()['auth']->user()->id != {$user}->id && {$user}->canBeImpersonated()): ?>";
89
        });
90
91
        Blade::directive('endCanBeImpersonated', function() {
92
            return '<?php endif; ?>';
93
        });
94
    }
95
96
    /**
97
     * Register routes macro.
98
     *
99
     * @param   void
100
     * @return  void
101
     */
102
    protected function registerRoutesMacro()
103
    {
104
        $router = $this->app['router'];
105
106
        $router->macro('impersonate', function () use ($router) {
107
            $router->get('/impersonate/take/{id}',
108
                '\Lab404\Impersonate\Controllers\ImpersonateController@take')->name('impersonate');
109
            $router->get('/impersonate/leave',
110
                '\Lab404\Impersonate\Controllers\ImpersonateController@leave')->name('impersonate.leave');
111
        });
112
    }
113
114
    /**
115
     * @param   void
116
     * @return  void
117
     */
118
    protected function registerAuthDriver()
119
    {
120
        /** @var AuthManager $auth */
121
        $auth = $this->app['auth'];
122
123
        $auth->extend('session', function (Application $app, $name, array $config) use ($auth) {
124
            $provider = $auth->createUserProvider($config['provider']);
125
126
            $guard = new SessionGuard($name, $provider, $app['session.store']);
127
128
            if (method_exists($guard, 'setCookieJar')) {
129
                $guard->setCookieJar($app['cookie']);
130
            }
131
132
            if (method_exists($guard, 'setDispatcher')) {
133
                $guard->setDispatcher($app['events']);
134
            }
135
136
            if (method_exists($guard, 'setRequest')) {
137
                $guard->setRequest($app->refresh('request', $guard, 'setRequest'));
138
            }
139
140
            return $guard;
141
        });
142
    }
143
144
    /**
145
     * Register plugin middleware.
146
     *
147
     * @param   void
148
     * @return  void
149
     */
150
    public function registerMiddleware()
151
    {
152
        $this->app['router']->aliasMiddleware('impersonate.protect', ProtectFromImpersonation::class);
153
    }
154
155
    /**
156
     * Merge config file.
157
     *
158
     * @param   void
159
     * @return  void
160
     */
161
    protected function mergeConfig()
162
    {
163
        $configPath = __DIR__ . '/../config/' . $this->configName . '.php';
164
165
        $this->mergeConfigFrom($configPath, $this->configName);
166
    }
167
168
    /**
169
     * Publish config file.
170
     *
171
     * @param   void
172
     * @return  void
173
     */
174
    protected function publishConfig()
175
    {
176
        $configPath = __DIR__ . '/../config/' . $this->configName . '.php';
177
178
        $this->publishes([$configPath => config_path($this->configName . '.php')], 'impersonate');
179
    }
180
}