Skip to content

Commit f9e6f2e

Browse files
committed
Use Psr\Log\LoggerInterface where it can easily be used in user_ldap
Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
1 parent d189a23 commit f9e6f2e

5 files changed

Lines changed: 136 additions & 78 deletions

File tree

apps/user_ldap/lib/Connection.php

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
namespace OCA\User_LDAP;
3838

3939
use OC\ServerNotAvailableException;
40-
use OCP\ILogger;
40+
use Psr\Log\LoggerInterface;
4141

4242
/**
4343
* magic properties (incomplete)
@@ -103,6 +103,9 @@ class Connection extends LDAPUtility {
103103

104104
protected $bindResult = [];
105105

106+
/** @var LoggerInterface */
107+
protected $logger;
108+
106109
/**
107110
* Constructor
108111
* @param ILDAPWrapper $ldap
@@ -122,6 +125,7 @@ public function __construct(ILDAPWrapper $ldap, $configPrefix = '', $configID =
122125
$helper = new Helper(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection());
123126
$this->doNotValidate = !in_array($this->configPrefix,
124127
$helper->getServerConfigurationPrefixes());
128+
$this->logger = \OC::$server->get(LoggerInterface::class);
125129
}
126130

127131
public function __destruct() {
@@ -208,7 +212,10 @@ public function getConnectionResource() {
208212
$this->establishConnection();
209213
}
210214
if (is_null($this->ldapConnectionRes)) {
211-
\OCP\Util::writeLog('user_ldap', 'No LDAP Connection to server ' . $this->configuration->ldapHost, ILogger::ERROR);
215+
$this->logger->error(
216+
'No LDAP Connection to server ' . $this->configuration->ldapHost,
217+
['app' => 'user_ldap']
218+
);
212219
throw new ServerNotAvailableException('Connection to LDAP server could not be established');
213220
}
214221
return $this->ldapConnectionRes;
@@ -378,10 +385,10 @@ private function doSoftValidation() {
378385
&& (!is_null($this->configID))) {
379386
$this->configuration->$effectiveSetting = 'auto';
380387
$this->configuration->saveConfiguration();
381-
\OCP\Util::writeLog('user_ldap',
382-
'Illegal value for the '.
383-
$effectiveSetting.', '.'reset to '.
384-
'autodetect.', ILogger::INFO);
388+
$this->logger->info(
389+
'Illegal value for the '.$effectiveSetting.', reset to autodetect.',
390+
['app' => 'user_ldap']
391+
);
385392
}
386393
}
387394
}
@@ -404,10 +411,9 @@ private function doSoftValidation() {
404411
if ((stripos($this->configuration->ldapHost, 'ldaps://') === 0)
405412
&& $this->configuration->ldapTLS) {
406413
$this->configuration->ldapTLS = false;
407-
\OCP\Util::writeLog(
408-
'user_ldap',
414+
$this->logger->info(
409415
'LDAPS (already using secure connection) and TLS do not work together. Switched off TLS.',
410-
ILogger::INFO
416+
['app' => 'user_ldap']
411417
);
412418
}
413419
}
@@ -447,10 +453,9 @@ private function doCriticalValidation() {
447453
break;
448454
}
449455
$configurationOK = false;
450-
\OCP\Util::writeLog(
451-
'user_ldap',
456+
$this->logger->warning(
452457
$errorStr.'No '.$subj.' given!',
453-
ILogger::WARN
458+
['app' => 'user_ldap']
454459
);
455460
}
456461
}
@@ -462,11 +467,11 @@ private function doCriticalValidation() {
462467
($agent === '' && $pwd !== '')
463468
|| ($agent !== '' && $pwd === '')
464469
) {
465-
\OCP\Util::writeLog(
466-
'user_ldap',
470+
$this->logger->warning(
467471
$errorStr.'either no password is given for the user ' .
468472
'agent or a password is given, but not an LDAP agent.',
469-
ILogger::WARN);
473+
['app' => 'user_ldap']
474+
);
470475
$configurationOK = false;
471476
}
472477

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

477482
if (empty($base) && empty($baseUsers) && empty($baseGroups)) {
478-
\OCP\Util::writeLog(
479-
'user_ldap',
483+
$this->logger->warning(
480484
$errorStr.'Not a single Base DN given.',
481-
ILogger::WARN
485+
['app' => 'user_ldap']
482486
);
483487
$configurationOK = false;
484488
}
485489

486490
if (mb_strpos($this->configuration->ldapLoginFilter, '%uid', 0, 'UTF-8')
487491
=== false) {
488-
\OCP\Util::writeLog(
489-
'user_ldap',
492+
$this->logger->warning(
490493
$errorStr.'login filter does not contain %uid place holder.',
491-
ILogger::WARN
494+
['app' => 'user_ldap']
492495
);
493496
$configurationOK = false;
494497
}
@@ -532,34 +535,32 @@ private function establishConnection() {
532535
return false;
533536
}
534537
if (!$this->ignoreValidation && !$this->configured) {
535-
\OCP\Util::writeLog(
536-
'user_ldap',
538+
$this->logger->warning(
537539
'Configuration is invalid, cannot connect',
538-
ILogger::WARN
540+
['app' => 'user_ldap']
539541
);
540542
return false;
541543
}
542544
if (!$this->ldapConnectionRes) {
543545
if (!$this->ldap->areLDAPFunctionsAvailable()) {
544546
$phpLDAPinstalled = false;
545-
\OCP\Util::writeLog(
546-
'user_ldap',
547+
$this->logger->error(
547548
'function ldap_connect is not available. Make sure that the PHP ldap module is installed.',
548-
ILogger::ERROR
549+
['app' => 'user_ldap']
549550
);
550551

551552
return false;
552553
}
553554
if ($this->configuration->turnOffCertCheck) {
554555
if (putenv('LDAPTLS_REQCERT=never')) {
555-
\OCP\Util::writeLog('user_ldap',
556+
$this->logger->debug(
556557
'Turned off SSL certificate validation successfully.',
557-
ILogger::DEBUG);
558+
['app' => 'user_ldap']
559+
);
558560
} else {
559-
\OCP\Util::writeLog(
560-
'user_ldap',
561+
$this->logger->warning(
561562
'Could not turn off SSL certificate validation.',
562-
ILogger::WARN
563+
['app' => 'user_ldap']
563564
);
564565
}
565566
}
@@ -669,9 +670,10 @@ public function bind() {
669670
if (!$ldapLogin) {
670671
$errno = $this->ldap->errno($cr);
671672

672-
\OCP\Util::writeLog('user_ldap',
673+
$this->logger->warning(
673674
'Bind failed: ' . $errno . ': ' . $this->ldap->error($cr),
674-
ILogger::WARN);
675+
['app' => 'user_ldap']
676+
);
675677

676678
// Set to failure mode, if LDAP error code is not one of
677679
// - LDAP_SUCCESS (0)

apps/user_ldap/lib/Group_LDAP.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
use OC\ServerNotAvailableException;
5050
use OCP\Group\Backend\IGetDisplayNameBackend;
5151
use OCP\GroupInterface;
52-
use OCP\ILogger;
52+
use Psr\Log\LoggerInterface;
5353

5454
class Group_LDAP extends BackendUtility implements GroupInterface, IGroupLDAP, IGetDisplayNameBackend {
5555
protected $enabled = false;
@@ -62,7 +62,7 @@ class Group_LDAP extends BackendUtility implements GroupInterface, IGroupLDAP, I
6262
protected $cachedNestedGroups;
6363
/** @var GroupPluginManager */
6464
protected $groupPluginManager;
65-
/** @var ILogger */
65+
/** @var LoggerInterface */
6666
protected $logger;
6767

