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

ImpersonateManager::getSessionGuard()   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
     * @param Model $from
81
     * @param Model $to
82
     * @param   string|null $guardName
83
     * @return bool
84
     */
85
    public function take($from, $to, $guardName = null)
86
    {
87
        try {
88
            session()->put(config('laravel-impersonate.session_key'), $from->getKey());
89
            session()->put(config('laravel-impersonate.session_guard'), $this->getCurrentAuthGuardName());
90
91
            $this->app['auth']->quietLogout();
92
            $this->app['auth']->guard($guardName)->quietLogin($to);
93
94
        } catch (\Exception $e) {
95
            unset($e);
96
            return false;
97
        }
98
99
        $this->app['events']->fire(new TakeImpersonation($from, $to));
100
101
        return true;
102
    }
103
104
    /**
105
     * @return  bool
106
     */
107
    public function leave()
108
    {
109
        try {
110
            $impersonated = $this->app['auth']->guard($this->getCurrentAuthGuardName())->user();
111
            $impersonator = $this->findUserById($this->getImpersonatorId(), $this->getImpersonatorGuardName());
112
113
            $this->app['auth']->guard($this->getCurrentAuthGuardName())->quietLogout();
114
            $this->app['auth']->guard($this->getImpersonatorGuardName())->quietLogin($impersonator);
115
116
            $this->clear();
117
118
        } catch (\Exception $e) {
119
            unset($e);
120
            return false;
121
        }
122
123
        $this->app['events']->fire(new LeaveImpersonation($impersonator, $impersonated));
124
125
        return true;
126
    }
127
128
    /**
129
     * @return void
130
     */
131
    public function clear()
132
    {
133
        session()->forget($this->getSessionKey());
134
        session()->forget($this->getSessionGuard());
135
    }
136
137
    /**
138
     * @return string
139
     */
140
    public function getSessionKey()
141
    {
142
        return config('laravel-impersonate.session_key');
143
    }
144
145
    /**
146
     * @return string
147
     */
148
    public function getSessionGuard()
149
    {
150
        return config('laravel-impersonate.session_guard');
151
    }
152
153
    /**
154
     * @return  string
155
     */
156
    public function getTakeRedirectTo()
157
    {
158
        try {
159
            $uri = route(config('laravel-impersonate.take_redirect_to'));
160
        } catch (\InvalidArgumentException $e) {
161
            $uri = config('laravel-impersonate.take_redirect_to');
162
        }
163
164
        return $uri;
165
    }
166
167
    /**
168
     * @return  string
169
     */
170
    public function getLeaveRedirectTo()
171
    {
172
        try {
173
            $uri = route(config('laravel-impersonate.leave_redirect_to'));
174
        } catch (\InvalidArgumentException $e) {
175
            $uri = config('laravel-impersonate.leave_redirect_to');
176
        }
177
178
        return $uri;
179
    }
180
181
    /**
182
     * @return string|null
183
     */
184
    public function getCurrentAuthGuardName()
185
    {
186
        $guards = array_keys(config('auth.guards'));
187
        foreach ($guards as $guard) {
188
            if ($this->app['auth']->guard($guard)->check()) {
189
                return $guard;
190
            }
191
        }
192
         return null;
193
    }
194
}
195