Skip to content

Commit

Permalink
feat: make systemtags public visible
Browse files Browse the repository at this point in the history
Signed-off-by: John Molakvoæ (skjnldsv) <[email protected]>
  • Loading branch information
skjnldsv committed Oct 8, 2024
1 parent e11520e commit 6973ce7
Show file tree
Hide file tree
Showing 12 changed files with 125 additions and 22 deletions.
8 changes: 7 additions & 1 deletion apps/dav/appinfo/v2/publicremote.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use OCA\DAV\Storage\PublicOwnerWrapper;
use OCA\DAV\Storage\PublicShareWrapper;
use OCA\FederatedFileSharing\FederatedShareProvider;
use OCP\BeforeSabrePubliclyLoadedEvent;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Mount\IMountManager;
use OCP\IConfig;
Expand All @@ -35,6 +36,7 @@

$session = \OCP\Server::get(ISession::class);
$request = \OCP\Server::get(IRequest::class);
$eventDispatcher = \OCP\Server::get(IEventDispatcher::class);

$session->close();
$requestUri = $request->getRequestUri();
Expand All @@ -59,7 +61,7 @@
\OCP\Server::get(ITagManager::class),
$request,
\OCP\Server::get(IPreview::class),
\OCP\Server::get(IEventDispatcher::class),
$eventDispatcher,
$l10nFactory->get('dav'),
);

Expand Down Expand Up @@ -131,5 +133,9 @@
$server->addPlugin($linkCheckPlugin);
$server->addPlugin($filesDropPlugin);

// allow setup of additional plugins
$event = new BeforeSabrePubliclyLoadedEvent($server);
$eventDispatcher->dispatchTyped($event);

// And off we go!
$server->exec();
4 changes: 0 additions & 4 deletions apps/dav/lib/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
use OCA\DAV\Files\LazySearchBackend;
use OCA\DAV\Profiler\ProfilerPlugin;
use OCA\DAV\Provisioning\Apple\AppleProvisioningPlugin;
use OCA\DAV\SystemTag\SystemTagPlugin;
use OCA\DAV\Upload\ChunkingPlugin;
use OCA\DAV\Upload\ChunkingV2Plugin;
use OCA\Theming\ThemingDefaults;
Expand Down Expand Up @@ -197,9 +196,6 @@ public function __construct(IRequest $request, string $baseUri) {
$this->server->addPlugin(\OCP\Server::get(CardDavValidatePlugin::class));
}

// system tags plugins
$this->server->addPlugin(\OC::$server->get(SystemTagPlugin::class));

