Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions lib/unstable/Sharing/ShareUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace NCU\Sharing;

use NCU\Sharing\Icon\ShareIconURL;
use NCU\Sharing\Recipient\TShareRecipientTypeDisplayNameAddressBook;
use OCP\AppFramework\Attribute\Consumable;
use OCP\IUserManager;
use RuntimeException;
Expand All @@ -20,6 +21,8 @@
*/
#[Consumable(since: '35.0.0')]
final readonly class ShareUser {
use TShareRecipientTypeDisplayNameAddressBook;

/**
* @experimental 35.0.0
*/
Expand All @@ -46,23 +49,27 @@ public function isCurrentUser(ShareAccessContext $accessContext): bool {
* @experimental 35.0.0
*/
public function format(IUserManager $userManager): array {
if ($this->instance !== null) {
// TODO: Support federation
throw new RuntimeException('Currently only local users are supported.');
if ($this->instance === null) {
$displayName = $userManager->getDisplayName($this->userId);
} else {
$displayName = $this->getRecipientDisplayNameFromAddressBook($this->userId . '@' . $this->instance, 'CLOUD');
}

$displayName = $userManager->getDisplayName($this->userId);
if ($displayName === null) {
throw new RuntimeException('No display name for user ' . $this->userId);
$displayName ??= $this->userId;

$id = $this->userId;
if ($this->instance !== null) {
// The instance could contain https:// or http://
$id .= '@' . urlencode($this->instance);
}

return [
'user_id' => $this->userId,
'instance' => $this->instance,
'display_name' => $displayName,
'icon' => (new ShareIconURL(
$userManager->getAvatarUrlLight($this->userId, 64),
$userManager->getAvatarUrlDark($this->userId, 64),
$userManager->getAvatarUrlLight($id, 64),
$userManager->getAvatarUrlDark($id, 64),
))->format(),
];
}
Expand Down
82 changes: 82 additions & 0 deletions tests/lib/Sharing/AbstractSharingManagerTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Test\Sharing;

use NCU\Sharing\ISharingBackend;
use NCU\Sharing\ISharingManager;
use NCU\Sharing\ISharingRegistry;
use NCU\Sharing\Permission\SharePermission;
Expand All @@ -32,6 +33,7 @@
use OCP\IUserManager;
use OCP\L10N\IFactory;
use OCP\Server;
use OCP\Snowflake\ISnowflakeGenerator;
use PHPUnit\Framework\Attributes\DataProvider;
use Test\TestCase;

Expand Down Expand Up @@ -737,6 +739,49 @@ public function testCreateShare(): void {
], $share);
}

public function testCreateShareRemoteOwner(): void {
$accessContext = new ShareAccessContext(overrideChecks: true);

$before = $this->manager->getTime();
$id = Server::get(ISnowflakeGenerator::class)->nextId();
$this->dbConnection->beginTransaction();
Server::get(ISharingBackend::class)->createShare($id, new ShareUser('owner', 'https://example.com'), $this->manager->getTime());
$this->dbConnection->commit();
$share = $this->getShare($accessContext, $id);
$after = $this->manager->getTime();
$this->assertDateBetween($before, $after, $this->parseTime($share['last_updated']));
unset($share['last_updated']);
$this->assertEquals([
'id' => $id,
'owner' => [
'user_id' => 'owner',
'instance' => 'https://example.com',
'display_name' => 'owner',
'icon' => [
'light' => 'http://localhost/index.php/avatar/owner@https%253A%2F%2Fexample.com/64',
'dark' => 'http://localhost/index.php/avatar/owner@https%253A%2F%2Fexample.com/64/dark',
],
],
'state' => ShareState::Draft->value,
'user_status' => null,
'sources' => [],
'recipients' => [],
'properties' => [],
'permissions' => [
[
'class' => ReshareSharePermissionType::class,
'source_class' => null,
'display_name' => 'Share with others',
'hint' => null,
'presets' => [],
'enabled' => false,
'priority' => 90,
],
],
'permission_preset' => null,
], $share);
}

/**
* @return list<array{list<ShareSource>, list<ShareRecipient>, list<ShareProperty>, list<SharePermission>, ?string}>
*/
Expand Down Expand Up @@ -1061,6 +1106,43 @@ public function testAddShareRecipientInteractionRestricted(ShareRecipient $recip
$eventDispatcher->removeListener(RestrictInteractionEvent::class, $listener);
}

public function testAddShareRecipientByRemoteInitiator(): void {
$accessContext = new ShareAccessContext($this->owner);

$before = $this->manager->getTime();
$this->dbConnection->beginTransaction();
$share = $this->manager->createShare($accessContext);
Server::get(ISharingBackend::class)->addShareRecipient($share->id, new ShareRecipient(TestShareRecipientType1::class, 'recipient1', null, $this->manager->generateSecret(), new ShareUser('initiator', 'https://example.com')));
$this->dbConnection->commit();
$after = $this->manager->getTime();
$formatted = $this->getShare($accessContext, $share->id);
$this->assertDateBetween($before, $after, $this->parseTime($formatted['last_updated']));
$this->assertEquals([
[
'class' => TestShareRecipientType1::class,
'value' => 'recipient1',
'instance' => null,
'display_name' => 'Recipient 1',
'icon' => [
'svg' => '<svg/>',
],
'secret' => [
'updatable' => false,
],
'initiator' => [
'user_id' => 'initiator',
'instance' => 'https://example.com',
'display_name' => 'initiator',
'icon' => [
'light' => 'http://localhost/index.php/avatar/initiator@https%253A%2F%2Fexample.com/64',
'dark' => 'http://localhost/index.php/avatar/initiator@https%253A%2F%2Fexample.com/64/dark',
],
],
'permissions' => [],
],
], $formatted['recipients']);
}

#[DataProvider(methodName: 'dataRecipient2')]
public function testAddChildShareRecipientWithoutResharePermission(ShareRecipient $recipient2, string $recipient2DisplayName): void {
$accessContext = new ShareAccessContext($this->owner);
Expand Down
91 changes: 91 additions & 0 deletions tests/lib/Sharing/ShareUserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

/*
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

declare(strict_types=1);

namespace Test\Sharing;

use NCU\Sharing\ISharingRegistry;
use NCU\Sharing\ShareUser;
use OCA\DAV\CardDAV\CardDavBackend;
use OCA\DAV\CardDAV\ContactsManager;
use OCP\Contacts\IManager;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\Server;
use PHPUnit\Framework\Attributes\Group;
use Test\TestCase;
use Test\Traits\UserTrait;

#[Group('DB')]
final class ShareUserTest extends TestCase {
use UserTrait;

private ISharingRegistry $registry;

private IUser $user;

#[\Override]
public function setUp(): void {
parent::setUp();

$this->registry = Server::get(ISharingRegistry::class);
$this->registry->clear();

$this->user = $this->createUser('user', 'password');
$this->userBackend->setDisplayName($this->user->getUID(), 'Local User');
}

#[\Override]
public function tearDown(): void {
$this->registry->clear();

parent::tearDown();
}

public function testFormatLocalUser(): void {
$user = new ShareUser($this->user->getUID(), null);

$this->assertEquals([
'user_id' => 'user',
'instance' => null,
'display_name' => 'Local User',
'icon' => [
'light' => 'http://localhost/index.php/avatar/user/64',
'dark' => 'http://localhost/index.php/avatar/user/64/dark',
],
], $user->format(Server::get(IUserManager::class)));
}

public function testFormatRemoteUser(): void {
$cardDavBackend = Server::get(CardDavBackend::class);
$contactsManager = Server::get(IManager::class);
$addressBookId = $cardDavBackend->createAddressBook('principals/users/' . $this->user->getUID(), 'Personal', []);
Server::get(ContactsManager::class)->setupContactsProvider($contactsManager, $this->user->getUID(), Server::get(IURLGenerator::class));

// Some other tests keeps a stray user in the session, which triggers the address book loader from the dav app. That one then fails, because the user actually doesn't exist anymore.
Server::get(IUserSession::class)->logout();

$contactsManager->createOrUpdate(['CLOUD' => $this->user->getUID() . '@https://example.com', 'FN' => 'Remote User'], (string)$addressBookId);

$user = new ShareUser($this->user->getUID(), 'https://example.com');

$this->assertEquals([
'user_id' => 'user',
'instance' => 'https://example.com',
'display_name' => 'Remote User',
'icon' => [
'light' => 'http://localhost/index.php/avatar/user@https%253A%2F%2Fexample.com/64',
'dark' => 'http://localhost/index.php/avatar/user@https%253A%2F%2Fexample.com/64/dark',
],
], $user->format(Server::get(IUserManager::class)));

$cardDavBackend->deleteAddressBook($addressBookId);
}
}
Loading