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