6868
/**
@@ -82,7 +82,7 @@ public function __construct(Access $access, GroupPluginManager $groupPluginManag
8282
$this->cachedGroupsByMember = new CappedMemoryCache();
8383
$this->cachedNestedGroups = new CappedMemoryCache();
8484
$this->groupPluginManager = $groupPluginManager;
85-
$this->logger = OC::$server->getLogger();
85+
$this->logger = OC::$server->get(LoggerInterface::class);
8686
$this->ldapGroupMemberAssocAttr = strtolower($gAssoc);
8787
}
8888

apps/user_ldap/lib/Jobs/UpdateGroups.php

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
use OCP\Group\Events\UserRemovedEvent;
3636
use OCP\IDBConnection;
3737
use OCP\IGroupManager;
38-
use OCP\ILogger;
3938
use OCP\IUser;
4039
use OCP\IUserManager;
4140
use Psr\Log\LoggerInterface;
@@ -89,23 +88,30 @@ public function run($argument) {
8988
}
9089

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

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

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

104107
$this->handleKnownGroups(array_intersect($actualGroups, $knownGroups));
105108
$this->handleCreatedGroups(array_diff($actualGroups, $knownGroups));
106109
$this->handleRemovedGroups(array_diff($knownGroups, $actualGroups));
107110

108-
\OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – Finished.', ILogger::DEBUG);
111+
$this->logger->debug(
112+
'bgJ "updateGroups" – Finished.',
113+
['app' => 'user_ldap']
114+
);
109115
}
110116

111117
/**
@@ -198,46 +204,56 @@ private function handleKnownGroups(array $groups) {
198204
* @param string[] $createdGroups
199205
*/
200206
private function handleCreatedGroups($createdGroups) {
201-
\OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – dealing with created Groups.', ILogger::DEBUG);
207+
$this->logger->debug(
208+
'bgJ "updateGroups" – dealing with created Groups.',
209+
['app' => 'user_ldap']
210+
);
202211

203212
$query = $this->dbc->getQueryBuilder();
204213
$query->insert('ldap_group_members')
205214
->setValue('owncloudname', $query->createParameter('owncloudname'))
206215
->setValue('owncloudusers', $query->createParameter('owncloudusers'));
207216
foreach ($createdGroups as $createdGroup) {
208-
\OCP\Util::writeLog('user_ldap',
217+
$this->logger->info(
209218
'bgJ "updateGroups" – new group "' . $createdGroup . '" found.',
210-
ILogger::INFO);
219+
['app' => 'user_ldap']
220+
);
211221
$users = serialize($this->groupBackend->usersInGroup($createdGroup));
212222

213223
$query->setParameter('owncloudname', $createdGroup)
214224
->setParameter('owncloudusers', $users);
215225
$query->execute();
216226
}
217-
\OCP\Util::writeLog('user_ldap',
227+
$this->logger->debug(
218228
'bgJ "updateGroups" – FINISHED dealing with created Groups.',
219-
ILogger::DEBUG);
229+
['app' => 'user_ldap']
230+
);
220231
}
221232

222233
/**
223234
* @param string[] $removedGroups
224235
*/
225236
private function handleRemovedGroups($removedGroups) {
226-
\OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – dealing with removed groups.', ILogger::DEBUG);
237+
$this->logger->debug(
238+
'bgJ "updateGroups" – dealing with removed groups.',
239+
['app' => 'user_ldap']
240+
);
227241

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

232246
foreach ($removedGroups as $removedGroup) {
233-
\OCP\Util::writeLog('user_ldap',
247+
$this->logger->info(
234248
'bgJ "updateGroups" – group "' . $removedGroup . '" was removed.',
235-
ILogger::INFO);
249+
['app' => 'user_ldap']
250+
);
236251
$query->setParameter('owncloudname', $removedGroup);
237252
$query->execute();
238253
}
239-
\OCP\Util::writeLog('user_ldap',
254+
$this->logger->debug(
240255
'bgJ "updateGroups" – FINISHED dealing with removed groups.',
241-
ILogger::DEBUG);
256+
['app' => 'user_ldap']
257+
);
242258
}
243259
}

0 commit comments

Comments
 (0)