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
|
|
|
* @return RedirectResponse |
28
|
|
|
*/ |
29
|
|
|
public function take(Request $request, $id) |
30
|
|
|
{ |
31
|
|
|
// Cannot impersonate yourself |
32
|
|
|
if ($id == $request->user()->id) { |
33
|
|
|
abort(403); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
// Cannot impersonate again if you're already impersonate a user |
37
|
|
|
if ($this->manager->isImpersonating()) { |
38
|
|
|
abort(403); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
if (!$request->user()->canImpersonate()) { |
42
|
|
|
abort(403); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if (!$this->manager->findUserById($id)) { |
46
|
|
|
abort(403); |
47
|
|
|
} else { |
48
|
|
|
$user_to_impersonate = $this->manager->findUserById($id); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if ($user_to_impersonate->canBeImpersonated() && $this->manager->take($request->user(), $user_to_impersonate)) { |
|
|
|
|
52
|
|
|
return redirect()->to($this->manager->getTakeRedirectTo()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return redirect()->back(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/* |
59
|
|
|
* @return RedirectResponse |
60
|
|
|
*/ |
61
|
|
|
public function leave() |
62
|
|
|
{ |
63
|
|
|
if (!$this->manager->isImpersonating()) { |
64
|
|
|
abort(403); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$this->manager->leave(); |
68
|
|
|
|
69
|
|
|
return redirect()->to($this->manager->getLeaveRedirectTo()); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: