Skip to content

Commit ccc0a5e

Browse files
authored
Merge pull request #16828 from nextcloud/feature/noid/accept-incoming-shares
🔗☑️ Accept all incoming shares
2 parents 5320f08 + e163213 commit ccc0a5e

File tree

19 files changed

+663
-38
lines changed

19 files changed

+663
-38
lines changed

apps/federatedfilesharing/lib/Notifier.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function getName(): string {
8686
* @throws \InvalidArgumentException
8787
*/
8888
public function prepare(INotification $notification, string $languageCode): INotification {
89-
if ($notification->getApp() !== 'files_sharing') {
89+
if ($notification->getApp() !== 'files_sharing' || $notification->getObjectType() !== 'remote_share') {
9090
// Not my app => throw
9191
throw new \InvalidArgumentException();
9292
}

apps/files_sharing/appinfo/app.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
$application = \OC::$server->query(Application::class);
4040
$application->registerMountProviders();
41+
$application->register();
4142

4243
$eventDispatcher = \OC::$server->getEventDispatcher();
4344
$eventDispatcher->addListener(

apps/files_sharing/appinfo/info.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
Turning the feature off removes shared files and folders on the server for all share recipients, and also on the sync clients and mobile apps. More information is available in the Nextcloud Documentation.
1010

1111
</description>
12-
<version>1.10.0</version>
12+
<version>1.10.1</version>
1313
<licence>agpl</licence>
1414
<author>Michael Gapczynski</author>
1515
<author>Bjoern Schiessle</author>
@@ -36,6 +36,7 @@ Turning the feature off removes shared files and folders on the server for all s
3636
<post-migration>
3737
<step>OCA\Files_Sharing\Migration\OwncloudGuestShareType</step>
3838
<step>OCA\Files_Sharing\Migration\SetPasswordColumn</step>
39+
<step>OCA\Files_Sharing\Migration\SetAcceptedStatus</step>
3940
</post-migration>
4041
</repair-steps>
4142

apps/files_sharing/appinfo/routes.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@
7373
'url' => '/api/v1/shares/{id}',
7474
'verb' => 'DELETE',
7575
],
76+
[
77+
'name' => 'ShareAPI#acceptShare',
78+
'url' => '/api/v1/shares/pending/{id}',
79+
'verb' => 'POST',
80+
],
7681
/*
7782
* Deleted Shares
7883
*/

apps/files_sharing/composer/composer/autoload_classmap.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@
4949
'OCA\\Files_Sharing\\Middleware\\ShareInfoMiddleware' => $baseDir . '/../lib/Middleware/ShareInfoMiddleware.php',
5050
'OCA\\Files_Sharing\\Middleware\\SharingCheckMiddleware' => $baseDir . '/../lib/Middleware/SharingCheckMiddleware.php',
5151
'OCA\\Files_Sharing\\Migration\\OwncloudGuestShareType' => $baseDir . '/../lib/Migration/OwncloudGuestShareType.php',
52+
'OCA\\Files_Sharing\\Migration\\SetAcceptedStatus' => $baseDir . '/../lib/Migration/SetAcceptedStatus.php',
5253
'OCA\\Files_Sharing\\Migration\\SetPasswordColumn' => $baseDir . '/../lib/Migration/SetPasswordColumn.php',
5354
'OCA\\Files_Sharing\\MountProvider' => $baseDir . '/../lib/MountProvider.php',
55+
'OCA\\Files_Sharing\\Notification\\Listener' => $baseDir . '/../lib/Notification/Listener.php',
5456
'OCA\\Files_Sharing\\Notification\\Notifier' => $baseDir . '/../lib/Notification/Notifier.php',
5557
'OCA\\Files_Sharing\\Scanner' => $baseDir . '/../lib/Scanner.php',
5658
'OCA\\Files_Sharing\\ShareBackend\\File' => $baseDir . '/../lib/ShareBackend/File.php',

apps/files_sharing/composer/composer/autoload_static.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,10 @@ class ComposerStaticInitFiles_Sharing
6464
'OCA\\Files_Sharing\\Middleware\\ShareInfoMiddleware' => __DIR__ . '/..' . '/../lib/Middleware/ShareInfoMiddleware.php',
6565
'OCA\\Files_Sharing\\Middleware\\SharingCheckMiddleware' => __DIR__ . '/..' . '/../lib/Middleware/SharingCheckMiddleware.php',
6666
'OCA\\Files_Sharing\\Migration\\OwncloudGuestShareType' => __DIR__ . '/..' . '/../lib/Migration/OwncloudGuestShareType.php',
67+
'OCA\\Files_Sharing\\Migration\\SetAcceptedStatus' => __DIR__ . '/..' . '/../lib/Migration/SetAcceptedStatus.php',
6768
'OCA\\Files_Sharing\\Migration\\SetPasswordColumn' => __DIR__ . '/..' . '/../lib/Migration/SetPasswordColumn.php',
6869
'OCA\\Files_Sharing\\MountProvider' => __DIR__ . '/..' . '/../lib/MountProvider.php',
70+
'OCA\\Files_Sharing\\Notification\\Listener' => __DIR__ . '/..' . '/../lib/Notification/Listener.php',
6971
'OCA\\Files_Sharing\\Notification\\Notifier' => __DIR__ . '/..' . '/../lib/Notification/Notifier.php',
7072
'OCA\\Files_Sharing\\Scanner' => __DIR__ . '/..' . '/../lib/Scanner.php',
7173
'OCA\\Files_Sharing\\ShareBackend\\File' => __DIR__ . '/..' . '/../lib/ShareBackend/File.php',

apps/files_sharing/lib/AppInfo/Application.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use OCA\Files_Sharing\Middleware\OCSShareAPIMiddleware;
3333
use OCA\Files_Sharing\Middleware\ShareInfoMiddleware;
3434
use OCA\Files_Sharing\MountProvider;
35+
use OCA\Files_Sharing\Notification\Listener;
3536
use OCA\Files_Sharing\Notification\Notifier;
3637
use OCP\AppFramework\App;
3738
use OC\AppFramework\Utility\SimpleContainer;
@@ -42,9 +43,11 @@
4243
use OCP\Defaults;
4344
use OCP\Federation\ICloudIdManager;
4445
use \OCP\IContainer;
46+
use OCP\IGroup;
4547
use OCP\IServerContainer;
4648
use OCA\Files_Sharing\Capabilities;
4749
use OCA\Files_Sharing\External\Manager;
50+
use Symfony\Component\EventDispatcher\GenericEvent;
4851

4952
class Application extends App {
5053
public function __construct(array $urlParams = array()) {
@@ -178,4 +181,18 @@ public function registerMountProviders() {
178181
$mountProviderCollection->registerProvider($this->getContainer()->query('MountProvider'));
179182
$mountProviderCollection->registerProvider($this->getContainer()->query('ExternalMountProvider'));
180183
}
184+
185+
public function register(): void {
186+
$dispatcher = $this->getContainer()->getServer()->getEventDispatcher();
187+
$dispatcher->addListener('OCP\Share::postShare', function(GenericEvent $event) {
188+
/** @var Listener $listener */
189+
$listener = $this->getContainer()->query(Listener::class);
190+
$listener->shareNotification($event);
191+
});
192+
$dispatcher->addListener(IGroup::class . '::postAddUser', function(GenericEvent $event) {
193+
/** @var Listener $listener */
194+
$listener = $this->getContainer()->query(Listener::class);
195+
$listener->userAddedToGroup($event);
196+
});
197+
}
181198
}

apps/files_sharing/lib/Controller/ShareAPIController.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,38 @@ public function updateShare(
946946
return new DataResponse($this->formatShare($share));
947947
}
948948

949+
/**
950+
* @NoAdminRequired
951+
*
952+
* @param string $id
953+
* @return DataResponse
954+
* @throws OCSNotFoundException
955+
* @throws OCSException
956+
* @throws OCSBadRequestException
957+
*/
958+
public function acceptShare(string $id): DataResponse {
959+
try {
960+
$share = $this->getShareById($id);
961+
} catch (ShareNotFound $e) {
962+
throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist'));
963+
}
964+
965+
if (!$this->canAccessShare($share)) {
966+
throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist'));
967+
}
968+
969+
try {
970+
$this->shareManager->acceptShare($share, $this->currentUser);
971+
} catch (GenericShareException $e) {
972+
$code = $e->getCode() === 0 ? 403 : $e->getCode();
973+
throw new OCSException($e->getHint(), $code);
974+
} catch (\Exception $e) {
975+
throw new OCSBadRequestException($e->getMessage(), $e);
976+
}
977+
978+
return new DataResponse();
979+
}
980+
949981
/**
950982
* Does the user have read permission on the share
951983
*
@@ -1078,8 +1110,8 @@ protected function canDeleteShare(\OCP\Share\IShare $share): bool {
10781110
* @suppress PhanUndeclaredClassMethod
10791111
*/
10801112
protected function canDeleteShareFromSelf(\OCP\Share\IShare $share): bool {
1081-
if ($share->getShareType() !== Share::SHARE_TYPE_GROUP &&
1082-
$share->getShareType() !== Share::SHARE_TYPE_ROOM
1113+
if ($share->getShareType() !== IShare::TYPE_GROUP &&
1114+
$share->getShareType() !== IShare::TYPE_ROOM
10831115
) {
10841116
return false;
10851117
}

apps/files_sharing/lib/External/Manager.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
use OCP\Notification\IManager;
4545
use OCP\OCS\IDiscoveryService;
4646
use OCP\Share;
47+
use OCP\Share\IShare;
4748

4849
class Manager {
4950
const STORAGE = '\OCA\Files_Sharing\External\Storage';
@@ -151,10 +152,10 @@ public function __construct(IDBConnection $connection,
151152
public function addShare($remote, $token, $password, $name, $owner, $shareType, $accepted=false, $user = null, $remoteId = -1, $parent = -1) {
152153

153154
$user = $user ? $user : $this->uid;
154-
$accepted = $accepted ? 1 : 0;
155+
$accepted = $accepted ? IShare::STATUS_ACCEPTED : IShare::STATUS_PENDING;
155156
$name = Filesystem::normalizePath('/' . $name);
156157

157-
if (!$accepted) {
158+
if ($accepted !== IShare::STATUS_ACCEPTED) {
158159
// To avoid conflicts with the mount point generation later,
159160
// we only use a temporary mount point name here. The real
160161
// mount point name will be generated when accepting the share,
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
declare(strict_types=1);
3+
/**
4+
* @copyright Copyright (c) 2019, Joas Schilling <coding@schilljs.com>
5+
*
6+
* @author Joas Schilling <coding@schilljs.com>
7+
*
8+
* @license GNU AGPL version 3 or any later version
9+
*
10+
* This program is free software: you can redistribute it and/or modify
11+
* it under the terms of the GNU Affero General Public License as
12+
* published by the Free Software Foundation, either version 3 of the
13+
* License, or (at your option) any later version.
14+
*
15+
* This program is distributed in the hope that it will be useful,
16+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
* GNU Affero General Public License for more details.
19+
*
20+
* You should have received a copy of the GNU Affero General Public License
21+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
22+
*
23+
*/
24+
25+
namespace OCA\Files_Sharing\Migration;
26+
27+
use OCP\DB\QueryBuilder\IQueryBuilder;
28+
use OCP\IConfig;
29+
use OCP\IDBConnection;
30+
use OCP\Migration\IOutput;
31+
use OCP\Migration\IRepairStep;
32+
use OCP\Share\IShare;
33+
34+
class SetAcceptedStatus implements IRepairStep {
35+
36+
/** @var IDBConnection */
37+
private $connection;
38+
39+
/** @var IConfig */
40+
private $config;
41+
42+
43+
public function __construct(IDBConnection $connection, IConfig $config) {
44+
$this->connection = $connection;
45+
$this->config = $config;
46+
}
47+
48+
/**
49+
* Returns the step's name
50+
*
51+
* @return string
52+
* @since 9.1.0
53+
*/
54+
public function getName(): string {
55+
return 'Set existing shares as accepted';
56+
}
57+
58+
/**
59+
* @param IOutput $output
60+
*/
61+
public function run(IOutput $output): void {
62+
if (!$this->shouldRun()) {
63+
return;
64+
}
65+
66+
$query = $this->connection->getQueryBuilder();
67+
$query
68+
->update('share')
69+
->set('accepted', $query->createNamedParameter(IShare::STATUS_ACCEPTED))
70+
->where($query->expr()->in('share_type', $query->createNamedParameter([IShare::TYPE_USER, IShare::TYPE_GROUP, IShare::TYPE_USERGROUP], IQueryBuilder::PARAM_INT_ARRAY)));
71+
$query->execute();
72+
}
73+
74+
protected function shouldRun() {
75+
$appVersion = $this->config->getAppValue('files_sharing', 'installed_version', '0.0.0');
76+
return version_compare($appVersion, '1.10.1', '<');
77+
}
78+
79+
}

0 commit comments

Comments
 (0)