1
|
|
|
<?php namespace Arcanedev\LaravelImpersonator; |
2
|
|
|
|
3
|
|
|
use Arcanedev\LaravelImpersonator\Contracts\Impersonatable; |
4
|
|
|
use Illuminate\Contracts\Foundation\Application; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class Impersonator |
8
|
|
|
* |
9
|
|
|
* @package Arcanedev\LaravelImpersonator |
10
|
|
|
* @author ARCANEDEV <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class Impersonator implements Contracts\Impersonator |
13
|
|
|
{ |
14
|
|
|
/* ----------------------------------------------------------------- |
15
|
|
|
| Properties |
16
|
|
|
| ----------------------------------------------------------------- |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
/** @var \Illuminate\Contracts\Foundation\Application */ |
20
|
|
|
protected $app; |
21
|
|
|
|
22
|
|
|
/* ----------------------------------------------------------------- |
23
|
|
|
| Constructor |
24
|
|
|
| ----------------------------------------------------------------- |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Impersonator constructor. |
29
|
|
|
* |
30
|
|
|
* @param \Illuminate\Contracts\Foundation\Application $app |
31
|
|
|
*/ |
32
|
39 |
|
public function __construct(Application $app) |
33
|
|
|
{ |
34
|
39 |
|
$this->app = $app; |
35
|
39 |
|
} |
36
|
|
|
|
37
|
|
|
/* ----------------------------------------------------------------- |
38
|
|
|
| Getters & Setters |
39
|
|
|
| ----------------------------------------------------------------- |
40
|
|
|
*/ |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Get the guard session instance. |
44
|
|
|
* |
45
|
|
|
* @return \Illuminate\Auth\AuthManager|\Arcanedev\LaravelImpersonator\Guard\SessionGuard |
46
|
|
|
*/ |
47
|
15 |
|
protected function auth() |
48
|
|
|
{ |
49
|
15 |
|
return $this->app['auth']; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get the session store instance. |
54
|
|
|
* |
55
|
|
|
* @return \Illuminate\Contracts\Session\Session |
56
|
|
|
*/ |
57
|
21 |
|
protected function session() |
58
|
|
|
{ |
59
|
21 |
|
return $this->app['session']; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get the config repository. |
64
|
|
|
* |
65
|
|
|
* @return \Illuminate\Contracts\Config\Repository |
66
|
|
|
*/ |
67
|
36 |
|
protected function config() |
68
|
|
|
{ |
69
|
36 |
|
return $this->app['config']; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get the event dispatcher. |
74
|
|
|
* |
75
|
|
|
* @return \Illuminate\Contracts\Events\Dispatcher |
76
|
|
|
*/ |
77
|
15 |
|
protected function events() |
78
|
|
|
{ |
79
|
15 |
|
return $this->app['events']; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get the session key. |
84
|
|
|
* |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
21 |
|
public function getSessionKey() |
88
|
|
|
{ |
89
|
21 |
|
return $this->config()->get('impersonator.session.key', 'impersonator_id'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get the impersonator id. |
94
|
|
|
* |
95
|
|
|
* @return int|null |
96
|
|
|
*/ |
97
|
18 |
|
public function getImpersonatorId() |
98
|
|
|
{ |
99
|
18 |
|
return $this->session()->get($this->getSessionKey(), null); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/* ----------------------------------------------------------------- |
103
|
|
|
| Main Methods |
104
|
|
|
| ----------------------------------------------------------------- |
105
|
|
|
*/ |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Start the impersonation. |
109
|
|
|
* |
110
|
|
|
* @param \Arcanedev\LaravelImpersonator\Contracts\Impersonatable $impersonator |
111
|
|
|
* @param \Arcanedev\LaravelImpersonator\Contracts\Impersonatable $impersonated |
112
|
|
|
* |
113
|
|
|
* @return bool |
114
|
|
|
*/ |
115
|
27 |
|
public function start(Impersonatable $impersonator, Impersonatable $impersonated) |
116
|
|
|
{ |
117
|
27 |
|
$this->checkImpersonation($impersonator, $impersonated); |
118
|
|
|
|
119
|
|
|
try { |
120
|
15 |
|
session()->put($this->getSessionKey(), $impersonator->getAuthIdentifier()); |
121
|
15 |
|
$this->auth()->silentLogout(); |
122
|
15 |
|
$this->auth()->silentLogin($impersonated); |
123
|
|
|
|
124
|
15 |
|
$this->events()->dispatch( |
125
|
15 |
|
new Events\ImpersonationStarted($impersonator, $impersonated) |
126
|
5 |
|
); |
127
|
|
|
|
128
|
15 |
|
return true; |
129
|
|
|
} |
130
|
|
|
catch (\Exception $e) { return false; } |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Stop the impersonation. |
135
|
|
|
* |
136
|
|
|
* @return bool |
137
|
|
|
*/ |
138
|
12 |
|
public function stop() |
139
|
|
|
{ |
140
|
|
|
try { |
141
|
12 |
|
$impersonated = $this->auth()->user(); |
142
|
12 |
|
$impersonator = $this->findUserById($this->getImpersonatorId()); |
143
|
|
|
|
144
|
12 |
|
$this->auth()->silentLogout(); |
145
|
12 |
|
$this->auth()->silentLogin($impersonator); |
146
|
12 |
|
$this->clear(); |
147
|
|
|
|
148
|
12 |
|
$this->events()->dispatch( |
149
|
12 |
|
new Events\ImpersonationStopped($impersonator, $impersonated) |
150
|
4 |
|
); |
151
|
|
|
|
152
|
12 |
|
return true; |
153
|
|
|
} |
154
|
|
|
catch (\Exception $e) { return false; } |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Clear the impersonation. |
159
|
|
|
*/ |
160
|
15 |
|
public function clear() |
161
|
|
|
{ |
162
|
15 |
|
$this->session()->forget($this->getSessionKey()); |
163
|
15 |
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Find a user by the given id. |
167
|
|
|
* |
168
|
|
|
* @param int|string $id |
169
|
|
|
* |
170
|
|
|
* @return \Arcanedev\LaravelImpersonator\Contracts\Impersonatable |
171
|
|
|
* |
172
|
|
|
* @throws \Exception |
173
|
|
|
*/ |
174
|
30 |
|
public function findUserById($id) |
175
|
|
|
{ |
176
|
30 |
|
return call_user_func([$this->config()->get('auth.providers.users.model'), 'findOrFail'], $id); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/* ----------------------------------------------------------------- |
180
|
|
|
| Check Functions |
181
|
|
|
| ----------------------------------------------------------------- |
182
|
|
|
*/ |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Check if it's impersonating. |
186
|
|
|
* |
187
|
|
|
* @return bool |
188
|
|
|
*/ |
189
|
18 |
|
public function isImpersonating() |
190
|
|
|
{ |
191
|
18 |
|
return $this->session()->has($this->getSessionKey()); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Check if the impersonations is enabled. |
196
|
|
|
* |
197
|
|
|
* @return bool |
198
|
|
|
*/ |
199
|
27 |
|
public function isEnabled() |
200
|
|
|
{ |
201
|
27 |
|
return $this->config()->get('impersonator.enabled', false); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/* ----------------------------------------------------------------- |
205
|
|
|
| Other Methods |
206
|
|
|
| ----------------------------------------------------------------- |
207
|
|
|
*/ |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Check the impersonation. |
211
|
|
|
* |
212
|
|
|
* @param \Arcanedev\LaravelImpersonator\Contracts\Impersonatable $impersonator |
213
|
|
|
* @param \Arcanedev\LaravelImpersonator\Contracts\Impersonatable $impersonated |
214
|
|
|
* |
215
|
|
|
* @throws Exceptions\ImpersonationException |
216
|
|
|
*/ |
217
|
27 |
|
private function checkImpersonation(Impersonatable $impersonator, Impersonatable $impersonated) |
218
|
|
|
{ |
219
|
27 |
|
if ( ! $this->isEnabled()) |
220
|
11 |
|
throw new Exceptions\ImpersonationException("The impersonation is disabled."); |
221
|
|
|
|
222
|
24 |
|
if ($impersonator->getAuthIdentifier() == $impersonated->getAuthIdentifier()) |
223
|
10 |
|
throw new Exceptions\ImpersonationException('The impersonator & impersonated with must be different.'); |
224
|
|
|
|
225
|
21 |
|
if ( ! $impersonator->canImpersonate()) |
226
|
9 |
|
throw new Exceptions\ImpersonationException( |
227
|
3 |
|
"The impersonator with `{$impersonator->getAuthIdentifierName()}`=[{$impersonator->getAuthIdentifier()}] doesn't have the ability to impersonate." |
228
|
1 |
|
); |
229
|
|
|
|
230
|
18 |
|
if ( ! $impersonated->canBeImpersonated()) |
231
|
8 |
|
throw new Exceptions\ImpersonationException( |
232
|
3 |
|
"The impersonated with `{$impersonated->getAuthIdentifierName()}`=[{$impersonated->getAuthIdentifier()}] cannot be impersonated." |
233
|
1 |
|
); |
234
|
15 |
|
} |
235
|
|
|
} |
236
|
|
|
|