Merge pull request #272 from hschletz/fix-270

Set up logger instance in base constructor
This commit is contained in:
tflidd
2025-09-23 14:56:12 +02:00
committed by GitHub
7 changed files with 19 additions and 14 deletions

View File

@@ -9,6 +9,9 @@
*/
namespace OCA\UserExternal;
use OCP\Server;
use Psr\Log\LoggerInterface;
/**
* Base class for external auth implementations that stores users
* on their first login in a local table.
@@ -23,6 +26,7 @@ namespace OCA\UserExternal;
*/
abstract class Base extends \OC\User\Backend {
protected $backend = '';
protected readonly LoggerInterface $logger;
/**
* Create new instance, set backend name
@@ -31,6 +35,7 @@ abstract class Base extends \OC\User\Backend {
*/
public function __construct($backend) {
$this->backend = $backend;
$this->logger = Server::get(LoggerInterface::class);
}
/**

View File

@@ -37,14 +37,14 @@ class BasicAuth extends Base {
);
$canary = get_headers($this->authUrl, 1, $context);
if (!$canary) {
\OC::$server->getLogger()->error(
$this->logger->error(
'ERROR: Not possible to connect to BasicAuth Url: '.$this->authUrl,
['app' => 'user_external']
);
return false;
}
if (!isset(array_change_key_case($canary, CASE_LOWER)['www-authenticate'])) {
\OC::$server->getLogger()->error(
$this->logger->error(
'ERROR: Mis-configured BasicAuth Url: '.$this->authUrl.', provided URL does not do authentication!',
['app' => 'user_external']
);
@@ -61,7 +61,7 @@ class BasicAuth extends Base {
$headers = get_headers($this->authUrl, 1, $context);
if (!$headers) {
\OC::$server->getLogger()->error(
$this->logger->error(
'ERROR: Not possible to connect to BasicAuth Url: '.$this->authUrl,
['app' => 'user_external']
);
@@ -82,7 +82,7 @@ class BasicAuth extends Base {
$this->storeUser($uid);
return $uid;
case "3":
\OC::$server->getLogger()->error(
$this->logger->error(
'ERROR: Too many redirects from BasicAuth Url: '.$this->authUrl,
['app' => 'user_external']
);

View File

@@ -48,7 +48,7 @@ class FTP extends Base {
*/
public function checkPassword($uid, $password) {
if (false === array_search($this->protocol, stream_get_wrappers())) {
\OC::$server->getLogger()->error(
$this->logger->error(
'ERROR: Stream wrapper not available: ' . $this->protocol,
['app' => 'user_external']
);

View File

@@ -71,7 +71,7 @@ class IMAP extends Base {
$uid = $pieces[0];
}
} else {
\OC::$server->getLogger()->error(
$this->logger->error(
'ERROR: User has a wrong domain! Expecting: '.$this->domain,
['app' => 'user_external']
);
@@ -111,7 +111,7 @@ class IMAP extends Base {
$errorcode === 28) {
# This is not defined in PHP-8.x
# 28: CURLE_OPERATION_TIMEDOUT
\OC::$server->getLogger()->error(
$this->logger->error(
'ERROR: Could not connect to imap server via curl: ' . curl_strerror($errorcode),
['app' => 'user_external']
);
@@ -122,12 +122,12 @@ class IMAP extends Base {
# 9: CURLE_REMOTE_ACCESS_DENIED
# 67: CURLE_LOGIN_DENIED
# 94: CURLE_AUTH_ERROR)
\OC::$server->getLogger()->error(
$this->logger->error(
'ERROR: IMAP Login failed via curl: ' . curl_strerror($errorcode),
['app' => 'user_external']
);
} else {
\OC::$server->getLogger()->error(
$this->logger->error(
'ERROR: IMAP server returned an error: ' . $errorcode . ' / ' . curl_strerror($errorcode),
['app' => 'user_external']
);

View File

@@ -43,7 +43,7 @@ class SMB extends Base {
$command = self::SMBCLIENT.' '.escapeshellarg('//' . $this->host . '/dummy').' -U '.$uidEscaped.'%'.$password;
$lastline = exec($command, $output, $retval);
if ($retval === 127) {
\OC::$server->getLogger()->error(
$this->logger->error(
'ERROR: smbclient executable missing',
['app' => 'user_external']
);
@@ -56,7 +56,7 @@ class SMB extends Base {
goto login;
} elseif ($retval !== 0) {
//some other error
\OC::$server->getLogger()->error(
$this->logger->error(
'ERROR: smbclient error: ' . trim($lastline),
['app' => 'user_external']
);

View File

@@ -44,7 +44,7 @@ class SSH extends Base {
*/
public function checkPassword($uid, $password) {
if (!extension_loaded('ssh2')) {
\OC::$server->getLogger()->error(
$this->logger->error(
'ERROR: php-ssh2 PECL module missing',
['app' => 'user_external']
);

View File

@@ -27,14 +27,14 @@ class WebDavAuth extends Base {
public function checkPassword($uid, $password) {
$arr = explode('://', $this->webDavAuthUrl, 2);
if (! isset($arr) or count($arr) !== 2) {
\OC::$server->getLogger()->error('ERROR: Invalid WebdavUrl: "'.$this->webDavAuthUrl.'" ', ['app' => 'user_external']);
$this->logger->error('ERROR: Invalid WebdavUrl: "'.$this->webDavAuthUrl.'" ', ['app' => 'user_external']);
return false;
}
list($protocol, $path) = $arr;
$url = $protocol.'://'.urlencode($uid).':'.urlencode($password).'@'.$path;
$headers = get_headers($url);
if ($headers === false) {
\OC::$server->getLogger()->error('ERROR: Not possible to connect to WebDAV Url: "'.$protocol.'://'.$path.'" ', ['app' => 'user_external']);
$this->logger->error('ERROR: Not possible to connect to WebDAV Url: "'.$protocol.'://'.$path.'" ', ['app' => 'user_external']);
return false;
}
$returnCode = substr($headers[0], 9, 3);