// comments plugin
$this->server->addPlugin(new CommentsPlugin(
\OC::$server->getCommentsManager(),
Expand Down
14 changes: 8 additions & 6 deletions apps/dav/lib/SystemTag/SystemTagList.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
class SystemTagList implements Element {
public const NS_NEXTCLOUD = 'http://nextcloud.org/ns';

/** @var ISystemTag[] */
private array $tags;
private ISystemTagManager $tagManager;
private IUser $user;

public function __construct(array $tags, ISystemTagManager $tagManager, IUser $user) {
/**
* @param ISystemTag[] $tags
*/
public function __construct(
private array $tags,
private ISystemTagManager $tagManager,
private ?IUser $user,
) {
$this->tags = $tags;
$this->tagManager = $tagManager;
$this->user = $user;
Expand Down
6 changes: 1 addition & 5 deletions apps/dav/lib/SystemTag/SystemTagPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,9 +305,6 @@ private function propfindForFile(PropFind $propFind, Node $node): void {

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

$tags = $this->getTagsForFile($node->getId(), $user);
usort($tags, function (ISystemTag $tagA, ISystemTag $tagB): int {
Expand All @@ -321,8 +318,7 @@ private function propfindForFile(PropFind $propFind, Node $node): void {
* @param int $fileId
* @return ISystemTag[]
*/
private function getTagsForFile(int $fileId, IUser $user): array {

private function getTagsForFile(int $fileId, ?IUser $user): array {
if (isset($this->cachedTagMappings[$fileId])) {
$tagIds = $this->cachedTagMappings[$fileId];
} else {
Expand Down
3 changes: 3 additions & 0 deletions apps/systemtags/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
'OCA\\SystemTags\\AppInfo\\Application' => $baseDir . '/../lib/AppInfo/Application.php',
'OCA\\SystemTags\\Capabilities' => $baseDir . '/../lib/Capabilities.php',
'OCA\\SystemTags\\Controller\\LastUsedController' => $baseDir . '/../lib/Controller/LastUsedController.php',
'OCA\\SystemTags\\Listeners\\BeforeSabrePubliclyLoadedListener' => $baseDir . '/../lib/Listeners/BeforeSabrePubliclyLoadedListener.php',
'OCA\\SystemTags\\Listeners\\BeforeTemplateRenderedListener' => $baseDir . '/../lib/Listeners/BeforeTemplateRenderedListener.php',
'OCA\\SystemTags\\Listeners\\LoadAdditionalScriptsListener' => $baseDir . '/../lib/Listeners/LoadAdditionalScriptsListener.php',
'OCA\\SystemTags\\Search\\TagSearchProvider' => $baseDir . '/../lib/Search/TagSearchProvider.php',
'OCA\\SystemTags\\Settings\\Admin' => $baseDir . '/../lib/Settings/Admin.php',
);
3 changes: 3 additions & 0 deletions apps/systemtags/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class ComposerStaticInitSystemTags
'OCA\\SystemTags\\AppInfo\\Application' => __DIR__ . '/..' . '/../lib/AppInfo/Application.php',
'OCA\\SystemTags\\Capabilities' => __DIR__ . '/..' . '/../lib/Capabilities.php',
'OCA\\SystemTags\\Controller\\LastUsedController' => __DIR__ . '/..' . '/../lib/Controller/LastUsedController.php',
'OCA\\SystemTags\\Listeners\\BeforeSabrePubliclyLoadedListener' => __DIR__ . '/..' . '/../lib/Listeners/BeforeSabrePubliclyLoadedListener.php',
'OCA\\SystemTags\\Listeners\\BeforeTemplateRenderedListener' => __DIR__ . '/..' . '/../lib/Listeners/BeforeTemplateRenderedListener.php',
'OCA\\SystemTags\\Listeners\\LoadAdditionalScriptsListener' => __DIR__ . '/..' . '/../lib/Listeners/LoadAdditionalScriptsListener.php',
'OCA\\SystemTags\\Search\\TagSearchProvider' => __DIR__ . '/..' . '/../lib/Search/TagSearchProvider.php',
'OCA\\SystemTags\\Settings\\Admin' => __DIR__ . '/..' . '/../lib/Settings/Admin.php',
);
Expand Down
8 changes: 8 additions & 0 deletions apps/systemtags/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@
namespace OCA\SystemTags\AppInfo;

use OCA\Files\Event\LoadAdditionalScriptsEvent;
use OCA\Files_Sharing\Event\BeforeTemplateRenderedEvent;
use OCA\SystemTags\Activity\Listener;
use OCA\SystemTags\Capabilities;
use OCA\SystemTags\Listeners\BeforeSabrePubliclyLoadedListener;
use OCA\SystemTags\Listeners\BeforeTemplateRenderedListener;
use OCA\SystemTags\Listeners\LoadAdditionalScriptsListener;
use OCA\SystemTags\Search\TagSearchProvider;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\BeforeSabrePubliclyLoadedEvent;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\SystemTag\ManagerEvent;
use OCP\SystemTag\MapperEvent;
Expand All @@ -30,6 +35,9 @@ public function __construct() {
public function register(IRegistrationContext $context): void {
$context->registerSearchProvider(TagSearchProvider::class);
$context->registerCapability(Capabilities::class);
$context->registerEventListener(LoadAdditionalScriptsEvent::class, LoadAdditionalScriptsListener::class);
$context->registerEventListener(BeforeTemplateRenderedEvent::class, BeforeTemplateRenderedListener::class);
$context->registerEventListener(BeforeSabrePubliclyLoadedEvent::class, BeforeSabrePubliclyLoadedListener::class);
}

public function boot(IBootContext $context): void {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\SystemTags\Listeners;

use OCA\DAV\SystemTag\SystemTagPlugin;
use OCP\BeforeSabrePubliclyLoadedEvent;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Server;

/**
* @template-implements IEventListener<BeforeSabrePubliclyLoadedEvent>
*/
class BeforeSabrePubliclyLoadedListener implements IEventListener {
public function handle(Event $event): void {
if (!$event instanceof BeforeSabrePubliclyLoadedEvent) {
return;
}
$event->getServer()->addPlugin(Server::get(SystemTagPlugin::class));
}
}
26 changes: 26 additions & 0 deletions apps/systemtags/lib/Listeners/BeforeTemplateRenderedListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\SystemTags\Listeners;

use OCA\Files_Sharing\Event\BeforeTemplateRenderedEvent;
use OCA\SystemTags\AppInfo\Application;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Util;

/**
* @template-implements IEventListener<BeforeTemplateRenderedEvent>
*/
class BeforeTemplateRenderedListener implements IEventListener {
public function handle(Event $event): void {
if (!$event instanceof BeforeTemplateRenderedEvent) {
return;
}
Util::addInitScript(Application::APP_ID, 'init');
}
}
26 changes: 26 additions & 0 deletions apps/systemtags/lib/Listeners/LoadAdditionalScriptsListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\SystemTags\Listeners;

use OCA\Files\Event\LoadAdditionalScriptsEvent;
use OCA\SystemTags\AppInfo\Application;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Util;

/**
* @template-implements IEventListener<LoadAdditionalScriptsEvent>
*/
class LoadAdditionalScriptsListener implements IEventListener {
public function handle(Event $event): void {
if (!$event instanceof LoadAdditionalScriptsEvent) {
return;
}
Util::addInitScript(Application::APP_ID, 'init');
}
}
13 changes: 11 additions & 2 deletions lib/private/SystemTag/SystemTagManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,11 @@ public function deleteTags($tagIds): void {
/**
* {@inheritdoc}
*/
public function canUserAssignTag(ISystemTag $tag, IUser $user): bool {
public function canUserAssignTag(ISystemTag $tag, ?IUser $user): bool {
if ($user === null) {
return false;
}

// early check to avoid unneeded group lookups
if ($tag->isUserAssignable() && $tag->isUserVisible()) {
return true;
Expand Down Expand Up @@ -333,11 +337,16 @@ public function canUserAssignTag(ISystemTag $tag, IUser $user): bool {
/**
* {@inheritdoc}
*/
public function canUserSeeTag(ISystemTag $tag, IUser $user): bool {
public function canUserSeeTag(ISystemTag $tag, ?IUser $user): bool {
if ($tag->isUserVisible()) {
return true;
}

// If no user and not user visible, then it's invisible
if ($user === null) {
return false;
}

if ($this->groupManager->isAdmin($user->getUID())) {
return true;
}
Expand Down
10 changes: 6 additions & 4 deletions lib/public/SystemTag/ISystemTagManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,25 +106,27 @@ public function deleteTags($tagIds);
* given id.
*
* @param ISystemTag $tag tag to check permission for
* @param IUser $user user to check permission for
* @param IUser|null $user user to check permission for
*
* @return bool true if the user is allowed to assign/unassign the tag, false otherwise
*
* @since 9.1.0
* @since 31.0.0 `$user` can be null to check anonymous permissions
*/
public function canUserAssignTag(ISystemTag $tag, IUser $user): bool;
public function canUserAssignTag(ISystemTag $tag, ?IUser $user): bool;

/**
* Checks whether the given user is allowed to see the tag with the given id.
*
* @param ISystemTag $tag tag to check permission for
* @param IUser $user user to check permission for
* @param IUser|null $user user to check permission for
*
* @return bool true if the user can see the tag, false otherwise
*
* @since 9.1.0
* @since 31.0.0 `$user` can be null to check anonymous permissions
*/
public function canUserSeeTag(ISystemTag $tag, IUser $user): bool;
public function canUserSeeTag(ISystemTag $tag, ?IUser $user): bool;

/**
* Set groups that can assign a given tag.
Expand Down

0 comments on commit 6973ce7

Please sign in to comment.