Browse Source

Also allow accessing file conversations when access is possible via external storage or groupfolder

Signed-off-by: Joas Schilling <coding@schilljs.com>
pull/4246/head
Joas Schilling 5 years ago
parent
commit
75f09c8216
No known key found for this signature in database GPG Key ID: 7076EA9751AACDDA
  1. 49
      lib/Controller/FilesIntegrationController.php
  2. 11
      lib/Files/Listener.php
  3. 49
      lib/Files/Util.php

49
lib/Controller/FilesIntegrationController.php

@ -45,7 +45,6 @@ use OCP\IUser;
use OCP\IUserSession;
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\IManager as IShareManager;
use OCP\Share\IShare;
class FilesIntegrationController extends OCSController {
@ -134,27 +133,15 @@ class FilesIntegrationController extends OCSController {
}
$share = $this->util->getAnyPublicShareOfFileOwnedByUserOrAnyDirectShareOfFileAccessibleByUser($fileId, $currentUser->getUID());
$groupFolder = null;
if (!$share) {
$groupFolder = $this->util->getGroupFolderNode($fileId, $currentUser->getUID());
if (!$groupFolder) {
throw new OCSNotFoundException($this->l->t('File is not shared, or shared but not with the user'));
}
$node = $this->util->getAnyNodeOfFileAccessibleByUser($fileId, $currentUser->getUID());
if ($node === null) {
throw new OCSNotFoundException($this->l->t('File is not shared, or shared but not with the user'));
}
try {
$room = $this->manager->getRoomByObject('file', $fileId);
} catch (RoomNotFoundException $e) {
if ($share) {
try {
$name = $this->getFileName($share, $fileId);
} catch (NotFoundException $e) {
throw new OCSNotFoundException($this->l->t('File is not shared, or shared but not with the user'));
}
} else {
$name = $groupFolder->getName();
}
$name = $node->getName();
$name = $this->roomService->prepareConversationName($name);
$room = $this->roomService->createConversation(Room::PUBLIC_CALL, $name, null, 'file', $fileId);
}
@ -244,32 +231,4 @@ class FilesIntegrationController extends OCSController {
'userDisplayName' => $currentUserDisplayName,
]);
}
/**
* Returns the name of the file in the share.
*
* If the given share itself is a file its name is returned; otherwise the
* file is looked for in the given shared folder and its name is returned.
*
* @param IShare $share
* @param string $fileId
* @return string
* @throws NotFoundException
*/
private function getFileName(IShare $share, string $fileId): string {
$node = $share->getNode();
if ($node->getType() === FileInfo::TYPE_FILE) {
return $node->getName();
}
$fileById = $node->getById($fileId);
if (empty($fileById)) {
throw new NotFoundException('File not found in share');
}
$file = array_shift($fileById);
return $file->getName();
}
}

11
lib/Files/Listener.php

@ -126,12 +126,9 @@ class Listener {
return;
}
$share = $this->util->getAnyPublicShareOfFileOwnedByUserOrAnyDirectShareOfFileAccessibleByUser($room->getObjectId(), $userId);
if (!$share) {
$groupFolder = $this->util->getGroupFolderNode($room->getObjectId(), $userId);
if (!$groupFolder) {
throw new UnauthorizedException('User does not have access to the file');
}
$node = $this->util->getAnyNodeOfFileAccessibleByUser($room->getObjectId(), $userId);
if ($node === null) {
throw new UnauthorizedException('User does not have access to the file');
}
}
@ -152,7 +149,7 @@ class Listener {
return;
}
if (!$this->util->getAnyPublicShareOfFileOwnedByUserOrAnyDirectShareOfFileAccessibleByUser($room->getObjectId(), $userId)) {
if ($this->util->getAnyNodeOfFileAccessibleByUser($room->getObjectId(), $userId) === null) {
return;
}

49
lib/Files/Util.php

@ -107,59 +107,26 @@ class Util {
}
/**
* Returns any share of the file that is public and owned by the user, or
* Returns any node of the file that is public and owned by the user, or
* that the user has direct access to.
*
* A public share is one accessible by any user, including guests, like a
* share by link. Note that only a share of the file itself is taken into
* account; if an ancestor folder is shared publicly that share will not be
* returned.
*
* A user has direct access to a share and, thus, to a file, if she received
* the file through a user, group, circle or room share (but not through a
* public link, for example), or if she is the owner of such a share.
* Note that this includes too files received as a descendant of a folder
* that meets the above conditions.
*
* Only files are taken into account; folders are ignored.
*
* @param string $fileId
* @param string $userId
* @return IShare|null
* @return Node|null
*/
public function getAnyPublicShareOfFileOwnedByUserOrAnyDirectShareOfFileAccessibleByUser(string $fileId, string $userId): ?IShare {
public function getAnyNodeOfFileAccessibleByUser(string $fileId, string $userId): ?Node {
$userFolder = $this->rootFolder->getUserFolder($userId);
$nodes = $userFolder->getById($fileId);
if (empty($nodes)) {
return null;
}
$nodes = $userFolder->getById((int) $fileId);
$nodes = array_filter($nodes, function ($node) {
$nodes = array_filter($nodes, static function (Node $node) {
return $node->getType() === FileInfo::TYPE_FILE;
});
if (!empty($nodes)) {
$share = $this->getAnyPublicShareOfNodeOwnedByUser($nodes[0], $userId);
if ($share) {
return $share;
}
}
while (!empty($nodes)) {
$node = array_pop($nodes);
$share = $this->getAnyDirectShareOfNodeAccessibleByUser($node, $userId);
if ($share) {
return $share;
}
try {
$nodes[] = $node->getParent();
} catch (NotFoundException $e) {
}
if (empty($nodes)) {
return null;
}
return null;
return array_shift($nodes);
}
/**

Loading…
Cancel
Save