1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lab404\Impersonate\Services; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Illuminate\Foundation\Application; |
7
|
|
|
use Illuminate\Support\Facades\Auth; |
8
|
|
|
|
9
|
|
|
class ImpersonateManager |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var Application |
13
|
|
|
*/ |
14
|
|
|
private $app; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* UserFinder constructor. |
18
|
|
|
* |
19
|
|
|
* @param Application $app |
20
|
|
|
*/ |
21
|
|
|
public function __construct(Application $app) |
22
|
|
|
{ |
23
|
|
|
$this->app = $app; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param int $id |
28
|
|
|
* @return Model |
29
|
|
|
*/ |
30
|
|
|
public function findUserById($id) |
31
|
|
|
{ |
32
|
|
|
$model = $this->app['config']->get('auth.providers.users.model'); |
33
|
|
|
|
34
|
|
|
$user = call_user_func([ |
35
|
|
|
$model, |
36
|
|
|
'findOrFail' |
37
|
|
|
], $id); |
38
|
|
|
|
39
|
|
|
return $user; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return bool |
44
|
|
|
*/ |
45
|
|
|
public function isImpersonating() |
46
|
|
|
{ |
47
|
|
|
return session()->has($this->getSessionKey()); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param void |
52
|
|
|
* @return int|null |
53
|
|
|
*/ |
54
|
|
|
public function getImpersonatorId() |
55
|
|
|
{ |
56
|
|
|
return session($this->getSessionKey(), null); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param Model $from |
61
|
|
|
* @param Model $to |
62
|
|
|
* @return bool |
63
|
|
|
*/ |
64
|
|
|
public function take($from, $to) |
65
|
|
|
{ |
66
|
|
|
try |
67
|
|
|
{ |
68
|
|
|
session()->put(config('laravel-impersonate.session_key'), $from->id); |
69
|
|
|
$this->app['auth']->logout(); |
70
|
|
|
$this->app['auth']->login($to); |
71
|
|
|
|
72
|
|
|
} catch (\Exception $e) |
73
|
|
|
{ |
74
|
|
|
unset($e); |
75
|
|
|
return false; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return true; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
|
|
public function leave() |
85
|
|
|
{ |
86
|
|
|
try |
87
|
|
|
{ |
88
|
|
|
$this->app['auth']->logout(); |
89
|
|
|
$this->app['auth']->loginUsingId($this->getImpersonatorId()); |
90
|
|
|
$this->clear(); |
91
|
|
|
} catch (\Exception $e) { |
92
|
|
|
unset($e); |
93
|
|
|
return false; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return true; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return void |
101
|
|
|
*/ |
102
|
|
|
public function clear() |
103
|
|
|
{ |
104
|
|
|
session()->forget($this->getSessionKey()); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
|
|
public function getSessionKey() |
111
|
|
|
{ |
112
|
|
|
return config('laravel-impersonate.session_key'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
|
|
public function getTakeRedirectTo() |
119
|
|
|
{ |
120
|
|
|
return config('laravel-impersonate.take_redirect_to'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return string |
125
|
|
|
*/ |
126
|
|
|
public function getLeaveRedirectTo() |
127
|
|
|
{ |
128
|
|
|
return config('laravel-impersonate.leave_redirect_to'); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
|
|
public function getCantAccesIfImpersonateRedirectTo() |
135
|
|
|
{ |
136
|
|
|
return config('laravel-impersonate.cant_acces_if_impersonate_redirect_to'); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|