Skip to content

Commit 832695d

Browse files
authored
Merge pull request #48206 from nextcloud/feat/systemtags-public
2 parents 07f822c + abd0cdd commit 832695d

File tree

11 files changed

+136
-18
lines changed

11 files changed

+136
-18
lines changed

apps/dav/appinfo/v2/publicremote.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use OCA\DAV\Storage\PublicOwnerWrapper;
1313
use OCA\DAV\Storage\PublicShareWrapper;
1414
use OCA\FederatedFileSharing\FederatedShareProvider;
15+
use OCP\BeforeSabrePubliclyLoadedEvent;
1516
use OCP\EventDispatcher\IEventDispatcher;
1617
use OCP\Files\Mount\IMountManager;
1718
use OCP\IConfig;
@@ -35,6 +36,7 @@
3536

3637
$session = \OCP\Server::get(ISession::class);
3738
$request = \OCP\Server::get(IRequest::class);
39+
$eventDispatcher = \OCP\Server::get(IEventDispatcher::class);
3840

3941
$session->close();
4042
$requestUri = $request->getRequestUri();
@@ -59,7 +61,7 @@
5961
\OCP\Server::get(ITagManager::class),
6062
$request,
6163
\OCP\Server::get(IPreview::class),
62-
\OCP\Server::get(IEventDispatcher::class),
64+
$eventDispatcher,
6365
$l10nFactory->get('dav'),
6466
);
6567

@@ -135,5 +137,9 @@
135137
$server->addPlugin($linkCheckPlugin);
136138
$server->addPlugin($filesDropPlugin);
137139

140+
// allow setup of additional plugins
141+
$event = new BeforeSabrePubliclyLoadedEvent($server);
142+
$eventDispatcher->dispatchTyped($event);
143+
138144
// And off we go!
139145
$server->start();

