Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

linked collection #868

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Core App of the full-text search framework for your Nextcloud.
<command>OCA\FullTextSearch\Command\Check</command>
<command>OCA\FullTextSearch\Command\CollectionInit</command>
<command>OCA\FullTextSearch\Command\CollectionDelete</command>
<command>OCA\FullTextSearch\Command\CollectionLink</command>
<command>OCA\FullTextSearch\Command\CollectionList</command>
<command>OCA\FullTextSearch\Command\Configure</command>
<command>OCA\FullTextSearch\Command\DocumentIndex</command>
Expand Down
101 changes: 101 additions & 0 deletions lib/Command/CollectionLink.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

declare(strict_types=1);
/**
* FullTextSearch - Full text search framework for Nextcloud
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Maxence Lange <[email protected]>
* @copyright 2024
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\FullTextSearch\Command;

use OC\Core\Command\Base;
use OCA\FullTextSearch\AppInfo\Application;
use OCA\FullTextSearch\Exceptions\CollectionArgumentException;
use OCA\FullTextSearch\Service\CollectionService;
use OCA\FullTextSearch\Service\ConfigService;
use OCP\IAppConfig;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class CollectionLink extends Base {
public function __construct(
private CollectionService $collectionService,
) {
parent::__construct();
}

protected function configure() {
parent::configure();
$this->setName('fulltextsearch:collection:link')
->setDescription('Link collection to a user')
->addArgument('collection', InputArgument::OPTIONAL, 'collection' , '')
->addArgument('userId', InputArgument::OPTIONAL, 'user to link a collection to', '')
->addOption('unlink', '', InputOption::VALUE_NONE, 'unlink collection');
}

/**
* @param InputInterface $input
* @param OutputInterface $output
*
* @return int
* @throws CollectionArgumentException
*/
protected function execute(InputInterface $input, OutputInterface $output): int {
$links = $this->collectionService->getLinks();
$collection = $input->getArgument('collection');

if ($collection === '') {
if (empty($links)) {
$output->writeln('no collection linked to any user');
}

foreach($links as $name => $userId) {
$output->writeln('- Collection <info>' . $name . '</info> linked to user <info>' . $userId . '</info>');
}

return 0;
}

if (!$this->collectionService->hasCollection($collection)) {
throw new CollectionArgumentException('unknown collection');
}

if ($input->getOption('unlink')) {
$this->collectionService->removeLink($collection);
$output->writeln('unlinked collection');
return 0;
}

$userId = $input->getArgument('userId');
if ($userId === '') {
throw new CollectionArgumentException('missing userId');
}

$this->collectionService->addLink($collection, $userId);
$output->writeln('linked collection');

return 0;
}
}
56 changes: 41 additions & 15 deletions lib/Controller/CollectionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,37 +31,31 @@
namespace OCA\FullTextSearch\Controller;


use OC\ForbiddenException;
use OCA\FullTextSearch\AppInfo\Application;
use OCA\FullTextSearch\Exceptions\CollectionArgumentException;
use OCA\FullTextSearch\Service\CollectionService;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCS\OCSException;
use OCP\AppFramework\OCSController;
use OCP\FullTextSearch\Model\IIndexDocument;
use OCP\IGroupManager;
use OCP\IRequest;

use OCP\IUserSession;

