Completed
Pull Request — master (#59)
by
unknown
07:22
created

ImpersonateManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
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
        $provider = $this->app['config']->get('auth.guards.'.$this->getImpersonatorGuardName().'.provider');
34
        $model = $this->app['config']->get('auth.providers.'.$provider.'.model');
35
36
        $user = call_user_func([
37
            $model,
38
            'findOrFail'
39
        ], $id);
40
41
        return $user;
42
    }
43
44
    /**
45
     * @return bool
46
     */
47
    public function isImpersonating()
48
    {
49
        return session()->has($this->getSessionKey());
50
    }
51
52
    /**
53
     * @param   void
54
     * @return  int|null
55
     */
56
    public function getImpersonatorId()
57
    {
58
        return session($this->getSessionKey(), null);
59
    }
60
61
    /**
62
     * @return string|null
63
     */
64
    public function getImpersonatorGuardName()
65
    {
66
        return session($this->getSessionGuard(), null);
67
    }
68
69
    /**
70
     * @param Model         $from
71
     * @param Model         $to
72
     * @param string|null   $guardName
73
     * @return bool
74
     */
75
    public function take($from, $to, $guardName = null)
76
    {
77
        try {
78
            session()->put($this->getSessionKey(), $from->getKey());
79
            session()->put($this->getSessionGuard(), $this->getCurrentAuthGuardName());
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
        return null;
182
    }
183
}
184