3 namespace BookStack\Auth\Access\Guards;
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;
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
19 * @package Illuminate\Auth
21 class ExternalBaseSessionGuard implements StatefulGuard
26 * The name of the Guard. Typically "session".
28 * Corresponds to guard name in authentication configuration.
35 * The user we last attempted to retrieve.
37 * @var \Illuminate\Contracts\Auth\Authenticatable
39 protected $lastAttempted;
42 * The session used by the guard.
44 * @var \Illuminate\Contracts\Session\Session
49 * Indicates if the logout method has been called.
53 protected $loggedOut = false;
56 * Service to handle common registration actions.
58 * @var RegistrationService
60 protected $registrationService;
63 * Create a new authentication guard.
67 public function __construct(string $name, UserProvider $provider, Session $session, RegistrationService $registrationService)
70 $this->session = $session;
71 $this->provider = $provider;
72 $this->registrationService = $registrationService;
76 * Get the currently authenticated user.
78 * @return \Illuminate\Contracts\Auth\Authenticatable|null
80 public function user()
82 if ($this->loggedOut) {
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)) {
93 $id = $this->session->get($this->getName());
95 // First we will try to load the user using the
96 // identifier in the session if one exists.
98 $this->user = $this->provider->retrieveById($id);
105 * Get the ID for the currently authenticated user.
111 if ($this->loggedOut) {
116 ? $this->user()->getAuthIdentifier()
117 : $this->session->get($this->getName());
121 * Log a user into the application without sessions or cookies.
123 * @param array $credentials
126 public function once(array $credentials = [])
128 if ($this->validate($credentials)) {
129 $this->setUser($this->lastAttempted);
138 * Log the given user ID into the application without sessions or cookies.
141 * @return \Illuminate\Contracts\Auth\Authenticatable|false
143 public function onceUsingId($id)
145 if (! is_null($user = $this->provider->retrieveById($id))) {
146 $this->setUser($user);
155 * Validate a user's credentials.
157 * @param array $credentials
160 public function validate(array $credentials = [])
167 * Attempt to authenticate a user using the given credentials.
169 * @param array $credentials
170 * @param bool $remember
173 public function attempt(array $credentials = [], $remember = false)
179 * Log the given user ID into the application.
182 * @param bool $remember
183 * @return \Illuminate\Contracts\Auth\Authenticatable|false
185 public function loginUsingId($id, $remember = false)
187 if (! is_null($user = $this->provider->retrieveById($id))) {
188 $this->login($user, $remember);
197 * Log a user into the application.
199 * @param \Illuminate\Contracts\Auth\Authenticatable $user
200 * @param bool $remember
203 public function login(AuthenticatableContract $user, $remember = false)
205 $this->updateSession($user->getAuthIdentifier());
207 $this->setUser($user);
211 * Update the session with the given ID.
216 protected function updateSession($id)
218 $this->session->put($this->getName(), $id);
220 $this->session->migrate(true);
224 * Log the user out of the application.
228 public function logout()
230 $this->clearUserDataFromStorage();
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.
237 $this->loggedOut = true;
241 * Remove the user data from the session and cookies.
245 protected function clearUserDataFromStorage()
247 $this->session->remove($this->getName());
251 * Get the last user we attempted to authenticate.
253 * @return \Illuminate\Contracts\Auth\Authenticatable
255 public function getLastAttempted()
257 return $this->lastAttempted;
261 * Get a unique identifier for the auth session value.
265 public function getName()
267 return 'login_'.$this->name.'_'.sha1(static::class);
271 * Determine if the user was authenticated via "remember me" cookie.
275 public function viaRemember()
281 * Return the currently cached user.
283 * @return \Illuminate\Contracts\Auth\Authenticatable|null
285 public function getUser()
291 * Set the current user.
293 * @param \Illuminate\Contracts\Auth\Authenticatable $user
296 public function setUser(AuthenticatableContract $user)
300 $this->loggedOut = false;