|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Lab404\Impersonate\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\RedirectResponse; |
|
6
|
|
|
use Illuminate\Http\Request; |
|
7
|
|
|
use Illuminate\Routing\Controller; |
|
8
|
|
|
use Lab404\Impersonate\Services\ImpersonateManager; |
|
9
|
|
|
|
|
10
|
|
|
class ImpersonateController extends Controller |
|
11
|
|
|
{ |
|
12
|
|
|
/** @var ImpersonateManager */ |
|
13
|
|
|
protected $manager; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* ImpersonateController constructor. |
|
17
|
|
|
*/ |
|
18
|
|
|
public function __construct() |
|
19
|
|
|
{ |
|
20
|
|
|
$this->middleware('auth'); |
|
21
|
|
|
|
|
22
|
|
|
$this->manager = app()->make(ImpersonateManager::class); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param int $id |
|
27
|
|
|
* @param string|null $guardName |
|
28
|
|
|
* @return RedirectResponse |
|
29
|
|
|
* @throws \Exception |
|
30
|
|
|
*/ |
|
31
|
|
|
public function take(Request $request, $id, $guardName = null) |
|
32
|
|
|
{ |
|
33
|
|
|
$guardName = $guardName ?? $this->manager->getDefaultSessionGuard(); |
|
34
|
|
|
|
|
35
|
|
|
// Cannot impersonate yourself |
|
36
|
|
|
if ($id == $request->user()->getAuthIdentifier() && ($this->manager->getCurrentAuthGuardName() == $guardName)) { |
|
37
|
|
|
abort(403); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
// Cannot impersonate again if you're already impersonate a user |
|
41
|
|
|
if ($this->manager->isImpersonating()) { |
|
42
|
|
|
abort(403); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
if (!$request->user()->canImpersonate()) { |
|
46
|
|
|
abort(403); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$userToImpersonate = $this->manager->findUserById($id, $guardName); |
|
50
|
|
|
|
|
51
|
|
|
if ($userToImpersonate->canBeImpersonated()) { |
|
|
|
|
|
|
52
|
|
|
if ($this->manager->take($request->user(), $userToImpersonate, $guardName)) { |
|
53
|
|
|
$takeRedirect = $this->manager->getTakeRedirectTo(); |
|
54
|
|
|
if ($takeRedirect !== 'back') { |
|
55
|
|
|
return redirect()->to($takeRedirect); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return redirect()->back(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return RedirectResponse |
|
65
|
|
|
*/ |
|
66
|
|
|
public function leave() |
|
67
|
|
|
{ |
|
68
|
|
|
if (!$this->manager->isImpersonating()) { |
|
69
|
|
|
abort(403); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$this->manager->leave(); |
|
73
|
|
|
|
|
74
|
|
|
$leaveRedirect = $this->manager->getLeaveRedirectTo(); |
|
75
|
|
|
if ($leaveRedirect !== 'back') { |
|
76
|
|
|
return redirect()->to($leaveRedirect); |
|
77
|
|
|
} |
|
78
|
|
|
return redirect()->back(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function info(Request $request) |
|
82
|
|
|
{ |
|
83
|
|
|
$token = $request->bearerToken(); |
|
84
|
|
|
$response = [ |
|
85
|
|
|
'impersonating' => $this->manager->isImpersonating(), |
|
86
|
|
|
'data' => [ |
|
87
|
|
|
'user_to_impersonate' => $request->user(), |
|
88
|
|
|
'impersonator' => $this->manager->isImpersonating() ? |
|
89
|
|
|
$this->manager->findUserById($this->manager->getImpersonatorId()) : null, |
|
90
|
|
|
'token' => $token, |
|
91
|
|
|
], |
|
92
|
|
|
]; |
|
93
|
|
|
return response()->json($response); |
|
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.