Completed
Pull Request — master (#58)
by
unknown
01:11
created

ImpersonateManager::getCurrentAuthGuardName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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