aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/Share20
diff options
context:
space:
mode:
Diffstat (limited to 'lib/private/Share20')
-rw-r--r--lib/private/Share20/DefaultShareProvider.php185
-rw-r--r--lib/private/Share20/Exception/BackendError.php1
-rw-r--r--lib/private/Share20/Exception/InvalidShare.php1
-rw-r--r--lib/private/Share20/GroupDeletedListener.php32
-rw-r--r--lib/private/Share20/Hooks.php20
-rw-r--r--lib/private/Share20/LegacyHooks.php9
-rw-r--r--lib/private/Share20/Manager.php256
-rw-r--r--lib/private/Share20/ProviderFactory.php212
-rw-r--r--lib/private/Share20/Share.php48
-rw-r--r--lib/private/Share20/ShareAttributes.php4
-rw-r--r--lib/private/Share20/ShareDisableChecker.php7
-rw-r--r--lib/private/Share20/ShareHelper.php1
-rw-r--r--lib/private/Share20/UserDeletedListener.php32
13 files changed, 362 insertions, 446 deletions
diff --git a/lib/private/Share20/DefaultShareProvider.php b/lib/private/Share20/DefaultShareProvider.php
index ca3c33d6dbb..5300e6e1baa 100644
--- a/lib/private/Share20/DefaultShareProvider.php
+++ b/lib/private/Share20/DefaultShareProvider.php
@@ -5,6 +5,7 @@
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
* SPDX-License-Identifier: AGPL-3.0-only
*/
+
namespace OC\Share20;
use OC\Files\Cache\Cache;
@@ -12,12 +13,14 @@ use OC\Share20\Exception\BackendError;
use OC\Share20\Exception\InvalidShare;
use OC\Share20\Exception\ProviderException;
use OC\User\LazyUser;
+use OCA\Files_Sharing\AppInfo\Application;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\Defaults;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
+use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IGroupManager;
use OCP\IL10N;
@@ -31,6 +34,7 @@ use OCP\Share\IAttributes;
use OCP\Share\IManager;
use OCP\Share\IShare;
use OCP\Share\IShareProviderSupportsAccept;
+use OCP\Share\IShareProviderSupportsAllSharesInFolder;
use OCP\Share\IShareProviderWithNotification;
use Psr\Log\LoggerInterface;
use function str_starts_with;
@@ -40,10 +44,7 @@ use function str_starts_with;
*
* @package OC\Share20
*/
-class DefaultShareProvider implements IShareProviderWithNotification, IShareProviderSupportsAccept {
- // Special share type for user modified group shares
- public const SHARE_TYPE_USERGROUP = 2;
-
+class DefaultShareProvider implements IShareProviderWithNotification, IShareProviderSupportsAccept, IShareProviderSupportsAllSharesInFolder {
public function __construct(
private IDBConnection $dbConn,
private IUserManager $userManager,
@@ -56,6 +57,7 @@ class DefaultShareProvider implements IShareProviderWithNotification, IShareProv
private ITimeFactory $timeFactory,
private LoggerInterface $logger,
private IManager $shareManager,
+ private IConfig $config,
) {
}
@@ -125,9 +127,7 @@ class DefaultShareProvider implements IShareProviderWithNotification, IShareProv
$qb->setValue('expiration', $qb->createNamedParameter($expirationDate, 'datetime'));
}
- if (method_exists($share, 'getParent')) {
- $qb->setValue('parent', $qb->createNamedParameter($share->getParent()));
- }
+ $qb->setValue('parent', $qb->createNamedParameter($share->getParent()));
$qb->setValue('hide_download', $qb->createNamedParameter($share->getHideDownload() ? 1 : 0, IQueryBuilder::PARAM_INT));
} else {
@@ -282,7 +282,7 @@ class DefaultShareProvider implements IShareProviderWithNotification, IShareProv
->set('expiration', $qb->createNamedParameter($expirationDate, IQueryBuilder::PARAM_DATETIME_MUTABLE))
->set('note', $qb->createNamedParameter($share->getNote()))
->set('label', $qb->createNamedParameter($share->getLabel()))
- ->set('hide_download', $qb->createNamedParameter($share->getHideDownload() ? 1 : 0), IQueryBuilder::PARAM_INT)
+ ->set('hide_download', $qb->createNamedParameter($share->getHideDownload() ? 1 : 0, IQueryBuilder::PARAM_INT))
->executeStatement();
}
@@ -322,10 +322,7 @@ class DefaultShareProvider implements IShareProviderWithNotification, IShareProv
->where($qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_USERGROUP)))
->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($recipient)))
->andWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))
- ->andWhere($qb->expr()->orX(
- $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
- $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
- ))
+ ->andWhere($qb->expr()->in('item_type', $qb->createNamedParameter(['file', 'folder'], IQueryBuilder::PARAM_STR_ARRAY)))
->executeQuery();
$data = $stmt->fetch();
@@ -359,14 +356,7 @@ class DefaultShareProvider implements IShareProviderWithNotification, IShareProv
return $share;
}
- /**
- * Get all children of this share
- * FIXME: remove once https://github.com/owncloud/core/pull/21660 is in
- *
- * @param \OCP\Share\IShare $parent
- * @return \OCP\Share\IShare[]
- */
- public function getChildren(\OCP\Share\IShare $parent) {
+ public function getChildren(IShare $parent): array {
$children = [];
$qb = $this->dbConn->getQueryBuilder();
@@ -383,10 +373,7 @@ class DefaultShareProvider implements IShareProviderWithNotification, IShareProv
], IQueryBuilder::PARAM_INT_ARRAY)
)
)
- ->andWhere($qb->expr()->orX(
- $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
- $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
- ))
+ ->andWhere($qb->expr()->in('item_type', $qb->createNamedParameter(['file', 'folder'], IQueryBuilder::PARAM_STR_ARRAY)))
->orderBy('id');
$cursor = $qb->executeQuery();
@@ -449,10 +436,7 @@ class DefaultShareProvider implements IShareProviderWithNotification, IShareProv
->where($qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_USERGROUP)))
->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($recipient)))
->andWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))
- ->andWhere($qb->expr()->orX(
- $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
- $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
- ))
+ ->andWhere($qb->expr()->in('item_type', $qb->createNamedParameter(['file', 'folder'], IQueryBuilder::PARAM_STR_ARRAY)))
->executeQuery();
$data = $stmt->fetch();
@@ -492,6 +476,15 @@ class DefaultShareProvider implements IShareProviderWithNotification, IShareProv
protected function createUserSpecificGroupShare(IShare $share, string $recipient): int {
$type = $share->getNodeType();
+ $shareFolder = $this->config->getSystemValue('share_folder', '/');
+ $allowCustomShareFolder = $this->config->getSystemValueBool('sharing.allow_custom_share_folder', true);
+ if ($allowCustomShareFolder) {
+ $shareFolder = $this->config->getUserValue($recipient, Application::APP_ID, 'share_folder', $shareFolder);
+ }
+
+ $target = $shareFolder . '/' . $share->getNode()->getName();
+ $target = \OC\Files\Filesystem::normalizePath($target);
+
$qb = $this->dbConn->getQueryBuilder();
$qb->insert('share')
->values([
@@ -503,7 +496,7 @@ class DefaultShareProvider implements IShareProviderWithNotification, IShareProv
'item_type' => $qb->createNamedParameter($type),
'item_source' => $qb->createNamedParameter($share->getNodeId()),
'file_source' => $qb->createNamedParameter($share->getNodeId()),
- 'file_target' => $qb->createNamedParameter($share->getTarget()),
+ 'file_target' => $qb->createNamedParameter($target),
'permissions' => $qb->createNamedParameter($share->getPermissions()),
'stime' => $qb->createNamedParameter($share->getShareTime()->getTimestamp()),
])->executeStatement();
@@ -565,10 +558,7 @@ class DefaultShareProvider implements IShareProviderWithNotification, IShareProv
->where($qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_USERGROUP)))
->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($recipient)))
->andWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))
- ->andWhere($qb->expr()->orX(
- $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
- $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
- ))
+ ->andWhere($qb->expr()->in('item_type', $qb->createNamedParameter(['file', 'folder'], IQueryBuilder::PARAM_STR_ARRAY)))
->setMaxResults(1)
->executeQuery();
@@ -615,35 +605,41 @@ class DefaultShareProvider implements IShareProviderWithNotification, IShareProv
throw new \Exception('non-shallow getSharesInFolder is no longer supported');
}
+ return $this->getSharesInFolderInternal($userId, $node, $reshares);
+ }
+
+ public function getAllSharesInFolder(Folder $node): array {
+ return $this->getSharesInFolderInternal(null, $node, null);
+ }
+
+ /**
+ * @return array<int, list<IShare>>
+ */
+ private function getSharesInFolderInternal(?string $userId, Folder $node, ?bool $reshares): array {
$qb = $this->dbConn->getQueryBuilder();
$qb->select('s.*',
'f.fileid', 'f.path', 'f.permissions AS f_permissions', 'f.storage', 'f.path_hash',
'f.parent AS f_parent', 'f.name', 'f.mimetype', 'f.mimepart', 'f.size', 'f.mtime', 'f.storage_mtime',
'f.encrypted', 'f.unencrypted_size', 'f.etag', 'f.checksum')
->from('share', 's')
- ->andWhere($qb->expr()->orX(
- $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
- $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
- ));
+ ->andWhere($qb->expr()->in('item_type', $qb->createNamedParameter(['file', 'folder'], IQueryBuilder::PARAM_STR_ARRAY)));
- $qb->andWhere($qb->expr()->orX(
- $qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_USER)),
- $qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_GROUP)),
- $qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_LINK))
- ));
+ $qb->andWhere($qb->expr()->in('share_type', $qb->createNamedParameter([IShare::TYPE_USER, IShare::TYPE_GROUP, IShare::TYPE_LINK], IQueryBuilder::PARAM_INT_ARRAY)));
- /**
- * Reshares for this user are shares where they are the owner.
- */
- if ($reshares === false) {
- $qb->andWhere($qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)));
- } else {
- $qb->andWhere(
- $qb->expr()->orX(
- $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
- $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))
- )
- );
+ if ($userId !== null) {
+ /**
+ * Reshares for this user are shares where they are the owner.
+ */
+ if ($reshares !== true) {
+ $qb->andWhere($qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)));
+ } else {
+ $qb->andWhere(
+ $qb->expr()->orX(
+ $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
+ $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))
+ )
+ );
+ }
}
// todo? maybe get these from the oc_mounts table
@@ -675,7 +671,6 @@ class DefaultShareProvider implements IShareProviderWithNotification, IShareProv
foreach ($chunks as $chunk) {
$qb->setParameter('chunk', $chunk, IQueryBuilder::PARAM_INT_ARRAY);
- $a = $qb->getSQL();
$cursor = $qb->executeQuery();
while ($data = $cursor->fetch()) {
$shares[$data['fileid']][] = $this->createShare($data);
@@ -693,10 +688,7 @@ class DefaultShareProvider implements IShareProviderWithNotification, IShareProv
$qb = $this->dbConn->getQueryBuilder();
$qb->select('*')
->from('share')
- ->andWhere($qb->expr()->orX(
- $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
- $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
- ));
+ ->andWhere($qb->expr()->in('item_type', $qb->createNamedParameter(['file', 'folder'], IQueryBuilder::PARAM_STR_ARRAY)));
$qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter($shareType)));
@@ -756,10 +748,7 @@ class DefaultShareProvider implements IShareProviderWithNotification, IShareProv
], IQueryBuilder::PARAM_INT_ARRAY)
)
)
- ->andWhere($qb->expr()->orX(
- $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
- $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
- ));
+ ->andWhere($qb->expr()->in('item_type', $qb->createNamedParameter(['file', 'folder'], IQueryBuilder::PARAM_STR_ARRAY)));
$cursor = $qb->executeQuery();
$data = $cursor->fetch();
@@ -795,17 +784,8 @@ class DefaultShareProvider implements IShareProviderWithNotification, IShareProv
$cursor = $qb->select('*')
->from('share')
->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($path->getId())))
- ->andWhere(
- $qb->expr()->orX(
- $qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_USER)),
- $qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_GROUP)),
- $qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_LINK)),
- )
- )
- ->andWhere($qb->expr()->orX(
- $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
- $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
- ))
+ ->andWhere($qb->expr()->in('share_type', $qb->createNamedParameter([IShare::TYPE_USER, IShare::TYPE_GROUP, IShare::TYPE_LINK], IQueryBuilder::PARAM_INT_ARRAY)))
+ ->andWhere($qb->expr()->in('item_type', $qb->createNamedParameter(['file', 'folder'], IQueryBuilder::PARAM_STR_ARRAY)))
->orderBy('id', 'ASC')
->executeQuery();
@@ -874,10 +854,7 @@ class DefaultShareProvider implements IShareProviderWithNotification, IShareProv
$qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_USER)))
->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId)))
- ->andWhere($qb->expr()->orX(
- $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
- $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
- ));
+ ->andWhere($qb->expr()->in('item_type', $qb->createNamedParameter(['file', 'folder'], IQueryBuilder::PARAM_STR_ARRAY)));
// Filter by node if provided
if ($node !== null) {
@@ -942,10 +919,7 @@ class DefaultShareProvider implements IShareProviderWithNotification, IShareProv
$groups,
IQueryBuilder::PARAM_STR_ARRAY
)))
- ->andWhere($qb->expr()->orX(
- $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
- $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
- ));
+ ->andWhere($qb->expr()->in('item_type', $qb->createNamedParameter(['file', 'folder'], IQueryBuilder::PARAM_STR_ARRAY)));
$cursor = $qb->executeQuery();
while ($data = $cursor->fetch()) {
@@ -988,10 +962,7 @@ class DefaultShareProvider implements IShareProviderWithNotification, IShareProv
->from('share')
->where($qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_LINK)))
->andWhere($qb->expr()->eq('token', $qb->createNamedParameter($token)))
- ->andWhere($qb->expr()->orX(
- $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
- $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
- ))
+ ->andWhere($qb->expr()->in('item_type', $qb->createNamedParameter(['file', 'folder'], IQueryBuilder::PARAM_STR_ARRAY)))
->executeQuery();
$data = $cursor->fetch();
@@ -1090,7 +1061,7 @@ class DefaultShareProvider implements IShareProviderWithNotification, IShareProv
->from('share')
->where($qb->expr()->eq('share_with', $qb->createNamedParameter($userId)))
->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_USERGROUP)))
- ->andWhere($qb->expr()->in('item_type', [$qb->createNamedParameter('file'), $qb->createNamedParameter('folder')]));
+ ->andWhere($qb->expr()->in('item_type', $qb->createNamedParameter(['file', 'folder'], IQueryBuilder::PARAM_STR_ARRAY)));
// this is called with either all group shares or one group share.
// for all shares it's easier to just only search by share_with,
@@ -1147,10 +1118,7 @@ class DefaultShareProvider implements IShareProviderWithNotification, IShareProv
*/
$qb->where(
$qb->expr()->andX(
- $qb->expr()->orX(
- $qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_GROUP)),
- $qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_USERGROUP))
- ),
+ $qb->expr()->in('share_type', $qb->createNamedParameter([IShare::TYPE_GROUP, IShare::TYPE_USERGROUP], IQueryBuilder::PARAM_INT_ARRAY)),
$qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid))
)
);
@@ -1323,26 +1291,31 @@ class DefaultShareProvider implements IShareProviderWithNotification, IShareProv
$qb = $this->dbConn->getQueryBuilder();
- $or = $qb->expr()->orX(
- $qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_USER)),
- $qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_GROUP)),
- $qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_LINK))
- );
+ $shareTypes = [IShare::TYPE_USER, IShare::TYPE_GROUP, IShare::TYPE_LINK];
if ($currentAccess) {
- $or->add($qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_USERGROUP)));
+ $shareTypes[] = IShare::TYPE_USERGROUP;
}
$qb->select('id', 'parent', 'share_type', 'share_with', 'file_source', 'file_target', 'permissions')
->from('share')
->where(
- $or
+ $qb->expr()->in('share_type', $qb->createNamedParameter($shareTypes, IQueryBuilder::PARAM_INT_ARRAY))
)
->andWhere($qb->expr()->in('file_source', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)))
- ->andWhere($qb->expr()->orX(
- $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
- $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
- ));
+ ->andWhere($qb->expr()->in('item_type', $qb->createNamedParameter(['file', 'folder'], IQueryBuilder::PARAM_STR_ARRAY)));
+
+ // Ensure accepted is true for user and usergroup type
+ $qb->andWhere(
+ $qb->expr()->orX(
+ $qb->expr()->andX(
+ $qb->expr()->neq('share_type', $qb->createNamedParameter(IShare::TYPE_USER)),
+ $qb->expr()->neq('share_type', $qb->createNamedParameter(IShare::TYPE_USERGROUP)),
+ ),
+ $qb->expr()->eq('accepted', $qb->createNamedParameter(IShare::STATUS_ACCEPTED, IQueryBuilder::PARAM_INT)),
+ ),
+ );
+
$cursor = $qb->executeQuery();
$users = [];
@@ -1647,13 +1620,7 @@ class DefaultShareProvider implements IShareProviderWithNotification, IShareProv
$qb->select('*')
->from('share')
- ->where(
- $qb->expr()->orX(
- $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share\IShare::TYPE_USER)),
- $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share\IShare::TYPE_GROUP)),
- $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share\IShare::TYPE_LINK))
- )
- );
+ ->where($qb->expr()->in('share_type', $qb->createNamedParameter([IShare::TYPE_USER, IShare::TYPE_GROUP, IShare::TYPE_LINK], IQueryBuilder::PARAM_INT_ARRAY)));
$cursor = $qb->executeQuery();
while ($data = $cursor->fetch()) {
diff --git a/lib/private/Share20/Exception/BackendError.php b/lib/private/Share20/Exception/BackendError.php
index 60f7dcc1a17..b2585367727 100644
--- a/lib/private/Share20/Exception/BackendError.php
+++ b/lib/private/Share20/Exception/BackendError.php
@@ -1,4 +1,5 @@
<?php
+
/**
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
diff --git a/lib/private/Share20/Exception/InvalidShare.php b/lib/private/Share20/Exception/InvalidShare.php
index 755efdfd2cc..8756455f9d2 100644
--- a/lib/private/Share20/Exception/InvalidShare.php
+++ b/lib/private/Share20/Exception/InvalidShare.php
@@ -1,4 +1,5 @@
<?php
+
/**
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
diff --git a/lib/private/Share20/GroupDeletedListener.php b/lib/private/Share20/GroupDeletedListener.php
new file mode 100644
index 00000000000..7e1ad71c465
--- /dev/null
+++ b/lib/private/Share20/GroupDeletedListener.php
@@ -0,0 +1,32 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+namespace OC\Share20;
+
+use OCP\EventDispatcher\Event;
+use OCP\EventDispatcher\IEventListener;
+use OCP\Group\Events\GroupDeletedEvent;
+use OCP\Share\IManager;
+
+/**
+ * @template-implements IEventListener<GroupDeletedEvent>
+ */
+class GroupDeletedListener implements IEventListener {
+ public function __construct(
+ protected IManager $shareManager,
+ ) {
+ }
+
+ public function handle(Event $event): void {
+ if (!$event instanceof GroupDeletedEvent) {
+ return;
+ }
+
+ $this->shareManager->groupDeleted($event->getGroup()->getGID());
+ }
+}
diff --git a/lib/private/Share20/Hooks.php b/lib/private/Share20/Hooks.php
deleted file mode 100644
index 809b50791e5..00000000000
--- a/lib/private/Share20/Hooks.php
+++ /dev/null
@@ -1,20 +0,0 @@
-<?php
-
-/**
- * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
- * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
- * SPDX-License-Identifier: AGPL-3.0-only
- */
-namespace OC\Share20;
-
-use OCP\Share\IManager as IShareManager;
-
-class Hooks {
- public static function post_deleteUser($arguments) {
- \OC::$server->get(IShareManager::class)->userDeleted($arguments['uid']);
- }
-
- public static function post_deleteGroup($arguments) {
- \OC::$server->get(IShareManager::class)->groupDeleted($arguments['gid']);
- }
-}
diff --git a/lib/private/Share20/LegacyHooks.php b/lib/private/Share20/LegacyHooks.php
index 99c2b0a9a87..d54c8e3203d 100644
--- a/lib/private/Share20/LegacyHooks.php
+++ b/lib/private/Share20/LegacyHooks.php
@@ -1,4 +1,5 @@
<?php
+
/**
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
@@ -69,9 +70,9 @@ class LegacyHooks {
// Prepare hook
$shareType = $share->getShareType();
$sharedWith = '';
- if ($shareType === IShare::TYPE_USER ||
- $shareType === IShare::TYPE_GROUP ||
- $shareType === IShare::TYPE_REMOTE) {
+ if ($shareType === IShare::TYPE_USER
+ || $shareType === IShare::TYPE_GROUP
+ || $shareType === IShare::TYPE_REMOTE) {
$sharedWith = $share->getSharedWith();
}
@@ -81,7 +82,7 @@ class LegacyHooks {
'itemSource' => $share->getNodeId(),
'shareType' => $shareType,
'shareWith' => $sharedWith,
- 'itemparent' => method_exists($share, 'getParent') ? $share->getParent() : '',
+ 'itemparent' => $share->getParent(),
'uidOwner' => $share->getSharedBy(),
'fileSource' => $share->getNodeId(),
'fileTarget' => $share->getTarget()
diff --git a/lib/private/Share20/Manager.php b/lib/private/Share20/Manager.php
index 219f3d86380..855bb173d56 100644
--- a/lib/private/Share20/Manager.php
+++ b/lib/private/Share20/Manager.php
@@ -7,11 +7,13 @@
*/
namespace OC\Share20;
+use OC\Core\AppInfo\ConfigLexicon;
use OC\Files\Mount\MoveableMount;
use OC\KnownUser\KnownUserService;
use OC\Share20\Exception\ProviderException;
use OCA\Files_Sharing\AppInfo\Application;
use OCA\Files_Sharing\SharedStorage;
+use OCA\ShareByMail\ShareByMailProvider;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\File;
use OCP\Files\Folder;
@@ -21,6 +23,7 @@ use OCP\Files\Mount\IShareOwnerlessMount;
use OCP\Files\Node;
use OCP\Files\NotFoundException;
use OCP\HintException;
+use OCP\IAppConfig;
use OCP\IConfig;
use OCP\IDateTimeZone;
use OCP\IGroupManager;
@@ -34,6 +37,7 @@ use OCP\Mail\IMailer;
use OCP\Security\Events\ValidatePasswordPolicyEvent;
use OCP\Security\IHasher;
use OCP\Security\ISecureRandom;
+use OCP\Security\PasswordContext;
use OCP\Share;
use OCP\Share\Events\BeforeShareDeletedEvent;
use OCP\Share\Events\ShareAcceptedEvent;
@@ -43,11 +47,13 @@ use OCP\Share\Events\ShareDeletedFromSelfEvent;
use OCP\Share\Exceptions\AlreadySharedException;
use OCP\Share\Exceptions\GenericShareException;
use OCP\Share\Exceptions\ShareNotFound;
+use OCP\Share\Exceptions\ShareTokenException;
use OCP\Share\IManager;
use OCP\Share\IProviderFactory;
use OCP\Share\IShare;
use OCP\Share\IShareProvider;
use OCP\Share\IShareProviderSupportsAccept;
+use OCP\Share\IShareProviderSupportsAllSharesInFolder;
use OCP\Share\IShareProviderWithNotification;
use Psr\Log\LoggerInterface;
@@ -78,6 +84,7 @@ class Manager implements IManager {
private KnownUserService $knownUserService,
private ShareDisableChecker $shareDisableChecker,
private IDateTimeZone $dateTimeZone,
+ private IAppConfig $appConfig,
) {
$this->l = $this->l10nFactory->get('lib');
// The constructor of LegacyHooks registers the listeners of share events
@@ -113,7 +120,8 @@ class Manager implements IManager {
// Let others verify the password
try {
- $this->dispatcher->dispatchTyped(new ValidatePasswordPolicyEvent($password));
+ $event = new ValidatePasswordPolicyEvent($password, PasswordContext::SHARING);
+ $this->dispatcher->dispatchTyped($event);
} catch (HintException $e) {
/* Wrap in a 400 bad request error */
throw new HintException($e->getMessage(), $e->getHint(), 400, $e);
@@ -176,8 +184,8 @@ class Manager implements IManager {
}
// Cannot share with yourself
- if ($share->getShareType() === IShare::TYPE_USER &&
- $share->getSharedWith() === $share->getSharedBy()) {
+ if ($share->getShareType() === IShare::TYPE_USER
+ && $share->getSharedWith() === $share->getSharedBy()) {
throw new \InvalidArgumentException($this->l->t('Cannot share with yourself'));
}
@@ -187,8 +195,8 @@ class Manager implements IManager {
}
// And it should be a file or a folder
- if (!($share->getNode() instanceof \OCP\Files\File) &&
- !($share->getNode() instanceof \OCP\Files\Folder)) {
+ if (!($share->getNode() instanceof \OCP\Files\File)
+ && !($share->getNode() instanceof \OCP\Files\Folder)) {
throw new \InvalidArgumentException($this->l->t('Shared path must be either a file or a folder'));
}
@@ -212,6 +220,17 @@ class Manager implements IManager {
throw new \InvalidArgumentException($this->l->t('Valid permissions are required for sharing'));
}
+ // Permissions must be valid
+ if ($share->getPermissions() < 0 || $share->getPermissions() > \OCP\Constants::PERMISSION_ALL) {
+ throw new \InvalidArgumentException($this->l->t('Valid permissions are required for sharing'));
+ }
+
+ // Single file shares should never have delete or create permissions
+ if (($share->getNode() instanceof File)
+ && (($share->getPermissions() & (\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_DELETE)) !== 0)) {
+ throw new \InvalidArgumentException($this->l->t('File shares cannot have create or delete permissions'));
+ }
+
$permissions = 0;
$nodesForUser = $userFolder->getById($share->getNodeId());
foreach ($nodesForUser as $node) {
@@ -234,8 +253,8 @@ class Manager implements IManager {
// Link shares are allowed to have no read permissions to allow upload to hidden folders
$noReadPermissionRequired = $share->getShareType() === IShare::TYPE_LINK
|| $share->getShareType() === IShare::TYPE_EMAIL;
- if (!$noReadPermissionRequired &&
- ($share->getPermissions() & \OCP\Constants::PERMISSION_READ) === 0) {
+ if (!$noReadPermissionRequired
+ && ($share->getPermissions() & \OCP\Constants::PERMISSION_READ) === 0) {
throw new \InvalidArgumentException($this->l->t('Shares need at least read permissions'));
}
@@ -544,8 +563,8 @@ class Manager implements IManager {
}
// Check if public upload is allowed
- if ($share->getNodeType() === 'folder' && !$this->shareApiLinkAllowPublicUpload() &&
- ($share->getPermissions() & (\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE))) {
+ if ($share->getNodeType() === 'folder' && !$this->shareApiLinkAllowPublicUpload()
+ && ($share->getPermissions() & (\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE))) {
throw new \InvalidArgumentException($this->l->t('Public upload is not allowed'));
}
}
@@ -562,13 +581,10 @@ class Manager implements IManager {
* @param IShare $share
*/
protected function setLinkParent(IShare $share) {
- // No sense in checking if the method is not there.
- if (method_exists($share, 'setParent')) {
- $storage = $share->getNode()->getStorage();
- if ($storage->instanceOfStorage(SharedStorage::class)) {
- /** @var \OCA\Files_Sharing\SharedStorage $storage */
- $share->setParent($storage->getShareId());
- }
+ $storage = $share->getNode()->getStorage();
+ if ($storage->instanceOfStorage(SharedStorage::class)) {
+ /** @var \OCA\Files_Sharing\SharedStorage $storage */
+ $share->setParent((int)$storage->getShareId());
}
}
@@ -581,7 +597,11 @@ class Manager implements IManager {
$mounts = $this->mountManager->findIn($path->getPath());
foreach ($mounts as $mount) {
if ($mount->getStorage()->instanceOfStorage('\OCA\Files_Sharing\ISharedStorage')) {
- throw new \InvalidArgumentException($this->l->t('Path contains files shared with you'));
+ // Using a flat sharing model ensures the file owner can always see who has access.
+ // Allowing parent folder sharing would require tracking inherited access, which adds complexity
+ // and hurts performance/scalability.
+ // So we forbid sharing a parent folder of a share you received.
+ throw new \InvalidArgumentException($this->l->t('You cannot share a folder that contains other shares'));
}
}
}
@@ -659,41 +679,7 @@ class Manager implements IManager {
$this->linkCreateChecks($share);
$this->setLinkParent($share);
- // Initial token length
- $tokenLength = \OC\Share\Helper::getTokenLength();
-
- do {
- $tokenExists = false;
-
- for ($i = 0; $i <= 2; $i++) {
- // Generate a new token
- $token = $this->secureRandom->generate(
- $tokenLength,
- \OCP\Security\ISecureRandom::CHAR_HUMAN_READABLE
- );
-
- try {
- // Try to fetch a share with the generated token
- $this->getShareByToken($token);
- $tokenExists = true; // Token exists, we need to try again
- } catch (\OCP\Share\Exceptions\ShareNotFound $e) {
- // Token is unique, exit the loop
- $tokenExists = false;
- break;
- }
- }
-
- // If we've reached the maximum attempts and the token still exists, increase the token length
- if ($tokenExists) {
- $tokenLength++;
-
- // Check if the token length exceeds the maximum allowed length
- if ($tokenLength > \OC\Share\Constants::MAX_TOKEN_LENGTH) {
- throw new \Exception('Unable to generate a unique share token. Maximum token length exceeded.');
- }
- }
- } while ($tokenExists);
-
+ $token = $this->generateToken();
// Set the unique token
$share->setToken($token);
@@ -711,18 +697,18 @@ class Manager implements IManager {
}
// Cannot share with the owner
- if ($share->getShareType() === IShare::TYPE_USER &&
- $share->getSharedWith() === $share->getShareOwner()) {
+ if ($share->getShareType() === IShare::TYPE_USER
+ && $share->getSharedWith() === $share->getShareOwner()) {
throw new \InvalidArgumentException($this->l->t('Cannot share with the share owner'));
}
// Generate the target
- $defaultShareFolder = $this->config->getSystemValue('share_folder', '/');
- $allowCustomShareFolder = $this->config->getSystemValueBool('sharing.allow_custom_share_folder', true);
- if ($allowCustomShareFolder) {
- $shareFolder = $this->config->getUserValue($share->getSharedWith(), Application::APP_ID, 'share_folder', $defaultShareFolder);
- } else {
- $shareFolder = $defaultShareFolder;
+ $shareFolder = $this->config->getSystemValue('share_folder', '/');
+ if ($share->getShareType() === IShare::TYPE_USER) {
+ $allowCustomShareFolder = $this->config->getSystemValueBool('sharing.allow_custom_share_folder', true);
+ if ($allowCustomShareFolder) {
+ $shareFolder = $this->config->getUserValue($share->getSharedWith(), Application::APP_ID, 'share_folder', $shareFolder);
+ }
}
$target = $shareFolder . '/' . $share->getNode()->getName();
@@ -804,14 +790,14 @@ class Manager implements IManager {
}
// We can only change the recipient on user shares
- if ($share->getSharedWith() !== $originalShare->getSharedWith() &&
- $share->getShareType() !== IShare::TYPE_USER) {
+ if ($share->getSharedWith() !== $originalShare->getSharedWith()
+ && $share->getShareType() !== IShare::TYPE_USER) {
throw new \InvalidArgumentException($this->l->t('Can only update recipient on user shares'));
}
// Cannot share with the owner
- if ($share->getShareType() === IShare::TYPE_USER &&
- $share->getSharedWith() === $share->getShareOwner()) {
+ if ($share->getShareType() === IShare::TYPE_USER
+ && $share->getSharedWith() === $share->getShareOwner()) {
throw new \InvalidArgumentException($this->l->t('Cannot share with the share owner'));
}
@@ -882,6 +868,7 @@ class Manager implements IManager {
// Now update the share!
$provider = $this->factory->getProviderForType($share->getShareType());
if ($share->getShareType() === IShare::TYPE_EMAIL) {
+ /** @var ShareByMailProvider $provider */
$share = $provider->update($share, $plainTextPassword);
} else {
$share = $provider->update($share);
@@ -962,11 +949,11 @@ class Manager implements IManager {
* @return boolean whether the password was updated or not.
*/
private function updateSharePasswordIfNeeded(IShare $share, IShare $originalShare) {
- $passwordsAreDifferent = ($share->getPassword() !== $originalShare->getPassword()) &&
- (($share->getPassword() !== null && $originalShare->getPassword() === null) ||
- ($share->getPassword() === null && $originalShare->getPassword() !== null) ||
- ($share->getPassword() !== null && $originalShare->getPassword() !== null &&
- !$this->hasher->verify($share->getPassword(), $originalShare->getPassword())));
+ $passwordsAreDifferent = ($share->getPassword() !== $originalShare->getPassword())
+ && (($share->getPassword() !== null && $originalShare->getPassword() === null)
+ || ($share->getPassword() === null && $originalShare->getPassword() !== null)
+ || ($share->getPassword() !== null && $originalShare->getPassword() !== null
+ && !$this->hasher->verify($share->getPassword(), $originalShare->getPassword())));
// Password updated.
if ($passwordsAreDifferent) {
@@ -1019,7 +1006,6 @@ class Manager implements IManager {
/**
* Delete all the children of this share
- * FIXME: remove once https://github.com/owncloud/core/pull/21660 is in
*
* @param IShare $share
* @return IShare[] List of deleted shares
@@ -1083,7 +1069,12 @@ class Manager implements IManager {
foreach ($userIds as $userId) {
foreach ($shareTypes as $shareType) {
- $provider = $this->factory->getProviderForType($shareType);
+ try {
+ $provider = $this->factory->getProviderForType($shareType);
+ } catch (ProviderException $e) {
+ continue;
+ }
+
if ($node instanceof Folder) {
/* We need to get all shares by this user to get subshares */
$shares = $provider->getSharesBy($userId, $shareType, null, false, -1, 0);
@@ -1222,11 +1213,13 @@ class Manager implements IManager {
$shares = [];
foreach ($providers as $provider) {
if ($isOwnerless) {
- foreach ($node->getDirectoryListing() as $childNode) {
- $data = $provider->getSharesByPath($childNode);
- $fid = $childNode->getId();
- $shares[$fid] ??= [];
- $shares[$fid] = array_merge($shares[$fid], $data);
+ // If the provider does not implement the additional interface,
+ // we lack a performant way of querying all shares and therefore ignore the provider.
+ if ($provider instanceof IShareProviderSupportsAllSharesInFolder) {
+ foreach ($provider->getAllSharesInFolder($node) as $fid => $data) {
+ $shares[$fid] ??= [];
+ $shares[$fid] = array_merge($shares[$fid], $data);
+ }
}
} else {
foreach ($provider->getSharesInFolder($userId, $node, $reshares) as $fid => $data) {
@@ -1243,9 +1236,9 @@ class Manager implements IManager {
* @inheritdoc
*/
public function getSharesBy($userId, $shareType, $path = null, $reshares = false, $limit = 50, $offset = 0, bool $onlyValid = true) {
- if ($path !== null &&
- !($path instanceof \OCP\Files\File) &&
- !($path instanceof \OCP\Files\Folder)) {
+ if ($path !== null
+ && !($path instanceof \OCP\Files\File)
+ && !($path instanceof \OCP\Files\Folder)) {
throw new \InvalidArgumentException($this->l->t('Invalid path'));
}
@@ -1493,15 +1486,6 @@ class Manager implements IManager {
$this->deleteShare($share);
throw new ShareNotFound($this->l->t('The requested share does not exist anymore'));
}
-
- try {
- $share->getNode();
- // Ignore share, file is still accessible
- } catch (NotFoundException) {
- // Access lost, but maybe only temporarily, so don't delete the share right away
- throw new ShareNotFound($this->l->t('The requested share does not exist anymore'));
- }
-
if ($this->config->getAppValue('files_sharing', 'hide_disabled_user_shares', 'no') === 'yes') {
$uids = array_unique([$share->getShareOwner(),$share->getSharedBy()]);
foreach ($uids as $uid) {
@@ -1567,8 +1551,14 @@ class Manager implements IManager {
* @inheritdoc
*/
public function groupDeleted($gid) {
- $provider = $this->factory->getProviderForType(IShare::TYPE_GROUP);
- $provider->groupDeleted($gid);
+ foreach ([IShare::TYPE_GROUP, IShare::TYPE_REMOTE_GROUP] as $type) {
+ try {
+ $provider = $this->factory->getProviderForType($type);
+ } catch (ProviderException $e) {
+ continue;
+ }
+ $provider->groupDeleted($gid);
+ }
$excludedGroups = $this->config->getAppValue('core', 'shareapi_exclude_groups_list', '');
if ($excludedGroups === '') {
@@ -1588,8 +1578,14 @@ class Manager implements IManager {
* @inheritdoc
*/
public function userDeletedFromGroup($uid, $gid) {
- $provider = $this->factory->getProviderForType(IShare::TYPE_GROUP);
- $provider->userDeletedFromGroup($uid, $gid);
+ foreach ([IShare::TYPE_GROUP, IShare::TYPE_REMOTE_GROUP] as $type) {
+ try {
+ $provider = $this->factory->getProviderForType($type);
+ } catch (ProviderException $e) {
+ continue;
+ }
+ $provider->userDeletedFromGroup($uid, $gid);
+ }
}
/**
@@ -1801,8 +1797,8 @@ class Manager implements IManager {
* @return bool
*/
public function shareApiLinkDefaultExpireDateEnforced() {
- return $this->shareApiLinkDefaultExpireDate() &&
- $this->config->getAppValue('core', 'shareapi_enforce_expire_date', 'no') === 'yes';
+ return $this->shareApiLinkDefaultExpireDate()
+ && $this->config->getAppValue('core', 'shareapi_enforce_expire_date', 'no') === 'yes';
}
@@ -1839,8 +1835,8 @@ class Manager implements IManager {
* @return bool
*/
public function shareApiInternalDefaultExpireDateEnforced(): bool {
- return $this->shareApiInternalDefaultExpireDate() &&
- $this->config->getAppValue('core', 'shareapi_enforce_internal_expire_date', 'no') === 'yes';
+ return $this->shareApiInternalDefaultExpireDate()
+ && $this->config->getAppValue('core', 'shareapi_enforce_internal_expire_date', 'no') === 'yes';
}
/**
@@ -1849,8 +1845,8 @@ class Manager implements IManager {
* @return bool
*/
public function shareApiRemoteDefaultExpireDateEnforced(): bool {
- return $this->shareApiRemoteDefaultExpireDate() &&
- $this->config->getAppValue('core', 'shareapi_enforce_remote_expire_date', 'no') === 'yes';
+ return $this->shareApiRemoteDefaultExpireDate()
+ && $this->config->getAppValue('core', 'shareapi_enforce_remote_expire_date', 'no') === 'yes';
}
/**
@@ -1918,13 +1914,13 @@ class Manager implements IManager {
}
public function limitEnumerationToGroups(): bool {
- return $this->allowEnumeration() &&
- $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_to_group', 'no') === 'yes';
+ return $this->allowEnumeration()
+ && $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_to_group', 'no') === 'yes';
}
public function limitEnumerationToPhone(): bool {
- return $this->allowEnumeration() &&
- $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_to_phone', 'no') === 'yes';
+ return $this->allowEnumeration()
+ && $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_to_phone', 'no') === 'yes';
}
public function allowEnumerationFullMatch(): bool {
@@ -1939,6 +1935,14 @@ class Manager implements IManager {
return $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_full_match_ignore_second_dn', 'no') === 'yes';
}
+ public function allowCustomTokens(): bool {
+ return $this->appConfig->getValueBool('core', ConfigLexicon::SHARE_CUSTOM_TOKEN);
+ }
+
+ public function allowViewWithoutDownload(): bool {
+ return $this->appConfig->getValueBool('core', 'shareapi_allow_view_without_download', true);
+ }
+
public function currentUserCanEnumerateTargetUser(?IUser $currentUser, IUser $targetUser): bool {
if ($this->allowEnumerationFullMatch()) {
return true;
@@ -1976,14 +1980,9 @@ class Manager implements IManager {
}
/**
- * Copied from \OC_Util::isSharingDisabledForUser
- *
- * TODO: Deprecate function from OC_Util
- *
- * @param string $userId
- * @return bool
+ * Check if sharing is disabled for the current user
*/
- public function sharingDisabledForUser($userId) {
+ public function sharingDisabledForUser(?string $userId): bool {
return $this->shareDisableChecker->sharingDisabledForUser($userId);
}
@@ -2025,4 +2024,43 @@ class Manager implements IManager {
yield from $provider->getAllShares();
}
}
+
+ public function generateToken(): string {
+ // Initial token length
+ $tokenLength = \OC\Share\Helper::getTokenLength();
+
+ do {
+ $tokenExists = false;
+
+ for ($i = 0; $i <= 2; $i++) {
+ // Generate a new token
+ $token = $this->secureRandom->generate(
+ $tokenLength,
+ ISecureRandom::CHAR_HUMAN_READABLE,
+ );
+
+ try {
+ // Try to fetch a share with the generated token
+ $this->getShareByToken($token);
+ $tokenExists = true; // Token exists, we need to try again
+ } catch (ShareNotFound $e) {
+ // Token is unique, exit the loop
+ $tokenExists = false;
+ break;
+ }
+ }
+
+ // If we've reached the maximum attempts and the token still exists, increase the token length
+ if ($tokenExists) {
+ $tokenLength++;
+
+ // Check if the token length exceeds the maximum allowed length
+ if ($tokenLength > \OC\Share\Constants::MAX_TOKEN_LENGTH) {
+ throw new ShareTokenException('Unable to generate a unique share token. Maximum token length exceeded.');
+ }
+ }
+ } while ($tokenExists);
+
+ return $token;
+ }
}
diff --git a/lib/private/Share20/ProviderFactory.php b/lib/private/Share20/ProviderFactory.php
index 7335c863df0..d920edfd90e 100644
--- a/lib/private/Share20/ProviderFactory.php
+++ b/lib/private/Share20/ProviderFactory.php
@@ -1,32 +1,21 @@
<?php
+declare(strict_types=1);
+
/**
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
* SPDX-License-Identifier: AGPL-3.0-only
*/
+
namespace OC\Share20;
use OC\Share20\Exception\ProviderException;
-use OCA\FederatedFileSharing\AddressHandler;
use OCA\FederatedFileSharing\FederatedShareProvider;
-use OCA\FederatedFileSharing\Notifications;
-use OCA\FederatedFileSharing\TokenHandler;
-use OCA\ShareByMail\Settings\SettingsManager;
use OCA\ShareByMail\ShareByMailProvider;
use OCA\Talk\Share\RoomShareProvider;
-use OCP\AppFramework\Utility\ITimeFactory;
-use OCP\Defaults;
-use OCP\EventDispatcher\IEventDispatcher;
-use OCP\Federation\ICloudFederationFactory;
-use OCP\Files\IRootFolder;
-use OCP\Http\Client\IClientService;
-use OCP\IServerContainer;
-use OCP\L10N\IFactory;
-use OCP\Mail\IMailer;
-use OCP\Security\IHasher;
-use OCP\Security\ISecureRandom;
-use OCP\Share\IManager;
+use OCP\App\IAppManager;
+use OCP\Server;
use OCP\Share\IProviderFactory;
use OCP\Share\IShare;
use OCP\Share\IShareProvider;
@@ -38,30 +27,22 @@ use Psr\Log\LoggerInterface;
* @package OC\Share20
*/
class ProviderFactory implements IProviderFactory {
- /** @var DefaultShareProvider */
- private $defaultProvider = null;
- /** @var FederatedShareProvider */
- private $federatedProvider = null;
- /** @var ShareByMailProvider */
- private $shareByMailProvider;
- /** @var \OCA\Circles\ShareByCircleProvider */
- private $shareByCircleProvider = null;
- /** @var bool */
- private $circlesAreNotAvailable = false;
- /** @var \OCA\Talk\Share\RoomShareProvider */
+ private ?DefaultShareProvider $defaultProvider = null;
+ private ?FederatedShareProvider $federatedProvider = null;
+ private ?ShareByMailProvider $shareByMailProvider = null;
+ /**
+ * @psalm-suppress UndefinedDocblockClass
+ * @var ?RoomShareProvider
+ */
private $roomShareProvider = null;
- private $registeredShareProviders = [];
+ private array $registeredShareProviders = [];
- private $shareProviders = [];
+ private array $shareProviders = [];
- /**
- * IProviderFactory constructor.
- *
- * @param IServerContainer $serverContainer
- */
public function __construct(
- private IServerContainer $serverContainer,
+ protected IAppManager $appManager,
+ protected LoggerInterface $logger,
) {
}
@@ -71,81 +52,24 @@ class ProviderFactory implements IProviderFactory {
/**
* Create the default share provider.
- *
- * @return DefaultShareProvider
*/
- protected function defaultShareProvider() {
- if ($this->defaultProvider === null) {
- $this->defaultProvider = new DefaultShareProvider(
- $this->serverContainer->getDatabaseConnection(),
- $this->serverContainer->getUserManager(),
- $this->serverContainer->getGroupManager(),
- $this->serverContainer->get(IRootFolder::class),
- $this->serverContainer->get(IMailer::class),
- $this->serverContainer->get(Defaults::class),
- $this->serverContainer->get(IFactory::class),
- $this->serverContainer->getURLGenerator(),
- $this->serverContainer->get(ITimeFactory::class),
- $this->serverContainer->get(LoggerInterface::class),
- $this->serverContainer->get(IManager::class),
- );
- }
-
- return $this->defaultProvider;
+ protected function defaultShareProvider(): DefaultShareProvider {
+ return Server::get(DefaultShareProvider::class);
}
/**
* Create the federated share provider
- *
- * @return FederatedShareProvider
*/
- protected function federatedShareProvider() {
+ protected function federatedShareProvider(): ?FederatedShareProvider {
if ($this->federatedProvider === null) {
/*
* Check if the app is enabled
*/
- $appManager = $this->serverContainer->getAppManager();
- if (!$appManager->isEnabledForUser('federatedfilesharing')) {
+ if (!$this->appManager->isEnabledForUser('federatedfilesharing')) {
return null;
}
- /*
- * TODO: add factory to federated sharing app
- */
- $l = $this->serverContainer->getL10N('federatedfilesharing');
- $addressHandler = new AddressHandler(
- $this->serverContainer->getURLGenerator(),
- $l,
- $this->serverContainer->getCloudIdManager()
- );
- $notifications = new Notifications(
- $addressHandler,
- $this->serverContainer->get(IClientService::class),
- $this->serverContainer->get(\OCP\OCS\IDiscoveryService::class),
- $this->serverContainer->getJobList(),
- \OC::$server->getCloudFederationProviderManager(),
- \OC::$server->get(ICloudFederationFactory::class),
- $this->serverContainer->get(IEventDispatcher::class),
- $this->serverContainer->get(LoggerInterface::class),
- );
- $tokenHandler = new TokenHandler(
- $this->serverContainer->get(ISecureRandom::class)
- );
-
- $this->federatedProvider = new FederatedShareProvider(
- $this->serverContainer->getDatabaseConnection(),
- $addressHandler,
- $notifications,
- $tokenHandler,
- $l,
- $this->serverContainer->get(IRootFolder::class),
- $this->serverContainer->getConfig(),
- $this->serverContainer->getUserManager(),
- $this->serverContainer->getCloudIdManager(),
- $this->serverContainer->getGlobalScaleConfig(),
- $this->serverContainer->getCloudFederationProviderManager(),
- $this->serverContainer->get(LoggerInterface::class),
- );
+ $this->federatedProvider = Server::get(FederatedShareProvider::class);
}
return $this->federatedProvider;
@@ -153,90 +77,34 @@ class ProviderFactory implements IProviderFactory {
/**
* Create the federated share provider
- *
- * @return ShareByMailProvider
*/
- protected function getShareByMailProvider() {
+ protected function getShareByMailProvider(): ?ShareByMailProvider {
if ($this->shareByMailProvider === null) {
/*
* Check if the app is enabled
*/
- $appManager = $this->serverContainer->getAppManager();
- if (!$appManager->isEnabledForUser('sharebymail')) {
+ if (!$this->appManager->isEnabledForUser('sharebymail')) {
return null;
}
- $settingsManager = new SettingsManager($this->serverContainer->getConfig());
-
- $this->shareByMailProvider = new ShareByMailProvider(
- $this->serverContainer->getConfig(),
- $this->serverContainer->getDatabaseConnection(),
- $this->serverContainer->get(ISecureRandom::class),
- $this->serverContainer->getUserManager(),
- $this->serverContainer->get(IRootFolder::class),
- $this->serverContainer->getL10N('sharebymail'),
- $this->serverContainer->get(LoggerInterface::class),
- $this->serverContainer->get(IMailer::class),
- $this->serverContainer->getURLGenerator(),
- $this->serverContainer->getActivityManager(),
- $settingsManager,
- $this->serverContainer->get(Defaults::class),
- $this->serverContainer->get(IHasher::class),
- $this->serverContainer->get(IEventDispatcher::class),
- $this->serverContainer->get(IManager::class)
- );
+ $this->shareByMailProvider = Server::get(ShareByMailProvider::class);
}
return $this->shareByMailProvider;
}
-
- /**
- * Create the circle share provider
- *
- * @return FederatedShareProvider
- *
- * @suppress PhanUndeclaredClassMethod
- */
- protected function getShareByCircleProvider() {
- if ($this->circlesAreNotAvailable) {
- return null;
- }
-
- if (!$this->serverContainer->getAppManager()->isEnabledForUser('circles') ||
- !class_exists('\OCA\Circles\ShareByCircleProvider')
- ) {
- $this->circlesAreNotAvailable = true;
- return null;
- }
-
- if ($this->shareByCircleProvider === null) {
- $this->shareByCircleProvider = new \OCA\Circles\ShareByCircleProvider(
- $this->serverContainer->getDatabaseConnection(),
- $this->serverContainer->get(ISecureRandom::class),
- $this->serverContainer->getUserManager(),
- $this->serverContainer->get(IRootFolder::class),
- $this->serverContainer->getL10N('circles'),
- $this->serverContainer->get(LoggerInterface::class),
- $this->serverContainer->getURLGenerator()
- );
- }
-
- return $this->shareByCircleProvider;
- }
-
/**
* Create the room share provider
*
- * @return RoomShareProvider
+ * @psalm-suppress UndefinedDocblockClass
+ * @return ?RoomShareProvider
*/
protected function getRoomShareProvider() {
if ($this->roomShareProvider === null) {
/*
* Check if the app is enabled
*/
- $appManager = $this->serverContainer->getAppManager();
- if (!$appManager->isEnabledForUser('spreed')) {
+ if (!$this->appManager->isEnabledForUser('spreed')) {
return null;
}
@@ -244,9 +112,9 @@ class ProviderFactory implements IProviderFactory {
/**
* @psalm-suppress UndefinedClass
*/
- $this->roomShareProvider = $this->serverContainer->get(RoomShareProvider::class);
+ $this->roomShareProvider = Server::get(RoomShareProvider::class);
} catch (\Throwable $e) {
- $this->serverContainer->get(LoggerInterface::class)->error(
+ $this->logger->error(
$e->getMessage(),
['exception' => $e]
);
@@ -272,8 +140,6 @@ class ProviderFactory implements IProviderFactory {
$provider = $this->federatedShareProvider();
} elseif ($id === 'ocMailShare') {
$provider = $this->getShareByMailProvider();
- } elseif ($id === 'ocCircleShare') {
- $provider = $this->getShareByCircleProvider();
} elseif ($id === 'ocRoomShare') {
$provider = $this->getRoomShareProvider();
}
@@ -281,10 +147,10 @@ class ProviderFactory implements IProviderFactory {
foreach ($this->registeredShareProviders as $shareProvider) {
try {
/** @var IShareProvider $instance */
- $instance = $this->serverContainer->get($shareProvider);
+ $instance = Server::get($shareProvider);
$this->shareProviders[$instance->identifier()] = $instance;
} catch (\Throwable $e) {
- $this->serverContainer->get(LoggerInterface::class)->error(
+ $this->logger->error(
$e->getMessage(),
['exception' => $e]
);
@@ -308,9 +174,9 @@ class ProviderFactory implements IProviderFactory {
public function getProviderForType($shareType) {
$provider = null;
- if ($shareType === IShare::TYPE_USER ||
- $shareType === IShare::TYPE_GROUP ||
- $shareType === IShare::TYPE_LINK
+ if ($shareType === IShare::TYPE_USER
+ || $shareType === IShare::TYPE_GROUP
+ || $shareType === IShare::TYPE_LINK
) {
$provider = $this->defaultShareProvider();
} elseif ($shareType === IShare::TYPE_REMOTE || $shareType === IShare::TYPE_REMOTE_GROUP) {
@@ -318,7 +184,7 @@ class ProviderFactory implements IProviderFactory {
} elseif ($shareType === IShare::TYPE_EMAIL) {
$provider = $this->getShareByMailProvider();
} elseif ($shareType === IShare::TYPE_CIRCLE) {
- $provider = $this->getShareByCircleProvider();
+ $provider = $this->getProvider('ocCircleShare');
} elseif ($shareType === IShare::TYPE_ROOM) {
$provider = $this->getRoomShareProvider();
} elseif ($shareType === IShare::TYPE_DECK) {
@@ -341,10 +207,6 @@ class ProviderFactory implements IProviderFactory {
if ($shareByMail !== null) {
$shares[] = $shareByMail;
}
- $shareByCircle = $this->getShareByCircleProvider();
- if ($shareByCircle !== null) {
- $shares[] = $shareByCircle;
- }
$roomShare = $this->getRoomShareProvider();
if ($roomShare !== null) {
$shares[] = $roomShare;
@@ -353,9 +215,9 @@ class ProviderFactory implements IProviderFactory {
foreach ($this->registeredShareProviders as $shareProvider) {
try {
/** @var IShareProvider $instance */
- $instance = $this->serverContainer->get($shareProvider);
+ $instance = Server::get($shareProvider);
} catch (\Throwable $e) {
- $this->serverContainer->get(LoggerInterface::class)->error(
+ $this->logger->error(
$e->getMessage(),
['exception' => $e]
);
diff --git a/lib/private/Share20/Share.php b/lib/private/Share20/Share.php
index c1c13c77fa4..571efc8c4be 100644
--- a/lib/private/Share20/Share.php
+++ b/lib/private/Share20/Share.php
@@ -14,8 +14,10 @@ use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\Files\NotFoundException;
use OCP\IUserManager;
+use OCP\Server;
use OCP\Share\Exceptions\IllegalIDChangeException;
use OCP\Share\IAttributes;
+use OCP\Share\IManager;
use OCP\Share\IShare;
class Share implements IShare {
@@ -58,22 +60,20 @@ class Share implements IShare {
private $sendPasswordByTalk = false;
/** @var string */
private $token;
- /** @var int */
- private $parent;
+ private ?int $parent = null;
/** @var string */
private $target;
/** @var \DateTime */
private $shareTime;
/** @var bool */
private $mailSend;
- /** @var string */
- private $label = '';
/** @var ICacheEntry|null */
private $nodeCacheEntry;
/** @var bool */
private $hideDownload = false;
private bool $reminderSent = false;
+ private string $label = '';
private bool $noExpirationDate = false;
public function __construct(
@@ -419,8 +419,8 @@ class Share implements IShare {
* @inheritdoc
*/
public function isExpired() {
- return $this->getExpirationDate() !== null &&
- $this->getExpirationDate() <= new \DateTime();
+ return $this->getExpirationDate() !== null
+ && $this->getExpirationDate() <= new \DateTime();
}
/**
@@ -525,25 +525,12 @@ class Share implements IShare {
return $this->token;
}
- /**
- * Set the parent of this share
- *
- * @param int $parent
- * @return IShare
- * @deprecated 12.0.0 The new shares do not have parents. This is just here for legacy reasons.
- */
- public function setParent($parent) {
+ public function setParent(int $parent): self {
$this->parent = $parent;
return $this;
}
- /**
- * Get the parent of this share.
- *
- * @return int
- * @deprecated 12.0.0 The new shares do not have parents. This is just here for legacy reasons.
- */
- public function getParent() {
+ public function getParent(): ?int {
return $this->parent;
}
@@ -623,4 +610,23 @@ class Share implements IShare {
public function getReminderSent(): bool {
return $this->reminderSent;
}
+
+ public function canSeeContent(): bool {
+ $shareManager = Server::get(IManager::class);
+
+ $allowViewWithoutDownload = $shareManager->allowViewWithoutDownload();
+ // If the share manager allows viewing without download, we can always see the content.
+ if ($allowViewWithoutDownload) {
+ return true;
+ }
+
+ // No "allow preview" header set, so we must check if
+ // the share has not explicitly disabled download permissions
+ $attributes = $this->getAttributes();
+ if ($attributes?->getAttribute('permissions', 'download') === false) {
+ return false;
+ }
+
+ return true;
+ }
}
diff --git a/lib/private/Share20/ShareAttributes.php b/lib/private/Share20/ShareAttributes.php
index 96da1e336e3..f90fbd9c6cd 100644
--- a/lib/private/Share20/ShareAttributes.php
+++ b/lib/private/Share20/ShareAttributes.php
@@ -32,8 +32,8 @@ class ShareAttributes implements IAttributes {
* @inheritdoc
*/
public function getAttribute(string $scope, string $key): mixed {
- if (\array_key_exists($scope, $this->attributes) &&
- \array_key_exists($key, $this->attributes[$scope])) {
+ if (\array_key_exists($scope, $this->attributes)
+ && \array_key_exists($key, $this->attributes[$scope])) {
return $this->attributes[$scope][$key];
}
return null;
diff --git a/lib/private/Share20/ShareDisableChecker.php b/lib/private/Share20/ShareDisableChecker.php
index 9b8bc01558c..8cf523b8b31 100644
--- a/lib/private/Share20/ShareDisableChecker.php
+++ b/lib/private/Share20/ShareDisableChecker.php
@@ -25,12 +25,7 @@ class ShareDisableChecker {
$this->sharingDisabledForUsersCache = new CappedMemoryCache();
}
-
- /**
- * @param ?string $userId
- * @return bool
- */
- public function sharingDisabledForUser(?string $userId) {
+ public function sharingDisabledForUser(?string $userId): bool {
if ($userId === null) {
return false;
}
diff --git a/lib/private/Share20/ShareHelper.php b/lib/private/Share20/ShareHelper.php
index d4a54f1d687..3f6bab98a7f 100644
--- a/lib/private/Share20/ShareHelper.php
+++ b/lib/private/Share20/ShareHelper.php
@@ -1,4 +1,5 @@
<?php
+
/**
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
diff --git a/lib/private/Share20/UserDeletedListener.php b/lib/private/Share20/UserDeletedListener.php
new file mode 100644
index 00000000000..e0e091454b0
--- /dev/null
+++ b/lib/private/Share20/UserDeletedListener.php
@@ -0,0 +1,32 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+namespace OC\Share20;
+
+use OCP\EventDispatcher\Event;
+use OCP\EventDispatcher\IEventListener;
+use OCP\Share\IManager;
+use OCP\User\Events\UserDeletedEvent;
+
+/**
+ * @template-implements IEventListener<UserDeletedEvent>
+ */
+class UserDeletedListener implements IEventListener {
+ public function __construct(
+ protected IManager $shareManager,
+ ) {
+ }
+
+ public function handle(Event $event): void {
+ if (!$event instanceof UserDeletedEvent) {
+ return;
+ }
+
+ $this->shareManager->userDeleted($event->getUser()->getUID());
+ }
+}