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

ImpersonateManager::findUserById()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 2
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 = 'web')
33
    {
34
        $provider = $this->app['config']->get('auth.guards.'.$guard.'.provider');
35
        $model = $this->app['config']->get('auth.providers.'.$provider.'.model');
36
37
        $user = call_user_func([
38
            $model,
39
            'findOrFail'
40
        ], $id);
41
42
        return $user;
43
    }
44
45
    /**
46
     * @return bool
47
     */
48
    public function isImpersonating()
49
    {
50
        return session()->has($this->getSessionKey());
51
    }
52
53
    /**
54
     * @param   void
55
     * @return  int|null
56
     */
57
    public function getImpersonatorId()
58
    {
59
        return session($this->getSessionKey(), null);
60
    }
61
62
    /**
63
     * @return string|null
64
     */
65
    public function getImpersonatorGuardName()
66
    {
67
        return session($this->getSessionGuard(), null);
68
    }
69
70
    /**
71
     * @param Model         $from
72
     * @param Model         $to
73
     * @param string|null   $guardName
74
     * @return bool
75
     */
76
    public function take($from, $to, $guardName = 'web')
77
    {
78
        try {
79
            session()->put($this->getSessionKey(), $from->getKey());
80
            session()->put($this->getSessionGuard(), $this->getCurrentAuthGuardName());
81
            $this->app['auth']->quietLogout();
82
            $this->app['auth']->guard($guardName)->quietLogin($to);
83
84
        } catch (\Exception $e) {
85
            unset($e);
86
            return false;
87
        }
88
89
        $this->app['events']->fire(new TakeImpersonation($from, $to));
90
91
        return true;
92
    }
93
94
    /**
95
     * @return  bool
96
     */
97
    public function leave()
98
    {
99
        try {
100
            $impersonated = $this->app['auth']->guard($this->getCurrentAuthGuardName())->user();
101
            $impersonator = $this->findUserById($this->getImpersonatorId(), $this->getImpersonatorGuardName());
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
        return null;
182
    }
183
}
184