Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Use Psr\Log\LoggerInterface where it can easily be used in user_ldap
Signed-off-by: Côme Chilliet <[email protected]>
  • Loading branch information
come-nc committed Oct 14, 2021
commit f9e6f2ea577b103b00be81bda3355a73245f1152
70 changes: 36 additions & 34 deletions apps/user_ldap/lib/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
namespace OCA\User_LDAP;

use OC\ServerNotAvailableException;
use OCP\ILogger;
use Psr\Log\LoggerInterface;

/**
* magic properties (incomplete)
Expand Down Expand Up @@ -103,6 +103,9 @@ class Connection extends LDAPUtility {

protected $bindResult = [];

/** @var LoggerInterface */
protected $logger;

/**
* Constructor
* @param ILDAPWrapper $ldap
Expand All @@ -122,6 +125,7 @@ public function __construct(ILDAPWrapper $ldap, $configPrefix = '', $configID =
$helper = new Helper(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection());
$this->doNotValidate = !in_array($this->configPrefix,
$helper->getServerConfigurationPrefixes());
$this->logger = \OC::$server->get(LoggerInterface::class);
}

public function __destruct() {
Expand Down Expand Up @@ -208,7 +212,10 @@ public function getConnectionResource() {
$this->establishConnection();
}
if (is_null($this->ldapConnectionRes)) {
\OCP\Util::writeLog('user_ldap', 'No LDAP Connection to server ' . $this->configuration->ldapHost, ILogger::ERROR);
$this->logger->error(
'No LDAP Connection to server ' . $this->configuration->ldapHost,
['app' => 'user_ldap']
);
throw new ServerNotAvailableException('Connection to LDAP server could not be established');
}
return $this->ldapConnectionRes;
Expand Down Expand Up @@ -378,10 +385,10 @@ private function doSoftValidation() {
&& (!is_null($this->configID))) {
$this->configuration->$effectiveSetting = 'auto';
$this->configuration->saveConfiguration();
\OCP\Util::writeLog('user_ldap',
'Illegal value for the '.
$effectiveSetting.', '.'reset to '.
'autodetect.', ILogger::INFO);
$this->logger->info(
'Illegal value for the '.$effectiveSetting.', reset to autodetect.',
['app' => 'user_ldap']
);
}
}
}
Expand All @@ -404,10 +411,9 @@ private function doSoftValidation() {
if ((stripos($this->configuration->ldapHost, 'ldaps://') === 0)
&& $this->configuration->ldapTLS) {
$this->configuration->ldapTLS = false;
\OCP\Util::writeLog(
'user_ldap',
$this->logger->info(
'LDAPS (already using secure connection) and TLS do not work together. Switched off TLS.',
ILogger::INFO
['app' => 'user_ldap']
);
}
}
Expand Down Expand Up @@ -447,10 +453,9 @@ private function doCriticalValidation() {
break;
}
$configurationOK = false;
\OCP\Util::writeLog(
'user_ldap',
$this->logger->warning(
$errorStr.'No '.$subj.' given!',
ILogger::WARN
['app' => 'user_ldap']
);
}
}
Expand All @@ -462,11 +467,11 @@ private function doCriticalValidation() {
($agent === '' && $pwd !== '')
|| ($agent !== '' && $pwd === '')
) {
\OCP\Util::writeLog(
'user_ldap',
$this->logger->warning(
$errorStr.'either no password is given for the user ' .
'agent or a password is given, but not an LDAP agent.',
ILogger::WARN);
['app' => 'user_ldap']
);
$configurationOK = false;
}

Expand All @@ -475,20 +480,18 @@ private function doCriticalValidation() {
$baseGroups = $this->configuration->ldapBaseGroups;

if (empty($base) && empty($baseUsers) && empty($baseGroups)) {
\OCP\Util::writeLog(
'user_ldap',
$this->logger->warning(
$errorStr.'Not a single Base DN given.',
ILogger::WARN
['app' => 'user_ldap']
);
$configurationOK = false;
}

if (mb_strpos($this->configuration->ldapLoginFilter, '%uid', 0, 'UTF-8')
=== false) {
\OCP\Util::writeLog(
'user_ldap',
$this->logger->warning(
$errorStr.'login filter does not contain %uid place holder.',
ILogger::WARN
['app' => 'user_ldap']
);
$configurationOK = false;
}
Expand Down Expand Up @@ -532,34 +535,32 @@ private function establishConnection() {
return false;
}
if (!$this->ignoreValidation && !$this->configured) {
\OCP\Util::writeLog(
'user_ldap',
$this->logger->warning(
'Configuration is invalid, cannot connect',
ILogger::WARN
['app' => 'user_ldap']
);
return false;
}
if (!$this->ldapConnectionRes) {
if (!$this->ldap->areLDAPFunctionsAvailable()) {
$phpLDAPinstalled = false;
\OCP\Util::writeLog(
'user_ldap',
$this->logger->error(
'function ldap_connect is not available. Make sure that the PHP ldap module is installed.',
ILogger::ERROR
['app' => 'user_ldap']
);

return false;
}
if ($this->configuration->turnOffCertCheck) {
if (putenv('LDAPTLS_REQCERT=never')) {
\OCP\Util::writeLog('user_ldap',
$this->logger->debug(
'Turned off SSL certificate validation successfully.',
ILogger::DEBUG);
['app' => 'user_ldap']
);
} else {
\OCP\Util::writeLog(
'user_ldap',
$this->logger->warning(
'Could not turn off SSL certificate validation.',
ILogger::WARN
['app' => 'user_ldap']
);
}
}
Expand Down Expand Up @@ -669,9 +670,10 @@ public function bind() {
if (!$ldapLogin) {
$errno = $this->ldap->errno($cr);

\OCP\Util::writeLog('user_ldap',
$this->logger->warning(
'Bind failed: ' . $errno . ': ' . $this->ldap->error($cr),
ILogger::WARN);
['app' => 'user_ldap']
);

// Set to failure mode, if LDAP error code is not one of
// - LDAP_SUCCESS (0)
Expand Down
6 changes: 3 additions & 3 deletions apps/user_ldap/lib/Group_LDAP.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
use OC\ServerNotAvailableException;
use OCP\Group\Backend\IGetDisplayNameBackend;
use OCP\GroupInterface;
use OCP\ILogger;
use Psr\Log\LoggerInterface;

class Group_LDAP extends BackendUtility implements GroupInterface, IGroupLDAP, IGetDisplayNameBackend {
protected $enabled = false;
Expand All @@ -62,7 +62,7 @@ class Group_LDAP extends BackendUtility implements GroupInterface, IGroupLDAP, I
protected $cachedNestedGroups;
/** @var GroupPluginManager */
protected $groupPluginManager;
/** @var ILogger */
/** @var LoggerInterface */
protected $logger;

