Skip to content

Commit ff6f6e6

Browse files
committed
feat(federatedfilesharing): auto-accept shares from trusted servers
Signed-off-by: skjnldsv <skjnldsv@protonmail.com>
1 parent 873d150 commit ff6f6e6

File tree

6 files changed

+47
-4
lines changed

6 files changed

+47
-4
lines changed

apps/federatedfilesharing/lib/Controller/RequestHandlerController.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@
3939
#[OpenAPI(scope: OpenAPI::SCOPE_FEDERATION)]
4040
class RequestHandlerController extends OCSController {
4141

42-
/** @var string */
43-
private $shareTable = 'share';
44-
4542
public function __construct(
4643
string $appName,
4744
IRequest $request,

apps/federatedfilesharing/lib/FederatedShareProvider.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -999,6 +999,11 @@ public function isLookupServerUploadEnabled() {
999999
return ($result === 'yes');
10001000
}
10011001

1002+
public function isFederatedTrustedShareAutoAccept() {
1003+
$result = $this->config->getAppValue('files_sharing', 'federatedTrustedShareAutoAccept', 'yes');
1004+
return ($result === 'yes');
1005+
}
1006+
10021007
/**
10031008
* @inheritdoc
10041009
*/

apps/federatedfilesharing/lib/OCM/CloudFederationProviderFiles.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use OC\Files\Filesystem;
1111
use OCA\FederatedFileSharing\AddressHandler;
1212
use OCA\FederatedFileSharing\FederatedShareProvider;
13+
use OCA\Federation\TrustedServers;
1314
use OCA\Files_Sharing\Activity\Providers\RemoteShares;
1415
use OCA\Files_Sharing\External\Manager;
1516
use OCA\GlobalSiteSelector\Service\SlaveService;
@@ -66,6 +67,7 @@ public function __construct(
6667
private LoggerInterface $logger,
6768
private IFilenameValidator $filenameValidator,
6869
private readonly IProviderFactory $shareProviderFactory,
70+
private TrustedServers $trustedServers,
6971
) {
7072
}
7173

@@ -163,6 +165,11 @@ public function shareReceived(ICloudFederationShare $share) {
163165
->setObject('remote_share', $shareId, $name);
164166
\OC::$server->getActivityManager()->publish($event);
165167
$this->notifyAboutNewShare($shareWith, $shareId, $ownerFederatedId, $sharedByFederatedId, $name, $ownerDisplayName);
168+
169+
// If auto-accept is enabled, accept the share
170+
if ($this->federatedShareProvider->isFederatedTrustedShareAutoAccept()) {
171+
$this->externalShareManager->acceptShare($shareId, $shareWith);
172+
}
166173
} else {
167174
$groupMembers = $this->groupManager->get($shareWith)->getUsers();
168175
foreach ($groupMembers as $user) {
@@ -174,8 +181,14 @@ public function shareReceived(ICloudFederationShare $share) {
174181
->setObject('remote_share', $shareId, $name);
175182
\OC::$server->getActivityManager()->publish($event);
176183
$this->notifyAboutNewShare($user->getUID(), $shareId, $ownerFederatedId, $sharedByFederatedId, $name, $ownerDisplayName);
184+
185+
// If auto-accept is enabled, accept the share
186+
if ($this->federatedShareProvider->isFederatedTrustedShareAutoAccept()) {
187+
$this->externalShareManager->acceptShare($shareId, $user->getUID());
188+
}
177189
}
178190
}
191+
179192
return $shareId;
180193
} catch (\Exception $e) {
181194
$this->logger->error('Server can not add remote share.', [

apps/federatedfilesharing/lib/Settings/Admin.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public function getForm() {
4040
$this->initialState->provideInitialState('incomingServer2serverGroupShareEnabled', $this->fedShareProvider->isIncomingServer2serverGroupShareEnabled());
4141
$this->initialState->provideInitialState('lookupServerEnabled', $this->fedShareProvider->isLookupServerQueriesEnabled());
4242
$this->initialState->provideInitialState('lookupServerUploadEnabled', $this->fedShareProvider->isLookupServerUploadEnabled());
43+
$this->initialState->provideInitialState('federatedTrustedShareAutoAccept', $this->fedShareProvider->isFederatedTrustedShareAutoAccept());
4344

4445
return new TemplateResponse('federatedfilesharing', 'settings-admin', [], '');
4546
}
@@ -76,6 +77,7 @@ public function getAuthorizedAppConfig(): array {
7677
'incomingServer2serverGroupShareEnabled',
7778
'lookupServerEnabled',
7879
'lookupServerUploadEnabled',
80+
'federatedTrustedShareAutoAccept',
7981
],
8082
];
8183
}

apps/federatedfilesharing/src/components/AdminSettings.vue

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@
4343
@update:checked="update('lookupServerUploadEnabled', lookupServerUploadEnabled)">
4444
{{ t('federatedfilesharing', 'Allow people to publish their data to a global and public address book') }}
4545
</NcCheckboxRadioSwitch>
46+
47+
<NcCheckboxRadioSwitch type="switch"
48+
:checked.sync="federatedTrustedShareAutoAccept"
49+
@update:checked="update('federatedTrustedShareAutoAccept', federatedTrustedShareAutoAccept)">
50+
{{ t('federatedfilesharing', 'Automatically accept shares from federated accounts and groups by default') }}
51+
</NcCheckboxRadioSwitch>
4652
</NcSettingsSection>
4753
</template>
4854

@@ -74,6 +80,7 @@ export default {
7480
federatedGroupSharingSupported: loadState('federatedfilesharing', 'federatedGroupSharingSupported'),
7581
lookupServerEnabled: loadState('federatedfilesharing', 'lookupServerEnabled'),
7682
lookupServerUploadEnabled: loadState('federatedfilesharing', 'lookupServerUploadEnabled'),
83+
federatedTrustedShareAutoAccept: loadState('federatedfilesharing', 'federatedTrustedShareAutoAccept'),
7784
internalOnly: loadState('federatedfilesharing', 'internalOnly'),
7885
sharingFederatedDocUrl: loadState('federatedfilesharing', 'sharingFederatedDocUrl'),
7986
}

apps/files_sharing/lib/External/Manager.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,18 @@ private function updateAccepted(int $shareId, bool $accepted) : void {
294294
* @param int $id
295295
* @return bool True if the share could be accepted, false otherwise
296296
*/
297-
public function acceptShare($id) {
297+
public function acceptShare($id, $userId = null) {
298+
// If we're auto-accepting a share, we need to know the user id
299+
// as there is no session available while processing the share
300+
// from the remote server request.
301+
if ($userId !== null) {
302+
$user = $this->userManager->get($userId);
303+
if ($user === null) {
304+
return false;
305+
}
306+
$this->uid = $userId;
307+
}
308+
298309
$share = $this->getShare($id);
299310
$result = false;
300311

@@ -357,6 +368,7 @@ public function acceptShare($id) {
357368
}
358369
}
359370
}
371+
360372
if ($userShareAccepted !== false) {
361373
$this->sendFeedbackToRemote($share['remote'], $share['share_token'], $share['remote_id'], 'accept');
362374
$event = new FederatedShareAddedEvent($share['remote']);
@@ -435,6 +447,13 @@ public function declineShare($id) {
435447
}
436448

437449
public function processNotification(int $remoteShare): void {
450+
$share = $this->fetchShare($remoteShare);
451+
if ($share === false) {
452+
return;
453+
}
454+
455+
// Extract the recipient user id from the share
456+
$userId = $share['user'];
438457
$filter = $this->notificationManager->createNotification();
439458
$filter->setApp('files_sharing')
440459
->setUser($this->uid)

0 commit comments

Comments
 (0)