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
16 changes: 9 additions & 7 deletions apps/files_sharing/lib/Controller/ShareesAPIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public function __construct(
protected IManager $shareManager,
protected ISearch $collaboratorSearch,
protected FederatedShareProvider $federatedShareProvider,
protected IAppManager $appManager,
) {
parent::__construct($appName, $request);
}
Expand Down Expand Up @@ -161,7 +162,7 @@ public function search(string $search = '', ?string $itemType = null, int $page
}

// FIXME: DI
if (Server::get(IAppManager::class)->isEnabledForUser('circles') && class_exists('\OCA\Circles\ShareByCircleProvider')) {
if ($this->appManager->isEnabledForUser('circles') && class_exists('\OCA\Circles\ShareByCircleProvider')) {
$shareTypes[] = IShare::TYPE_CIRCLE;
}

Expand All @@ -183,25 +184,26 @@ public function search(string $search = '', ?string $itemType = null, int $page
$this->result['lookupEnabled'] = Server::get(GlobalScaleIConfig::class)->isGlobalScaleEnabled();
// TODO: Reconsider using lookup server for non-global-scale federation

[$result, $hasMoreResults] = $this->collaboratorSearch->search($search, $shareTypes, $this->result['lookupEnabled'], $this->limit, $this->offset);
[$result, $hasMoreResults] = $this->collaboratorSearch->filteredSearch($search, $shareTypes, $this->result['lookupEnabled'], $itemType, null, $this->limit, $this->offset);

// extra treatment for 'exact' subarray, with a single merge expected keys might be lost
if (isset($result['exact'])) {
$result['exact'] = array_merge($this->result['exact'], $result['exact']);
}
$this->result = array_merge($this->result, $result);
$response = new DataResponse($this->result);
/** @var Files_SharingShareesSearchResult $result */
$result = array_merge($this->result, $result);

$headers = [];
if ($hasMoreResults) {
$response->setHeaders(['Link' => $this->getPaginationLink($page, [
$headers['Link'] = $this->getPaginationLink($page, [
'search' => $search,
'itemType' => $itemType,
'shareType' => $shareTypes,
'perPage' => $perPage,
])]);
]);
}

return $response;
return new DataResponse($result, Http::STATUS_OK, $headers);
}

/**
Expand Down
44 changes: 18 additions & 26 deletions apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use OCA\FederatedFileSharing\FederatedShareProvider;
use OCA\Files_Sharing\Controller\ShareesAPIController;
use OCA\Files_Sharing\Tests\TestCase;
use OCP\App\IAppManager;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCS\OCSBadRequestException;
use OCP\Collaboration\Collaborators\ISearch;
Expand All @@ -30,26 +31,14 @@
*/
#[\PHPUnit\Framework\Attributes\Group(name: 'DB')]
class ShareesAPIControllerTest extends TestCase {
/** @var ShareesAPIController */
protected $sharees;

/** @var string */
protected $uid;

/** @var IRequest|MockObject */
protected $request;

/** @var IManager|MockObject */
protected ShareesAPIController $sharees;
protected string $uid;
protected IRequest&MockObject $request;
protected $shareManager;

/** @var ISearch|MockObject */
protected $collaboratorSearch;

/** @var IConfig|MockObject */
protected $config;

/** @var FederatedShareProvider|MockObject */
protected $federatedShareProvider;
protected ISearch&MockObject $collaboratorSearch;
protected IConfig&MockObject $config;
protected FederatedShareProvider&MockObject $federatedShareProvider;
protected IAppManager&MockObject $appManager;

protected function setUp(): void {
parent::setUp();
Expand All @@ -59,11 +48,11 @@ protected function setUp(): void {
$this->shareManager = $this->createMock(IManager::class);
$this->config = $this->createMock(IConfig::class);

/** @var IURLGenerator|MockObject $urlGeneratorMock */
$urlGeneratorMock = $this->createMock(IURLGenerator::class);

$this->collaboratorSearch = $this->createMock(ISearch::class);
$this->federatedShareProvider = $this->createMock(FederatedShareProvider::class);
$this->appManager = $this->createMock(IAppManager::class);

$this->sharees = new ShareesAPIController(
'files_sharing',
Expand All @@ -73,7 +62,8 @@ protected function setUp(): void {
$urlGeneratorMock,
$this->shareManager,
$this->collaboratorSearch,
$this->federatedShareProvider
$this->federatedShareProvider,
$this->appManager,
);
}

Expand Down Expand Up @@ -267,7 +257,8 @@ public function testSearch(
$urlGenerator,
$this->shareManager,
$this->collaboratorSearch,
$this->federatedShareProvider
$this->federatedShareProvider,
$this->appManager,
])
->onlyMethods(['isRemoteSharingAllowed', 'isRemoteGroupSharingAllowed'])
->getMock();
Expand All @@ -276,8 +267,8 @@ public function testSearch(
sort($expectedShareTypes);

$this->collaboratorSearch->expects($this->once())
->method('search')
->with($search, $expectedShareTypes, $this->anything(), $perPage, $perPage * ($page - 1))
->method('filteredSearch')
->with($search, $expectedShareTypes, $this->anything(), $itemType, null, $perPage, $perPage * ($page - 1))
->willReturn([[], false]);

$sharees->expects($this->any())
Expand Down Expand Up @@ -364,15 +355,16 @@ public function testSearchInvalid($getData, $message): void {
$urlGenerator,
$this->shareManager,
$this->collaboratorSearch,
$this->federatedShareProvider
$this->federatedShareProvider,
$this->appManager,
])
->onlyMethods(['isRemoteSharingAllowed'])
->getMock();
$sharees->expects($this->never())
->method('isRemoteSharingAllowed');

$this->collaboratorSearch->expects($this->never())
->method('search');
->method('filteredSearch');

try {
$sharees->search('', null, $page, $perPage, null);
Expand Down
14 changes: 13 additions & 1 deletion apps/user_status/lib/Controller/StatusesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCS\OCSNotFoundException;
use OCP\AppFramework\OCSController;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IRequest;
use OCP\User\Events\UserEnumerationFilterEvent;
use OCP\UserStatus\IUserStatus;

/**
Expand All @@ -38,7 +40,8 @@ class StatusesController extends OCSController {
public function __construct(
string $appName,
IRequest $request,
private StatusService $service,
private readonly StatusService $service,
private readonly IEventDispatcher $eventDispatcher,
) {
parent::__construct($appName, $request);
}
Expand All @@ -57,6 +60,15 @@ public function __construct(
public function findAll(?int $limit = null, ?int $offset = null): DataResponse {
$allStatuses = $this->service->findAll($limit, $offset);

$users = array_map(fn (UserStatus $userStatus): string => $userStatus->getUserId(), $allStatuses);
$event = new UserEnumerationFilterEvent($users);
$this->eventDispatcher->dispatchTyped($event);

if ($users !== $event->getUsers()) {
$removedUsers = $event->getFilteredOutUsers();
$allStatuses = array_filter($allStatuses, fn (UserStatus $userStatus): bool => !in_array($userStatus->getUserId(), $removedUsers, true));
}

return new DataResponse(array_values(array_map(function ($userStatus) {
return $this->formatStatus($userStatus);
}, $allStatuses)));
Expand Down
6 changes: 3 additions & 3 deletions apps/user_status/lib/Service/StatusService.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function __construct(
/**
* @param int|null $limit
* @param int|null $offset
* @return UserStatus[]
* @return list<UserStatus>
*/
public function findAll(?int $limit = null, ?int $offset = null): array {
// Return empty array if user enumeration is disabled or limited to groups
Expand All @@ -92,9 +92,9 @@ public function findAll(?int $limit = null, ?int $offset = null): array {
return [];
}

return array_map(function ($status) {
return array_values(array_map(function ($status) {
return $this->processStatus($status);
}, $this->mapper->findAll($limit, $offset));
}, $this->mapper->findAll($limit, $offset)));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,24 @@
use OCA\UserStatus\Service\StatusService;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\OCS\OCSNotFoundException;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IRequest;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;

class StatusesControllerTest extends TestCase {
private StatusService&MockObject $service;
private IEventDispatcher&MockObject $eventDispatcher;
private StatusesController $controller;

protected function setUp(): void {
parent::setUp();

$request = $this->createMock(IRequest::class);
$this->service = $this->createMock(StatusService::class);
$this->eventDispatcher = $this->createMock(IEventDispatcher::class);

$this->controller = new StatusesController('user_status', $request, $this->service);
$this->controller = new StatusesController('user_status', $request, $this->service, $this->eventDispatcher);
}

public function testFindAll(): void {
Expand Down
17 changes: 1 addition & 16 deletions core/Controller/AutoCompleteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCSController;
use OCP\Collaboration\AutoComplete\AutoCompleteFilterEvent;
use OCP\Collaboration\AutoComplete\IManager;
use OCP\Collaboration\Collaborators\ISearch;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IRequest;
use OCP\Share\IShare;

Expand All @@ -31,7 +29,6 @@ public function __construct(
IRequest $request,
private ISearch $collaboratorSearch,
private IManager $autoCompleteManager,
private IEventDispatcher $dispatcher,
) {
parent::__construct($appName, $request);
}
Expand All @@ -55,19 +52,7 @@ public function __construct(
public function get(string $search, ?string $itemType, ?string $itemId, ?string $sorter = null, array $shareTypes = [IShare::TYPE_USER], int $limit = 10): DataResponse {
// if enumeration/user listings are disabled, we'll receive an empty
// result from search() – thus nothing else to do here.
[$results,] = $this->collaboratorSearch->search($search, $shareTypes, false, $limit, 0);

$event = new AutoCompleteFilterEvent(
$results,
$search,
$itemType,
$itemId,
$sorter,
$shareTypes,
$limit,
);
$this->dispatcher->dispatchTyped($event);
$results = $event->getResults();
[$results,] = $this->collaboratorSearch->filteredSearch($search, $shareTypes, false, $itemType, $itemId, $limit, 0);

$exactMatches = $results['exact'];
unset($results['exact']);
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -1145,6 +1145,7 @@
'OCP\\User\\Events\\UserConfigChangedEvent' => $baseDir . '/lib/public/User/Events/UserConfigChangedEvent.php',
'OCP\\User\\Events\\UserCreatedEvent' => $baseDir . '/lib/public/User/Events/UserCreatedEvent.php',
'OCP\\User\\Events\\UserDeletedEvent' => $baseDir . '/lib/public/User/Events/UserDeletedEvent.php',
'OCP\\User\\Events\\UserEnumerationFilterEvent' => $baseDir . '/lib/public/User/Events/UserEnumerationFilterEvent.php',
'OCP\\User\\Events\\UserFirstTimeLoggedInEvent' => $baseDir . '/lib/public/User/Events/UserFirstTimeLoggedInEvent.php',
'OCP\\User\\Events\\UserIdAssignedEvent' => $baseDir . '/lib/public/User/Events/UserIdAssignedEvent.php',
'OCP\\User\\Events\\UserIdUnassignedEvent' => $baseDir . '/lib/public/User/Events/UserIdUnassignedEvent.php',
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -1186,6 +1186,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OCP\\User\\Events\\UserConfigChangedEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/UserConfigChangedEvent.php',
'OCP\\User\\Events\\UserCreatedEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/UserCreatedEvent.php',
'OCP\\User\\Events\\UserDeletedEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/UserDeletedEvent.php',
'OCP\\User\\Events\\UserEnumerationFilterEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/UserEnumerationFilterEvent.php',
'OCP\\User\\Events\\UserFirstTimeLoggedInEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/UserFirstTimeLoggedInEvent.php',
'OCP\\User\\Events\\UserIdAssignedEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/UserIdAssignedEvent.php',
'OCP\\User\\Events\\UserIdUnassignedEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/UserIdUnassignedEvent.php',
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Collaboration/Collaborators/GroupPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function __construct(
}

#[\Override]
public function search($search, $limit, $offset, ISearchResult $searchResult): bool {
public function search(string $search, int $limit, int $offset, ISearchResult $searchResult): bool {
if ($this->groupSharingDisabled) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Collaboration/Collaborators/LookupPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __construct(
}

#[\Override]
public function search($search, $limit, $offset, ISearchResult $searchResult): bool {
public function search(string $search, int $limit, int $offset, ISearchResult $searchResult): bool {
$isGlobalScaleEnabled = $this->globalScaleConfig->isGlobalScaleEnabled();
$isLookupServerEnabled = $this->config->getAppValue('files_sharing', 'lookupServerEnabled', 'no') === 'yes';
$hasInternetConnection = $this->config->getSystemValueBool('has_internet_connection', true);
Expand Down
5 changes: 1 addition & 4 deletions lib/private/Collaboration/Collaborators/MailPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,8 @@ public function __construct(
}
}

/**
* {@inheritdoc}
*/
#[\Override]
public function search($search, $limit, $offset, ISearchResult $searchResult): bool {
public function search(string $search, int $limit, int $offset, ISearchResult $searchResult): bool {
if ($this->shareeEnumerationFullMatch && !$this->shareeEnumerationFullMatchEmail) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct(
}

#[\Override]
public function search($search, $limit, $offset, ISearchResult $searchResult): bool {
public function search(string $search, int $limit, int $offset, ISearchResult $searchResult): bool {
$result = ['wide' => [], 'exact' => []];
$resultType = new SearchResultType('remote_groups');

Expand Down
2 changes: 1 addition & 1 deletion lib/private/Collaboration/Collaborators/RemotePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct(
}

#[\Override]
public function search($search, $limit, $offset, ISearchResult $searchResult): bool {
public function search(string $search, int $limit, int $offset, ISearchResult $searchResult): bool {
$result = ['wide' => [], 'exact' => []];
$resultType = new SearchResultType('remotes');

Expand Down
Loading
Loading