55use BookStack \Access \Mfa \MfaSession ;
66use BookStack \Activity \ActivityType ;
77use BookStack \Exceptions \LoginAttemptException ;
8+ use BookStack \Exceptions \LoginAttemptInvalidUserException ;
89use BookStack \Exceptions \StoppedAuthenticationException ;
910use BookStack \Facades \Activity ;
1011use BookStack \Facades \Theme ;
@@ -29,10 +30,14 @@ public function __construct(
2930 * a reason to (MFA or Unconfirmed Email).
3031 * Returns a boolean to indicate the current login result.
3132 *
32- * @throws StoppedAuthenticationException
33+ * @throws StoppedAuthenticationException|LoginAttemptInvalidUserException
3334 */
3435 public function login (User $ user , string $ method , bool $ remember = false ): void
3536 {
37+ if ($ user ->isGuest ()) {
38+ throw new LoginAttemptInvalidUserException ('Login not allowed for guest user ' );
39+ }
40+
3641 if ($ this ->awaitingEmailConfirmation ($ user ) || $ this ->needsMfaVerification ($ user )) {
3742 $ this ->setLastLoginAttemptedForUser ($ user , $ method , $ remember );
3843
@@ -58,7 +63,7 @@ public function login(User $user, string $method, bool $remember = false): void
5863 *
5964 * @throws Exception
6065 */
61- public function reattemptLoginFor (User $ user )
66+ public function reattemptLoginFor (User $ user ): void
6267 {
6368 if ($ user ->id !== ($ this ->getLastLoginAttemptUser ()->id ?? null )) {
6469 throw new Exception ('Login reattempt user does align with current session state ' );
@@ -152,16 +157,40 @@ public function awaitingEmailConfirmation(User $user): bool
152157 */
153158 public function attempt (array $ credentials , string $ method , bool $ remember = false ): bool
154159 {
160+ if ($ this ->areCredentialsForGuest ($ credentials )) {
161+ return false ;
162+ }
163+
155164 $ result = auth ()->attempt ($ credentials , $ remember );
156165 if ($ result ) {
157166 $ user = auth ()->user ();
158167 auth ()->logout ();
159- $ this ->login ($ user , $ method , $ remember );
168+ try {
169+ $ this ->login ($ user , $ method , $ remember );
170+ } catch (LoginAttemptInvalidUserException $ e ) {
171+ // Catch and return false for non-login accounts
172+ // so it looks like a normal invalid login.
173+ return false ;
174+ }
160175 }
161176
162177 return $ result ;
163178 }
164179
180+ /**
181+ * Check if the given credentials are likely for the system guest account.
182+ */
183+ protected function areCredentialsForGuest (array $ credentials ): bool
184+ {
185+ if (isset ($ credentials ['email ' ])) {
186+ return User::query ()->where ('email ' , '= ' , $ credentials ['email ' ])
187+ ->where ('system_name ' , '= ' , 'public ' )
188+ ->exists ();
189+ }
190+
191+ return false ;
192+ }
193+
165194 /**
166195 * Logs the current user out of the application.
167196 * Returns an app post-redirect path.
0 commit comments