|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Arcanedev\LaravelImpersonator; |
|
6
|
|
|
|
|
7
|
|
|
use Arcanedev\LaravelImpersonator\Contracts\Impersonator as ImpersonatorContract; |
|
8
|
|
|
use Arcanedev\Support\Providers\PackageServiceProvider; |
|
9
|
|
|
use Illuminate\Auth\SessionGuard as IlluminateSessionGuard; |
|
10
|
|
|
use Illuminate\Contracts\Foundation\Application; |
|
11
|
|
|
use Illuminate\Contracts\Support\DeferrableProvider; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class ImpersonatorServiceProvider |
|
15
|
|
|
* |
|
16
|
|
|
* @author ARCANEDEV <[email protected]> |
|
17
|
|
|
*/ |
|
18
|
|
|
class ImpersonatorServiceProvider extends PackageServiceProvider implements DeferrableProvider |
|
19
|
|
|
{ |
|
20
|
|
|
/* ----------------------------------------------------------------- |
|
21
|
|
|
| Properties |
|
22
|
|
|
| ----------------------------------------------------------------- |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Package name. |
|
27
|
|
|
* |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $package = 'impersonator'; |
|
31
|
|
|
|
|
32
|
|
|
/* ----------------------------------------------------------------- |
|
33
|
|
|
| Main Methods |
|
34
|
|
|
| ----------------------------------------------------------------- |
|
35
|
|
|
*/ |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Register the service provider. |
|
39
|
|
|
*/ |
|
40
|
112 |
|
public function register(): void |
|
41
|
|
|
{ |
|
42
|
112 |
|
parent::register(); |
|
43
|
|
|
|
|
44
|
112 |
|
$this->registerConfig(); |
|
45
|
|
|
|
|
46
|
56 |
|
$this->singleton(ImpersonatorContract::class, Impersonator::class); |
|
47
|
112 |
|
$this->extendAuthDriver(); |
|
48
|
112 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Boot the service provider. |
|
52
|
|
|
*/ |
|
53
|
112 |
|
public function boot(): void |
|
54
|
|
|
{ |
|
55
|
112 |
|
if ($this->app->runningInConsole()) { |
|
56
|
112 |
|
$this->publishConfig(); |
|
57
|
|
|
} |
|
58
|
112 |
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Get the services provided by the provider. |
|
62
|
|
|
* |
|
63
|
|
|
* @return array |
|
64
|
|
|
*/ |
|
65
|
4 |
|
public function provides(): array |
|
66
|
|
|
{ |
|
67
|
|
|
return [ |
|
68
|
4 |
|
ImpersonatorContract::class, |
|
69
|
|
|
]; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/* ----------------------------------------------------------------- |
|
73
|
|
|
| Other Methods |
|
74
|
|
|
| ----------------------------------------------------------------- |
|
75
|
|
|
*/ |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Extend the auth session driver. |
|
79
|
|
|
*/ |
|
80
|
112 |
|
private function extendAuthDriver(): void |
|
81
|
|
|
{ |
|
82
|
|
|
/** @var \Illuminate\Auth\AuthManager $auth */ |
|
83
|
112 |
|
$auth = $this->app['auth']; |
|
84
|
|
|
|
|
85
|
112 |
|
$auth->extend('session', function (Application $app, $name, array $config) use ($auth) { |
|
86
|
84 |
|
$provider = $auth->createUserProvider($config['provider']); |
|
87
|
84 |
|
$store = $app['session']->driver( |
|
88
|
84 |
|
$app['config']['impersonator.session.store'] |
|
89
|
|
|
); |
|
90
|
|
|
|
|
91
|
84 |
|
return tap( |
|
92
|
84 |
|
new Guard\SessionGuard($name, $provider, $store), |
|
|
|
|
|
|
93
|
84 |
|
function (IlluminateSessionGuard $guard) use ($app) { |
|
94
|
84 |
|
if (method_exists($guard, 'setCookieJar')) |
|
95
|
84 |
|
$guard->setCookieJar($app['cookie']); |
|
96
|
|
|
|
|
97
|
84 |
|
if (method_exists($guard, 'setDispatcher')) |
|
98
|
84 |
|
$guard->setDispatcher($app['events']); |
|
99
|
|
|
|
|
100
|
84 |
|
if (method_exists($guard, 'setRequest')) |
|
101
|
84 |
|
$guard->setRequest($app->refresh('request', $guard, 'setRequest')); |
|
102
|
84 |
|
} |
|
103
|
|
|
); |
|
104
|
112 |
|
}); |
|
105
|
112 |
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: