From 8fe914f07e1b4d41c02e127e2242e0a770535455 Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Thu, 15 Mar 2018 14:16:43 +0100 Subject: [PATCH] LDAP backend to emit announce and revoke signals on mapping changes Signed-off-by: Arthur Schiwon --- apps/user_ldap/ajax/clearMappings.php | 22 +++++++++++----- apps/user_ldap/lib/Access.php | 2 ++ .../user_ldap/lib/Mapping/AbstractMapping.php | 26 +++++++++++++++++++ apps/user_ldap/lib/User_LDAP.php | 2 +- .../tests/Mapping/AbstractMappingTest.php | 23 ++++++++++++++++ 5 files changed, 68 insertions(+), 7 deletions(-) diff --git a/apps/user_ldap/ajax/clearMappings.php b/apps/user_ldap/ajax/clearMappings.php index 01b6b7f0ef..009a491d34 100644 --- a/apps/user_ldap/ajax/clearMappings.php +++ b/apps/user_ldap/ajax/clearMappings.php @@ -33,13 +33,23 @@ use OCA\User_LDAP\Mapping\GroupMapping; $subject = (string)$_POST['ldap_clear_mapping']; $mapping = null; -if($subject === 'user') { - $mapping = new UserMapping(\OC::$server->getDatabaseConnection()); -} else if($subject === 'group') { - $mapping = new GroupMapping(\OC::$server->getDatabaseConnection()); -} try { - if(is_null($mapping) || !$mapping->clear()) { + if($subject === 'user') { + $mapping = new UserMapping(\OC::$server->getDatabaseConnection()); + $result = $mapping->clearCb( + function ($uid) { + \OC::$server->getUserManager()->emit('\OC\User', 'preRevokeUser', [$uid]); + }, + function ($uid) { + \OC::$server->getUserManager()->emit('\OC\User', 'postRevokeUser', [$uid]); + } + ); + } else if($subject === 'group') { + $mapping = new GroupMapping(\OC::$server->getDatabaseConnection()); + $result = $mapping->clear(); + } + + if($mapping === null || !$result) { $l = \OC::$server->getL10N('user_ldap'); throw new \Exception($l->t('Failed to clear the mappings.')); } diff --git a/apps/user_ldap/lib/Access.php b/apps/user_ldap/lib/Access.php index 9fb3709027..c9a7cd557a 100644 --- a/apps/user_ldap/lib/Access.php +++ b/apps/user_ldap/lib/Access.php @@ -609,6 +609,7 @@ class Access extends LDAPUtility implements IUserTools { || (!$isUser && !\OC::$server->getGroupManager()->groupExists($intName))) { if($mapper->map($fdn, $intName, $uuid)) { $this->connection->setConfiguration(array('ldapCacheTTL' => $originalTTL)); + \OC::$server->getUserManager()->emit('\OC\User', 'announceUser', [$intName]); $newlyMapped = true; return $intName; } @@ -617,6 +618,7 @@ class Access extends LDAPUtility implements IUserTools { $altName = $this->createAltInternalOwnCloudName($intName, $isUser); if(is_string($altName) && $mapper->map($fdn, $altName, $uuid)) { + \OC::$server->getUserManager()->emit('\OC\User', 'announceUser', [$intName]); $newlyMapped = true; return $altName; } diff --git a/apps/user_ldap/lib/Mapping/AbstractMapping.php b/apps/user_ldap/lib/Mapping/AbstractMapping.php index f5f56ce03d..c7d737a763 100644 --- a/apps/user_ldap/lib/Mapping/AbstractMapping.php +++ b/apps/user_ldap/lib/Mapping/AbstractMapping.php @@ -278,6 +278,32 @@ abstract class AbstractMapping { return $this->dbc->prepare($sql)->execute(); } + /** + * clears the mapping table one by one and executing a callback with + * each row's id (=owncloud_name col) + * + * @param callable $preCallback + * @param callable $postCallback + * @return bool true on success, false when at least one row was not + * deleted + */ + public function clearCb(Callable $preCallback, Callable $postCallback): bool { + $picker = $this->dbc->getQueryBuilder(); + $picker->select('owncloud_name') + ->from($this->getTableName()); + $cursor = $picker->execute(); + $result = true; + while($id = $cursor->fetchColumn(0)) { + $preCallback($id); + if($isUnmapped = $this->unmap($id)) { + $postCallback($id); + } + $result &= $isUnmapped; + } + $cursor->closeCursor(); + return $result; + } + /** * returns the number of entries in the mappings table * diff --git a/apps/user_ldap/lib/User_LDAP.php b/apps/user_ldap/lib/User_LDAP.php index 5a2b993c33..3868645cae 100644 --- a/apps/user_ldap/lib/User_LDAP.php +++ b/apps/user_ldap/lib/User_LDAP.php @@ -396,7 +396,7 @@ class User_LDAP extends BackendUtility implements \OCP\IUserBackend, \OCP\UserIn \OC::$server->getLogger()->info('Cleaning up after user ' . $uid, array('app' => 'user_ldap')); - $this->access->getUserMapper()->unmap($uid); + $this->access->getUserMapper()->unmap($uid); // we don't emit revoke signals here, since it is implicit to delete signals fired from core $this->access->userManager->invalidate($uid); return true; } diff --git a/apps/user_ldap/tests/Mapping/AbstractMappingTest.php b/apps/user_ldap/tests/Mapping/AbstractMappingTest.php index d3d33a82da..54d8b49cdc 100644 --- a/apps/user_ldap/tests/Mapping/AbstractMappingTest.php +++ b/apps/user_ldap/tests/Mapping/AbstractMappingTest.php @@ -234,6 +234,29 @@ abstract class AbstractMappingTest extends \Test\TestCase { } } + /** + * tests clear() for successful update. + */ + public function testClearCb() { + list($mapper, $data) = $this->initTest(); + + $callbackCalls = 0; + $test = $this; + + $callback = function (string $id) use ($test, &$callbackCalls) { + $test->assertTrue(trim($id) !== ''); + $callbackCalls++; + }; + + $done = $mapper->clearCb($callback, $callback); + $this->assertTrue($done); + $this->assertSame(count($data) * 2, $callbackCalls); + foreach($data as $entry) { + $name = $mapper->getNameByUUID($entry['uuid']); + $this->assertFalse($name); + } + } + /** * tests getList() method */