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