class CollectionController extends OCSController {


/** @var CollectionService */
private $collectionService;


/**
* @param IRequest $request
* @param CollectionService $collectionService
*/
public function __construct(
IRequest $request,
CollectionService $collectionService
private IUserSession $userSession,
private IGroupManager $groupManager,
private CollectionService $collectionService
) {
parent::__construct(Application::APP_ID, $request);

$this->collectionService = $collectionService;
}


/**
* @NoAdminRequired
*
* @param string $collection
* @param int $length
*
Expand All @@ -71,6 +65,7 @@ public function __construct(
public function getQueue(string $collection, int $length = 0): DataResponse {
try {
$this->collectionService->confirmCollection($collection);
$this->confirmAccess($collection);

return new DataResponse($this->collectionService->getQueue($collection, $length));
} catch (\Exception $e) {
Expand All @@ -81,6 +76,8 @@ public function getQueue(string $collection, int $length = 0): DataResponse {


/**
* @NoAdminRequired
*
* @param string $collection
* @param string $providerId
* @param string $documentId
Expand All @@ -91,6 +88,8 @@ public function getQueue(string $collection, int $length = 0): DataResponse {
public function indexDocument(string $collection, string $providerId, string $documentId): DataResponse {
try {
$this->collectionService->confirmCollection($collection);
$this->confirmAccess($collection);

$document = $this->collectionService->getDocument(
$collection,
$providerId,
Expand All @@ -105,6 +104,8 @@ public function indexDocument(string $collection, string $providerId, string $do


/**
* @NoAdminRequired
*
* @param string $collection
* @param string $providerId
* @param string $documentId
Expand All @@ -119,6 +120,8 @@ public function updateStatusDone(
): DataResponse {
try {
$this->collectionService->confirmCollection($collection);
$this->confirmAccess($collection);

$this->collectionService->setAsDone($collection, $providerId, $documentId);

return new DataResponse([]);
Expand All @@ -127,6 +130,29 @@ public function updateStatusDone(
}
}

/**
* confirm that current session have access to collection
*
* @param string $collection
*
* @return void
* @throws ForbiddenException
*/
private function confirmAccess(string $collection): void {
$currentUserId = $this->userSession->getUser()->getUID();
if ($this->groupManager->isAdmin($currentUserId)) {
return;
}

try {
if ($this->collectionService->getLinkedAccount($collection) === $currentUserId) {
return;
}
} catch (CollectionArgumentException) {
}

throw new ForbiddenException('API access not allowed');
}

/**
* @param IIndexDocument $document
Expand Down
56 changes: 55 additions & 1 deletion lib/Service/CollectionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
namespace OCA\FullTextSearch\Service;


use OCA\FullTextSearch\AppInfo\Application;
use OCA\FullTextSearch\Db\IndexesRequest;
use OCA\FullTextSearch\Exceptions\CollectionArgumentException;
use OCA\FullTextSearch\Exceptions\IndexDoesNotExistException;
Expand All @@ -41,9 +42,9 @@
use OCP\FullTextSearch\IFullTextSearchProvider;
use OCP\FullTextSearch\Model\IIndex;
use OCP\FullTextSearch\Model\IIndexDocument;
use OCP\IAppConfig;
use OCP\IURLGenerator;


class CollectionService {

/** @var IURLGenerator */
Expand Down Expand Up @@ -74,6 +75,7 @@ class CollectionService {
* @param ConfigService $configService
*/
public function __construct(
private IAppConfig $appConfig,
IURLGenerator $urlGenerator,
IndexesRequest $indexesRequest,
ProviderService $providerService,
Expand Down Expand Up @@ -258,6 +260,58 @@ public function getDocument(string $collection, string $providerId, string $docu
return $provider->updateDocument($index);
}

/**
* @return array
*/
public function getLinks(): array {
return $this->appConfig->getValueArray(Application::APP_ID, ConfigService::COLLECTION_LINKS, lazy: true);
}

/**
* @param array $links
*/
public function saveLinks(array $links): void {
$this->appConfig->setValueArray(Application::APP_ID, ConfigService::COLLECTION_LINKS, $links, lazy: true);
}

/**
* @param string $collection
* @param string $userId
*/
public function addLink(string $collection, string $userId): void {
$links = $this->getLinks();
$links[$collection] = $userId;
$this->saveLinks($links);
}

/**
* @param string $collection
*/
public function removeLink(string $collection): void {
$links = $this->getLinks();
unset($links[$collection]);
$this->saveLinks($links);
}

/**
* @param string $collection
*
* @return string
* @throws CollectionArgumentException
*/
public function getLinkedAccount(string $collection): string {
if (!$this->hasCollection($collection)) {
throw new CollectionArgumentException('unknown collection');
}

$links = $this->getLinks();
$userId = $links[$collection] ?? '';
if ($userId === '') {
throw new CollectionArgumentException('no linked account');
}

return $userId;
}

/**
* @param string $info
Expand Down
4 changes: 3 additions & 1 deletion lib/Service/ConfigService.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class ConfigService {
const TICK_TTL = 'tick_ttl';
const COLLECTION_INDEXING_LIST = 'collection_indexing_list';
const COLLECTION_INTERNAL = 'collection_internal';
const COLLECTION_LINKS = 'collection_links';

// Temp. can be removed after few major releases
const MIGRATION_24 = 'migration_24';
Expand All @@ -67,7 +68,8 @@ class ConfigService {
self::TICK_TTL => '1800',
self::COLLECTION_INDEXING_LIST => 50,
self::MIGRATION_24 => 1,
self::COLLECTION_INTERNAL => 'local'
self::COLLECTION_INTERNAL => 'local',
self::COLLECTION_LINKS => '[]'
];


Expand Down