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->manager = app()->make(ImpersonateManager::class); |
21
|
|
|
|
22
|
|
|
$guard = $this->manager->getDefaultSessionGuard(); |
23
|
|
|
$this->middleware('auth:' . $guard)->only('take'); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param int $id |
28
|
|
|
* @param string|null $guardName |
29
|
|
|
* @return RedirectResponse |
30
|
|
|
* @throws \Exception |
31
|
|
|
*/ |
32
|
|
|
public function take(Request $request, $id, $guardName = null) |
33
|
|
|
{ |
34
|
|
|
$guardName = $guardName ?? $this->manager->getDefaultSessionGuard(); |
35
|
|
|
|
36
|
|
|
// Cannot impersonate yourself |
37
|
|
|
if ($id == $request->user()->getAuthIdentifier() && ($this->manager->getCurrentAuthGuardName() == $guardName)) { |
38
|
|
|
abort(403); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
// Cannot impersonate again if you're already impersonate a user |
42
|
|
|
if ($this->manager->isImpersonating()) { |
43
|
|
|
abort(403); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if (!$request->user()->canImpersonate()) { |
47
|
|
|
abort(403); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$userToImpersonate = $this->manager->findUserById($id, $guardName); |
51
|
|
|
|
52
|
|
|
if ($userToImpersonate->canBeImpersonated()) { |
|
|
|
|
53
|
|
|
if ($this->manager->take($request->user(), $userToImpersonate, $guardName)) { |
54
|
|
|
$takeRedirect = $this->manager->getTakeRedirectTo(); |
55
|
|
|
if ($takeRedirect !== 'back') { |
56
|
|
|
return redirect()->to($takeRedirect); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return redirect()->back(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return RedirectResponse |
66
|
|
|
*/ |
67
|
|
|
public function leave() |
68
|
|
|
{ |
69
|
|
|
if (!$this->manager->isImpersonating()) { |
70
|
|
|
abort(403); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$this->manager->leave(); |
74
|
|
|
|
75
|
|
|
$leaveRedirect = $this->manager->getLeaveRedirectTo(); |
76
|
|
|
if ($leaveRedirect !== 'back') { |
77
|
|
|
return redirect()->to($leaveRedirect); |
78
|
|
|
} |
79
|
|
|
return redirect()->back(); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
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.