Completed
Pull Request — master (#44)
by
unknown
01:33
created

ImpersonateManager::findUserById()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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