Skip to content
Draft
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
use OCP\Security\Signature\IIncomingSignedRequest;
use OCP\Server;
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Util;
use Psr\Log\LoggerInterface;

/**
Expand Down Expand Up @@ -409,21 +408,12 @@ private function protocolCarriesSharedSecret(array $protocol): bool {
}

/**
* map login name to internal LDAP UID if a LDAP backend is in use
*
* @param string $uid
* @return string mixed
* Map login name to internal LDAP UID if an LDAP backend is in use
*/
private function mapUid($uid) {
// FIXME this should be a method in the user management instead
private function mapUid(string $uid): string {
$this->logger->debug('shareWith before, ' . $uid, ['app' => $this->appName]);
Util::emitHook(
'\OCA\Files_Sharing\API\Server2Server',
'preLoginNameUsedAsUserName',
['uid' => &$uid]
);
$uid = $this->userManager->getUserNameFromLoginName($uid);
$this->logger->debug('shareWith after, ' . $uid, ['app' => $this->appName]);

return $uid;
}

Expand Down
4 changes: 2 additions & 2 deletions apps/dashboard/composer/composer/installed.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
'name' => '__root__',
'pretty_version' => 'dev-master',
'version' => 'dev-master',
'reference' => 'b1797842784b250fb01ed5e3bf130705eb94751b',
'reference' => '707699d6351faa181d14cf50abe6be4343938400',
'type' => 'library',
'install_path' => __DIR__ . '/../',
'aliases' => array(),
Expand All @@ -13,7 +13,7 @@
'__root__' => array(
'pretty_version' => 'dev-master',
'version' => 'dev-master',
'reference' => 'b1797842784b250fb01ed5e3bf130705eb94751b',
'reference' => '707699d6351faa181d14cf50abe6be4343938400',
'type' => 'library',
'install_path' => __DIR__ . '/../',
'aliases' => array(),
Expand Down
72 changes: 21 additions & 51 deletions apps/federatedfilesharing/lib/AddressHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use OCP\HintException;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\Util;
use OCP\IUserManager;

/**
* Class AddressHandler - parse, modify and construct federated sharing addresses
Expand All @@ -23,26 +23,23 @@ class AddressHandler {

/**
* AddressHandler constructor.
*
* @param IURLGenerator $urlGenerator
* @param IL10N $l
* @param ICloudIdManager $cloudIdManager
*/
public function __construct(
private IURLGenerator $urlGenerator,
private IL10N $l,
private ICloudIdManager $cloudIdManager,
private readonly IURLGenerator $urlGenerator,
private readonly IL10N $l,
private readonly ICloudIdManager $cloudIdManager,
private readonly IUserManager $userManager,
) {
}

/**
* split user and remote from federated cloud id
* Split user and remote from federated cloud id.
*
* @param string $address federated share address
* @return array<string> [user, remoteURL]
* @throws HintException
*/
public function splitUserRemote($address) {
public function splitUserRemote(string $address): array {
try {
$cloudId = $this->cloudIdManager->resolveCloudId($address);
return [$cloudId->getUser(), $cloudId->getRemote()];
Expand All @@ -53,55 +50,36 @@ public function splitUserRemote($address) {
}

/**
* generate remote URL part of federated ID
* Generate remote URL part of federated ID
*
* @return string url of the current server
*/
public function generateRemoteURL() {
public function generateRemoteURL(): string {
return $this->urlGenerator->getAbsoluteURL('/');
}

/**
* check if two federated cloud IDs refer to the same user
* Check if two federated cloud IDs refer to the same user
*
* @param string $user1
* @param string $server1
* @param string $user2
* @param string $server2
* @return bool true if both users and servers are the same
*/
public function compareAddresses($user1, $server1, $user2, $server2) {
public function compareAddresses(string $user1, string $server1, string $user2, string $server2): bool {
$normalizedServer1 = strtolower($this->removeProtocolFromUrl($server1));
$normalizedServer2 = strtolower($this->removeProtocolFromUrl($server2));

if (rtrim($normalizedServer1, '/') === rtrim($normalizedServer2, '/')) {
// FIXME this should be a method in the user management instead
Util::emitHook(
'\OCA\Files_Sharing\API\Server2Server',
'preLoginNameUsedAsUserName',
['uid' => &$user1]
);
Util::emitHook(
'\OCA\Files_Sharing\API\Server2Server',
'preLoginNameUsedAsUserName',
['uid' => &$user2]
);

if ($user1 === $user2) {
return true;
}
if (rtrim($normalizedServer1, '/') !== rtrim($normalizedServer2, '/')) {
return false;
}

return false;
$user1 = $this->userManager->getUserNameFromLoginName($user1);
$user2 = $this->userManager->getUserNameFromLoginName($user2);
return $user1 === $user2;
}

/**
* remove protocol from URL
*
* @param string $url
* @return string
* Remove protocol from URL
*/
public function removeProtocolFromUrl($url) {
public function removeProtocolFromUrl(string $url): string {
if (str_starts_with($url, 'https://')) {
return substr($url, strlen('https://'));
} elseif (str_starts_with($url, 'http://')) {
Expand All @@ -112,17 +90,9 @@ public function removeProtocolFromUrl($url) {
}

/**
* check if the url contain the protocol (http or https)
*
* @param string $url
* @return bool
* Check if the url contain the protocol (http or https).
*/
public function urlContainProtocol($url) {
if (str_starts_with($url, 'https://')
|| str_starts_with($url, 'http://')) {
return true;
}

return false;
public function urlContainProtocol(string $url): bool {
return str_starts_with($url, 'https://') || str_starts_with($url, 'http://');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
use OCP\Share\IManager;
use OCP\Share\IProviderFactory;
use OCP\Share\IShare;
use OCP\Util;
use Override;
use Psr\Log\LoggerInterface;
use SensitiveParameter;
Expand Down Expand Up @@ -167,11 +166,7 @@ public function shareReceived(ICloudFederationShare $share): string {

if ($shareType === IShare::TYPE_USER) {
$this->logger->debug('shareWith before, ' . $shareWith, ['app' => 'files_sharing']);
Util::emitHook(
'\OCA\Files_Sharing\API\Server2Server',
'preLoginNameUsedAsUserName',
['uid' => &$shareWith]
);
$shareWith = $this->userManager->getUserNameFromLoginName($shareWith);
$this->logger->debug('shareWith after, ' . $shareWith, ['app' => 'files_sharing']);

$user = $this->userManager->get($shareWith);
Expand Down
6 changes: 4 additions & 2 deletions apps/federatedfilesharing/tests/AddressHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,18 @@ protected function setUp(): void {
$this->urlGenerator = $this->createMock(IURLGenerator::class);
$this->il10n = $this->createMock(IL10N::class);
$this->contactsManager = $this->createMock(IManager::class);
$userManager = $this->createMock(IUserManager::class);
$userManager->method('getUserNameFromLoginName')->willReturnArgument(0);

$this->cloudIdManager = new CloudIdManager(
$this->createMock(ICacheFactory::class),
$this->createMock(IEventDispatcher::class),
$this->contactsManager,
$this->urlGenerator,
$this->createMock(IUserManager::class),
$userManager,
);

$this->addressHandler = new AddressHandler($this->urlGenerator, $this->il10n, $this->cloudIdManager);
$this->addressHandler = new AddressHandler($this->urlGenerator, $this->il10n, $this->cloudIdManager, $userManager);
}

public static function dataTestSplitUserRemote(): array {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ public function testShareReceivedAcceptsMultiProtocolEnvelope(): void {
$this->discoveryService->method('discover')
->willThrowException(new \Exception('network error'));

$this->userManager->method('getUserNameFromLoginName')->with('localuser')->willReturn('localuser');
$this->userManager->method('get')->with('localuser')->willReturn(null);
$this->filenameValidator->method('isFilenameValid')->willReturn(true);

Expand Down Expand Up @@ -268,6 +269,7 @@ public function testShareReceivedMustExchangeTokenStoresAccessToken(): void {

// Exchange succeeds → share creation continues; we stop it at the user
// lookup stage to avoid a full integration setup.
$this->userManager->method('getUserNameFromLoginName')->with('localuser')->willReturn('localuser');
$this->userManager->method('get')->with('localuser')->willReturn(null);
$this->filenameValidator->method('isFilenameValid')->willReturn(true);

Expand Down Expand Up @@ -296,6 +298,7 @@ public function testShareReceivedOptionalExchangeGracefulOnDiscoveryFailure(): v

// Discovery failure is caught and logged; share creation continues.
// We stop it at the user lookup stage.
$this->userManager->method('getUserNameFromLoginName')->with('localuser')->willReturn('localuser');
$this->userManager->method('get')->with('localuser')->willReturn(null);
$this->filenameValidator->method('isFilenameValid')->willReturn(true);

Expand Down Expand Up @@ -348,6 +351,7 @@ public function testShareReceivedOptionalExchangeStoresAccessTokenOnSuccess(): v
$httpClient->method('post')->willReturn($response);
$this->clientService->method('newClient')->willReturn($httpClient);

$this->userManager->method('getUserNameFromLoginName')->with('localuser')->willReturn('localuser');
$this->userManager->method('get')->with('localuser')->willReturn(null);
$this->filenameValidator->method('isFilenameValid')->willReturn(true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
'OCA\\Files_External\\Controller\\StoragesController' => $baseDir . '/../lib/Controller/StoragesController.php',
'OCA\\Files_External\\Controller\\UserGlobalStoragesController' => $baseDir . '/../lib/Controller/UserGlobalStoragesController.php',
'OCA\\Files_External\\Controller\\UserStoragesController' => $baseDir . '/../lib/Controller/UserStoragesController.php',
'OCA\\Files_External\\Event\\LoadAdditionalBackendEvent' => $baseDir . '/../lib/Event/LoadAdditionalBackendEvent.php',
'OCA\\Files_External\\Event\\StorageCreatedEvent' => $baseDir . '/../lib/Event/StorageCreatedEvent.php',
'OCA\\Files_External\\Event\\StorageDeletedEvent' => $baseDir . '/../lib/Event/StorageDeletedEvent.php',
'OCA\\Files_External\\Event\\StorageUpdatedEvent' => $baseDir . '/../lib/Event/StorageUpdatedEvent.php',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class ComposerStaticInitFiles_External
'OCA\\Files_External\\Controller\\StoragesController' => __DIR__ . '/..' . '/../lib/Controller/StoragesController.php',
'OCA\\Files_External\\Controller\\UserGlobalStoragesController' => __DIR__ . '/..' . '/../lib/Controller/UserGlobalStoragesController.php',
'OCA\\Files_External\\Controller\\UserStoragesController' => __DIR__ . '/..' . '/../lib/Controller/UserStoragesController.php',
'OCA\\Files_External\\Event\\LoadAdditionalBackendEvent' => __DIR__ . '/..' . '/../lib/Event/LoadAdditionalBackendEvent.php',
'OCA\\Files_External\\Event\\StorageCreatedEvent' => __DIR__ . '/..' . '/../lib/Event/StorageCreatedEvent.php',
'OCA\\Files_External\\Event\\StorageDeletedEvent' => __DIR__ . '/..' . '/../lib/Event/StorageDeletedEvent.php',
'OCA\\Files_External\\Event\\StorageUpdatedEvent' => __DIR__ . '/..' . '/../lib/Event/StorageUpdatedEvent.php',
Expand Down
17 changes: 17 additions & 0 deletions apps/files_external/lib/Event/LoadAdditionalBackendEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH
* SPDX-FileContributor: Carl Schwan
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Files_External\Event;

use OCP\EventDispatcher\Event;

class LoadAdditionalBackendEvent extends Event {

}
Loading
Loading