LDAP backend to emit announce and revoke signals on mapping changes

Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
This commit is contained in:
Arthur Schiwon 2018-03-15 14:16:43 +01:00
parent feef3cbba0
commit 8fe914f07e
No known key found for this signature in database
GPG Key ID: 7424F1874854DF23
5 changed files with 68 additions and 7 deletions

View File

@ -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.'));
}

View File

@ -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;
}

View File

@ -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
*

View File

@ -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;
}

View File

@ -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
*/