5
5
use BookStack \Access \Mfa \MfaSession ;
6
6
use BookStack \Activity \ActivityType ;
7
7
use BookStack \Exceptions \LoginAttemptException ;
8
+ use BookStack \Exceptions \LoginAttemptInvalidUserException ;
8
9
use BookStack \Exceptions \StoppedAuthenticationException ;
9
10
use BookStack \Facades \Activity ;
10
11
use BookStack \Facades \Theme ;
@@ -29,10 +30,14 @@ public function __construct(
29
30
* a reason to (MFA or Unconfirmed Email).
30
31
* Returns a boolean to indicate the current login result.
31
32
*
32
- * @throws StoppedAuthenticationException
33
+ * @throws StoppedAuthenticationException|LoginAttemptInvalidUserException
33
34
*/
34
35
public function login (User $ user , string $ method , bool $ remember = false ): void
35
36
{
37
+ if ($ user ->isGuest ()) {
38
+ throw new LoginAttemptInvalidUserException ('Login not allowed for guest user ' );
39
+ }
40
+
36
41
if ($ this ->awaitingEmailConfirmation ($ user ) || $ this ->needsMfaVerification ($ user )) {
37
42
$ this ->setLastLoginAttemptedForUser ($ user , $ method , $ remember );
38
43
@@ -58,7 +63,7 @@ public function login(User $user, string $method, bool $remember = false): void
58
63
*
59
64
* @throws Exception
60
65
*/
61
- public function reattemptLoginFor (User $ user )
66
+ public function reattemptLoginFor (User $ user ): void
62
67
{
63
68
if ($ user ->id !== ($ this ->getLastLoginAttemptUser ()->id ?? null )) {
64
69
throw new Exception ('Login reattempt user does align with current session state ' );
@@ -152,16 +157,40 @@ public function awaitingEmailConfirmation(User $user): bool
152
157
*/
153
158
public function attempt (array $ credentials , string $ method , bool $ remember = false ): bool
154
159
{
160
+ if ($ this ->areCredentialsForGuest ($ credentials )) {
161
+ return false ;
162
+ }
163
+
155
164
$ result = auth ()->attempt ($ credentials , $ remember );
156
165
if ($ result ) {
157
166
$ user = auth ()->user ();
158
167
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
+ }
160
175
}
161
176
162
177
return $ result ;
163
178
}
164
179
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
+
165
194
/**
166
195
* Logs the current user out of the application.
167
196
* Returns an app post-redirect path.
0 commit comments