X Tutup
Skip to content

Commit 8a0625d

Browse files
authored
Merge pull request #38000 from owncloud/filter_by_sharetype
Allow getting the share list filtered by share type via API
2 parents 45e3c20 + 95fc0aa commit 8a0625d

15 files changed

+1014
-47
lines changed

apps/files_sharing/js/sharedfilelist.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,13 @@
229229
if (this._sharedWithUser) {
230230
requestData.shared_with_me = true;
231231
requestData.state = 'all';
232+
requestData.share_types = [
233+
OC.Share.SHARE_TYPE_USER,
234+
OC.Share.SHARE_TYPE_GROUP,
235+
OC.Share.SHARE_TYPE_REMOTE
236+
].join(',');
237+
} else if (this._linksOnly) {
238+
requestData.share_types = [OC.Share.SHARE_TYPE_LINK].join(',');
232239
}
233240
requestData.include_tags = true;
234241
var shares = $.ajax({

apps/files_sharing/lib/Controller/Share20OcsController.php

Lines changed: 82 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -553,13 +553,27 @@ public function createShare() {
553553
* @param File|Folder $node
554554
* @param boolean $includeTags include tags in response
555555
* @param int|null $stateFilter state filter or empty for all, defaults to 0 (accepted)
556+
* @param array $requestedShareTypes a key-value array with the requested share types to
557+
* be returned. The keys of the array are the share types to be returned, and the values
558+
* whether the share type will be returned or not.
559+
* [Share::SHARE_TYPE_USER => true, Share::SHARE_TYPE_GROUP => false]
556560
* @return Result
557561
*/
558-
private function getSharedWithMe($node = null, $includeTags, $stateFilter = 0) {
559-
$userShares = $this->shareManager->getSharedWith($this->userSession->getUser()->getUID(), Share::SHARE_TYPE_USER, $node, -1, 0);
560-
$groupShares = $this->shareManager->getSharedWith($this->userSession->getUser()->getUID(), Share::SHARE_TYPE_GROUP, $node, -1, 0);
561-
562-
$shares = \array_merge($userShares, $groupShares);
562+
private function getSharedWithMe($node = null, $includeTags, $requestedShareTypes, $stateFilter = 0) {
563+
// sharedWithMe is limited to user and group shares for compatibility.
564+
$shares = [];
565+
if (isset($requestedShareTypes[Share::SHARE_TYPE_USER]) && $requestedShareTypes[Share::SHARE_TYPE_USER]) {
566+
$shares = \array_merge(
567+
$shares,
568+
$this->shareManager->getSharedWith($this->userSession->getUser()->getUID(), Share::SHARE_TYPE_USER, $node, -1, 0)
569+
);
570+
}
571+
if (isset($requestedShareTypes[Share::SHARE_TYPE_GROUP]) && $requestedShareTypes[Share::SHARE_TYPE_GROUP]) {
572+
$shares = \array_merge(
573+
$shares,
574+
$this->shareManager->getSharedWith($this->userSession->getUser()->getUID(), Share::SHARE_TYPE_GROUP, $node, -1, 0)
575+
);
576+
}
563577

564578
$shares = \array_filter($shares, function (IShare $share) {
565579
return $share->getShareOwner() !== $this->userSession->getUser()->getUID();
@@ -597,10 +611,14 @@ private function getSharedWithMe($node = null, $includeTags, $stateFilter = 0) {
597611

598612
/**
599613
* @param Folder $folder
614+
* @param array $requestedShareTypes a key-value array with the requested share types to
615+
* be returned. The keys of the array are the share types to be returned, and the values
616+
* whether the share type will be returned or not.
617+
* [Share::SHARE_TYPE_USER => true, Share::SHARE_TYPE_GROUP => false]
600618
* @return Result
601619
* @throws NotFoundException
602620
*/
603-
private function getSharesInDir($folder) {
621+
private function getSharesInDir($folder, $requestedShareTypes) {
604622
if (!($folder instanceof Folder)) {
605623
return new Result(null, 400, $this->l->t('Not a directory'));
606624
}
@@ -609,11 +627,18 @@ private function getSharesInDir($folder) {
609627
/** @var IShare[] $shares */
610628
$shares = [];
611629
foreach ($nodes as $node) {
612-
$shares = \array_merge($shares, $this->shareManager->getSharesBy($this->userSession->getUser()->getUID(), Share::SHARE_TYPE_USER, $node, false, -1, 0));
613-
$shares = \array_merge($shares, $this->shareManager->getSharesBy($this->userSession->getUser()->getUID(), Share::SHARE_TYPE_GROUP, $node, false, -1, 0));
614-
$shares = \array_merge($shares, $this->shareManager->getSharesBy($this->userSession->getUser()->getUID(), Share::SHARE_TYPE_LINK, $node, false, -1, 0));
615-
if ($this->shareManager->outgoingServer2ServerSharesAllowed()) {
616-
$shares = \array_merge($shares, $this->shareManager->getSharesBy($this->userSession->getUser()->getUID(), Share::SHARE_TYPE_REMOTE, $node, false, -1, 0));
630+
foreach ($requestedShareTypes as $shareType => $requested) {
631+
if (!$requested) {
632+
continue;
633+
}
634+
635+
// if outgoingServer2ServerSharesAllowed is false, remote shares shouldn't be
636+
// returned. This must be checked in the caller method, so the remote share type
637+
// shouldn't be present if outgoing remotes shares aren't allowed.
638+
$shares = \array_merge(
639+
$shares,
640+
$this->shareManager->getSharesBy($this->userSession->getUser()->getUID(), $shareType, $node, false, -1, 0)
641+
);
617642
}
618643
}
619644

@@ -631,6 +656,10 @@ private function getSharesInDir($folder) {
631656

632657
/**
633658
* The getShares function.
659+
* For the share type filter, if it isn't provided or is an empty string,
660+
* all the share types will be returned, otherwise just the requested ones.
661+
* Invalid share types will be ignored. If only invalid share types are requested,
662+
* the function will return an empty list.
634663
*
635664
* @NoCSRFRequired
636665
* @NoAdminRequired
@@ -640,6 +669,7 @@ private function getSharesInDir($folder) {
640669
* - Get shares with the current user (?shared_with_me=true)
641670
* - Get shares for a specific path (?path=...)
642671
* - Get all shares in a folder (?subfiles=true&path=..)
672+
* - Filter by share type (?share_types=0,1,3,6)
643673
*
644674
* @return Result
645675
* @throws LockedException
@@ -655,6 +685,36 @@ public function getShares() {
655685
$path = $this->request->getParam('path', null);
656686

657687
$includeTags = $this->request->getParam('include_tags', false);
688+
$shareTypes = $this->request->getParam('share_types', '');
689+
if ($shareTypes === '') {
690+
$shareTypes = [
691+
Share::SHARE_TYPE_USER,
692+
Share::SHARE_TYPE_GROUP,
693+
Share::SHARE_TYPE_LINK,
694+
Share::SHARE_TYPE_REMOTE,
695+
];
696+
} else {
697+
$shareTypes = \explode(',', $shareTypes);
698+
}
699+
700+
$requestedShareTypes = [
701+
Share::SHARE_TYPE_USER => false,
702+
Share::SHARE_TYPE_GROUP => false,
703+
Share::SHARE_TYPE_LINK => false,
704+
Share::SHARE_TYPE_REMOTE => false,
705+
];
706+
707+
if ($this->shareManager->outgoingServer2ServerSharesAllowed() === false) {
708+
// if outgoing remote shares aren't allowed, the remote share type can't be chosen
709+
unset($requestedShareTypes[Share::SHARE_TYPE_REMOTE]);
710+
}
711+
foreach ($shareTypes as $shareType) {
712+
if (isset($requestedShareTypes[$shareType])) {
713+
$requestedShareTypes[$shareType] = true;
714+
}
715+
}
716+
// requestedShareTypes now contains as keys the share type that has been requested
717+
// (with "true" value), without duplicate elements, and only valid share types
658718

659719
if ($path !== null) {
660720
$userFolder = $this->rootFolder->getUserFolder($this->userSession->getUser()->getUID());
@@ -677,15 +737,15 @@ public function getShares() {
677737
} else {
678738
$stateFilter = (int)$stateFilter;
679739
}
680-
$result = $this->getSharedWithMe($path, $includeTags, $stateFilter);
740+
$result = $this->getSharedWithMe($path, $includeTags, $requestedShareTypes, $stateFilter);
681741
if ($path !== null) {
682742
$path->unlock(ILockingProvider::LOCK_SHARED);
683743
}
684744
return $result;
685745
}
686746

687747
if ($subfiles === 'true') {
688-
$result = $this->getSharesInDir($path);
748+
$result = $this->getSharesInDir($path, $requestedShareTypes);
689749
if ($path !== null) {
690750
$path->unlock(ILockingProvider::LOCK_SHARED);
691751
}
@@ -698,15 +758,16 @@ public function getShares() {
698758
$reshares = false;
699759
}
700760

701-
// Get all shares
702-
$userShares = $this->shareManager->getSharesBy($this->userSession->getUser()->getUID(), Share::SHARE_TYPE_USER, $path, $reshares, -1, 0);
703-
$groupShares = $this->shareManager->getSharesBy($this->userSession->getUser()->getUID(), Share::SHARE_TYPE_GROUP, $path, $reshares, -1, 0);
704-
$linkShares = $this->shareManager->getSharesBy($this->userSession->getUser()->getUID(), Share::SHARE_TYPE_LINK, $path, $reshares, -1, 0);
705-
$shares = \array_merge($userShares, $groupShares, $linkShares);
761+
$shares = [];
762+
foreach ($requestedShareTypes as $shareType => $requested) {
763+
if (!$requested) {
764+
continue;
765+
}
706766

707-
if ($this->shareManager->outgoingServer2ServerSharesAllowed()) {
708-
$federatedShares = $this->shareManager->getSharesBy($this->userSession->getUser()->getUID(), Share::SHARE_TYPE_REMOTE, $path, $reshares, -1, 0);
709-
$shares = \array_merge($shares, $federatedShares);
767+
$shares = \array_merge(
768+
$shares,
769+
$this->shareManager->getSharesBy($this->userSession->getUser()->getUID(), $shareType, $path, $reshares, -1, 0)
770+
);
710771
}
711772

712773
$formatted = [];

0 commit comments

Comments
 (0)
X Tutup