Skip to content

Commit

Permalink
Cleanup and do not list current user shares in getShares too
Browse files Browse the repository at this point in the history
Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
  • Loading branch information
skjnldsv committed Oct 4, 2019
1 parent 2169c26 commit 3492fd9
Showing 1 changed file with 85 additions and 78 deletions.
163 changes: 85 additions & 78 deletions apps/files_sharing/lib/Controller/ShareAPIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -612,60 +612,31 @@ private function getSharedWithMe($node = null, bool $includeTags): DataResponse

/**
* @param \OCP\Files\Folder $folder
* @return DataResponse
* @return array
* @throws OCSBadRequestException
*/
private function getSharesInDir(Node $folder): DataResponse {
private function getSharesInDir(Node $folder): array {
if (!($folder instanceof \OCP\Files\Folder)) {
throw new OCSBadRequestException($this->l->t('Not a directory'));
}

$nodes = $folder->getDirectoryListing();
/** @var \OCP\Share\IShare[] $shares */
$shares = [];
foreach ($nodes as $node) {

$shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_USER, $node, true, -1, 0));
$shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_GROUP, $node, true, -1, 0));
$shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_LINK, $node, true, -1, 0));
if ($this->shareManager->shareProviderExists(Share::SHARE_TYPE_EMAIL)) {
$shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_EMAIL, $node, true, -1, 0));
}
if ($this->shareManager->outgoingServer2ServerSharesAllowed()) {
$shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_REMOTE, $node, true, -1, 0));
}
$shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_ROOM, $node, true, -1, 0));
}
/** @var \OCP\Share\IShare[] $shares */
$shares = array_reduce($nodes, function($carry, $node) {
$carry = array_merge($carry, $this->getAllShares($node, true));
return $carry;
}, []);

$formatted = $miniFormatted = [];
$resharingRight = false;
// filter out duplicate shares
$known = [];
foreach ($shares as $share) {
if (in_array($share->getId(), $known) || $share->getSharedWith() === $this->currentUser) {
continue;
}

try {
$format = $this->formatShare($share);

$known[] = $share->getId();
$formatted[] = $format;
if ($share->getSharedBy() === $this->currentUser) {
$miniFormatted[] = $format;
}
if (!$resharingRight && $this->shareProviderResharingRights($this->currentUser, $share, $folder)) {
$resharingRight = true;
}
} catch (\Exception $e) {
//Ignore this share
return array_filter($shares, function($share) use ($shares, $known) {
if (in_array($share->getId(), $shares)) {
return false;
}
}

if (!$resharingRight) {
$formatted = $miniFormatted;
}

return new DataResponse($formatted);
$known[] = $share->getId();
return true;
});
}

/**
Expand Down Expand Up @@ -714,59 +685,46 @@ public function getShares(
return $result;
}

if ($subfiles === 'true') {
$result = $this->getSharesInDir($path);
return $result;
}

if ($reshares === 'true') {
$reshares = true;
} else {
$reshares = false;
}

// Get all shares
$userShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_USER, $path, $reshares, -1, 0);
$groupShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_GROUP, $path, $reshares, -1, 0);
$linkShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_LINK, $path, $reshares, -1, 0);
if ($this->shareManager->shareProviderExists(Share::SHARE_TYPE_EMAIL)) {
$mailShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_EMAIL, $path, $reshares, -1, 0);
} else {
$mailShares = [];
}
if ($this->shareManager->shareProviderExists(Share::SHARE_TYPE_CIRCLE)) {
$circleShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_CIRCLE, $path, $reshares, -1, 0);
} else {
$circleShares = [];
}
$roomShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_ROOM, $path, $reshares, -1, 0);

$shares = array_merge($userShares, $groupShares, $linkShares, $mailShares, $circleShares, $roomShares);

if ($this->shareManager->outgoingServer2ServerSharesAllowed()) {
$federatedShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_REMOTE, $path, $reshares, -1, 0);
$shares = array_merge($shares, $federatedShares);
}

if ($this->shareManager->outgoingServer2ServerGroupSharesAllowed()) {
$federatedShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_REMOTE_GROUP, $path, $reshares, -1, 0);
$shares = array_merge($shares, $federatedShares);
if ($subfiles === 'true') {
$shares = $this->getSharesInDir($path);
} else {
// get all shares
$shares = $this->getAllShares($path, $reshares);
}

// process all shares
$formatted = $miniFormatted = [];
$resharingRight = false;
foreach ($shares as $share) {
/** @var IShare $share */

// check if one of those share is shared with me
// and if I have resharing rights on it
if (!$resharingRight && $this->shareProviderResharingRights($this->currentUser, $share, $path)) {
$resharingRight = true;
}

// do not list the shares of the current user
if ($share->getSharedWith() === $this->currentUser) {
continue;
}

try {
$format = $this->formatShare($share, $path);
$formatted[] = $format;

// let's also build a list of shares created
// by the current user only, in case
// there is no resharing rights
if ($share->getSharedBy() === $this->currentUser) {
$miniFormatted[] = $format;
}

if (!$resharingRight && $this->shareProviderResharingRights($this->currentUser, $share, $path)) {
$resharingRight = true;
}
} catch (\Exception $e) {
//Ignore share
}
Expand Down Expand Up @@ -1324,4 +1282,53 @@ private function shareProviderResharingRights(string $userId, IShare $share, $no
return false;
}

/**
* Get all the shares for the current user
*
* @param string $path
* @param boolean $reshares
* @return void
*/
private function getAllShares($path = null, $reshares = false) {
// Get all shares
$userShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_USER, $path, $reshares, -1, 0);
$groupShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_GROUP, $path, $reshares, -1, 0);
$linkShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_LINK, $path, $reshares, -1, 0);

// EMAIL SHARES
if ($this->shareManager->shareProviderExists(Share::SHARE_TYPE_EMAIL)) {
$mailShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_EMAIL, $path, $reshares, -1, 0);
} else {
$mailShares = [];
}

// CIRCLE SHARES
if ($this->shareManager->shareProviderExists(Share::SHARE_TYPE_CIRCLE)) {
$circleShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_CIRCLE, $path, $reshares, -1, 0);
} else {
$circleShares = [];
}

// TALK SHARES
if ($this->shareManager->shareProviderExists(Share::SHARE_TYPE_ROOM)) {
$roomShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_ROOM, $path, $reshares, -1, 0);
} else {
$circleShares = [];
}

// FEDERATION
if ($this->shareManager->outgoingServer2ServerSharesAllowed()) {
$federatedShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_REMOTE, $path, $reshares, -1, 0);
} else {
$federatedShares = [];
}
if ($this->shareManager->outgoingServer2ServerGroupSharesAllowed()) {
$federatedGroupShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_REMOTE_GROUP, $path, $reshares, -1, 0);
} else {
$federatedGroupShares = [];
}

return array_merge($userShares, $groupShares, $linkShares, $mailShares, $circleShares, $roomShares, $federatedShares, $federatedGroupShares);
}

}

0 comments on commit 3492fd9

Please sign in to comment.