aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMorris Jobke <hey@morrisjobke.de>2015-01-22 00:37:39 +0100
committerMorris Jobke <hey@morrisjobke.de>2015-01-22 00:37:39 +0100
commit4dac664002f9f66f3f2dff3525dce084badb1cb5 (patch)
tree44959a1b52627c3cfe67d79a126daeb5e5030a99
parent570799ba40b1e296bb08281d5b801df08f86f2df (diff)
parent7216983a0834324fb7d062721062385e805ddf94 (diff)
downloadnextcloud-server-4dac664002f9f66f3f2dff3525dce084badb1cb5.tar.gz
nextcloud-server-4dac664002f9f66f3f2dff3525dce084badb1cb5.zip
Merge pull request #13568 from owncloud/s2s_delete_shares_jenkins
[ignore] delete all server-to-server shares if a user gets deleted
-rw-r--r--apps/files_sharing/lib/external/manager.php27
-rw-r--r--apps/files_sharing/lib/helper.php2
-rw-r--r--apps/files_sharing/lib/hooks.php39
-rw-r--r--apps/files_sharing/tests/server2server.php76
4 files changed, 144 insertions, 0 deletions
diff --git a/apps/files_sharing/lib/external/manager.php b/apps/files_sharing/lib/external/manager.php
index 665e47c0fe9..57dc38a9954 100644
--- a/apps/files_sharing/lib/external/manager.php
+++ b/apps/files_sharing/lib/external/manager.php
@@ -261,6 +261,33 @@ class Manager {
}
/**
+ * 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
*
* @return array list of open server-to-server shares
diff --git a/apps/files_sharing/lib/helper.php b/apps/files_sharing/lib/helper.php
index 001d0387fa4..8fabd8d42d7 100644
--- a/apps/files_sharing/lib/helper.php
+++ b/apps/files_sharing/lib/helper.php
@@ -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');
}
/**
diff --git a/apps/files_sharing/lib/hooks.php b/apps/files_sharing/lib/hooks.php
new file mode 100644
index 00000000000..6f23c103b1d
--- /dev/null
+++ b/apps/files_sharing/lib/hooks.php
@@ -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']);
+ }
+
+}
diff --git a/apps/files_sharing/tests/server2server.php b/apps/files_sharing/tests/server2server.php
index 0400d357b82..2d59f7be9cd 100644
--- a/apps/files_sharing/tests/server2server.php
+++ b/apps/files_sharing/tests/server2server.php
@@ -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));
+ }
+
}