]> source.dussan.org Git - nextcloud-server.git/commitdiff
delete all server-to-server shares if a user gets deleted
authorBjoern Schiessle <schiessle@owncloud.com>
Tue, 20 Jan 2015 23:11:15 +0000 (00:11 +0100)
committerBjoern Schiessle <schiessle@owncloud.com>
Wed, 21 Jan 2015 16:11:34 +0000 (17:11 +0100)
apps/files_sharing/lib/external/manager.php
apps/files_sharing/lib/helper.php
apps/files_sharing/lib/hooks.php [new file with mode: 0644]
apps/files_sharing/tests/server2server.php

index 665e47c0fe998f17fa007866eab978b1a54b82b8..57dc38a99544f5bf7a6b55bdc1a1c29a43c558af 100644 (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
         *
index 001d0387fa4de843381adf7093381bfb4164dd79..8fabd8d42d70cd7696e6865253d10c4fc6080c7a 100644 (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');
        }
 
        /**
diff --git a/apps/files_sharing/lib/hooks.php b/apps/files_sharing/lib/hooks.php
new file mode 100644 (file)
index 0000000..6f23c10
--- /dev/null
@@ -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']);
+       }
+
+}
index 0400d357b82195454b029a77324f412e7f12476b..2d59f7be9cd3511e905752361bb3c991542b86db 100644 (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));
+       }
+
 }