/**
Expand All @@ -82,7 +82,7 @@ public function __construct(Access $access, GroupPluginManager $groupPluginManag
$this->cachedGroupsByMember = new CappedMemoryCache();
$this->cachedNestedGroups = new CappedMemoryCache();
$this->groupPluginManager = $groupPluginManager;
$this->logger = OC::$server->getLogger();
$this->logger = OC::$server->get(LoggerInterface::class);
$this->ldapGroupMemberAssocAttr = strtolower($gAssoc);
}

Expand Down
46 changes: 31 additions & 15 deletions apps/user_ldap/lib/Jobs/UpdateGroups.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
use OCP\Group\Events\UserRemovedEvent;
use OCP\IDBConnection;
use OCP\IGroupManager;
use OCP\ILogger;
use OCP\IUser;
use OCP\IUserManager;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -89,23 +88,30 @@ public function run($argument) {
}

public function updateGroups() {
\OCP\Util::writeLog('user_ldap', 'Run background job "updateGroups"', ILogger::DEBUG);
$this->logger->debug(
'Run background job "updateGroups"',
['app' => 'user_ldap']
);

$knownGroups = array_keys($this->getKnownGroups());
$actualGroups = $this->groupBackend->getGroups();

if (empty($actualGroups) && empty($knownGroups)) {
\OCP\Util::writeLog('user_ldap',
$this->logger->info(
'bgJ "updateGroups" – groups do not seem to be configured properly, aborting.',
ILogger::INFO);
['app' => 'user_ldap']
);
return;
}

$this->handleKnownGroups(array_intersect($actualGroups, $knownGroups));
$this->handleCreatedGroups(array_diff($actualGroups, $knownGroups));
$this->handleRemovedGroups(array_diff($knownGroups, $actualGroups));

\OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – Finished.', ILogger::DEBUG);
$this->logger->debug(
'bgJ "updateGroups" – Finished.',
['app' => 'user_ldap']
);
}

/**
Expand Down Expand Up @@ -198,46 +204,56 @@ private function handleKnownGroups(array $groups) {
* @param string[] $createdGroups
*/
private function handleCreatedGroups($createdGroups) {
\OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – dealing with created Groups.', ILogger::DEBUG);
$this->logger->debug(
'bgJ "updateGroups" – dealing with created Groups.',
['app' => 'user_ldap']
);

$query = $this->dbc->getQueryBuilder();
$query->insert('ldap_group_members')
->setValue('owncloudname', $query->createParameter('owncloudname'))
->setValue('owncloudusers', $query->createParameter('owncloudusers'));
foreach ($createdGroups as $createdGroup) {
\OCP\Util::writeLog('user_ldap',
$this->logger->info(
'bgJ "updateGroups" – new group "' . $createdGroup . '" found.',
ILogger::INFO);
['app' => 'user_ldap']
);
$users = serialize($this->groupBackend->usersInGroup($createdGroup));

$query->setParameter('owncloudname', $createdGroup)
->setParameter('owncloudusers', $users);
$query->execute();
}
\OCP\Util::writeLog('user_ldap',
$this->logger->debug(
'bgJ "updateGroups" – FINISHED dealing with created Groups.',
ILogger::DEBUG);
['app' => 'user_ldap']
);
}

/**
* @param string[] $removedGroups
*/
private function handleRemovedGroups($removedGroups) {
\OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – dealing with removed groups.', ILogger::DEBUG);
$this->logger->debug(
'bgJ "updateGroups" – dealing with removed groups.',
['app' => 'user_ldap']
);

$query = $this->dbc->getQueryBuilder();
$query->delete('ldap_group_members')
->where($query->expr()->eq('owncloudname', $query->createParameter('owncloudname')));

foreach ($removedGroups as $removedGroup) {
\OCP\Util::writeLog('user_ldap',
$this->logger->info(
'bgJ "updateGroups" – group "' . $removedGroup . '" was removed.',
ILogger::INFO);
['app' => 'user_ldap']
);
$query->setParameter('owncloudname', $removedGroup);
$query->execute();
}
\OCP\Util::writeLog('user_ldap',
$this->logger->debug(
'bgJ "updateGroups" – FINISHED dealing with removed groups.',
ILogger::DEBUG);
['app' => 'user_ldap']
);
}
}
Loading