2424
2525namespace phpMyFAQ \User ;
2626
27+ use phpMyFAQ \Auth \AuthException ;
2728use phpMyFAQ \Auth \AuthLdap ;
2829use phpMyFAQ \Auth \AuthSso ;
2930use phpMyFAQ \Configuration ;
@@ -65,9 +66,9 @@ public function setTwoFactorAuth(bool $twoFactorAuth): void
6566
6667 /**
6768 * Authenticates a user with a given username and password against
68- * LDAP, SSO or local database.
69+ * LDAP, SSO, or local database.
6970 *
70- * @throws UserException|Exception
71+ * @throws UserException
7172 */
7273 public function authenticate (string $ username , #[SensitiveParameter] string $ password ): CurrentUser
7374 {
@@ -78,20 +79,24 @@ public function authenticate(string $username, #[SensitiveParameter] string $pas
7879 $ this ->authenticateLdap ();
7980 $ this ->authenticateSso ();
8081
81- if ($ this ->currentUser ->login ($ username , $ password )) {
82- if ($ this ->currentUser ->getUserData ('twofactor_enabled ' )) {
83- $ this ->setTwoFactorAuth (true );
84- $ this ->currentUser ->setLoggedIn (false );
85- } elseif ($ this ->currentUser ->getStatus () !== 'blocked ' ) {
86- $ this ->currentUser ->setLoggedIn (true );
82+ try {
83+ if ($ this ->currentUser ->login ($ username , $ password )) {
84+ if ($ this ->currentUser ->getUserData ('twofactor_enabled ' )) {
85+ $ this ->setTwoFactorAuth (true );
86+ $ this ->currentUser ->setLoggedIn (false );
87+ } elseif ($ this ->currentUser ->getStatus () !== 'blocked ' ) {
88+ $ this ->currentUser ->setLoggedIn (true );
89+ } else {
90+ $ this ->currentUser ->setLoggedIn (false );
91+ throw new UserException (
92+ (Translation::get (key: 'ad_auth_fail ' ) ?? 'Authentication failed ' ) . ' ( ' . $ username . ') ' ,
93+ );
94+ }
8795 } else {
88- $ this ->currentUser ->setLoggedIn (false );
89- throw new UserException (
90- (Translation::get (key: 'ad_auth_fail ' ) ?? 'Authentication failed ' ) . ' ( ' . $ username . ') ' ,
91- );
96+ throw new UserException (Translation::get (key: 'ad_auth_fail ' ) ?? 'Authentication failed ' );
9297 }
93- } else {
94- throw new UserException (Translation:: get (key: ' ad_auth_fail ' ) ?? ' Authentication failed ' );
98+ } catch ( AuthException $ e ) {
99+ throw new UserException ($ e -> getMessage () );
95100 }
96101
97102 return $ this ->currentUser ;
0 commit comments