Completed
Pull Request — master (#64)
by
unknown
01:15
created

ImpersonateManager   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 206
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 3
dl 0
loc 206
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A findUserById() 0 21 3
A isImpersonating() 0 4 1
A getImpersonatorId() 0 4 1
A getImpersonatorGuardName() 0 4 1
A getImpersonatorGuardUsingName() 0 4 1
A take() 0 20 2
A leave() 0 22 2
A clear() 0 6 1
A getSessionKey() 0 4 1
A getSessionGuard() 0 4 1
A getSessionGuardUsing() 0 4 1
A getTakeRedirectTo() 0 10 2
A getLeaveRedirectTo() 0 10 2
A getCurrentAuthGuardName() 0 10 3
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|null $guardName
30
     * @return  Model
31
     */
32
    public function findUserById($id, $guardName = null)
33
    {
34
      if($guardName)
0 ignored issues
show
Bug Best Practice introduced by
The expression $guardName of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
35
      {
36
          $userProvider = $this->app['config']->get('auth.guards.'. $guardName .'.provider');
37
          $model = $this->app['config']->get('auth.providers.'. $userProvider .'.model');
38
39
          if(!$model)
40
            throw new \Exception("Auth guard doesn not exist.", 1);
41
42
      } else {
43
        $model = $this->app['config']->get('auth.providers.users.model');
44
      }
45
46
        $user = call_user_func([
47
            $model,
48
            'findOrFail'
49
        ], $id);
50
51
        return $user;
52
    }
53
54
    /**
55
     * @return bool
56
     */
57
    public function isImpersonating()
58
    {
59
        return session()->has($this->getSessionKey());
60
    }
61
62
    /**
63
     * @param   void
64
     * @return  int|null
65
     */
66
    public function getImpersonatorId()
67
    {
68
        return session($this->getSessionKey(), null);
69
    }
70
71
    /**
72
    * @return string|null
73
    */
74
   public function getImpersonatorGuardName()
75
   {
76
       return session($this->getSessionGuard(), null);
77
   }
78
79
   /**
80
   * @return string|null
81
   */
82
  public function getImpersonatorGuardUsingName()
83
  {
84
      return session($this->getSessionGuardUsing(), null);
85
  }
86
87
    /**
88
     * @param Model $from
89
     * @param Model $to
90
     * @param   string|null $guardName
91
     * @return bool
92
     */
93
    public function take($from, $to, $guardName = null)
94
    {
95
        try {
96
            session()->put(config('laravel-impersonate.session_key'), $from->getKey());
97
            session()->put(config('laravel-impersonate.session_guard'), $this->getCurrentAuthGuardName());
98
            session()->put(config('laravel-impersonate.session_guard_using'), $guardName);
99
100
            $this->app['auth']->guard($this->getCurrentAuthGuardName())->quietLogout();
101
            $this->app['auth']->guard($guardName)->quietLogin($to);
102
103
        } catch (\Exception $e) {
104
105
            unset($e);
106
            return false;
107
        }
108
109
        $this->app['events']->fire(new TakeImpersonation($from, $to));
110
111
        return true;
112
    }
113
114
    /**
115
     * @return  bool
116
     */
117
    public function leave()
118
    {
119
        try {
120
121
            $impersonated = $this->app['auth']->guard($this->getImpersonatorGuardUsingName())->user();
122
            $impersonator = $this->findUserById($this->getImpersonatorId(), $this->getImpersonatorGuardName());
123
124
            $this->app['auth']->guard($this->getCurrentAuthGuardName())->quietLogout();
125
            $this->app['auth']->guard($this->getImpersonatorGuardName())->quietLogin($impersonator);
126
127
            $this->clear();
128
129
        } catch (\Exception $e) {
130
          dd($e);
131
            unset($e);
132
            return false;
133
        }
134
135
        $this->app['events']->fire(new LeaveImpersonation($impersonator, $impersonated));
136
137
        return true;
138
    }
139
140
    /**
141
     * @return void
142
     */
143
    public function clear()
144
    {
145
        session()->forget($this->getSessionKey());
146
        session()->forget($this->getSessionGuard());
147
        session()->forget($this->getSessionGuardUsing());
148
    }
149
150
    /**
151
     * @return string
152
     */
153
    public function getSessionKey()
154
    {
155
        return config('laravel-impersonate.session_key');
156
    }
157
158
    /**
159
     * @return string
160
     */
161
    public function getSessionGuard()
162
    {
163
        return config('laravel-impersonate.session_guard');
164
    }
165
166
    /**
167
     * @return string
168
     */
169
    public function getSessionGuardUsing()
170
    {
171
        return config('laravel-impersonate.session_guard_using');
172
    }
173
174
    /**
175
     * @return  string
176
     */
177
    public function getTakeRedirectTo()
178
    {
179
        try {
180
            $uri = route(config('laravel-impersonate.take_redirect_to'));
181
        } catch (\InvalidArgumentException $e) {
182
            $uri = config('laravel-impersonate.take_redirect_to');
183
        }
184
185
        return $uri;
186
    }
187
188
    /**
189
     * @return  string
190
     */
191
    public function getLeaveRedirectTo()
192
    {
193
        try {
194
            $uri = route(config('laravel-impersonate.leave_redirect_to'));
195
        } catch (\InvalidArgumentException $e) {
196
            $uri = config('laravel-impersonate.leave_redirect_to');
197
        }
198
199
        return $uri;
200
    }
201
202
    /**
203
     * @return string|null
204
     */
205
    public function getCurrentAuthGuardName()
206
    {
207
        $guards = array_keys(config('auth.guards'));
208
        foreach ($guards as $guard) {
209
            if ($this->app['auth']->guard($guard)->check()) {
210
                return $guard;
211
            }
212
        }
213
        return null;
214
    }
215
}
216