apps/dav/lib/SystemTag/SystemTagList.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@
2020
class SystemTagList implements Element {
2121
public const NS_NEXTCLOUD = 'http://nextcloud.org/ns';
2222

23-
/** @var ISystemTag[] */
24-
private array $tags;
25-
private ISystemTagManager $tagManager;
26-
private IUser $user;
27-
28-
public function __construct(array $tags, ISystemTagManager $tagManager, IUser $user) {
23+
/**
24+
* @param ISystemTag[] $tags
25+
*/
26+
public function __construct(
27+
private array $tags,
28+
private ISystemTagManager $tagManager,
29+
private ?IUser $user,
30+
) {
2931
$this->tags = $tags;
3032
$this->tagManager = $tagManager;
3133
$this->user = $user;

apps/dav/lib/SystemTag/SystemTagPlugin.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,6 @@ private function propfindForFile(PropFind $propFind, Node $node): void {
305305

306306
$propFind->handle(self::SYSTEM_TAGS_PROPERTYNAME, function () use ($node) {
307307
$user = $this->userSession->getUser();
308-
if ($user === null) {
309-
return;
310-
}
311308

312309
$tags = $this->getTagsForFile($node->getId(), $user);
313310
usort($tags, function (ISystemTag $tagA, ISystemTag $tagB): int {
@@ -321,8 +318,7 @@ private function propfindForFile(PropFind $propFind, Node $node): void {
321318
* @param int $fileId
322319
* @return ISystemTag[]
323320
*/
324-
private function getTagsForFile(int $fileId, IUser $user): array {
325-
321+
private function getTagsForFile(int $fileId, ?IUser $user): array {
326322
if (isset($this->cachedTagMappings[$fileId])) {
327323
$tagIds = $this->cachedTagMappings[$fileId];
328324
} else {

apps/systemtags/composer/composer/autoload_classmap.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
'OCA\\SystemTags\\AppInfo\\Application' => $baseDir . '/../lib/AppInfo/Application.php',
1414
'OCA\\SystemTags\\Capabilities' => $baseDir . '/../lib/Capabilities.php',
1515
'OCA\\SystemTags\\Controller\\LastUsedController' => $baseDir . '/../lib/Controller/LastUsedController.php',
16+
'OCA\\SystemTags\\Listeners\\BeforeSabrePubliclyLoadedListener' => $baseDir . '/../lib/Listeners/BeforeSabrePubliclyLoadedListener.php',
17+
'OCA\\SystemTags\\Listeners\\BeforeTemplateRenderedListener' => $baseDir . '/../lib/Listeners/BeforeTemplateRenderedListener.php',
18+
'OCA\\SystemTags\\Listeners\\LoadAdditionalScriptsListener' => $baseDir . '/../lib/Listeners/LoadAdditionalScriptsListener.php',
1619
'OCA\\SystemTags\\Search\\TagSearchProvider' => $baseDir . '/../lib/Search/TagSearchProvider.php',
1720
'OCA\\SystemTags\\Settings\\Admin' => $baseDir . '/../lib/Settings/Admin.php',
1821
);

apps/systemtags/composer/composer/autoload_static.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ class ComposerStaticInitSystemTags
2828
'OCA\\SystemTags\\AppInfo\\Application' => __DIR__ . '/..' . '/../lib/AppInfo/Application.php',
2929
'OCA\\SystemTags\\Capabilities' => __DIR__ . '/..' . '/../lib/Capabilities.php',
3030
'OCA\\SystemTags\\Controller\\LastUsedController' => __DIR__ . '/..' . '/../lib/Controller/LastUsedController.php',
31+
'OCA\\SystemTags\\Listeners\\BeforeSabrePubliclyLoadedListener' => __DIR__ . '/..' . '/../lib/Listeners/BeforeSabrePubliclyLoadedListener.php',
32+
'OCA\\SystemTags\\Listeners\\BeforeTemplateRenderedListener' => __DIR__ . '/..' . '/../lib/Listeners/BeforeTemplateRenderedListener.php',
33+
'OCA\\SystemTags\\Listeners\\LoadAdditionalScriptsListener' => __DIR__ . '/..' . '/../lib/Listeners/LoadAdditionalScriptsListener.php',
3134
'OCA\\SystemTags\\Search\\TagSearchProvider' => __DIR__ . '/..' . '/../lib/Search/TagSearchProvider.php',
3235
'OCA\\SystemTags\\Settings\\Admin' => __DIR__ . '/..' . '/../lib/Settings/Admin.php',
3336
);

apps/systemtags/lib/AppInfo/Application.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@
99
namespace OCA\SystemTags\AppInfo;
1010

1111
use OCA\Files\Event\LoadAdditionalScriptsEvent;
12+
use OCA\Files_Sharing\Event\BeforeTemplateRenderedEvent;
1213
use OCA\SystemTags\Activity\Listener;
1314
use OCA\SystemTags\Capabilities;
15+
use OCA\SystemTags\Listeners\BeforeSabrePubliclyLoadedListener;
16+
use OCA\SystemTags\Listeners\BeforeTemplateRenderedListener;
17+
use OCA\SystemTags\Listeners\LoadAdditionalScriptsListener;
1418
use OCA\SystemTags\Search\TagSearchProvider;
1519
use OCP\AppFramework\App;
1620
use OCP\AppFramework\Bootstrap\IBootContext;
1721
use OCP\AppFramework\Bootstrap\IBootstrap;
1822
use OCP\AppFramework\Bootstrap\IRegistrationContext;
23+
use OCP\BeforeSabrePubliclyLoadedEvent;
1924
use OCP\EventDispatcher\IEventDispatcher;
2025
use OCP\SystemTag\ManagerEvent;
2126
use OCP\SystemTag\MapperEvent;
@@ -30,6 +35,9 @@ public function __construct() {
3035
public function register(IRegistrationContext $context): void {
3136
$context->registerSearchProvider(TagSearchProvider::class);
3237
$context->registerCapability(Capabilities::class);
38+
$context->registerEventListener(LoadAdditionalScriptsEvent::class, LoadAdditionalScriptsListener::class);
39+
$context->registerEventListener(BeforeTemplateRenderedEvent::class, BeforeTemplateRenderedListener::class);
40+
$context->registerEventListener(BeforeSabrePubliclyLoadedEvent::class, BeforeSabrePubliclyLoadedListener::class);
3341
}
3442

3543
public function boot(IBootContext $context): void {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
6+
* SPDX-License-Identifier: AGPL-3.0-or-later
7+
*/
8+
namespace OCA\SystemTags\Listeners;
9+
10+
use OCA\DAV\SystemTag\SystemTagPlugin;
11+
use OCP\BeforeSabrePubliclyLoadedEvent;
12+
use OCP\EventDispatcher\Event;
13+
use OCP\EventDispatcher\IEventListener;
14+
use OCP\Server;
15+
16+
/**
17+
* @template-implements IEventListener<BeforeSabrePubliclyLoadedEvent>
18+
*/
19+
class BeforeSabrePubliclyLoadedListener implements IEventListener {
20+
public function handle(Event $event): void {
21+
if (!$event instanceof BeforeSabrePubliclyLoadedEvent) {
22+
return;
23+
}
24+
25+
$server = $event->getServer();
26+
if ($server === null) {
27+
return;
28+
}
29+
30+
$server->addPlugin(Server::get(SystemTagPlugin::class));
31+
}
32+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
6+
* SPDX-License-Identifier: AGPL-3.0-or-later
7+
*/
8+
namespace OCA\SystemTags\Listeners;
9+
10+
use OCA\Files_Sharing\Event\BeforeTemplateRenderedEvent;
11+
use OCA\SystemTags\AppInfo\Application;
12+
use OCP\EventDispatcher\Event;
13+
use OCP\EventDispatcher\IEventListener;
14+
use OCP\Util;
15+
16+
/**
17+
* @template-implements IEventListener<BeforeTemplateRenderedEvent>
18+
*/
19+
class BeforeTemplateRenderedListener implements IEventListener {
20+
public function handle(Event $event): void {
21+
if (!$event instanceof BeforeTemplateRenderedEvent) {
22+
return;
23+
}
24+
Util::addInitScript(Application::APP_ID, 'init');
25+
}
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
6+
* SPDX-License-Identifier: AGPL-3.0-or-later
7+
*/
8+
namespace OCA\SystemTags\Listeners;
9+
10+
use OCA\Files\Event\LoadAdditionalScriptsEvent;
11+
use OCA\SystemTags\AppInfo\Application;
12+
use OCP\EventDispatcher\Event;
13+
use OCP\EventDispatcher\IEventListener;
14+
use OCP\Util;
15+
16+
/**
17+
* @template-implements IEventListener<LoadAdditionalScriptsEvent>
18+
*/
19+
class LoadAdditionalScriptsListener implements IEventListener {
20+
public function handle(Event $event): void {
21+
if (!$event instanceof LoadAdditionalScriptsEvent) {
22+
return;
23+
}
24+
Util::addInitScript(Application::APP_ID, 'init');
25+
}
26+
}

lib/private/SystemTag/SystemTagManager.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,11 @@ public function deleteTags($tagIds): void {
305305
/**
306306
* {@inheritdoc}
307307
*/
308-
public function canUserAssignTag(ISystemTag $tag, IUser $user): bool {
308+
public function canUserAssignTag(ISystemTag $tag, ?IUser $user): bool {
309+
if ($user === null) {
310+
return false;
311+
}
312+
309313
// early check to avoid unneeded group lookups
310314
if ($tag->isUserAssignable() && $tag->isUserVisible()) {
311315
return true;
@@ -333,11 +337,21 @@ public function canUserAssignTag(ISystemTag $tag, IUser $user): bool {
333337
/**
334338
* {@inheritdoc}
335339
*/
336-
public function canUserSeeTag(ISystemTag $tag, IUser $user): bool {
340+
public function canUserSeeTag(ISystemTag $tag, ?IUser $user): bool {
341+
// If no user, then we only show public tags
342+
if (!$user && $tag->getAccessLevel() === ISystemTag::ACCESS_LEVEL_PUBLIC) {
343+
return true;
344+
}
345+
337346
if ($tag->isUserVisible()) {
338347
return true;
339348
}
340349

350+
// if not returned yet, and user is not logged in, then the tag is not visible
351+
if ($user === null) {
352+
return false;
353+
}
354+
341355
if ($this->groupManager->isAdmin($user->getUID())) {
342356
return true;
343357
}

0 commit comments

Comments
 (0)