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 class ExternalBaseSessionGuard implements StatefulGuard
24 * The name of the Guard. Typically "session".
26 * Corresponds to guard name in authentication configuration.
33 * The user we last attempted to retrieve.
35 * @var \Illuminate\Contracts\Auth\Authenticatable
37 protected $lastAttempted;
40 * The session used by the guard.
42 * @var \Illuminate\Contracts\Session\Session
47 * Indicates if the logout method has been called.
51 protected $loggedOut = false;
54 * Service to handle common registration actions.
56 * @var RegistrationService
58 protected $registrationService;
61 * Create a new authentication guard.
65 public function __construct(string $name, UserProvider $provider, Session $session, RegistrationService $registrationService)
68 $this->session = $session;
69 $this->provider = $provider;
70 $this->registrationService = $registrationService;
74 * Get the currently authenticated user.
76 * @return \Illuminate\Contracts\Auth\Authenticatable|null
78 public function user()
80 if ($this->loggedOut) {
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)) {
91 $id = $this->session->get($this->getName());
93 // First we will try to load the user using the
94 // identifier in the session if one exists.
96 $this->user = $this->provider->retrieveById($id);
103 * Get the ID for the currently authenticated user.
109 if ($this->loggedOut) {
114 ? $this->user()->getAuthIdentifier()
115 : $this->session->get($this->getName());
119 * Log a user into the application without sessions or cookies.
121 * @param array $credentials
125 public function once(array $credentials = [])
127 if ($this->validate($credentials)) {
128 $this->setUser($this->lastAttempted);
137 * 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
161 public function validate(array $credentials = [])
167 * Attempt to authenticate a user using the given credentials.
169 * @param array $credentials
170 * @param bool $remember
174 public function attempt(array $credentials = [], $remember = false)
180 * Log the given user ID into the application.
183 * @param bool $remember
185 * @return \Illuminate\Contracts\Auth\Authenticatable|false
187 public function loginUsingId($id, $remember = false)
189 if (!is_null($user = $this->provider->retrieveById($id))) {
190 $this->login($user, $remember);
199 * Log a user into the application.
201 * @param \Illuminate\Contracts\Auth\Authenticatable $user
202 * @param bool $remember
206 public function login(AuthenticatableContract $user, $remember = false)
208 $this->updateSession($user->getAuthIdentifier());
210 $this->setUser($user);
214 * Update the session with the given ID.
220 protected function updateSession($id)
222 $this->session->put($this->getName(), $id);
224 $this->session->migrate(true);
228 * Log the user out of the application.
232 public function logout()
234 $this->clearUserDataFromStorage();
236 // Now we will clear the users out of memory so they are no longer available
237 // as the user is no longer considered as being signed into this
238 // application and should not be available here.
241 $this->loggedOut = true;
245 * Remove the user data from the session and cookies.
249 protected function clearUserDataFromStorage()
251 $this->session->remove($this->getName());
255 * Get the last user we attempted to authenticate.
257 * @return \Illuminate\Contracts\Auth\Authenticatable
259 public function getLastAttempted()
261 return $this->lastAttempted;
265 * Get a unique identifier for the auth session value.
269 public function getName()
271 return 'login_' . $this->name . '_' . sha1(static::class);
275 * Determine if the user was authenticated via "remember me" cookie.
279 public function viaRemember()
285 * Return the currently cached user.
287 * @return \Illuminate\Contracts\Auth\Authenticatable|null
289 public function getUser()
295 * Set the current user.
297 * @param \Illuminate\Contracts\Auth\Authenticatable $user
301 public function setUser(AuthenticatableContract $user)
305 $this->loggedOut = false;