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

getImpersonatorGuardUsingName()   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 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|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']->quietLogout();
101
            $this->app['auth']->guard($guardName)->quietLogin($to);
102
103
        } catch (\Exception $e) {
104
            unset($e);
105
            return false;
106
        }
107
108
        $this->app['events']->fire(new TakeImpersonation($from, $to));
109
110
        return true;
111
    }
112
113
    /**
114
     * @return  bool
115
     */
116
    public function leave()
117
    {
118
        try {
119
120
            $impersonated = $this->app['auth']->guard($this->getImpersonatorGuardUsingName())->user();
121
            $impersonator = $this->findUserById($this->getImpersonatorId(), $this->getImpersonatorGuardName());
122
123
            $this->app['auth']->guard($this->getCurrentAuthGuardName())->quietLogout();
124
            $this->app['auth']->guard($this->getImpersonatorGuardName())->quietLogin($impersonator);
125
126
            $this->clear();
127
128
        } catch (\Exception $e) {
129
          dd($e);
130
            unset($e);
131
            return false;
132
        }
133
134
        $this->app['events']->fire(new LeaveImpersonation($impersonator, $impersonated));
135
136
        return true;
137
    }
138
139
    /**
140
     * @return void
141
     */
142
    public function clear()
143
    {
144
        session()->forget($this->getSessionKey());
145
        session()->forget($this->getSessionGuard());
146
        session()->forget($this->getSessionGuardUsing());
147
    }
148
149
    /**
150
     * @return string
151
     */
152
    public function getSessionKey()
153
    {
154
        return config('laravel-impersonate.session_key');
155
    }
156
157
    /**
158
     * @return string
159
     */
160
    public function getSessionGuard()
161
    {
162
        return config('laravel-impersonate.session_guard');
163
    }
164
165
    /**
166
     * @return string
167
     */
168
    public function getSessionGuardUsing()
169
    {
170
        return config('laravel-impersonate.session_guard_using');
171
    }
172
173
    /**
174
     * @return  string
175
     */
176
    public function getTakeRedirectTo()
177
    {
178
        try {
179
            $uri = route(config('laravel-impersonate.take_redirect_to'));
180
        } catch (\InvalidArgumentException $e) {
181
            $uri = config('laravel-impersonate.take_redirect_to');
182
        }
183
184
        return $uri;
185
    }
186
187
    /**
188
     * @return  string
189
     */
190
    public function getLeaveRedirectTo()
191
    {
192
        try {
193
            $uri = route(config('laravel-impersonate.leave_redirect_to'));
194
        } catch (\InvalidArgumentException $e) {
195
            $uri = config('laravel-impersonate.leave_redirect_to');
196
        }
197
198
        return $uri;
199
    }
200
201
    /**
202
     * @return string|null
203
     */
204
    public function getCurrentAuthGuardName()
205
    {
206
        $guards = array_keys(config('auth.guards'));
207
        foreach ($guards as $guard) {
208
            if ($this->app['auth']->guard($guard)->check()) {
209
                return $guard;
210
            }
211
        }
212
        return null;
213
    }
214
}
215