]> BookStack Code Mirror - bookstack/blob - app/Auth/Access/Guards/ExternalBaseSessionGuard.php
3022b7f8e5c083a8bec06a4d9be5abe06a973651
[bookstack] / app / Auth / Access / Guards / ExternalBaseSessionGuard.php
1 <?php
2
3 namespace BookStack\Auth\Access\Guards;
4
5 use Illuminate\Auth\GuardHelpers;
6 use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
7 use Illuminate\Contracts\Auth\StatefulGuard;
8 use Illuminate\Contracts\Auth\UserProvider;
9 use Illuminate\Contracts\Session\Session;
10
11 /**
12  * Class BaseSessionGuard
13  * A base implementation of a session guard. Is a copy of the default Laravel
14  * guard with 'remember' functionality removed. Basic auth and event emission
15  * has also been removed to keep this simple. Designed to be extended by external
16  * Auth Guards.
17  *
18  * @package Illuminate\Auth
19  */
20 class ExternalBaseSessionGuard implements StatefulGuard
21 {
22     use GuardHelpers;
23
24     /**
25      * The name of the Guard. Typically "session".
26      *
27      * Corresponds to guard name in authentication configuration.
28      *
29      * @var string
30      */
31     protected $name;
32
33     /**
34      * The user we last attempted to retrieve.
35      *
36      * @var \Illuminate\Contracts\Auth\Authenticatable
37      */
38     protected $lastAttempted;
39
40     /**
41      * The session used by the guard.
42      *
43      * @var \Illuminate\Contracts\Session\Session
44      */
45     protected $session;
46
47     /**
48      * Indicates if the logout method has been called.
49      *
50      * @var bool
51      */
52     protected $loggedOut = false;
53
54     /**
55      * Create a new authentication guard.
56      *
57      * @param  string  $name
58      * @param  \Illuminate\Contracts\Auth\UserProvider  $provider
59      * @param  \Illuminate\Contracts\Session\Session  $session
60      * @return void
61      */
62     public function __construct($name,
63         UserProvider $provider,
64         Session $session)
65     {
66         $this->name = $name;
67         $this->session = $session;
68         $this->provider = $provider;
69     }
70
71     /**
72      * Get the currently authenticated user.
73      *
74      * @return \Illuminate\Contracts\Auth\Authenticatable|null
75      */
76     public function user()
77     {
78         if ($this->loggedOut) {
79             return;
80         }
81
82         // If we've already retrieved the user for the current request we can just
83         // return it back immediately. We do not want to fetch the user data on
84         // every call to this method because that would be tremendously slow.
85         if (! is_null($this->user)) {
86             return $this->user;
87         }
88
89         $id = $this->session->get($this->getName());
90
91         // First we will try to load the user using the
92         // identifier in the session if one exists.
93         if (! is_null($id)) {
94             $this->user = $this->provider->retrieveById($id);
95         }
96
97         return $this->user;
98     }
99
100     /**
101      * Get the ID for the currently authenticated user.
102      *
103      * @return int|null
104      */
105     public function id()
106     {
107         if ($this->loggedOut) {
108             return;
109         }
110
111         return $this->user()
112             ? $this->user()->getAuthIdentifier()
113             : $this->session->get($this->getName());
114     }
115
116     /**
117      * Log a user into the application without sessions or cookies.
118      *
119      * @param  array  $credentials
120      * @return bool
121      */
122     public function once(array $credentials = [])
123     {
124         if ($this->validate($credentials)) {
125             $this->setUser($this->lastAttempted);
126
127             return true;
128         }
129
130         return false;
131     }
132
133     /**
134      * Log the given user ID into the application without sessions or cookies.
135      *
136      * @param  mixed  $id
137      * @return \Illuminate\Contracts\Auth\Authenticatable|false
138      */
139     public function onceUsingId($id)
140     {
141         if (! is_null($user = $this->provider->retrieveById($id))) {
142             $this->setUser($user);
143
144             return $user;
145         }
146
147         return false;
148     }
149
150     /**
151      * Validate a user's credentials.
152      *
153      * @param  array  $credentials
154      * @return bool
155      */
156     public function validate(array $credentials = [])
157     {
158         return false;
159     }
160
161
162     /**
163      * Attempt to authenticate a user using the given credentials.
164      *
165      * @param  array  $credentials
166      * @param  bool  $remember
167      * @return bool
168      */
169     public function attempt(array $credentials = [], $remember = false)
170     {
171         return false;
172     }
173
174     /**
175      * Log the given user ID into the application.
176      *
177      * @param  mixed  $id
178      * @param  bool  $remember
179      * @return \Illuminate\Contracts\Auth\Authenticatable|false
180      */
181     public function loginUsingId($id, $remember = false)
182     {
183         if (! is_null($user = $this->provider->retrieveById($id))) {
184             $this->login($user, $remember);
185
186             return $user;
187         }
188
189         return false;
190     }
191
192     /**
193      * Log a user into the application.
194      *
195      * @param  \Illuminate\Contracts\Auth\Authenticatable  $user
196      * @param  bool  $remember
197      * @return void
198      */
199     public function login(AuthenticatableContract $user, $remember = false)
200     {
201         $this->updateSession($user->getAuthIdentifier());
202
203         $this->setUser($user);
204     }
205
206     /**
207      * Update the session with the given ID.
208      *
209      * @param  string  $id
210      * @return void
211      */
212     protected function updateSession($id)
213     {
214         $this->session->put($this->getName(), $id);
215
216         $this->session->migrate(true);
217     }
218
219     /**
220      * Log the user out of the application.
221      *
222      * @return void
223      */
224     public function logout()
225     {
226         $this->clearUserDataFromStorage();
227
228         // Now we will clear the users out of memory so they are no longer available
229         // as the user is no longer considered as being signed into this
230         // application and should not be available here.
231         $this->user = null;
232
233         $this->loggedOut = true;
234     }
235
236     /**
237      * Remove the user data from the session and cookies.
238      *
239      * @return void
240      */
241     protected function clearUserDataFromStorage()
242     {
243         $this->session->remove($this->getName());
244     }
245
246     /**
247      * Get the last user we attempted to authenticate.
248      *
249      * @return \Illuminate\Contracts\Auth\Authenticatable
250      */
251     public function getLastAttempted()
252     {
253         return $this->lastAttempted;
254     }
255
256     /**
257      * Get a unique identifier for the auth session value.
258      *
259      * @return string
260      */
261     public function getName()
262     {
263         return 'login_'.$this->name.'_'.sha1(static::class);
264     }
265
266     /**
267      * Determine if the user was authenticated via "remember me" cookie.
268      *
269      * @return bool
270      */
271     public function viaRemember()
272     {
273         return false;
274     }
275
276     /**
277      * Return the currently cached user.
278      *
279      * @return \Illuminate\Contracts\Auth\Authenticatable|null
280      */
281     public function getUser()
282     {
283         return $this->user;
284     }
285
286     /**
287      * Set the current user.
288      *
289      * @param  \Illuminate\Contracts\Auth\Authenticatable  $user
290      * @return $this
291      */
292     public function setUser(AuthenticatableContract $user)
293     {
294         $this->user = $user;
295
296         $this->loggedOut = false;
297
298         return $this;
299     }
300
301 }