3 namespace BookStack\Auth\Access\Guards;
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;
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
18 * @package Illuminate\Auth
20 class ExternalBaseSessionGuard implements StatefulGuard
25 * The name of the Guard. Typically "session".
27 * Corresponds to guard name in authentication configuration.
34 * The user we last attempted to retrieve.
36 * @var \Illuminate\Contracts\Auth\Authenticatable
38 protected $lastAttempted;
41 * The session used by the guard.
43 * @var \Illuminate\Contracts\Session\Session
48 * Indicates if the logout method has been called.
52 protected $loggedOut = false;
55 * Create a new authentication guard.
58 * @param \Illuminate\Contracts\Auth\UserProvider $provider
59 * @param \Illuminate\Contracts\Session\Session $session
62 public function __construct($name,
63 UserProvider $provider,
67 $this->session = $session;
68 $this->provider = $provider;
72 * Get the currently authenticated user.
74 * @return \Illuminate\Contracts\Auth\Authenticatable|null
76 public function user()
78 if ($this->loggedOut) {
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)) {
89 $id = $this->session->get($this->getName());
91 // First we will try to load the user using the
92 // identifier in the session if one exists.
94 $this->user = $this->provider->retrieveById($id);
101 * Get the ID for the currently authenticated user.
107 if ($this->loggedOut) {
112 ? $this->user()->getAuthIdentifier()
113 : $this->session->get($this->getName());
117 * Log a user into the application without sessions or cookies.
119 * @param array $credentials
122 public function once(array $credentials = [])
124 if ($this->validate($credentials)) {
125 $this->setUser($this->lastAttempted);
134 * Log the given user ID into the application without sessions or cookies.
137 * @return \Illuminate\Contracts\Auth\Authenticatable|false
139 public function onceUsingId($id)
141 if (! is_null($user = $this->provider->retrieveById($id))) {
142 $this->setUser($user);
151 * Validate a user's credentials.
153 * @param array $credentials
156 public function validate(array $credentials = [])
163 * Attempt to authenticate a user using the given credentials.
165 * @param array $credentials
166 * @param bool $remember
169 public function attempt(array $credentials = [], $remember = false)
175 * Log the given user ID into the application.
178 * @param bool $remember
179 * @return \Illuminate\Contracts\Auth\Authenticatable|false
181 public function loginUsingId($id, $remember = false)
183 if (! is_null($user = $this->provider->retrieveById($id))) {
184 $this->login($user, $remember);
193 * Log a user into the application.
195 * @param \Illuminate\Contracts\Auth\Authenticatable $user
196 * @param bool $remember
199 public function login(AuthenticatableContract $user, $remember = false)
201 $this->updateSession($user->getAuthIdentifier());
203 $this->setUser($user);
207 * Update the session with the given ID.
212 protected function updateSession($id)
214 $this->session->put($this->getName(), $id);
216 $this->session->migrate(true);
220 * Log the user out of the application.
224 public function logout()
226 $this->clearUserDataFromStorage();
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.
233 $this->loggedOut = true;
237 * Remove the user data from the session and cookies.
241 protected function clearUserDataFromStorage()
243 $this->session->remove($this->getName());
247 * Get the last user we attempted to authenticate.
249 * @return \Illuminate\Contracts\Auth\Authenticatable
251 public function getLastAttempted()
253 return $this->lastAttempted;
257 * Get a unique identifier for the auth session value.
261 public function getName()
263 return 'login_'.$this->name.'_'.sha1(static::class);
267 * Determine if the user was authenticated via "remember me" cookie.
271 public function viaRemember()
277 * Return the currently cached user.
279 * @return \Illuminate\Contracts\Auth\Authenticatable|null
281 public function getUser()
287 * Set the current user.
289 * @param \Illuminate\Contracts\Auth\Authenticatable $user
292 public function setUser(AuthenticatableContract $user)
296 $this->loggedOut = false;