|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Lab404\Impersonate\Services; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
use Illuminate\Foundation\Application; |
|
7
|
|
|
use Lab404\Impersonate\Events\LeaveImpersonation; |
|
8
|
|
|
use Lab404\Impersonate\Events\TakeImpersonation; |
|
9
|
|
|
|
|
10
|
|
|
class ImpersonateManager |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var Application |
|
14
|
|
|
*/ |
|
15
|
|
|
private $app; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* UserFinder constructor. |
|
19
|
|
|
* |
|
20
|
|
|
* @param Application $app |
|
21
|
|
|
*/ |
|
22
|
|
|
public function __construct(Application $app) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->app = $app; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param int $id |
|
29
|
|
|
* @param string|null $guardName |
|
30
|
|
|
* @return Model |
|
31
|
|
|
*/ |
|
32
|
|
|
public function findUserById($id, $guardName = null) |
|
33
|
|
|
{ |
|
34
|
|
|
if($guardName) |
|
|
|
|
|
|
35
|
|
|
{ |
|
36
|
|
|
$userProvider = $this->app['config']->get('auth.guards.'. $guardName .'.provider'); |
|
37
|
|
|
$model = $this->app['config']->get('auth.providers.'. $userProvider .'.model'); |
|
38
|
|
|
|
|
39
|
|
|
if(!$model) |
|
40
|
|
|
throw new \Exception("Auth guard doesn not exist.", 1); |
|
41
|
|
|
|
|
42
|
|
|
} else { |
|
43
|
|
|
$model = $this->app['config']->get('auth.providers.users.model'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$user = call_user_func([ |
|
47
|
|
|
$model, |
|
48
|
|
|
'findOrFail' |
|
49
|
|
|
], $id); |
|
50
|
|
|
|
|
51
|
|
|
return $user; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return bool |
|
56
|
|
|
*/ |
|
57
|
|
|
public function isImpersonating() |
|
58
|
|
|
{ |
|
59
|
|
|
return session()->has($this->getSessionKey()); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param void |
|
64
|
|
|
* @return int|null |
|
65
|
|
|
*/ |
|
66
|
|
|
public function getImpersonatorId() |
|
67
|
|
|
{ |
|
68
|
|
|
return session($this->getSessionKey(), null); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return string|null |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getImpersonatorGuardName() |
|
75
|
|
|
{ |
|
76
|
|
|
return session($this->getSessionGuard(), null); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return string|null |
|
81
|
|
|
*/ |
|
82
|
|
|
public function getImpersonatorGuardUsingName() |
|
83
|
|
|
{ |
|
84
|
|
|
return session($this->getSessionGuardUsing(), null); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param Model $from |
|
89
|
|
|
* @param Model $to |
|
90
|
|
|
* @param string|null $guardName |
|
91
|
|
|
* @return bool |
|
92
|
|
|
*/ |
|
93
|
|
|
public function take($from, $to, $guardName = null) |
|
94
|
|
|
{ |
|
95
|
|
|
try { |
|
96
|
|
|
session()->put(config('laravel-impersonate.session_key'), $from->getKey()); |
|
97
|
|
|
session()->put(config('laravel-impersonate.session_guard'), $this->getCurrentAuthGuardName()); |
|
98
|
|
|
session()->put(config('laravel-impersonate.session_guard_using'), $guardName); |
|
99
|
|
|
|
|
100
|
|
|
$this->app['auth']->guard($this->getCurrentAuthGuardName())->quietLogout(); |
|
101
|
|
|
$this->app['auth']->guard($guardName)->quietLogin($to); |
|
102
|
|
|
|
|
103
|
|
|
} catch (\Exception $e) { |
|
104
|
|
|
|
|
105
|
|
|
unset($e); |
|
106
|
|
|
return false; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
$this->app['events']->fire(new TakeImpersonation($from, $to)); |
|
110
|
|
|
|
|
111
|
|
|
return true; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @return bool |
|
116
|
|
|
*/ |
|
117
|
|
|
public function leave() |
|
118
|
|
|
{ |
|
119
|
|
|
try { |
|
120
|
|
|
|
|
121
|
|
|
$impersonated = $this->app['auth']->guard($this->getImpersonatorGuardUsingName())->user(); |
|
122
|
|
|
$impersonator = $this->findUserById($this->getImpersonatorId(), $this->getImpersonatorGuardName()); |
|
123
|
|
|
|
|
124
|
|
|
$this->app['auth']->guard($this->getCurrentAuthGuardName())->quietLogout(); |
|
125
|
|
|
$this->app['auth']->guard($this->getImpersonatorGuardName())->quietLogin($impersonator); |
|
126
|
|
|
|
|
127
|
|
|
$this->clear(); |
|
128
|
|
|
|
|
129
|
|
|
} catch (\Exception $e) { |
|
130
|
|
|
dd($e); |
|
131
|
|
|
unset($e); |
|
132
|
|
|
return false; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
$this->app['events']->fire(new LeaveImpersonation($impersonator, $impersonated)); |
|
136
|
|
|
|
|
137
|
|
|
return true; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @return void |
|
142
|
|
|
*/ |
|
143
|
|
|
public function clear() |
|
144
|
|
|
{ |
|
145
|
|
|
session()->forget($this->getSessionKey()); |
|
146
|
|
|
session()->forget($this->getSessionGuard()); |
|
147
|
|
|
session()->forget($this->getSessionGuardUsing()); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @return string |
|
152
|
|
|
*/ |
|
153
|
|
|
public function getSessionKey() |
|
154
|
|
|
{ |
|
155
|
|
|
return config('laravel-impersonate.session_key'); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @return string |
|
160
|
|
|
*/ |
|
161
|
|
|
public function getSessionGuard() |
|
162
|
|
|
{ |
|
163
|
|
|
return config('laravel-impersonate.session_guard'); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @return string |
|
168
|
|
|
*/ |
|
169
|
|
|
public function getSessionGuardUsing() |
|
170
|
|
|
{ |
|
171
|
|
|
return config('laravel-impersonate.session_guard_using'); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @return string |
|
176
|
|
|
*/ |
|
177
|
|
|
public function getTakeRedirectTo() |
|
178
|
|
|
{ |
|
179
|
|
|
try { |
|
180
|
|
|
$uri = route(config('laravel-impersonate.take_redirect_to')); |
|
181
|
|
|
} catch (\InvalidArgumentException $e) { |
|
182
|
|
|
$uri = config('laravel-impersonate.take_redirect_to'); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
return $uri; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* @return string |
|
190
|
|
|
*/ |
|
191
|
|
|
public function getLeaveRedirectTo() |
|
192
|
|
|
{ |
|
193
|
|
|
try { |
|
194
|
|
|
$uri = route(config('laravel-impersonate.leave_redirect_to')); |
|
195
|
|
|
} catch (\InvalidArgumentException $e) { |
|
196
|
|
|
$uri = config('laravel-impersonate.leave_redirect_to'); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
return $uri; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* @return string|null |
|
204
|
|
|
*/ |
|
205
|
|
|
public function getCurrentAuthGuardName() |
|
206
|
|
|
{ |
|
207
|
|
|
$guards = array_keys(config('auth.guards')); |
|
208
|
|
|
foreach ($guards as $guard) { |
|
209
|
|
|
if ($this->app['auth']->guard($guard)->check()) { |
|
210
|
|
|
return $guard; |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
return null; |
|
214
|
|
|
} |
|
215
|
|
|
} |
|
216
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: