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
*
--- /dev/null
+<?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']);
+ }
+
+}
const TEST_FOLDER_NAME = '/folder_share_api_test';
+ /**
+ * @var \OCP\IDBConnection
+ */
+ private $connection;
+
+ /**
+ * @var \OCA\Files_Sharing\API\Server2Server
+ */
private $s2s;
protected function setUp() {
$this->registerHttpHelper($httpHelperMock);
$this->s2s = new \OCA\Files_Sharing\API\Server2Server();
+
+ $this->connection = \OC::$server->getDatabaseConnection();
}
protected function tearDown() {
$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));
+ }
+
}