Replace call for Logger with DI::logger() in ldapauth addon

This commit is contained in:
Art4 2025-01-24 11:44:48 +00:00
parent 5d51c5b56e
commit a488f25131

View file

@ -30,7 +30,6 @@
*/
use Friendica\Core\Hook;
use Friendica\Core\Logger;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Model\User;
@ -69,54 +68,54 @@ function ldapauth_authenticate($username, $password)
$ldap_autocreateaccount_nameattribute = DI::config()->get('ldapauth', 'ldap_autocreateaccount_nameattribute');
if (!extension_loaded('ldap') || !strlen($ldap_server)) {
Logger::error('Addon not configured or missing php-ldap extension', ['extension_loaded' => extension_loaded('ldap'), 'server' => $ldap_server]);
DI::logger()->error('Addon not configured or missing php-ldap extension', ['extension_loaded' => extension_loaded('ldap'), 'server' => $ldap_server]);
return false;
}
if (!strlen($password)) {
Logger::error('Empty password disallowed', ['provided_password_length' => strlen($password)]);
DI::logger()->error('Empty password disallowed', ['provided_password_length' => strlen($password)]);
return false;
}
$connect = @ldap_connect($ldap_server);
if ($connect === false) {
Logger::warning('Could not connect to LDAP server', ['server' => $ldap_server]);
DI::logger()->warning('Could not connect to LDAP server', ['server' => $ldap_server]);
return false;
}
@ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3);
@ldap_set_option($connect, LDAP_OPT_REFERRALS, 0);
if ((@ldap_bind($connect, $ldap_binddn, $ldap_bindpw)) === false) {
Logger::warning('Could not bind to LDAP server', ['server' => $ldap_server, 'binddn' => $ldap_binddn, 'errno' => ldap_errno($connect), 'error' => ldap_error($connect)]);
DI::logger()->warning('Could not bind to LDAP server', ['server' => $ldap_server, 'binddn' => $ldap_binddn, 'errno' => ldap_errno($connect), 'error' => ldap_error($connect)]);
return false;
}
$res = @ldap_search($connect, $ldap_searchdn, $ldap_userattr . '=' . $username);
if (!$res) {
Logger::notice('LDAP user not found.', ['searchdn' => $ldap_searchdn, 'userattr' => $ldap_userattr, 'username' => $username, 'errno' => ldap_errno($connect), 'error' => ldap_error($connect)]);
DI::logger()->notice('LDAP user not found.', ['searchdn' => $ldap_searchdn, 'userattr' => $ldap_userattr, 'username' => $username, 'errno' => ldap_errno($connect), 'error' => ldap_error($connect)]);
return false;
}
$id = @ldap_first_entry($connect, $res);
if (!$id) {
Logger::notice('Could not retrieve first LDAP entry.', ['searchdn' => $ldap_searchdn, 'userattr' => $ldap_userattr, 'username' => $username, 'errno' => ldap_errno($connect), 'error' => ldap_error($connect)]);
DI::logger()->notice('Could not retrieve first LDAP entry.', ['searchdn' => $ldap_searchdn, 'userattr' => $ldap_userattr, 'username' => $username, 'errno' => ldap_errno($connect), 'error' => ldap_error($connect)]);
return false;
}
$dn = @ldap_get_dn($connect, $id);
if (!@ldap_bind($connect, $dn, $password)) {
Logger::notice('Could not authenticate LDAP user with provided password', ['errno' => ldap_errno($connect), 'error' => ldap_error($connect)]);
DI::logger()->notice('Could not authenticate LDAP user with provided password', ['errno' => ldap_errno($connect), 'error' => ldap_error($connect)]);
return false;
}
if (strlen($ldap_group) && @ldap_compare($connect, $ldap_group, 'member', $dn) !== true) {
$errno = @ldap_errno($connect);
if ($errno === 32) {
Logger::notice('LDAP Access Control Group does not exist', ['errno' => $errno, 'error' => ldap_error($connect)]);
DI::logger()->notice('LDAP Access Control Group does not exist', ['errno' => $errno, 'error' => ldap_error($connect)]);
} elseif ($errno === 16) {
Logger::notice('LDAP membership attribute does not exist in access control group', ['errno' => $errno, 'error' => ldap_error($connect)]);
DI::logger()->notice('LDAP membership attribute does not exist in access control group', ['errno' => $errno, 'error' => ldap_error($connect)]);
} else {
Logger::notice('LDAP user isn\'t part of the authorized group', ['dn' => $dn]);
DI::logger()->notice('LDAP user isn\'t part of the authorized group', ['dn' => $dn]);
}
@ldap_close($connect);
@ -140,7 +139,7 @@ function ldapauth_authenticate($username, $password)
$authentication = User::getAuthenticationInfo($username);
return User::getById($authentication['uid']);
} catch (Exception $e) {
Logger::notice('LDAP authentication error: ' . $e->getMessage());
DI::logger()->notice('LDAP authentication error: ' . $e->getMessage());
return false;
}
}
@ -148,7 +147,7 @@ function ldapauth_authenticate($username, $password)
function ldap_createaccount($username, $password, $email, $name)
{
if (!strlen($email) || !strlen($name)) {
Logger::notice('Could not create local user from LDAP data, no email or nickname provided');
DI::logger()->notice('Could not create local user from LDAP data, no email or nickname provided');
return false;
}
@ -160,10 +159,10 @@ function ldap_createaccount($username, $password, $email, $name)
'password' => $password,
'verified' => 1
]);
Logger::info('Local user created from LDAP data', ['username' => $username, 'name' => $name]);
DI::logger()->info('Local user created from LDAP data', ['username' => $username, 'name' => $name]);
return $user;
} catch (Exception $ex) {
Logger::error('Could not create local user from LDAP data', ['username' => $username, 'exception' => $ex->getMessage()]);
DI::logger()->error('Could not create local user from LDAP data', ['username' => $username, 'exception' => $ex->getMessage()]);
}
return false;