fix: LDAP Exception

This commit is contained in:
kuaifan 2023-02-14 00:23:42 +08:00
parent f3e41f2ff4
commit 7fd0ef4ac6

View File

@ -2,7 +2,6 @@
namespace App\Ldap; namespace App\Ldap;
use App\Exceptions\ApiException;
use App\Models\User; use App\Models\User;
use App\Module\Base; use App\Module\Base;
use LdapRecord\Configuration\ConfigurationException; use LdapRecord\Configuration\ConfigurationException;
@ -12,7 +11,7 @@ use LdapRecord\Models\Model;
class LdapUser extends Model class LdapUser extends Model
{ {
protected static bool $init = false; protected static $init = null;
/** /**
* The object classes of the LDAP model. * The object classes of the LDAP model.
* *
@ -71,14 +70,13 @@ class LdapUser extends Model
/** /**
* 初始化配置 * 初始化配置
* @return void * @return bool
*/ */
public static function initConfig() public static function initConfig()
{ {
if (self::$init) { if (is_bool(self::$init)) {
return; return self::$init;
} }
self::$init = true;
// //
$setting = Base::setting('thirdAccessSetting'); $setting = Base::setting('thirdAccessSetting');
$connection = Container::getDefaultConnection(); $connection = Container::getDefaultConnection();
@ -90,8 +88,10 @@ class LdapUser extends Model
"username" => $setting['ldap_user_dn'], "username" => $setting['ldap_user_dn'],
"password" => $setting['ldap_password'], "password" => $setting['ldap_password'],
]); ]);
return self::$init = true;
} catch (ConfigurationException $e) { } catch (ConfigurationException $e) {
throw new ApiException($e->getMessage()); info($e->getMessage());
return self::$init = false;
} }
} }
@ -103,7 +103,9 @@ class LdapUser extends Model
*/ */
public static function userFirst($username, $password): ?Model public static function userFirst($username, $password): ?Model
{ {
self::initConfig(); if (!self::initConfig()) {
return null;
}
try { try {
return self::static() return self::static()
->where([ ->where([
@ -124,7 +126,9 @@ class LdapUser extends Model
*/ */
public static function userLogin($username, $password, $user = null) public static function userLogin($username, $password, $user = null)
{ {
self::initConfig(); if (!self::initConfig()) {
return null;
}
$row = self::userFirst($username, $password); $row = self::userFirst($username, $password);
if (!$row) { if (!$row) {
return null; return null;
@ -158,7 +162,9 @@ class LdapUser extends Model
return; return;
} }
// //
self::initConfig(); if (!self::initConfig()) {
return;
}
// //
if (self::isSyncLocal()) { if (self::isSyncLocal()) {
$row = self::userFirst($user->email, $password); $row = self::userFirst($user->email, $password);
@ -202,15 +208,17 @@ class LdapUser extends Model
if (empty($array)) { if (empty($array)) {
return; return;
} }
self::initConfig(); if (!self::initConfig()) {
return;
}
try {
$row = self::static() $row = self::static()
->where([ ->where([
'cn' => $username, 'cn' => $username,
])->first(); ])->first();
try {
$row?->update($array); $row?->update($array);
} catch (LdapRecordException $e) { } catch (\Exception $e) {
throw new ApiException("[LDAP] update fail: " . $e->getMessage()); info("[LDAP] update fail: " . $e->getMessage());
} }
} }
@ -221,15 +229,17 @@ class LdapUser extends Model
*/ */
public static function userDelete($username) public static function userDelete($username)
{ {
self::initConfig(); if (!self::initConfig()) {
return;
}
try {
$row = self::static() $row = self::static()
->where([ ->where([
'cn' => $username, 'cn' => $username,
])->first(); ])->first();
try {
$row?->delete(); $row?->delete();
} catch (LdapRecordException $e) { } catch (\Exception $e) {
throw new ApiException("[LDAP] delete fail: " . $e->getMessage()); info("[LDAP] delete fail: " . $e->getMessage());
} }
} }
} }