Merge pull request #13568 from owncloud/s2s_delete_shares_jenkins

[ignore] delete all server-to-server shares if a user gets deleted
This commit is contained in:
Morris Jobke 2015-01-22 00:37:39 +01:00
commit 4dac664002
4 changed files with 144 additions and 0 deletions

View File

@ -260,6 +260,33 @@ class Manager {
return (bool)$query->execute(array($hash, $user->getUID()));
}
/**
* remove all shares for user $uid if the user was deleted
*
* @param string $uid
* @return bool
*/
public function removeUserShares($uid) {
$getShare = $this->connection->prepare('
SELECT `remote`, `share_token`, `remote_id`
FROM `*PREFIX*share_external`
WHERE `user` = ?');
$result = $getShare->execute(array($uid));
if ($result) {
$shares = $getShare->fetchAll();
foreach($shares as $share) {
$this->sendFeedbackToRemote($share['remote'], $share['share_token'], $share['remote_id'], 'decline');
}
}
$query = $this->connection->prepare('
DELETE FROM `*PREFIX*share_external`
WHERE `user` = ?
');
return (bool)$query->execute(array($uid));
}
/**
* return a list of shares which are not yet accepted by the user
*

View File

@ -16,6 +16,8 @@ class Helper {
\OCP\Util::connectHook('OCP\Share', 'post_shared', '\OC\Files\Cache\Shared_Updater', 'postShareHook');
\OCP\Util::connectHook('OCP\Share', 'post_unshare', '\OC\Files\Cache\Shared_Updater', 'postUnshareHook');
\OCP\Util::connectHook('OCP\Share', 'post_unshareFromSelf', '\OC\Files\Cache\Shared_Updater', 'postUnshareFromSelfHook');
\OCP\Util::connectHook('OC_User', 'post_deleteUser', '\OCA\Files_Sharing\Hooks', 'deleteUser');
}
/**

View File

@ -0,0 +1,39 @@
<?php
/**
* ownCloud
*
* @copyright (C) 2015 ownCloud, Inc.
*
* @author Bjoern Schiessle <schiessle@owncloud.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
namespace OCA\Files_Sharing;
class Hooks {
public static function deleteUser($params) {
$manager = new External\Manager(
\OC::$server->getDatabaseConnection(),
\OC\Files\Filesystem::getMountManager(),
\OC\Files\Filesystem::getLoader(),
\OC::$server->getUserSession(),
\OC::$server->getHTTPHelper());
$manager->removeUserShares($params['uid']);
}
}

View File

@ -30,6 +30,14 @@ class Test_Files_Sharing_S2S_OCS_API extends TestCase {
const TEST_FOLDER_NAME = '/folder_share_api_test';
/**
* @var \OCP\IDBConnection
*/
private $connection;
/**
* @var \OCA\Files_Sharing\API\Server2Server
*/
private $s2s;
protected function setUp() {
@ -49,6 +57,8 @@ class Test_Files_Sharing_S2S_OCS_API extends TestCase {
$this->registerHttpHelper($httpHelperMock);
$this->s2s = new \OCA\Files_Sharing\API\Server2Server();
$this->connection = \OC::$server->getDatabaseConnection();
}
protected function tearDown() {
@ -132,4 +142,70 @@ class Test_Files_Sharing_S2S_OCS_API extends TestCase {
$data = $result->fetchAll();
$this->assertEmpty($data);
}
/**
* @dataProvider dataTestDeleteUser
*/
function testDeleteUser($toDelete, $expected, $remainingUsers) {
$this->createDummyS2SShares();
$manager = new OCA\Files_Sharing\External\Manager(
\OC::$server->getDatabaseConnection(),
\OC\Files\Filesystem::getMountManager(),
\OC\Files\Filesystem::getLoader(),
\OC::$server->getUserSession(),
\OC::$server->getHTTPHelper());
$manager->removeUserShares($toDelete);
$query = $this->connection->prepare('SELECT `user` FROM `*PREFIX*share_external`');
$query->execute();
$result = $query->fetchAll();
foreach ($result as $r) {
$remainingShares[$r['user']] = isset($remainingShares[$r['user']]) ? $remainingShares[$r['user']] + 1 : 1;
}
$this->assertSame($remainingUsers, count($remainingShares));
foreach ($expected as $key => $value) {
if ($key === $toDelete) {
$this->assertArrayNotHasKey($key, $remainingShares);
} else {
$this->assertSame($value, $remainingShares[$key]);
}
}
}
function dataTestDeleteUser() {
return array(
array('user1', array('user1' => 0, 'user2' => 3, 'user3' => 3), 2),
array('user2', array('user1' => 4, 'user2' => 0, 'user3' => 3), 2),
array('user3', array('user1' => 4, 'user2' => 3, 'user3' => 0), 2),
array('user4', array('user1' => 4, 'user2' => 3, 'user3' => 3), 3),
);
}
private function createDummyS2SShares() {
$query = $this->connection->prepare('
INSERT INTO `*PREFIX*share_external`
(`remote`, `share_token`, `password`, `name`, `owner`, `user`, `mountpoint`, `mountpoint_hash`, `remote_id`, `accepted`)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
');
$users = array('user1', 'user2', 'user3');
for ($i = 0; $i < 10; $i++) {
$user = $users[$i%3];
$query->execute(array('remote', 'token', 'password', 'name', 'owner', $user, 'mount point', $i, $i, 0));
}
$query = $this->connection->prepare('SELECT `id` FROM `*PREFIX*share_external`');
$query->execute();
$dummyEntries = $query->fetchAll();
$this->assertSame(10, count($dummyEntries));
}
}