Skip to content

Commit b95563e

Browse files
committed
feat(files_sharing): Toggle display for trusted server shares
Signed-off-by: nfebe <fenn25.fn@gmail.com>
1 parent 3fa81d0 commit b95563e

File tree

8 files changed

+197
-2
lines changed

8 files changed

+197
-2
lines changed

apps/files_sharing/lib/Config/ConfigLexicon.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
*/
2323
class ConfigLexicon implements IConfigLexicon {
2424
public const SHOW_FEDERATED_AS_INTERNAL = 'show_federated_shares_as_internal';
25+
public const SHOW_FEDERATED_TO_TRUSTED_AS_INTERNAL = 'show_federated_shares_to_trusted_servers_as_internal';
2526

2627
public function getStrictness(): ConfigLexiconStrictness {
2728
return ConfigLexiconStrictness::IGNORE;
@@ -30,6 +31,7 @@ public function getStrictness(): ConfigLexiconStrictness {
3031
public function getAppConfigs(): array {
3132
return [
3233
new ConfigLexiconEntry(self::SHOW_FEDERATED_AS_INTERNAL, ValueType::BOOL, false, 'shows federated shares as internal shares', true),
34+
new ConfigLexiconEntry(self::SHOW_FEDERATED_TO_TRUSTED_AS_INTERNAL, ValueType::BOOL, false, 'shows federated shares to trusted servers as internal shares', true),
3335
];
3436
}
3537

apps/files_sharing/lib/Controller/ShareAPIController.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@
6060
use OCP\Share\IShare;
6161
use OCP\Share\IShareProviderWithNotification;
6262
use OCP\UserStatus\IManager as IUserStatusManager;
63+
use OCA\Federation\TrustedServers;
64+
use OCP\User\IUserSession;
6365
use Psr\Container\ContainerExceptionInterface;
6466
use Psr\Container\ContainerInterface;
6567
use Psr\Log\LoggerInterface;
@@ -95,6 +97,7 @@ public function __construct(
9597
private IProviderFactory $factory,
9698
private IMailer $mailer,
9799
private ITagManager $tagManager,
100+
private ?TrustedServers $trustedServers,
98101
private ?string $userId = null,
99102
) {
100103
parent::__construct($appName, $request);
@@ -197,6 +200,22 @@ protected function formatShare(IShare $share, ?Node $recipientNode = null): arra
197200
$result['item_size'] = $node->getSize();
198201
$result['item_mtime'] = $node->getMTime();
199202

203+
if ($this->trustedServers !== null && in_array($share->getShareType(), [IShare::TYPE_REMOTE, IShare::TYPE_REMOTE_GROUP])) {
204+
$result['is_trusted_server'] = false;
205+
$sharedWith = $share->getSharedWith();
206+
$remoteIdentifier = is_string($sharedWith) ? strrchr($sharedWith, '@') : false;
207+
if ($remoteIdentifier !== false) {
208+
$remote = substr($remoteIdentifier, 1);
209+
try {
210+
if ($this->trustedServers->isTrustedServer($remote)) {
211+
$result['is_trusted_server'] = true;
212+
}
213+
} catch (\Exception $e) {
214+
// Server not found or other issue, we consider it not trusted
215+
}
216+
}
217+
}
218+
200219
$expiration = $share->getExpirationDate();
201220
if ($expiration !== null) {
202221
$expiration->setTimezone($this->dateTimeZone->getTimeZone());

apps/files_sharing/lib/Listener/LoadSidebarListener.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public function handle(Event $event): void {
3838

3939
$appConfig = Server::get(IAppConfig::class);
4040
$this->initialState->provideInitialState('showFederatedSharesAsInternal', $appConfig->getValueBool('files_sharing', ConfigLexicon::SHOW_FEDERATED_AS_INTERNAL));
41+
$this->initialState->provideInitialState('showFederatedSharesToTrustedServersAsInternal', $appConfig->getValueBool('files_sharing', ConfigLexicon::SHOW_FEDERATED_TO_TRUSTED_AS_INTERNAL));
4142
Util::addScript(Application::APP_ID, 'files_sharing_tab', 'files');
4243
}
4344
}

apps/files_sharing/src/models/Share.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,4 +486,11 @@ export default class Share {
486486
return this._share.status
487487
}
488488

489+
/**
490+
* Is the share from a trusted server
491+
*/
492+
get isTrustedServer(): boolean {
493+
return !!this._share.is_trusted_server
494+
}
495+
489496
}

apps/files_sharing/src/services/ConfigService.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,4 +315,12 @@ export default class Config {
315315
return loadState('files_sharing', 'showFederatedSharesAsInternal', false)
316316
}
317317

318+
/**
319+
* Show federated shares to trusted servers as internal shares
320+
* @return {boolean}
321+
*/
322+
get showFederatedSharesToTrustedServersAsInternal(): boolean {
323+
return loadState('files_sharing', 'showFederatedSharesToTrustedServersAsInternal', false)
324+
}
325+
318326
}

apps/files_sharing/src/views/SharingTab.vue

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,13 @@ export default {
402402
if ([ShareType.Link, ShareType.Email].includes(share.type)) {
403403
this.linkShares.push(share)
404404
} else if ([ShareType.Remote, ShareType.RemoteGroup].includes(share.type)) {
405-
if (this.config.showFederatedSharesAsInternal) {
405+
if (this.config.showFederatedSharesToTrustedServersAsInternal) {
406+
if (share.isTrustedServer) {
407+
this.shares.push(share)
408+
} else {
409+
this.externalShares.push(share)
410+
}
411+
} else if (this.config.showFederatedSharesAsInternal) {
406412
this.shares.push(share)
407413
} else {
408414
this.externalShares.push(share)

apps/files_sharing/tests/ApiTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use OCP\Share\IProviderFactory;
3535
use OCP\Share\IShare;
3636
use OCP\UserStatus\IManager as IUserStatusManager;
37+
use OCA\Federation\TrustedServers;
3738
use Psr\Container\ContainerInterface;
3839
use Psr\Log\LoggerInterface;
3940

@@ -113,6 +114,7 @@ private function createOCS($userId) {
113114
$providerFactory = $this->createMock(IProviderFactory::class);
114115
$mailer = $this->createMock(IMailer::class);
115116
$tagManager = $this->createMock(ITagManager::class);
117+
$trustedServers = $this->createMock(TrustedServers::class);
116118
$dateTimeZone->method('getTimeZone')->willReturn(new \DateTimeZone(date_default_timezone_get()));
117119

118120
return new ShareAPIController(
@@ -134,6 +136,7 @@ private function createOCS($userId) {
134136
$providerFactory,
135137
$mailer,
136138
$tagManager,
139+
$trustedServers,
137140
$userId,
138141
);
139142
}

apps/files_sharing/tests/Controller/ShareAPIControllerTest.php

Lines changed: 150 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
use OCP\Share\IProviderFactory;
4545
use OCP\Share\IShare;
4646
use OCP\UserStatus\IManager as IUserStatusManager;
47+
use OCA\Federation\TrustedServers;
4748
use PHPUnit\Framework\MockObject\MockObject;
4849
use Psr\Container\ContainerInterface;
4950
use Psr\Log\LoggerInterface;
@@ -79,6 +80,7 @@ class ShareAPIControllerTest extends TestCase {
7980
private IProviderFactory&MockObject $factory;
8081
private IMailer&MockObject $mailer;
8182
private ITagManager&MockObject $tagManager;
83+
private TrustedServers&MockObject $trustedServers;
8284

8385
protected function setUp(): void {
8486
$this->shareManager = $this->createMock(IManager::class);
@@ -115,6 +117,7 @@ protected function setUp(): void {
115117
$this->factory = $this->createMock(IProviderFactory::class);
116118
$this->mailer = $this->createMock(IMailer::class);
117119
$this->tagManager = $this->createMock(ITagManager::class);
120+
$this->trustedServers = $this->createMock(TrustedServers::class);
118121

119122
$this->ocs = new ShareAPIController(
120123
$this->appName,
@@ -135,8 +138,10 @@ protected function setUp(): void {
135138
$this->factory,
136139
$this->mailer,
137140
$this->tagManager,
138-
$this->currentUser,
141+
$this->trustedServers,
142+
$this->currentUser
139143
);
144+
140145
}
141146

142147
/**
@@ -163,6 +168,7 @@ private function mockFormatShare() {
163168
$this->factory,
164169
$this->mailer,
165170
$this->tagManager,
171+
$this->trustedServers,
166172
$this->currentUser,
167173
])->onlyMethods(['formatShare'])
168174
->getMock();
@@ -848,6 +854,7 @@ public function testGetShare(IShare $share, array $result): void {
848854
$this->factory,
849855
$this->mailer,
850856
$this->tagManager,
857+
$this->trustedServers,
851858
$this->currentUser,
852859
])
853860
->onlyMethods(['canAccessShare'])
@@ -1481,6 +1488,7 @@ public function testGetShares(array $getSharesParameters, array $shares, array $
14811488
$this->factory,
14821489
$this->mailer,
14831490
$this->tagManager,
1491+
$this->trustedServers,
14841492
$this->currentUser,
14851493
])
14861494
->onlyMethods(['formatShare'])
@@ -1872,6 +1880,7 @@ public function testCreateShareUser(): void {
18721880
$this->factory,
18731881
$this->mailer,
18741882
$this->tagManager,
1883+
$this->trustedServers,
18751884
$this->currentUser,
18761885
])->onlyMethods(['formatShare'])
18771886
->getMock();
@@ -1970,6 +1979,7 @@ public function testCreateShareGroup(): void {
19701979
$this->factory,
19711980
$this->mailer,
19721981
$this->tagManager,
1982+
$this->trustedServers,
19731983
$this->currentUser,
19741984
])->onlyMethods(['formatShare'])
19751985
->getMock();
@@ -2396,6 +2406,7 @@ public function testCreateShareRemote(): void {
23962406
$this->factory,
23972407
$this->mailer,
23982408
$this->tagManager,
2409+
$this->trustedServers,
23992410
$this->currentUser,
24002411
])->onlyMethods(['formatShare'])
24012412
->getMock();
@@ -2467,6 +2478,7 @@ public function testCreateShareRemoteGroup(): void {
24672478
$this->factory,
24682479
$this->mailer,
24692480
$this->tagManager,
2481+
$this->trustedServers,
24702482
$this->currentUser,
24712483
])->onlyMethods(['formatShare'])
24722484
->getMock();
@@ -2705,6 +2717,7 @@ public function testCreateReshareOfFederatedMountNoDeletePermissions(): void {
27052717
$this->factory,
27062718
$this->mailer,
27072719
$this->tagManager,
2720+
$this->trustedServers,
27082721
$this->currentUser,
27092722
])->onlyMethods(['formatShare'])
27102723
->getMock();
@@ -4497,6 +4510,7 @@ public function dataFormatShare() {
44974510
'mount-type' => '',
44984511
'attributes' => null,
44994512
'item_permissions' => 1,
4513+
'is_trusted_server' => false,
45004514
], $share, [], false
45014515
];
45024516

@@ -4550,6 +4564,7 @@ public function dataFormatShare() {
45504564
'mount-type' => '',
45514565
'attributes' => null,
45524566
'item_permissions' => 1,
4567+
'is_trusted_server' => false,
45534568
], $share, [], false
45544569
];
45554570

@@ -5233,4 +5248,138 @@ public function testPopulateTags(): void {
52335248
['file_source' => 42, 'x' => 'y', 'tags' => ['tag1', 'tag2']],
52345249
], $result);
52355250
}
5251+
5252+
public function trustedServerProvider(): array {
5253+
return [
5254+
'Trusted server' => [true, true],
5255+
'Untrusted server' => [false, false],
5256+
];
5257+
}
5258+
5259+
/**
5260+
* @dataProvider trustedServerProvider
5261+
*/
5262+
public function testFormatShareWithFederatedShare(bool $isKnownServer, bool $isTrusted): void {
5263+
$nodeId = 12;
5264+
$nodePath = '/test.txt';
5265+
$share = $this->createShare(
5266+
1,
5267+
IShare::TYPE_REMOTE,
5268+
'recipient@remoteserver.com', // shared with
5269+
'sender@testserver.com', // shared by
5270+
'shareOwner', // share owner
5271+
$nodePath, // path
5272+
Constants::PERMISSION_READ,
5273+
time(),
5274+
null,
5275+
null,
5276+
$nodePath,
5277+
$nodeId
5278+
);
5279+
5280+
$node = $this->createMock(\OCP\Files\File::class);
5281+
$node->method('getId')->willReturn($nodeId);
5282+
$node->method('getPath')->willReturn($nodePath);
5283+
$node->method('getInternalPath')->willReturn(ltrim($nodePath, '/'));
5284+
$mountPoint = $this->createMock(\OCP\Files\Mount\IMountPoint::class);
5285+
$mountPoint->method('getMountType')->willReturn('local');
5286+
$node->method('getMountPoint')->willReturn($mountPoint);
5287+
$node->method('getMimetype')->willReturn('text/plain');
5288+
$storage = $this->createMock(\OCP\Files\Storage\IStorage::class);
5289+
$storageCache = $this->createMock(\OCP\Files\Cache\ICache::class);
5290+
$storageCache->method('getNumericStorageId')->willReturn(1);
5291+
$storage->method('getCache')->willReturn($storageCache);
5292+
$storage->method('getId')->willReturn('home::shareOwner');
5293+
$node->method('getStorage')->willReturn($storage);
5294+
$parent = $this->createMock(\OCP\Files\Folder::class);
5295+
$parent->method('getId')->willReturn(2);
5296+
$node->method('getParent')->willReturn($parent);
5297+
$node->method('getSize')->willReturn(1234);
5298+
$node->method('getMTime')->willReturn(1234567890);
5299+
5300+
$this->previewManager->method('isAvailable')->with($node)->willReturn(false);
5301+
5302+
$this->rootFolder->method('getUserFolder')
5303+
->with($this->currentUser)
5304+
->willReturnSelf();
5305+
5306+
$this->rootFolder->method('getFirstNodeById')
5307+
->with($share->getNodeId())
5308+
->willReturn($node);
5309+
5310+
$this->rootFolder->method('getRelativePath')
5311+
->with($node->getPath())
5312+
->willReturnArgument(0);
5313+
5314+
$serverName = 'remoteserver.com';
5315+
$this->trustedServers->method('isTrustedServer')
5316+
->with($serverName)
5317+
->willReturn($isKnownServer);
5318+
5319+
$result = $this->invokePrivate($this->ocs, 'formatShare', [$share]);
5320+
5321+
$this->assertSame($isTrusted, $result['is_trusted_server']);
5322+
}
5323+
5324+
public function testFormatShareWithFederatedShareWithAtInUsername(): void {
5325+
$nodeId = 12;
5326+
$nodePath = '/test.txt';
5327+
$share = $this->createShare(
5328+
1,
5329+
IShare::TYPE_REMOTE,
5330+
'recipient@domain.com@remoteserver.com', // shared with
5331+
'sender@testserver.com', // shared by
5332+
'shareOwner', // share owner
5333+
$nodePath, // path
5334+
Constants::PERMISSION_READ,
5335+
time(),
5336+
null,
5337+
null,
5338+
$nodePath,
5339+
$nodeId
5340+
);
5341+
5342+
$node = $this->createMock(\OCP\Files\File::class);
5343+
$node->method('getId')->willReturn($nodeId);
5344+
$node->method('getPath')->willReturn($nodePath);
5345+
$node->method('getInternalPath')->willReturn(ltrim($nodePath, '/'));
5346+
$mountPoint = $this->createMock(\OCP\Files\Mount\IMountPoint::class);
5347+
$mountPoint->method('getMountType')->willReturn('local');
5348+
$node->method('getMountPoint')->willReturn($mountPoint);
5349+
$node->method('getMimetype')->willReturn('text/plain');
5350+
$storage = $this->createMock(\OCP\Files\Storage\IStorage::class);
5351+
$storageCache = $this->createMock(\OCP\Files\Cache\ICache::class);
5352+
$storageCache->method('getNumericStorageId')->willReturn(1);
5353+
$storage->method('getCache')->willReturn($storageCache);
5354+
$storage->method('getId')->willReturn('home::shareOwner');
5355+
$node->method('getStorage')->willReturn($storage);
5356+
$parent = $this->createMock(\OCP\Files\Folder::class);
5357+
$parent->method('getId')->willReturn(2);
5358+
$node->method('getParent')->willReturn($parent);
5359+
$node->method('getSize')->willReturn(1234);
5360+
$node->method('getMTime')->willReturn(1234567890);
5361+
5362+
$this->previewManager->method('isAvailable')->with($node)->willReturn(false);
5363+
5364+
$this->rootFolder->method('getUserFolder')
5365+
->with($this->currentUser)
5366+
->willReturnSelf();
5367+
5368+
$this->rootFolder->method('getFirstNodeById')
5369+
->with($share->getNodeId())
5370+
->willReturn($node);
5371+
5372+
$this->rootFolder->method('getRelativePath')
5373+
->with($node->getPath())
5374+
->willReturnArgument(0);
5375+
5376+
$serverName = 'remoteserver.com';
5377+
$this->trustedServers->method('isTrustedServer')
5378+
->with($serverName)
5379+
->willReturn(true);
5380+
5381+
$result = $this->invokePrivate($this->ocs, 'formatShare', [$share]);
5382+
5383+
$this->assertTrue($result['is_trusted_server']);
5384+
}
52365385
}

0 commit comments

Comments
 (0)