Completed
Pull Request — master (#59)
by
unknown
01:24
created

ImpersonateManager::leave()   A

Complexity

Conditions 2
Paths 6

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

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