@@ -34,182 +34,182 @@ |
||
| 34 | 34 | */ |
| 35 | 35 | class SharesPlugin extends \Sabre\DAV\ServerPlugin { |
| 36 | 36 | |
| 37 | - const NS_OWNCLOUD = 'http://owncloud.org/ns'; |
|
| 38 | - const SHARETYPES_PROPERTYNAME = '{http://owncloud.org/ns}share-types'; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Reference to main server object |
|
| 42 | - * |
|
| 43 | - * @var \Sabre\DAV\Server |
|
| 44 | - */ |
|
| 45 | - private $server; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * @var \OCP\Share\IManager |
|
| 49 | - */ |
|
| 50 | - private $shareManager; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * @var \Sabre\DAV\Tree |
|
| 54 | - */ |
|
| 55 | - private $tree; |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * @var string |
|
| 59 | - */ |
|
| 60 | - private $userId; |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * @var \OCP\Files\Folder |
|
| 64 | - */ |
|
| 65 | - private $userFolder; |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * @var IShare[] |
|
| 69 | - */ |
|
| 70 | - private $cachedShareTypes; |
|
| 71 | - |
|
| 72 | - private $cachedFolders = []; |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * @param \Sabre\DAV\Tree $tree tree |
|
| 76 | - * @param IUserSession $userSession user session |
|
| 77 | - * @param \OCP\Files\Folder $userFolder user home folder |
|
| 78 | - * @param \OCP\Share\IManager $shareManager share manager |
|
| 79 | - */ |
|
| 80 | - public function __construct( |
|
| 81 | - \Sabre\DAV\Tree $tree, |
|
| 82 | - IUserSession $userSession, |
|
| 83 | - \OCP\Files\Folder $userFolder, |
|
| 84 | - \OCP\Share\IManager $shareManager |
|
| 85 | - ) { |
|
| 86 | - $this->tree = $tree; |
|
| 87 | - $this->shareManager = $shareManager; |
|
| 88 | - $this->userFolder = $userFolder; |
|
| 89 | - $this->userId = $userSession->getUser()->getUID(); |
|
| 90 | - $this->cachedShareTypes = []; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * This initializes the plugin. |
|
| 95 | - * |
|
| 96 | - * This function is called by \Sabre\DAV\Server, after |
|
| 97 | - * addPlugin is called. |
|
| 98 | - * |
|
| 99 | - * This method should set up the required event subscriptions. |
|
| 100 | - * |
|
| 101 | - * @param \Sabre\DAV\Server $server |
|
| 102 | - */ |
|
| 103 | - public function initialize(\Sabre\DAV\Server $server) { |
|
| 104 | - $server->xml->namespacesMap[self::NS_OWNCLOUD] = 'oc'; |
|
| 105 | - $server->xml->elementMap[self::SHARETYPES_PROPERTYNAME] = ShareTypeList::class; |
|
| 106 | - $server->protectedProperties[] = self::SHARETYPES_PROPERTYNAME; |
|
| 107 | - |
|
| 108 | - $this->server = $server; |
|
| 109 | - $this->server->on('propFind', array($this, 'handleGetProperties')); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * Return a list of share types for outgoing shares |
|
| 114 | - * |
|
| 115 | - * @param \OCP\Files\Node $node file node |
|
| 116 | - * |
|
| 117 | - * @return int[] array of share types |
|
| 118 | - */ |
|
| 119 | - private function getShareTypes(\OCP\Files\Node $node) { |
|
| 120 | - $shareTypes = []; |
|
| 121 | - $requestedShareTypes = [ |
|
| 122 | - \OCP\Share::SHARE_TYPE_USER, |
|
| 123 | - \OCP\Share::SHARE_TYPE_GROUP, |
|
| 124 | - \OCP\Share::SHARE_TYPE_LINK, |
|
| 125 | - \OCP\Share::SHARE_TYPE_REMOTE, |
|
| 126 | - \OCP\Share::SHARE_TYPE_EMAIL, |
|
| 127 | - \OCP\Share::SHARE_TYPE_ROOM, |
|
| 128 | - ]; |
|
| 129 | - foreach ($requestedShareTypes as $requestedShareType) { |
|
| 130 | - // one of each type is enough to find out about the types |
|
| 131 | - $shares = $this->shareManager->getSharesBy( |
|
| 132 | - $this->userId, |
|
| 133 | - $requestedShareType, |
|
| 134 | - $node, |
|
| 135 | - false, |
|
| 136 | - 1 |
|
| 137 | - ); |
|
| 138 | - if (!empty($shares)) { |
|
| 139 | - $shareTypes[] = $requestedShareType; |
|
| 140 | - } |
|
| 141 | - } |
|
| 142 | - return $shareTypes; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - private function getSharesTypesInFolder(\OCP\Files\Folder $node) { |
|
| 146 | - $shares = $this->shareManager->getSharesInFolder( |
|
| 147 | - $this->userId, |
|
| 148 | - $node, |
|
| 149 | - true |
|
| 150 | - ); |
|
| 151 | - |
|
| 152 | - $shareTypesByFileId = []; |
|
| 153 | - |
|
| 154 | - foreach($shares as $fileId => $sharesForFile) { |
|
| 155 | - $types = array_map(function(IShare $share) { |
|
| 156 | - return $share->getShareType(); |
|
| 157 | - }, $sharesForFile); |
|
| 158 | - $types = array_unique($types); |
|
| 159 | - sort($types); |
|
| 160 | - $shareTypesByFileId[$fileId] = $types; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - return $shareTypesByFileId; |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * Adds shares to propfind response |
|
| 168 | - * |
|
| 169 | - * @param PropFind $propFind propfind object |
|
| 170 | - * @param \Sabre\DAV\INode $sabreNode sabre node |
|
| 171 | - */ |
|
| 172 | - public function handleGetProperties( |
|
| 173 | - PropFind $propFind, |
|
| 174 | - \Sabre\DAV\INode $sabreNode |
|
| 175 | - ) { |
|
| 176 | - if (!($sabreNode instanceof \OCA\DAV\Connector\Sabre\Node)) { |
|
| 177 | - return; |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - // need prefetch ? |
|
| 181 | - if ($sabreNode instanceof \OCA\DAV\Connector\Sabre\Directory |
|
| 182 | - && $propFind->getDepth() !== 0 |
|
| 183 | - && !is_null($propFind->getStatus(self::SHARETYPES_PROPERTYNAME)) |
|
| 184 | - ) { |
|
| 185 | - $folderNode = $this->userFolder->get($sabreNode->getPath()); |
|
| 186 | - |
|
| 187 | - $childShares = $this->getSharesTypesInFolder($folderNode); |
|
| 188 | - $this->cachedFolders[] = $sabreNode->getPath(); |
|
| 189 | - $this->cachedShareTypes[$folderNode->getId()] = $this->getShareTypes($folderNode); |
|
| 190 | - foreach ($childShares as $id => $shares) { |
|
| 191 | - $this->cachedShareTypes[$id] = $shares; |
|
| 192 | - } |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - $propFind->handle(self::SHARETYPES_PROPERTYNAME, function () use ($sabreNode) { |
|
| 196 | - if (isset($this->cachedShareTypes[$sabreNode->getId()])) { |
|
| 197 | - $shareTypes = $this->cachedShareTypes[$sabreNode->getId()]; |
|
| 198 | - } else { |
|
| 199 | - list($parentPath,) = \Sabre\Uri\split($sabreNode->getPath()); |
|
| 200 | - if ($parentPath === '') { |
|
| 201 | - $parentPath = '/'; |
|
| 202 | - } |
|
| 203 | - // if we already cached the folder this file is in we know there are no shares for this file |
|
| 204 | - if (array_search($parentPath, $this->cachedFolders) === false) { |
|
| 205 | - $node = $this->userFolder->get($sabreNode->getPath()); |
|
| 206 | - $shareTypes = $this->getShareTypes($node); |
|
| 207 | - } else { |
|
| 208 | - return []; |
|
| 209 | - } |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - return new ShareTypeList($shareTypes); |
|
| 213 | - }); |
|
| 214 | - } |
|
| 37 | + const NS_OWNCLOUD = 'http://owncloud.org/ns'; |
|
| 38 | + const SHARETYPES_PROPERTYNAME = '{http://owncloud.org/ns}share-types'; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Reference to main server object |
|
| 42 | + * |
|
| 43 | + * @var \Sabre\DAV\Server |
|
| 44 | + */ |
|
| 45 | + private $server; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * @var \OCP\Share\IManager |
|
| 49 | + */ |
|
| 50 | + private $shareManager; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * @var \Sabre\DAV\Tree |
|
| 54 | + */ |
|
| 55 | + private $tree; |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * @var string |
|
| 59 | + */ |
|
| 60 | + private $userId; |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * @var \OCP\Files\Folder |
|
| 64 | + */ |
|
| 65 | + private $userFolder; |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * @var IShare[] |
|
| 69 | + */ |
|
| 70 | + private $cachedShareTypes; |
|
| 71 | + |
|
| 72 | + private $cachedFolders = []; |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * @param \Sabre\DAV\Tree $tree tree |
|
| 76 | + * @param IUserSession $userSession user session |
|
| 77 | + * @param \OCP\Files\Folder $userFolder user home folder |
|
| 78 | + * @param \OCP\Share\IManager $shareManager share manager |
|
| 79 | + */ |
|
| 80 | + public function __construct( |
|
| 81 | + \Sabre\DAV\Tree $tree, |
|
| 82 | + IUserSession $userSession, |
|
| 83 | + \OCP\Files\Folder $userFolder, |
|
| 84 | + \OCP\Share\IManager $shareManager |
|
| 85 | + ) { |
|
| 86 | + $this->tree = $tree; |
|
| 87 | + $this->shareManager = $shareManager; |
|
| 88 | + $this->userFolder = $userFolder; |
|
| 89 | + $this->userId = $userSession->getUser()->getUID(); |
|
| 90 | + $this->cachedShareTypes = []; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * This initializes the plugin. |
|
| 95 | + * |
|
| 96 | + * This function is called by \Sabre\DAV\Server, after |
|
| 97 | + * addPlugin is called. |
|
| 98 | + * |
|
| 99 | + * This method should set up the required event subscriptions. |
|
| 100 | + * |
|
| 101 | + * @param \Sabre\DAV\Server $server |
|
| 102 | + */ |
|
| 103 | + public function initialize(\Sabre\DAV\Server $server) { |
|
| 104 | + $server->xml->namespacesMap[self::NS_OWNCLOUD] = 'oc'; |
|
| 105 | + $server->xml->elementMap[self::SHARETYPES_PROPERTYNAME] = ShareTypeList::class; |
|
| 106 | + $server->protectedProperties[] = self::SHARETYPES_PROPERTYNAME; |
|
| 107 | + |
|
| 108 | + $this->server = $server; |
|
| 109 | + $this->server->on('propFind', array($this, 'handleGetProperties')); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * Return a list of share types for outgoing shares |
|
| 114 | + * |
|
| 115 | + * @param \OCP\Files\Node $node file node |
|
| 116 | + * |
|
| 117 | + * @return int[] array of share types |
|
| 118 | + */ |
|
| 119 | + private function getShareTypes(\OCP\Files\Node $node) { |
|
| 120 | + $shareTypes = []; |
|
| 121 | + $requestedShareTypes = [ |
|
| 122 | + \OCP\Share::SHARE_TYPE_USER, |
|
| 123 | + \OCP\Share::SHARE_TYPE_GROUP, |
|
| 124 | + \OCP\Share::SHARE_TYPE_LINK, |
|
| 125 | + \OCP\Share::SHARE_TYPE_REMOTE, |
|
| 126 | + \OCP\Share::SHARE_TYPE_EMAIL, |
|
| 127 | + \OCP\Share::SHARE_TYPE_ROOM, |
|
| 128 | + ]; |
|
| 129 | + foreach ($requestedShareTypes as $requestedShareType) { |
|
| 130 | + // one of each type is enough to find out about the types |
|
| 131 | + $shares = $this->shareManager->getSharesBy( |
|
| 132 | + $this->userId, |
|
| 133 | + $requestedShareType, |
|
| 134 | + $node, |
|
| 135 | + false, |
|
| 136 | + 1 |
|
| 137 | + ); |
|
| 138 | + if (!empty($shares)) { |
|
| 139 | + $shareTypes[] = $requestedShareType; |
|
| 140 | + } |
|
| 141 | + } |
|
| 142 | + return $shareTypes; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + private function getSharesTypesInFolder(\OCP\Files\Folder $node) { |
|
| 146 | + $shares = $this->shareManager->getSharesInFolder( |
|
| 147 | + $this->userId, |
|
| 148 | + $node, |
|
| 149 | + true |
|
| 150 | + ); |
|
| 151 | + |
|
| 152 | + $shareTypesByFileId = []; |
|
| 153 | + |
|
| 154 | + foreach($shares as $fileId => $sharesForFile) { |
|
| 155 | + $types = array_map(function(IShare $share) { |
|
| 156 | + return $share->getShareType(); |
|
| 157 | + }, $sharesForFile); |
|
| 158 | + $types = array_unique($types); |
|
| 159 | + sort($types); |
|
| 160 | + $shareTypesByFileId[$fileId] = $types; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + return $shareTypesByFileId; |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * Adds shares to propfind response |
|
| 168 | + * |
|
| 169 | + * @param PropFind $propFind propfind object |
|
| 170 | + * @param \Sabre\DAV\INode $sabreNode sabre node |
|
| 171 | + */ |
|
| 172 | + public function handleGetProperties( |
|
| 173 | + PropFind $propFind, |
|
| 174 | + \Sabre\DAV\INode $sabreNode |
|
| 175 | + ) { |
|
| 176 | + if (!($sabreNode instanceof \OCA\DAV\Connector\Sabre\Node)) { |
|
| 177 | + return; |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + // need prefetch ? |
|
| 181 | + if ($sabreNode instanceof \OCA\DAV\Connector\Sabre\Directory |
|
| 182 | + && $propFind->getDepth() !== 0 |
|
| 183 | + && !is_null($propFind->getStatus(self::SHARETYPES_PROPERTYNAME)) |
|
| 184 | + ) { |
|
| 185 | + $folderNode = $this->userFolder->get($sabreNode->getPath()); |
|
| 186 | + |
|
| 187 | + $childShares = $this->getSharesTypesInFolder($folderNode); |
|
| 188 | + $this->cachedFolders[] = $sabreNode->getPath(); |
|
| 189 | + $this->cachedShareTypes[$folderNode->getId()] = $this->getShareTypes($folderNode); |
|
| 190 | + foreach ($childShares as $id => $shares) { |
|
| 191 | + $this->cachedShareTypes[$id] = $shares; |
|
| 192 | + } |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + $propFind->handle(self::SHARETYPES_PROPERTYNAME, function () use ($sabreNode) { |
|
| 196 | + if (isset($this->cachedShareTypes[$sabreNode->getId()])) { |
|
| 197 | + $shareTypes = $this->cachedShareTypes[$sabreNode->getId()]; |
|
| 198 | + } else { |
|
| 199 | + list($parentPath,) = \Sabre\Uri\split($sabreNode->getPath()); |
|
| 200 | + if ($parentPath === '') { |
|
| 201 | + $parentPath = '/'; |
|
| 202 | + } |
|
| 203 | + // if we already cached the folder this file is in we know there are no shares for this file |
|
| 204 | + if (array_search($parentPath, $this->cachedFolders) === false) { |
|
| 205 | + $node = $this->userFolder->get($sabreNode->getPath()); |
|
| 206 | + $shareTypes = $this->getShareTypes($node); |
|
| 207 | + } else { |
|
| 208 | + return []; |
|
| 209 | + } |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + return new ShareTypeList($shareTypes); |
|
| 213 | + }); |
|
| 214 | + } |
|
| 215 | 215 | } |
@@ -27,82 +27,82 @@ |
||
| 27 | 27 | |
| 28 | 28 | class Updater { |
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * @param array $params |
|
| 32 | - */ |
|
| 33 | - static public function renameHook($params) { |
|
| 34 | - self::renameChildren($params['oldpath'], $params['newpath']); |
|
| 35 | - self::moveShareToShare($params['newpath']); |
|
| 36 | - } |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * Fix for https://github.com/owncloud/core/issues/20769 |
|
| 40 | - * |
|
| 41 | - * The owner is allowed to move their files (if they are shared) into a receiving folder |
|
| 42 | - * In this case we need to update the parent of the moved share. Since they are |
|
| 43 | - * effectively handing over ownership of the file the rest of the code needs to know |
|
| 44 | - * they need to build up the reshare tree. |
|
| 45 | - * |
|
| 46 | - * @param string $path |
|
| 47 | - */ |
|
| 48 | - static private function moveShareToShare($path) { |
|
| 49 | - $userFolder = \OC::$server->getUserFolder(); |
|
| 50 | - |
|
| 51 | - // If the user folder can't be constructed (e.g. link share) just return. |
|
| 52 | - if ($userFolder === null) { |
|
| 53 | - return; |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - $src = $userFolder->get($path); |
|
| 57 | - |
|
| 58 | - $shareManager = \OC::$server->getShareManager(); |
|
| 59 | - |
|
| 60 | - $shares = $shareManager->getSharesBy($userFolder->getOwner()->getUID(), \OCP\Share::SHARE_TYPE_USER, $src, false, -1); |
|
| 61 | - $shares = array_merge($shares, $shareManager->getSharesBy($userFolder->getOwner()->getUID(), \OCP\Share::SHARE_TYPE_GROUP, $src, false, -1)); |
|
| 62 | - $shares = array_merge($shares, $shareManager->getSharesBy($userFolder->getOwner()->getUID(), \OCP\Share::SHARE_TYPE_ROOM, $src, false, -1)); |
|
| 63 | - |
|
| 64 | - // If the path we move is not a share we don't care |
|
| 65 | - if (empty($shares)) { |
|
| 66 | - return; |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - // Check if the destination is inside a share |
|
| 70 | - $mountManager = \OC::$server->getMountManager(); |
|
| 71 | - $dstMount = $mountManager->find($src->getPath()); |
|
| 72 | - if (!($dstMount instanceof \OCA\Files_Sharing\SharedMount)) { |
|
| 73 | - return; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - $newOwner = $dstMount->getShare()->getShareOwner(); |
|
| 77 | - |
|
| 78 | - //Ownership is moved over |
|
| 79 | - foreach ($shares as $share) { |
|
| 80 | - /** @var \OCP\Share\IShare $share */ |
|
| 81 | - $share->setShareOwner($newOwner); |
|
| 82 | - $shareManager->updateShare($share); |
|
| 83 | - } |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * rename mount point from the children if the parent was renamed |
|
| 88 | - * |
|
| 89 | - * @param string $oldPath old path relative to data/user/files |
|
| 90 | - * @param string $newPath new path relative to data/user/files |
|
| 91 | - */ |
|
| 92 | - static private function renameChildren($oldPath, $newPath) { |
|
| 93 | - |
|
| 94 | - $absNewPath = \OC\Files\Filesystem::normalizePath('/' . \OCP\User::getUser() . '/files/' . $newPath); |
|
| 95 | - $absOldPath = \OC\Files\Filesystem::normalizePath('/' . \OCP\User::getUser() . '/files/' . $oldPath); |
|
| 96 | - |
|
| 97 | - $mountManager = \OC\Files\Filesystem::getMountManager(); |
|
| 98 | - $mountedShares = $mountManager->findIn('/' . \OCP\User::getUser() . '/files/' . $oldPath); |
|
| 99 | - foreach ($mountedShares as $mount) { |
|
| 100 | - if ($mount->getStorage()->instanceOfStorage(ISharedStorage::class)) { |
|
| 101 | - $mountPoint = $mount->getMountPoint(); |
|
| 102 | - $target = str_replace($absOldPath, $absNewPath, $mountPoint); |
|
| 103 | - $mount->moveMount($target); |
|
| 104 | - } |
|
| 105 | - } |
|
| 106 | - } |
|
| 30 | + /** |
|
| 31 | + * @param array $params |
|
| 32 | + */ |
|
| 33 | + static public function renameHook($params) { |
|
| 34 | + self::renameChildren($params['oldpath'], $params['newpath']); |
|
| 35 | + self::moveShareToShare($params['newpath']); |
|
| 36 | + } |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Fix for https://github.com/owncloud/core/issues/20769 |
|
| 40 | + * |
|
| 41 | + * The owner is allowed to move their files (if they are shared) into a receiving folder |
|
| 42 | + * In this case we need to update the parent of the moved share. Since they are |
|
| 43 | + * effectively handing over ownership of the file the rest of the code needs to know |
|
| 44 | + * they need to build up the reshare tree. |
|
| 45 | + * |
|
| 46 | + * @param string $path |
|
| 47 | + */ |
|
| 48 | + static private function moveShareToShare($path) { |
|
| 49 | + $userFolder = \OC::$server->getUserFolder(); |
|
| 50 | + |
|
| 51 | + // If the user folder can't be constructed (e.g. link share) just return. |
|
| 52 | + if ($userFolder === null) { |
|
| 53 | + return; |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + $src = $userFolder->get($path); |
|
| 57 | + |
|
| 58 | + $shareManager = \OC::$server->getShareManager(); |
|
| 59 | + |
|
| 60 | + $shares = $shareManager->getSharesBy($userFolder->getOwner()->getUID(), \OCP\Share::SHARE_TYPE_USER, $src, false, -1); |
|
| 61 | + $shares = array_merge($shares, $shareManager->getSharesBy($userFolder->getOwner()->getUID(), \OCP\Share::SHARE_TYPE_GROUP, $src, false, -1)); |
|
| 62 | + $shares = array_merge($shares, $shareManager->getSharesBy($userFolder->getOwner()->getUID(), \OCP\Share::SHARE_TYPE_ROOM, $src, false, -1)); |
|
| 63 | + |
|
| 64 | + // If the path we move is not a share we don't care |
|
| 65 | + if (empty($shares)) { |
|
| 66 | + return; |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + // Check if the destination is inside a share |
|
| 70 | + $mountManager = \OC::$server->getMountManager(); |
|
| 71 | + $dstMount = $mountManager->find($src->getPath()); |
|
| 72 | + if (!($dstMount instanceof \OCA\Files_Sharing\SharedMount)) { |
|
| 73 | + return; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + $newOwner = $dstMount->getShare()->getShareOwner(); |
|
| 77 | + |
|
| 78 | + //Ownership is moved over |
|
| 79 | + foreach ($shares as $share) { |
|
| 80 | + /** @var \OCP\Share\IShare $share */ |
|
| 81 | + $share->setShareOwner($newOwner); |
|
| 82 | + $shareManager->updateShare($share); |
|
| 83 | + } |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * rename mount point from the children if the parent was renamed |
|
| 88 | + * |
|
| 89 | + * @param string $oldPath old path relative to data/user/files |
|
| 90 | + * @param string $newPath new path relative to data/user/files |
|
| 91 | + */ |
|
| 92 | + static private function renameChildren($oldPath, $newPath) { |
|
| 93 | + |
|
| 94 | + $absNewPath = \OC\Files\Filesystem::normalizePath('/' . \OCP\User::getUser() . '/files/' . $newPath); |
|
| 95 | + $absOldPath = \OC\Files\Filesystem::normalizePath('/' . \OCP\User::getUser() . '/files/' . $oldPath); |
|
| 96 | + |
|
| 97 | + $mountManager = \OC\Files\Filesystem::getMountManager(); |
|
| 98 | + $mountedShares = $mountManager->findIn('/' . \OCP\User::getUser() . '/files/' . $oldPath); |
|
| 99 | + foreach ($mountedShares as $mount) { |
|
| 100 | + if ($mount->getStorage()->instanceOfStorage(ISharedStorage::class)) { |
|
| 101 | + $mountPoint = $mount->getMountPoint(); |
|
| 102 | + $target = str_replace($absOldPath, $absNewPath, $mountPoint); |
|
| 103 | + $mount->moveMount($target); |
|
| 104 | + } |
|
| 105 | + } |
|
| 106 | + } |
|
| 107 | 107 | |
| 108 | 108 | } |
@@ -45,170 +45,170 @@ |
||
| 45 | 45 | |
| 46 | 46 | class DeletedShareAPIController extends OCSController { |
| 47 | 47 | |
| 48 | - /** @var ShareManager */ |
|
| 49 | - private $shareManager; |
|
| 50 | - |
|
| 51 | - /** @var string */ |
|
| 52 | - private $userId; |
|
| 53 | - |
|
| 54 | - /** @var IUserManager */ |
|
| 55 | - private $userManager; |
|
| 56 | - |
|
| 57 | - /** @var IGroupManager */ |
|
| 58 | - private $groupManager; |
|
| 59 | - |
|
| 60 | - /** @var IRootFolder */ |
|
| 61 | - private $rootFolder; |
|
| 62 | - |
|
| 63 | - /** @var IAppManager */ |
|
| 64 | - private $appManager; |
|
| 65 | - |
|
| 66 | - /** @var IServerContainer */ |
|
| 67 | - private $serverContainer; |
|
| 68 | - |
|
| 69 | - public function __construct(string $appName, |
|
| 70 | - IRequest $request, |
|
| 71 | - ShareManager $shareManager, |
|
| 72 | - string $UserId, |
|
| 73 | - IUserManager $userManager, |
|
| 74 | - IGroupManager $groupManager, |
|
| 75 | - IRootFolder $rootFolder, |
|
| 76 | - IAppManager $appManager, |
|
| 77 | - IServerContainer $serverContainer) { |
|
| 78 | - parent::__construct($appName, $request); |
|
| 79 | - |
|
| 80 | - $this->shareManager = $shareManager; |
|
| 81 | - $this->userId = $UserId; |
|
| 82 | - $this->userManager = $userManager; |
|
| 83 | - $this->groupManager = $groupManager; |
|
| 84 | - $this->rootFolder = $rootFolder; |
|
| 85 | - $this->appManager = $appManager; |
|
| 86 | - $this->serverContainer = $serverContainer; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * @suppress PhanUndeclaredClassMethod |
|
| 91 | - */ |
|
| 92 | - private function formatShare(IShare $share): array { |
|
| 93 | - |
|
| 94 | - $result = [ |
|
| 95 | - 'id' => $share->getFullId(), |
|
| 96 | - 'share_type' => $share->getShareType(), |
|
| 97 | - 'uid_owner' => $share->getSharedBy(), |
|
| 98 | - 'displayname_owner' => $this->userManager->get($share->getSharedBy())->getDisplayName(), |
|
| 99 | - 'permissions' => 0, |
|
| 100 | - 'stime' => $share->getShareTime()->getTimestamp(), |
|
| 101 | - 'parent' => null, |
|
| 102 | - 'expiration' => null, |
|
| 103 | - 'token' => null, |
|
| 104 | - 'uid_file_owner' => $share->getShareOwner(), |
|
| 105 | - 'displayname_file_owner' => $this->userManager->get($share->getShareOwner())->getDisplayName(), |
|
| 106 | - 'path' => $share->getTarget(), |
|
| 107 | - ]; |
|
| 108 | - $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy()); |
|
| 109 | - $nodes = $userFolder->getById($share->getNodeId()); |
|
| 110 | - if (empty($nodes)) { |
|
| 111 | - // fallback to guessing the path |
|
| 112 | - $node = $userFolder->get($share->getTarget()); |
|
| 113 | - if ($node === null || $share->getTarget() === '') { |
|
| 114 | - throw new NotFoundException(); |
|
| 115 | - } |
|
| 116 | - } else { |
|
| 117 | - $node = $nodes[0]; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - $result['path'] = $userFolder->getRelativePath($node->getPath()); |
|
| 121 | - if ($node instanceOf \OCP\Files\Folder) { |
|
| 122 | - $result['item_type'] = 'folder'; |
|
| 123 | - } else { |
|
| 124 | - $result['item_type'] = 'file'; |
|
| 125 | - } |
|
| 126 | - $result['mimetype'] = $node->getMimetype(); |
|
| 127 | - $result['storage_id'] = $node->getStorage()->getId(); |
|
| 128 | - $result['storage'] = $node->getStorage()->getCache()->getNumericStorageId(); |
|
| 129 | - $result['item_source'] = $node->getId(); |
|
| 130 | - $result['file_source'] = $node->getId(); |
|
| 131 | - $result['file_parent'] = $node->getParent()->getId(); |
|
| 132 | - $result['file_target'] = $share->getTarget(); |
|
| 133 | - |
|
| 134 | - $expiration = $share->getExpirationDate(); |
|
| 135 | - if ($expiration !== null) { |
|
| 136 | - $result['expiration'] = $expiration->format('Y-m-d 00:00:00'); |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 140 | - $group = $this->groupManager->get($share->getSharedWith()); |
|
| 141 | - $result['share_with'] = $share->getSharedWith(); |
|
| 142 | - $result['share_with_displayname'] = $group !== null ? $group->getDisplayName() : $share->getSharedWith(); |
|
| 143 | - } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_ROOM) { |
|
| 144 | - $result['share_with'] = $share->getSharedWith(); |
|
| 145 | - $result['share_with_displayname'] = ''; |
|
| 146 | - |
|
| 147 | - try { |
|
| 148 | - $result = array_merge($result, $this->getRoomShareHelper()->formatShare($share)); |
|
| 149 | - } catch (QueryException $e) { |
|
| 150 | - } |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - return $result; |
|
| 154 | - |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * @NoAdminRequired |
|
| 159 | - */ |
|
| 160 | - public function index(): DataResponse { |
|
| 161 | - $groupShares = $this->shareManager->getDeletedSharedWith($this->userId, \OCP\Share::SHARE_TYPE_GROUP, null, -1, 0); |
|
| 162 | - $roomShares = $this->shareManager->getDeletedSharedWith($this->userId, \OCP\Share::SHARE_TYPE_ROOM, null, -1, 0); |
|
| 163 | - |
|
| 164 | - $shares = array_merge($groupShares, $roomShares); |
|
| 165 | - |
|
| 166 | - $shares = array_map(function (IShare $share) { |
|
| 167 | - return $this->formatShare($share); |
|
| 168 | - }, $shares); |
|
| 169 | - |
|
| 170 | - return new DataResponse($shares); |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - /** |
|
| 174 | - * @NoAdminRequired |
|
| 175 | - * |
|
| 176 | - * @throws OCSException |
|
| 177 | - */ |
|
| 178 | - public function undelete(string $id): DataResponse { |
|
| 179 | - try { |
|
| 180 | - $share = $this->shareManager->getShareById($id, $this->userId); |
|
| 181 | - } catch (ShareNotFound $e) { |
|
| 182 | - throw new OCSNotFoundException('Share not found'); |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - if ($share->getPermissions() !== 0) { |
|
| 186 | - throw new OCSNotFoundException('No deleted share found'); |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - try { |
|
| 190 | - $this->shareManager->restoreShare($share, $this->userId); |
|
| 191 | - } catch (GenericShareException $e) { |
|
| 192 | - throw new OCSException('Something went wrong'); |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - return new DataResponse([]); |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - /** |
|
| 199 | - * Returns the helper of DeletedShareAPIController for room shares. |
|
| 200 | - * |
|
| 201 | - * If the Talk application is not enabled or the helper is not available |
|
| 202 | - * a QueryException is thrown instead. |
|
| 203 | - * |
|
| 204 | - * @return \OCA\Spreed\Share\Helper\DeletedShareAPIController |
|
| 205 | - * @throws QueryException |
|
| 206 | - */ |
|
| 207 | - private function getRoomShareHelper() { |
|
| 208 | - if (!$this->appManager->isEnabledForUser('spreed')) { |
|
| 209 | - throw new QueryException(); |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - return $this->serverContainer->query('\OCA\Spreed\Share\Helper\DeletedShareAPIController'); |
|
| 213 | - } |
|
| 48 | + /** @var ShareManager */ |
|
| 49 | + private $shareManager; |
|
| 50 | + |
|
| 51 | + /** @var string */ |
|
| 52 | + private $userId; |
|
| 53 | + |
|
| 54 | + /** @var IUserManager */ |
|
| 55 | + private $userManager; |
|
| 56 | + |
|
| 57 | + /** @var IGroupManager */ |
|
| 58 | + private $groupManager; |
|
| 59 | + |
|
| 60 | + /** @var IRootFolder */ |
|
| 61 | + private $rootFolder; |
|
| 62 | + |
|
| 63 | + /** @var IAppManager */ |
|
| 64 | + private $appManager; |
|
| 65 | + |
|
| 66 | + /** @var IServerContainer */ |
|
| 67 | + private $serverContainer; |
|
| 68 | + |
|
| 69 | + public function __construct(string $appName, |
|
| 70 | + IRequest $request, |
|
| 71 | + ShareManager $shareManager, |
|
| 72 | + string $UserId, |
|
| 73 | + IUserManager $userManager, |
|
| 74 | + IGroupManager $groupManager, |
|
| 75 | + IRootFolder $rootFolder, |
|
| 76 | + IAppManager $appManager, |
|
| 77 | + IServerContainer $serverContainer) { |
|
| 78 | + parent::__construct($appName, $request); |
|
| 79 | + |
|
| 80 | + $this->shareManager = $shareManager; |
|
| 81 | + $this->userId = $UserId; |
|
| 82 | + $this->userManager = $userManager; |
|
| 83 | + $this->groupManager = $groupManager; |
|
| 84 | + $this->rootFolder = $rootFolder; |
|
| 85 | + $this->appManager = $appManager; |
|
| 86 | + $this->serverContainer = $serverContainer; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * @suppress PhanUndeclaredClassMethod |
|
| 91 | + */ |
|
| 92 | + private function formatShare(IShare $share): array { |
|
| 93 | + |
|
| 94 | + $result = [ |
|
| 95 | + 'id' => $share->getFullId(), |
|
| 96 | + 'share_type' => $share->getShareType(), |
|
| 97 | + 'uid_owner' => $share->getSharedBy(), |
|
| 98 | + 'displayname_owner' => $this->userManager->get($share->getSharedBy())->getDisplayName(), |
|
| 99 | + 'permissions' => 0, |
|
| 100 | + 'stime' => $share->getShareTime()->getTimestamp(), |
|
| 101 | + 'parent' => null, |
|
| 102 | + 'expiration' => null, |
|
| 103 | + 'token' => null, |
|
| 104 | + 'uid_file_owner' => $share->getShareOwner(), |
|
| 105 | + 'displayname_file_owner' => $this->userManager->get($share->getShareOwner())->getDisplayName(), |
|
| 106 | + 'path' => $share->getTarget(), |
|
| 107 | + ]; |
|
| 108 | + $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy()); |
|
| 109 | + $nodes = $userFolder->getById($share->getNodeId()); |
|
| 110 | + if (empty($nodes)) { |
|
| 111 | + // fallback to guessing the path |
|
| 112 | + $node = $userFolder->get($share->getTarget()); |
|
| 113 | + if ($node === null || $share->getTarget() === '') { |
|
| 114 | + throw new NotFoundException(); |
|
| 115 | + } |
|
| 116 | + } else { |
|
| 117 | + $node = $nodes[0]; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + $result['path'] = $userFolder->getRelativePath($node->getPath()); |
|
| 121 | + if ($node instanceOf \OCP\Files\Folder) { |
|
| 122 | + $result['item_type'] = 'folder'; |
|
| 123 | + } else { |
|
| 124 | + $result['item_type'] = 'file'; |
|
| 125 | + } |
|
| 126 | + $result['mimetype'] = $node->getMimetype(); |
|
| 127 | + $result['storage_id'] = $node->getStorage()->getId(); |
|
| 128 | + $result['storage'] = $node->getStorage()->getCache()->getNumericStorageId(); |
|
| 129 | + $result['item_source'] = $node->getId(); |
|
| 130 | + $result['file_source'] = $node->getId(); |
|
| 131 | + $result['file_parent'] = $node->getParent()->getId(); |
|
| 132 | + $result['file_target'] = $share->getTarget(); |
|
| 133 | + |
|
| 134 | + $expiration = $share->getExpirationDate(); |
|
| 135 | + if ($expiration !== null) { |
|
| 136 | + $result['expiration'] = $expiration->format('Y-m-d 00:00:00'); |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 140 | + $group = $this->groupManager->get($share->getSharedWith()); |
|
| 141 | + $result['share_with'] = $share->getSharedWith(); |
|
| 142 | + $result['share_with_displayname'] = $group !== null ? $group->getDisplayName() : $share->getSharedWith(); |
|
| 143 | + } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_ROOM) { |
|
| 144 | + $result['share_with'] = $share->getSharedWith(); |
|
| 145 | + $result['share_with_displayname'] = ''; |
|
| 146 | + |
|
| 147 | + try { |
|
| 148 | + $result = array_merge($result, $this->getRoomShareHelper()->formatShare($share)); |
|
| 149 | + } catch (QueryException $e) { |
|
| 150 | + } |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + return $result; |
|
| 154 | + |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * @NoAdminRequired |
|
| 159 | + */ |
|
| 160 | + public function index(): DataResponse { |
|
| 161 | + $groupShares = $this->shareManager->getDeletedSharedWith($this->userId, \OCP\Share::SHARE_TYPE_GROUP, null, -1, 0); |
|
| 162 | + $roomShares = $this->shareManager->getDeletedSharedWith($this->userId, \OCP\Share::SHARE_TYPE_ROOM, null, -1, 0); |
|
| 163 | + |
|
| 164 | + $shares = array_merge($groupShares, $roomShares); |
|
| 165 | + |
|
| 166 | + $shares = array_map(function (IShare $share) { |
|
| 167 | + return $this->formatShare($share); |
|
| 168 | + }, $shares); |
|
| 169 | + |
|
| 170 | + return new DataResponse($shares); |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + /** |
|
| 174 | + * @NoAdminRequired |
|
| 175 | + * |
|
| 176 | + * @throws OCSException |
|
| 177 | + */ |
|
| 178 | + public function undelete(string $id): DataResponse { |
|
| 179 | + try { |
|
| 180 | + $share = $this->shareManager->getShareById($id, $this->userId); |
|
| 181 | + } catch (ShareNotFound $e) { |
|
| 182 | + throw new OCSNotFoundException('Share not found'); |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + if ($share->getPermissions() !== 0) { |
|
| 186 | + throw new OCSNotFoundException('No deleted share found'); |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + try { |
|
| 190 | + $this->shareManager->restoreShare($share, $this->userId); |
|
| 191 | + } catch (GenericShareException $e) { |
|
| 192 | + throw new OCSException('Something went wrong'); |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + return new DataResponse([]); |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + /** |
|
| 199 | + * Returns the helper of DeletedShareAPIController for room shares. |
|
| 200 | + * |
|
| 201 | + * If the Talk application is not enabled or the helper is not available |
|
| 202 | + * a QueryException is thrown instead. |
|
| 203 | + * |
|
| 204 | + * @return \OCA\Spreed\Share\Helper\DeletedShareAPIController |
|
| 205 | + * @throws QueryException |
|
| 206 | + */ |
|
| 207 | + private function getRoomShareHelper() { |
|
| 208 | + if (!$this->appManager->isEnabledForUser('spreed')) { |
|
| 209 | + throw new QueryException(); |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + return $this->serverContainer->query('\OCA\Spreed\Share\Helper\DeletedShareAPIController'); |
|
| 213 | + } |
|
| 214 | 214 | } |
@@ -163,7 +163,7 @@ |
||
| 163 | 163 | |
| 164 | 164 | $shares = array_merge($groupShares, $roomShares); |
| 165 | 165 | |
| 166 | - $shares = array_map(function (IShare $share) { |
|
| 166 | + $shares = array_map(function(IShare $share) { |
|
| 167 | 167 | return $this->formatShare($share); |
| 168 | 168 | }, $shares); |
| 169 | 169 | |
@@ -66,1017 +66,1017 @@ |
||
| 66 | 66 | */ |
| 67 | 67 | class ShareAPIController extends OCSController { |
| 68 | 68 | |
| 69 | - /** @var IManager */ |
|
| 70 | - private $shareManager; |
|
| 71 | - /** @var IGroupManager */ |
|
| 72 | - private $groupManager; |
|
| 73 | - /** @var IUserManager */ |
|
| 74 | - private $userManager; |
|
| 75 | - /** @var IRootFolder */ |
|
| 76 | - private $rootFolder; |
|
| 77 | - /** @var IURLGenerator */ |
|
| 78 | - private $urlGenerator; |
|
| 79 | - /** @var string */ |
|
| 80 | - private $currentUser; |
|
| 81 | - /** @var IL10N */ |
|
| 82 | - private $l; |
|
| 83 | - /** @var \OCP\Files\Node */ |
|
| 84 | - private $lockedNode; |
|
| 85 | - /** @var IConfig */ |
|
| 86 | - private $config; |
|
| 87 | - /** @var IAppManager */ |
|
| 88 | - private $appManager; |
|
| 89 | - /** @var IServerContainer */ |
|
| 90 | - private $serverContainer; |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * Share20OCS constructor. |
|
| 94 | - * |
|
| 95 | - * @param string $appName |
|
| 96 | - * @param IRequest $request |
|
| 97 | - * @param IManager $shareManager |
|
| 98 | - * @param IGroupManager $groupManager |
|
| 99 | - * @param IUserManager $userManager |
|
| 100 | - * @param IRootFolder $rootFolder |
|
| 101 | - * @param IURLGenerator $urlGenerator |
|
| 102 | - * @param string $userId |
|
| 103 | - * @param IL10N $l10n |
|
| 104 | - * @param IConfig $config |
|
| 105 | - * @param IAppManager $appManager |
|
| 106 | - * @param IServerContainer $serverContainer |
|
| 107 | - */ |
|
| 108 | - public function __construct( |
|
| 109 | - string $appName, |
|
| 110 | - IRequest $request, |
|
| 111 | - IManager $shareManager, |
|
| 112 | - IGroupManager $groupManager, |
|
| 113 | - IUserManager $userManager, |
|
| 114 | - IRootFolder $rootFolder, |
|
| 115 | - IURLGenerator $urlGenerator, |
|
| 116 | - string $userId, |
|
| 117 | - IL10N $l10n, |
|
| 118 | - IConfig $config, |
|
| 119 | - IAppManager $appManager, |
|
| 120 | - IServerContainer $serverContainer |
|
| 121 | - ) { |
|
| 122 | - parent::__construct($appName, $request); |
|
| 123 | - |
|
| 124 | - $this->shareManager = $shareManager; |
|
| 125 | - $this->userManager = $userManager; |
|
| 126 | - $this->groupManager = $groupManager; |
|
| 127 | - $this->request = $request; |
|
| 128 | - $this->rootFolder = $rootFolder; |
|
| 129 | - $this->urlGenerator = $urlGenerator; |
|
| 130 | - $this->currentUser = $userId; |
|
| 131 | - $this->l = $l10n; |
|
| 132 | - $this->config = $config; |
|
| 133 | - $this->appManager = $appManager; |
|
| 134 | - $this->serverContainer = $serverContainer; |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * Convert an IShare to an array for OCS output |
|
| 139 | - * |
|
| 140 | - * @param \OCP\Share\IShare $share |
|
| 141 | - * @param Node|null $recipientNode |
|
| 142 | - * @return array |
|
| 143 | - * @throws NotFoundException In case the node can't be resolved. |
|
| 144 | - * |
|
| 145 | - * @suppress PhanUndeclaredClassMethod |
|
| 146 | - */ |
|
| 147 | - protected function formatShare(\OCP\Share\IShare $share, Node $recipientNode = null): array { |
|
| 148 | - $sharedBy = $this->userManager->get($share->getSharedBy()); |
|
| 149 | - $shareOwner = $this->userManager->get($share->getShareOwner()); |
|
| 150 | - |
|
| 151 | - $result = [ |
|
| 152 | - 'id' => $share->getId(), |
|
| 153 | - 'share_type' => $share->getShareType(), |
|
| 154 | - 'uid_owner' => $share->getSharedBy(), |
|
| 155 | - 'displayname_owner' => $sharedBy !== null ? $sharedBy->getDisplayName() : $share->getSharedBy(), |
|
| 156 | - 'permissions' => $share->getPermissions(), |
|
| 157 | - 'stime' => $share->getShareTime()->getTimestamp(), |
|
| 158 | - 'parent' => null, |
|
| 159 | - 'expiration' => null, |
|
| 160 | - 'token' => null, |
|
| 161 | - 'uid_file_owner' => $share->getShareOwner(), |
|
| 162 | - 'note' => $share->getNote(), |
|
| 163 | - 'displayname_file_owner' => $shareOwner !== null ? $shareOwner->getDisplayName() : $share->getShareOwner(), |
|
| 164 | - ]; |
|
| 165 | - |
|
| 166 | - $userFolder = $this->rootFolder->getUserFolder($this->currentUser); |
|
| 167 | - if ($recipientNode) { |
|
| 168 | - $node = $recipientNode; |
|
| 169 | - } else { |
|
| 170 | - $nodes = $userFolder->getById($share->getNodeId()); |
|
| 171 | - if (empty($nodes)) { |
|
| 172 | - // fallback to guessing the path |
|
| 173 | - $node = $userFolder->get($share->getTarget()); |
|
| 174 | - if ($node === null || $share->getTarget() === '') { |
|
| 175 | - throw new NotFoundException(); |
|
| 176 | - } |
|
| 177 | - } else { |
|
| 178 | - $node = $nodes[0]; |
|
| 179 | - } |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - $result['path'] = $userFolder->getRelativePath($node->getPath()); |
|
| 183 | - if ($node instanceOf \OCP\Files\Folder) { |
|
| 184 | - $result['item_type'] = 'folder'; |
|
| 185 | - } else { |
|
| 186 | - $result['item_type'] = 'file'; |
|
| 187 | - } |
|
| 188 | - $result['mimetype'] = $node->getMimetype(); |
|
| 189 | - $result['storage_id'] = $node->getStorage()->getId(); |
|
| 190 | - $result['storage'] = $node->getStorage()->getCache()->getNumericStorageId(); |
|
| 191 | - $result['item_source'] = $node->getId(); |
|
| 192 | - $result['file_source'] = $node->getId(); |
|
| 193 | - $result['file_parent'] = $node->getParent()->getId(); |
|
| 194 | - $result['file_target'] = $share->getTarget(); |
|
| 195 | - |
|
| 196 | - $expiration = $share->getExpirationDate(); |
|
| 197 | - if ($expiration !== null) { |
|
| 198 | - $result['expiration'] = $expiration->format('Y-m-d 00:00:00'); |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - if ($share->getShareType() === Share::SHARE_TYPE_USER) { |
|
| 202 | - $sharedWith = $this->userManager->get($share->getSharedWith()); |
|
| 203 | - $result['share_with'] = $share->getSharedWith(); |
|
| 204 | - $result['share_with_displayname'] = $sharedWith !== null ? $sharedWith->getDisplayName() : $share->getSharedWith(); |
|
| 205 | - } else if ($share->getShareType() === Share::SHARE_TYPE_GROUP) { |
|
| 206 | - $group = $this->groupManager->get($share->getSharedWith()); |
|
| 207 | - $result['share_with'] = $share->getSharedWith(); |
|
| 208 | - $result['share_with_displayname'] = $group !== null ? $group->getDisplayName() : $share->getSharedWith(); |
|
| 209 | - } else if ($share->getShareType() === Share::SHARE_TYPE_LINK) { |
|
| 210 | - |
|
| 211 | - $result['share_with'] = $share->getPassword(); |
|
| 212 | - $result['share_with_displayname'] = $share->getPassword(); |
|
| 213 | - |
|
| 214 | - $result['token'] = $share->getToken(); |
|
| 215 | - $result['url'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare', ['token' => $share->getToken()]); |
|
| 216 | - |
|
| 217 | - } else if ($share->getShareType() === Share::SHARE_TYPE_REMOTE || $share->getShareType() === Share::SHARE_TYPE_REMOTE_GROUP) { |
|
| 218 | - $result['share_with'] = $share->getSharedWith(); |
|
| 219 | - $result['share_with_displayname'] = $this->getDisplayNameFromAddressBook($share->getSharedWith(), 'CLOUD'); |
|
| 220 | - $result['token'] = $share->getToken(); |
|
| 221 | - } else if ($share->getShareType() === Share::SHARE_TYPE_EMAIL) { |
|
| 222 | - $result['share_with'] = $share->getSharedWith(); |
|
| 223 | - $result['password'] = $share->getPassword(); |
|
| 224 | - $result['send_password_by_talk'] = $share->getSendPasswordByTalk(); |
|
| 225 | - $result['share_with_displayname'] = $this->getDisplayNameFromAddressBook($share->getSharedWith(), 'EMAIL'); |
|
| 226 | - $result['token'] = $share->getToken(); |
|
| 227 | - } else if ($share->getShareType() === Share::SHARE_TYPE_CIRCLE) { |
|
| 228 | - // getSharedWith() returns either "name (type, owner)" or |
|
| 229 | - // "name (type, owner) [id]", depending on the Circles app version. |
|
| 230 | - $hasCircleId = (substr($share->getSharedWith(), -1) === ']'); |
|
| 231 | - |
|
| 232 | - $result['share_with_displayname'] = $share->getSharedWithDisplayName(); |
|
| 233 | - if (empty($result['share_with_displayname'])) { |
|
| 234 | - $displayNameLength = ($hasCircleId? strrpos($share->getSharedWith(), ' '): strlen($share->getSharedWith())); |
|
| 235 | - $result['share_with_displayname'] = substr($share->getSharedWith(), 0, $displayNameLength); |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - $result['share_with_avatar'] = $share->getSharedWithAvatar(); |
|
| 239 | - |
|
| 240 | - $shareWithStart = ($hasCircleId? strrpos($share->getSharedWith(), '[') + 1: 0); |
|
| 241 | - $shareWithLength = ($hasCircleId? -1: strpos($share->getSharedWith(), ' ')); |
|
| 242 | - $result['share_with'] = substr($share->getSharedWith(), $shareWithStart, $shareWithLength); |
|
| 243 | - } else if ($share->getShareType() === Share::SHARE_TYPE_ROOM) { |
|
| 244 | - $result['share_with'] = $share->getSharedWith(); |
|
| 245 | - $result['share_with_displayname'] = ''; |
|
| 246 | - |
|
| 247 | - try { |
|
| 248 | - $result = array_merge($result, $this->getRoomShareHelper()->formatShare($share)); |
|
| 249 | - } catch (QueryException $e) { |
|
| 250 | - } |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - |
|
| 254 | - $result['mail_send'] = $share->getMailSend() ? 1 : 0; |
|
| 255 | - |
|
| 256 | - return $result; |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - /** |
|
| 260 | - * Check if one of the users address books knows the exact property, if |
|
| 261 | - * yes we return the full name. |
|
| 262 | - * |
|
| 263 | - * @param string $query |
|
| 264 | - * @param string $property |
|
| 265 | - * @return string |
|
| 266 | - */ |
|
| 267 | - private function getDisplayNameFromAddressBook(string $query, string $property): string { |
|
| 268 | - // FIXME: If we inject the contacts manager it gets initialized bofore any address books are registered |
|
| 269 | - $result = \OC::$server->getContactsManager()->search($query, [$property]); |
|
| 270 | - foreach ($result as $r) { |
|
| 271 | - foreach($r[$property] as $value) { |
|
| 272 | - if ($value === $query) { |
|
| 273 | - return $r['FN']; |
|
| 274 | - } |
|
| 275 | - } |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - return $query; |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - /** |
|
| 282 | - * Get a specific share by id |
|
| 283 | - * |
|
| 284 | - * @NoAdminRequired |
|
| 285 | - * |
|
| 286 | - * @param string $id |
|
| 287 | - * @return DataResponse |
|
| 288 | - * @throws OCSNotFoundException |
|
| 289 | - */ |
|
| 290 | - public function getShare(string $id): DataResponse { |
|
| 291 | - try { |
|
| 292 | - $share = $this->getShareById($id); |
|
| 293 | - } catch (ShareNotFound $e) { |
|
| 294 | - throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist')); |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - if ($this->canAccessShare($share)) { |
|
| 298 | - try { |
|
| 299 | - $share = $this->formatShare($share); |
|
| 300 | - return new DataResponse([$share]); |
|
| 301 | - } catch (NotFoundException $e) { |
|
| 302 | - //Fall trough |
|
| 303 | - } |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist')); |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - /** |
|
| 310 | - * Delete a share |
|
| 311 | - * |
|
| 312 | - * @NoAdminRequired |
|
| 313 | - * |
|
| 314 | - * @param string $id |
|
| 315 | - * @return DataResponse |
|
| 316 | - * @throws OCSNotFoundException |
|
| 317 | - */ |
|
| 318 | - public function deleteShare(string $id): DataResponse { |
|
| 319 | - try { |
|
| 320 | - $share = $this->getShareById($id); |
|
| 321 | - } catch (ShareNotFound $e) { |
|
| 322 | - throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist')); |
|
| 323 | - } |
|
| 324 | - |
|
| 325 | - try { |
|
| 326 | - $this->lock($share->getNode()); |
|
| 327 | - } catch (LockedException $e) { |
|
| 328 | - throw new OCSNotFoundException($this->l->t('could not delete share')); |
|
| 329 | - } |
|
| 330 | - |
|
| 331 | - if (!$this->canAccessShare($share)) { |
|
| 332 | - throw new OCSNotFoundException($this->l->t('Could not delete share')); |
|
| 333 | - } |
|
| 334 | - |
|
| 335 | - if (($share->getShareType() === Share::SHARE_TYPE_GROUP || |
|
| 336 | - $share->getShareType() === Share::SHARE_TYPE_ROOM) && |
|
| 337 | - $share->getShareOwner() !== $this->currentUser && |
|
| 338 | - $share->getSharedBy() !== $this->currentUser) { |
|
| 339 | - $this->shareManager->deleteFromSelf($share, $this->currentUser); |
|
| 340 | - } else { |
|
| 341 | - $this->shareManager->deleteShare($share); |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - return new DataResponse(); |
|
| 345 | - } |
|
| 346 | - |
|
| 347 | - /** |
|
| 348 | - * @NoAdminRequired |
|
| 349 | - * |
|
| 350 | - * @param string $path |
|
| 351 | - * @param int $permissions |
|
| 352 | - * @param int $shareType |
|
| 353 | - * @param string $shareWith |
|
| 354 | - * @param string $publicUpload |
|
| 355 | - * @param string $password |
|
| 356 | - * @param bool $sendPasswordByTalk |
|
| 357 | - * @param string $expireDate |
|
| 358 | - * |
|
| 359 | - * @return DataResponse |
|
| 360 | - * @throws OCSNotFoundException |
|
| 361 | - * @throws OCSForbiddenException |
|
| 362 | - * @throws OCSBadRequestException |
|
| 363 | - * @throws OCSException |
|
| 364 | - * |
|
| 365 | - * @suppress PhanUndeclaredClassMethod |
|
| 366 | - */ |
|
| 367 | - public function createShare( |
|
| 368 | - string $path = null, |
|
| 369 | - int $permissions = null, |
|
| 370 | - int $shareType = -1, |
|
| 371 | - string $shareWith = null, |
|
| 372 | - string $publicUpload = 'false', |
|
| 373 | - string $password = '', |
|
| 374 | - string $sendPasswordByTalk = null, |
|
| 375 | - string $expireDate = '' |
|
| 376 | - ): DataResponse { |
|
| 377 | - $share = $this->shareManager->newShare(); |
|
| 378 | - |
|
| 379 | - if ($permissions === null) { |
|
| 380 | - $permissions = $this->config->getAppValue('core', 'shareapi_default_permissions', Constants::PERMISSION_ALL); |
|
| 381 | - } |
|
| 382 | - |
|
| 383 | - // Verify path |
|
| 384 | - if ($path === null) { |
|
| 385 | - throw new OCSNotFoundException($this->l->t('Please specify a file or folder path')); |
|
| 386 | - } |
|
| 387 | - |
|
| 388 | - $userFolder = $this->rootFolder->getUserFolder($this->currentUser); |
|
| 389 | - try { |
|
| 390 | - $path = $userFolder->get($path); |
|
| 391 | - } catch (NotFoundException $e) { |
|
| 392 | - throw new OCSNotFoundException($this->l->t('Wrong path, file/folder doesn\'t exist')); |
|
| 393 | - } |
|
| 394 | - |
|
| 395 | - $share->setNode($path); |
|
| 396 | - |
|
| 397 | - try { |
|
| 398 | - $this->lock($share->getNode()); |
|
| 399 | - } catch (LockedException $e) { |
|
| 400 | - throw new OCSNotFoundException($this->l->t('Could not create share')); |
|
| 401 | - } |
|
| 402 | - |
|
| 403 | - if ($permissions < 0 || $permissions > Constants::PERMISSION_ALL) { |
|
| 404 | - throw new OCSNotFoundException($this->l->t('invalid permissions')); |
|
| 405 | - } |
|
| 406 | - |
|
| 407 | - // Shares always require read permissions |
|
| 408 | - $permissions |= Constants::PERMISSION_READ; |
|
| 409 | - |
|
| 410 | - if ($path instanceof \OCP\Files\File) { |
|
| 411 | - // Single file shares should never have delete or create permissions |
|
| 412 | - $permissions &= ~Constants::PERMISSION_DELETE; |
|
| 413 | - $permissions &= ~Constants::PERMISSION_CREATE; |
|
| 414 | - } |
|
| 415 | - |
|
| 416 | - /* |
|
| 69 | + /** @var IManager */ |
|
| 70 | + private $shareManager; |
|
| 71 | + /** @var IGroupManager */ |
|
| 72 | + private $groupManager; |
|
| 73 | + /** @var IUserManager */ |
|
| 74 | + private $userManager; |
|
| 75 | + /** @var IRootFolder */ |
|
| 76 | + private $rootFolder; |
|
| 77 | + /** @var IURLGenerator */ |
|
| 78 | + private $urlGenerator; |
|
| 79 | + /** @var string */ |
|
| 80 | + private $currentUser; |
|
| 81 | + /** @var IL10N */ |
|
| 82 | + private $l; |
|
| 83 | + /** @var \OCP\Files\Node */ |
|
| 84 | + private $lockedNode; |
|
| 85 | + /** @var IConfig */ |
|
| 86 | + private $config; |
|
| 87 | + /** @var IAppManager */ |
|
| 88 | + private $appManager; |
|
| 89 | + /** @var IServerContainer */ |
|
| 90 | + private $serverContainer; |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * Share20OCS constructor. |
|
| 94 | + * |
|
| 95 | + * @param string $appName |
|
| 96 | + * @param IRequest $request |
|
| 97 | + * @param IManager $shareManager |
|
| 98 | + * @param IGroupManager $groupManager |
|
| 99 | + * @param IUserManager $userManager |
|
| 100 | + * @param IRootFolder $rootFolder |
|
| 101 | + * @param IURLGenerator $urlGenerator |
|
| 102 | + * @param string $userId |
|
| 103 | + * @param IL10N $l10n |
|
| 104 | + * @param IConfig $config |
|
| 105 | + * @param IAppManager $appManager |
|
| 106 | + * @param IServerContainer $serverContainer |
|
| 107 | + */ |
|
| 108 | + public function __construct( |
|
| 109 | + string $appName, |
|
| 110 | + IRequest $request, |
|
| 111 | + IManager $shareManager, |
|
| 112 | + IGroupManager $groupManager, |
|
| 113 | + IUserManager $userManager, |
|
| 114 | + IRootFolder $rootFolder, |
|
| 115 | + IURLGenerator $urlGenerator, |
|
| 116 | + string $userId, |
|
| 117 | + IL10N $l10n, |
|
| 118 | + IConfig $config, |
|
| 119 | + IAppManager $appManager, |
|
| 120 | + IServerContainer $serverContainer |
|
| 121 | + ) { |
|
| 122 | + parent::__construct($appName, $request); |
|
| 123 | + |
|
| 124 | + $this->shareManager = $shareManager; |
|
| 125 | + $this->userManager = $userManager; |
|
| 126 | + $this->groupManager = $groupManager; |
|
| 127 | + $this->request = $request; |
|
| 128 | + $this->rootFolder = $rootFolder; |
|
| 129 | + $this->urlGenerator = $urlGenerator; |
|
| 130 | + $this->currentUser = $userId; |
|
| 131 | + $this->l = $l10n; |
|
| 132 | + $this->config = $config; |
|
| 133 | + $this->appManager = $appManager; |
|
| 134 | + $this->serverContainer = $serverContainer; |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * Convert an IShare to an array for OCS output |
|
| 139 | + * |
|
| 140 | + * @param \OCP\Share\IShare $share |
|
| 141 | + * @param Node|null $recipientNode |
|
| 142 | + * @return array |
|
| 143 | + * @throws NotFoundException In case the node can't be resolved. |
|
| 144 | + * |
|
| 145 | + * @suppress PhanUndeclaredClassMethod |
|
| 146 | + */ |
|
| 147 | + protected function formatShare(\OCP\Share\IShare $share, Node $recipientNode = null): array { |
|
| 148 | + $sharedBy = $this->userManager->get($share->getSharedBy()); |
|
| 149 | + $shareOwner = $this->userManager->get($share->getShareOwner()); |
|
| 150 | + |
|
| 151 | + $result = [ |
|
| 152 | + 'id' => $share->getId(), |
|
| 153 | + 'share_type' => $share->getShareType(), |
|
| 154 | + 'uid_owner' => $share->getSharedBy(), |
|
| 155 | + 'displayname_owner' => $sharedBy !== null ? $sharedBy->getDisplayName() : $share->getSharedBy(), |
|
| 156 | + 'permissions' => $share->getPermissions(), |
|
| 157 | + 'stime' => $share->getShareTime()->getTimestamp(), |
|
| 158 | + 'parent' => null, |
|
| 159 | + 'expiration' => null, |
|
| 160 | + 'token' => null, |
|
| 161 | + 'uid_file_owner' => $share->getShareOwner(), |
|
| 162 | + 'note' => $share->getNote(), |
|
| 163 | + 'displayname_file_owner' => $shareOwner !== null ? $shareOwner->getDisplayName() : $share->getShareOwner(), |
|
| 164 | + ]; |
|
| 165 | + |
|
| 166 | + $userFolder = $this->rootFolder->getUserFolder($this->currentUser); |
|
| 167 | + if ($recipientNode) { |
|
| 168 | + $node = $recipientNode; |
|
| 169 | + } else { |
|
| 170 | + $nodes = $userFolder->getById($share->getNodeId()); |
|
| 171 | + if (empty($nodes)) { |
|
| 172 | + // fallback to guessing the path |
|
| 173 | + $node = $userFolder->get($share->getTarget()); |
|
| 174 | + if ($node === null || $share->getTarget() === '') { |
|
| 175 | + throw new NotFoundException(); |
|
| 176 | + } |
|
| 177 | + } else { |
|
| 178 | + $node = $nodes[0]; |
|
| 179 | + } |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + $result['path'] = $userFolder->getRelativePath($node->getPath()); |
|
| 183 | + if ($node instanceOf \OCP\Files\Folder) { |
|
| 184 | + $result['item_type'] = 'folder'; |
|
| 185 | + } else { |
|
| 186 | + $result['item_type'] = 'file'; |
|
| 187 | + } |
|
| 188 | + $result['mimetype'] = $node->getMimetype(); |
|
| 189 | + $result['storage_id'] = $node->getStorage()->getId(); |
|
| 190 | + $result['storage'] = $node->getStorage()->getCache()->getNumericStorageId(); |
|
| 191 | + $result['item_source'] = $node->getId(); |
|
| 192 | + $result['file_source'] = $node->getId(); |
|
| 193 | + $result['file_parent'] = $node->getParent()->getId(); |
|
| 194 | + $result['file_target'] = $share->getTarget(); |
|
| 195 | + |
|
| 196 | + $expiration = $share->getExpirationDate(); |
|
| 197 | + if ($expiration !== null) { |
|
| 198 | + $result['expiration'] = $expiration->format('Y-m-d 00:00:00'); |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + if ($share->getShareType() === Share::SHARE_TYPE_USER) { |
|
| 202 | + $sharedWith = $this->userManager->get($share->getSharedWith()); |
|
| 203 | + $result['share_with'] = $share->getSharedWith(); |
|
| 204 | + $result['share_with_displayname'] = $sharedWith !== null ? $sharedWith->getDisplayName() : $share->getSharedWith(); |
|
| 205 | + } else if ($share->getShareType() === Share::SHARE_TYPE_GROUP) { |
|
| 206 | + $group = $this->groupManager->get($share->getSharedWith()); |
|
| 207 | + $result['share_with'] = $share->getSharedWith(); |
|
| 208 | + $result['share_with_displayname'] = $group !== null ? $group->getDisplayName() : $share->getSharedWith(); |
|
| 209 | + } else if ($share->getShareType() === Share::SHARE_TYPE_LINK) { |
|
| 210 | + |
|
| 211 | + $result['share_with'] = $share->getPassword(); |
|
| 212 | + $result['share_with_displayname'] = $share->getPassword(); |
|
| 213 | + |
|
| 214 | + $result['token'] = $share->getToken(); |
|
| 215 | + $result['url'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare', ['token' => $share->getToken()]); |
|
| 216 | + |
|
| 217 | + } else if ($share->getShareType() === Share::SHARE_TYPE_REMOTE || $share->getShareType() === Share::SHARE_TYPE_REMOTE_GROUP) { |
|
| 218 | + $result['share_with'] = $share->getSharedWith(); |
|
| 219 | + $result['share_with_displayname'] = $this->getDisplayNameFromAddressBook($share->getSharedWith(), 'CLOUD'); |
|
| 220 | + $result['token'] = $share->getToken(); |
|
| 221 | + } else if ($share->getShareType() === Share::SHARE_TYPE_EMAIL) { |
|
| 222 | + $result['share_with'] = $share->getSharedWith(); |
|
| 223 | + $result['password'] = $share->getPassword(); |
|
| 224 | + $result['send_password_by_talk'] = $share->getSendPasswordByTalk(); |
|
| 225 | + $result['share_with_displayname'] = $this->getDisplayNameFromAddressBook($share->getSharedWith(), 'EMAIL'); |
|
| 226 | + $result['token'] = $share->getToken(); |
|
| 227 | + } else if ($share->getShareType() === Share::SHARE_TYPE_CIRCLE) { |
|
| 228 | + // getSharedWith() returns either "name (type, owner)" or |
|
| 229 | + // "name (type, owner) [id]", depending on the Circles app version. |
|
| 230 | + $hasCircleId = (substr($share->getSharedWith(), -1) === ']'); |
|
| 231 | + |
|
| 232 | + $result['share_with_displayname'] = $share->getSharedWithDisplayName(); |
|
| 233 | + if (empty($result['share_with_displayname'])) { |
|
| 234 | + $displayNameLength = ($hasCircleId? strrpos($share->getSharedWith(), ' '): strlen($share->getSharedWith())); |
|
| 235 | + $result['share_with_displayname'] = substr($share->getSharedWith(), 0, $displayNameLength); |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + $result['share_with_avatar'] = $share->getSharedWithAvatar(); |
|
| 239 | + |
|
| 240 | + $shareWithStart = ($hasCircleId? strrpos($share->getSharedWith(), '[') + 1: 0); |
|
| 241 | + $shareWithLength = ($hasCircleId? -1: strpos($share->getSharedWith(), ' ')); |
|
| 242 | + $result['share_with'] = substr($share->getSharedWith(), $shareWithStart, $shareWithLength); |
|
| 243 | + } else if ($share->getShareType() === Share::SHARE_TYPE_ROOM) { |
|
| 244 | + $result['share_with'] = $share->getSharedWith(); |
|
| 245 | + $result['share_with_displayname'] = ''; |
|
| 246 | + |
|
| 247 | + try { |
|
| 248 | + $result = array_merge($result, $this->getRoomShareHelper()->formatShare($share)); |
|
| 249 | + } catch (QueryException $e) { |
|
| 250 | + } |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + |
|
| 254 | + $result['mail_send'] = $share->getMailSend() ? 1 : 0; |
|
| 255 | + |
|
| 256 | + return $result; |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + /** |
|
| 260 | + * Check if one of the users address books knows the exact property, if |
|
| 261 | + * yes we return the full name. |
|
| 262 | + * |
|
| 263 | + * @param string $query |
|
| 264 | + * @param string $property |
|
| 265 | + * @return string |
|
| 266 | + */ |
|
| 267 | + private function getDisplayNameFromAddressBook(string $query, string $property): string { |
|
| 268 | + // FIXME: If we inject the contacts manager it gets initialized bofore any address books are registered |
|
| 269 | + $result = \OC::$server->getContactsManager()->search($query, [$property]); |
|
| 270 | + foreach ($result as $r) { |
|
| 271 | + foreach($r[$property] as $value) { |
|
| 272 | + if ($value === $query) { |
|
| 273 | + return $r['FN']; |
|
| 274 | + } |
|
| 275 | + } |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + return $query; |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + /** |
|
| 282 | + * Get a specific share by id |
|
| 283 | + * |
|
| 284 | + * @NoAdminRequired |
|
| 285 | + * |
|
| 286 | + * @param string $id |
|
| 287 | + * @return DataResponse |
|
| 288 | + * @throws OCSNotFoundException |
|
| 289 | + */ |
|
| 290 | + public function getShare(string $id): DataResponse { |
|
| 291 | + try { |
|
| 292 | + $share = $this->getShareById($id); |
|
| 293 | + } catch (ShareNotFound $e) { |
|
| 294 | + throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist')); |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + if ($this->canAccessShare($share)) { |
|
| 298 | + try { |
|
| 299 | + $share = $this->formatShare($share); |
|
| 300 | + return new DataResponse([$share]); |
|
| 301 | + } catch (NotFoundException $e) { |
|
| 302 | + //Fall trough |
|
| 303 | + } |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist')); |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + /** |
|
| 310 | + * Delete a share |
|
| 311 | + * |
|
| 312 | + * @NoAdminRequired |
|
| 313 | + * |
|
| 314 | + * @param string $id |
|
| 315 | + * @return DataResponse |
|
| 316 | + * @throws OCSNotFoundException |
|
| 317 | + */ |
|
| 318 | + public function deleteShare(string $id): DataResponse { |
|
| 319 | + try { |
|
| 320 | + $share = $this->getShareById($id); |
|
| 321 | + } catch (ShareNotFound $e) { |
|
| 322 | + throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist')); |
|
| 323 | + } |
|
| 324 | + |
|
| 325 | + try { |
|
| 326 | + $this->lock($share->getNode()); |
|
| 327 | + } catch (LockedException $e) { |
|
| 328 | + throw new OCSNotFoundException($this->l->t('could not delete share')); |
|
| 329 | + } |
|
| 330 | + |
|
| 331 | + if (!$this->canAccessShare($share)) { |
|
| 332 | + throw new OCSNotFoundException($this->l->t('Could not delete share')); |
|
| 333 | + } |
|
| 334 | + |
|
| 335 | + if (($share->getShareType() === Share::SHARE_TYPE_GROUP || |
|
| 336 | + $share->getShareType() === Share::SHARE_TYPE_ROOM) && |
|
| 337 | + $share->getShareOwner() !== $this->currentUser && |
|
| 338 | + $share->getSharedBy() !== $this->currentUser) { |
|
| 339 | + $this->shareManager->deleteFromSelf($share, $this->currentUser); |
|
| 340 | + } else { |
|
| 341 | + $this->shareManager->deleteShare($share); |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + return new DataResponse(); |
|
| 345 | + } |
|
| 346 | + |
|
| 347 | + /** |
|
| 348 | + * @NoAdminRequired |
|
| 349 | + * |
|
| 350 | + * @param string $path |
|
| 351 | + * @param int $permissions |
|
| 352 | + * @param int $shareType |
|
| 353 | + * @param string $shareWith |
|
| 354 | + * @param string $publicUpload |
|
| 355 | + * @param string $password |
|
| 356 | + * @param bool $sendPasswordByTalk |
|
| 357 | + * @param string $expireDate |
|
| 358 | + * |
|
| 359 | + * @return DataResponse |
|
| 360 | + * @throws OCSNotFoundException |
|
| 361 | + * @throws OCSForbiddenException |
|
| 362 | + * @throws OCSBadRequestException |
|
| 363 | + * @throws OCSException |
|
| 364 | + * |
|
| 365 | + * @suppress PhanUndeclaredClassMethod |
|
| 366 | + */ |
|
| 367 | + public function createShare( |
|
| 368 | + string $path = null, |
|
| 369 | + int $permissions = null, |
|
| 370 | + int $shareType = -1, |
|
| 371 | + string $shareWith = null, |
|
| 372 | + string $publicUpload = 'false', |
|
| 373 | + string $password = '', |
|
| 374 | + string $sendPasswordByTalk = null, |
|
| 375 | + string $expireDate = '' |
|
| 376 | + ): DataResponse { |
|
| 377 | + $share = $this->shareManager->newShare(); |
|
| 378 | + |
|
| 379 | + if ($permissions === null) { |
|
| 380 | + $permissions = $this->config->getAppValue('core', 'shareapi_default_permissions', Constants::PERMISSION_ALL); |
|
| 381 | + } |
|
| 382 | + |
|
| 383 | + // Verify path |
|
| 384 | + if ($path === null) { |
|
| 385 | + throw new OCSNotFoundException($this->l->t('Please specify a file or folder path')); |
|
| 386 | + } |
|
| 387 | + |
|
| 388 | + $userFolder = $this->rootFolder->getUserFolder($this->currentUser); |
|
| 389 | + try { |
|
| 390 | + $path = $userFolder->get($path); |
|
| 391 | + } catch (NotFoundException $e) { |
|
| 392 | + throw new OCSNotFoundException($this->l->t('Wrong path, file/folder doesn\'t exist')); |
|
| 393 | + } |
|
| 394 | + |
|
| 395 | + $share->setNode($path); |
|
| 396 | + |
|
| 397 | + try { |
|
| 398 | + $this->lock($share->getNode()); |
|
| 399 | + } catch (LockedException $e) { |
|
| 400 | + throw new OCSNotFoundException($this->l->t('Could not create share')); |
|
| 401 | + } |
|
| 402 | + |
|
| 403 | + if ($permissions < 0 || $permissions > Constants::PERMISSION_ALL) { |
|
| 404 | + throw new OCSNotFoundException($this->l->t('invalid permissions')); |
|
| 405 | + } |
|
| 406 | + |
|
| 407 | + // Shares always require read permissions |
|
| 408 | + $permissions |= Constants::PERMISSION_READ; |
|
| 409 | + |
|
| 410 | + if ($path instanceof \OCP\Files\File) { |
|
| 411 | + // Single file shares should never have delete or create permissions |
|
| 412 | + $permissions &= ~Constants::PERMISSION_DELETE; |
|
| 413 | + $permissions &= ~Constants::PERMISSION_CREATE; |
|
| 414 | + } |
|
| 415 | + |
|
| 416 | + /* |
|
| 417 | 417 | * Hack for https://github.com/owncloud/core/issues/22587 |
| 418 | 418 | * We check the permissions via webdav. But the permissions of the mount point |
| 419 | 419 | * do not equal the share permissions. Here we fix that for federated mounts. |
| 420 | 420 | */ |
| 421 | - if ($path->getStorage()->instanceOfStorage(Storage::class)) { |
|
| 422 | - $permissions &= ~($permissions & ~$path->getPermissions()); |
|
| 423 | - } |
|
| 424 | - |
|
| 425 | - if ($shareType === Share::SHARE_TYPE_USER) { |
|
| 426 | - // Valid user is required to share |
|
| 427 | - if ($shareWith === null || !$this->userManager->userExists($shareWith)) { |
|
| 428 | - throw new OCSNotFoundException($this->l->t('Please specify a valid user')); |
|
| 429 | - } |
|
| 430 | - $share->setSharedWith($shareWith); |
|
| 431 | - $share->setPermissions($permissions); |
|
| 432 | - } else if ($shareType === Share::SHARE_TYPE_GROUP) { |
|
| 433 | - if (!$this->shareManager->allowGroupSharing()) { |
|
| 434 | - throw new OCSNotFoundException($this->l->t('Group sharing is disabled by the administrator')); |
|
| 435 | - } |
|
| 436 | - |
|
| 437 | - // Valid group is required to share |
|
| 438 | - if ($shareWith === null || !$this->groupManager->groupExists($shareWith)) { |
|
| 439 | - throw new OCSNotFoundException($this->l->t('Please specify a valid group')); |
|
| 440 | - } |
|
| 441 | - $share->setSharedWith($shareWith); |
|
| 442 | - $share->setPermissions($permissions); |
|
| 443 | - } else if ($shareType === Share::SHARE_TYPE_LINK) { |
|
| 444 | - //Can we even share links? |
|
| 445 | - if (!$this->shareManager->shareApiAllowLinks()) { |
|
| 446 | - throw new OCSNotFoundException($this->l->t('Public link sharing is disabled by the administrator')); |
|
| 447 | - } |
|
| 448 | - |
|
| 449 | - /* |
|
| 421 | + if ($path->getStorage()->instanceOfStorage(Storage::class)) { |
|
| 422 | + $permissions &= ~($permissions & ~$path->getPermissions()); |
|
| 423 | + } |
|
| 424 | + |
|
| 425 | + if ($shareType === Share::SHARE_TYPE_USER) { |
|
| 426 | + // Valid user is required to share |
|
| 427 | + if ($shareWith === null || !$this->userManager->userExists($shareWith)) { |
|
| 428 | + throw new OCSNotFoundException($this->l->t('Please specify a valid user')); |
|
| 429 | + } |
|
| 430 | + $share->setSharedWith($shareWith); |
|
| 431 | + $share->setPermissions($permissions); |
|
| 432 | + } else if ($shareType === Share::SHARE_TYPE_GROUP) { |
|
| 433 | + if (!$this->shareManager->allowGroupSharing()) { |
|
| 434 | + throw new OCSNotFoundException($this->l->t('Group sharing is disabled by the administrator')); |
|
| 435 | + } |
|
| 436 | + |
|
| 437 | + // Valid group is required to share |
|
| 438 | + if ($shareWith === null || !$this->groupManager->groupExists($shareWith)) { |
|
| 439 | + throw new OCSNotFoundException($this->l->t('Please specify a valid group')); |
|
| 440 | + } |
|
| 441 | + $share->setSharedWith($shareWith); |
|
| 442 | + $share->setPermissions($permissions); |
|
| 443 | + } else if ($shareType === Share::SHARE_TYPE_LINK) { |
|
| 444 | + //Can we even share links? |
|
| 445 | + if (!$this->shareManager->shareApiAllowLinks()) { |
|
| 446 | + throw new OCSNotFoundException($this->l->t('Public link sharing is disabled by the administrator')); |
|
| 447 | + } |
|
| 448 | + |
|
| 449 | + /* |
|
| 450 | 450 | * For now we only allow 1 link share. |
| 451 | 451 | * Return the existing link share if this is a duplicate |
| 452 | 452 | */ |
| 453 | - $existingShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_LINK, $path, false, 1, 0); |
|
| 454 | - if (!empty($existingShares)) { |
|
| 455 | - return new DataResponse($this->formatShare($existingShares[0])); |
|
| 456 | - } |
|
| 457 | - |
|
| 458 | - if ($publicUpload === 'true') { |
|
| 459 | - // Check if public upload is allowed |
|
| 460 | - if (!$this->shareManager->shareApiLinkAllowPublicUpload()) { |
|
| 461 | - throw new OCSForbiddenException($this->l->t('Public upload disabled by the administrator')); |
|
| 462 | - } |
|
| 463 | - |
|
| 464 | - // Public upload can only be set for folders |
|
| 465 | - if ($path instanceof \OCP\Files\File) { |
|
| 466 | - throw new OCSNotFoundException($this->l->t('Public upload is only possible for publicly shared folders')); |
|
| 467 | - } |
|
| 468 | - |
|
| 469 | - $share->setPermissions( |
|
| 470 | - Constants::PERMISSION_READ | |
|
| 471 | - Constants::PERMISSION_CREATE | |
|
| 472 | - Constants::PERMISSION_UPDATE | |
|
| 473 | - Constants::PERMISSION_DELETE |
|
| 474 | - ); |
|
| 475 | - } else { |
|
| 476 | - $share->setPermissions(Constants::PERMISSION_READ); |
|
| 477 | - } |
|
| 478 | - |
|
| 479 | - // Set password |
|
| 480 | - if ($password !== '') { |
|
| 481 | - $share->setPassword($password); |
|
| 482 | - } |
|
| 483 | - |
|
| 484 | - //Expire date |
|
| 485 | - if ($expireDate !== '') { |
|
| 486 | - try { |
|
| 487 | - $expireDate = $this->parseDate($expireDate); |
|
| 488 | - $share->setExpirationDate($expireDate); |
|
| 489 | - } catch (\Exception $e) { |
|
| 490 | - throw new OCSNotFoundException($this->l->t('Invalid date, date format must be YYYY-MM-DD')); |
|
| 491 | - } |
|
| 492 | - } |
|
| 493 | - |
|
| 494 | - } else if ($shareType === Share::SHARE_TYPE_REMOTE) { |
|
| 495 | - if (!$this->shareManager->outgoingServer2ServerSharesAllowed()) { |
|
| 496 | - throw new OCSForbiddenException($this->l->t('Sharing %s failed because the back end does not allow shares from type %s', [$path->getPath(), $shareType])); |
|
| 497 | - } |
|
| 498 | - |
|
| 499 | - $share->setSharedWith($shareWith); |
|
| 500 | - $share->setPermissions($permissions); |
|
| 501 | - } else if ($shareType === Share::SHARE_TYPE_REMOTE_GROUP) { |
|
| 502 | - if (!$this->shareManager->outgoingServer2ServerGroupSharesAllowed()) { |
|
| 503 | - throw new OCSForbiddenException($this->l->t('Sharing %s failed because the back end does not allow shares from type %s', [$path->getPath(), $shareType])); |
|
| 504 | - } |
|
| 505 | - |
|
| 506 | - $share->setSharedWith($shareWith); |
|
| 507 | - $share->setPermissions($permissions); |
|
| 508 | - } else if ($shareType === Share::SHARE_TYPE_EMAIL) { |
|
| 509 | - if ($share->getNodeType() === 'file') { |
|
| 510 | - $share->setPermissions(Constants::PERMISSION_READ); |
|
| 511 | - } else { |
|
| 512 | - $share->setPermissions($permissions); |
|
| 513 | - } |
|
| 514 | - $share->setSharedWith($shareWith); |
|
| 515 | - |
|
| 516 | - if ($sendPasswordByTalk === 'true') { |
|
| 517 | - if (!$this->appManager->isEnabledForUser('spreed')) { |
|
| 518 | - throw new OCSForbiddenException($this->l->t('Sharing %s sending the password by Nextcloud Talk failed because Nextcloud Talk is not enabled', [$path->getPath()])); |
|
| 519 | - } |
|
| 520 | - |
|
| 521 | - $share->setSendPasswordByTalk(true); |
|
| 522 | - } |
|
| 523 | - } else if ($shareType === Share::SHARE_TYPE_CIRCLE) { |
|
| 524 | - if (!\OC::$server->getAppManager()->isEnabledForUser('circles') || !class_exists('\OCA\Circles\ShareByCircleProvider')) { |
|
| 525 | - throw new OCSNotFoundException($this->l->t('You cannot share to a Circle if the app is not enabled')); |
|
| 526 | - } |
|
| 527 | - |
|
| 528 | - $circle = \OCA\Circles\Api\v1\Circles::detailsCircle($shareWith); |
|
| 529 | - |
|
| 530 | - // Valid circle is required to share |
|
| 531 | - if ($circle === null) { |
|
| 532 | - throw new OCSNotFoundException($this->l->t('Please specify a valid circle')); |
|
| 533 | - } |
|
| 534 | - $share->setSharedWith($shareWith); |
|
| 535 | - $share->setPermissions($permissions); |
|
| 536 | - } else if ($shareType === Share::SHARE_TYPE_ROOM) { |
|
| 537 | - try { |
|
| 538 | - $this->getRoomShareHelper()->createShare($share, $shareWith, $permissions, $expireDate); |
|
| 539 | - } catch (QueryException $e) { |
|
| 540 | - throw new OCSForbiddenException($this->l->t('Sharing %s failed because the back end does not support room shares', [$path->getPath()])); |
|
| 541 | - } |
|
| 542 | - } else { |
|
| 543 | - throw new OCSBadRequestException($this->l->t('Unknown share type')); |
|
| 544 | - } |
|
| 545 | - |
|
| 546 | - $share->setShareType($shareType); |
|
| 547 | - $share->setSharedBy($this->currentUser); |
|
| 548 | - |
|
| 549 | - try { |
|
| 550 | - $share = $this->shareManager->createShare($share); |
|
| 551 | - } catch (GenericShareException $e) { |
|
| 552 | - $code = $e->getCode() === 0 ? 403 : $e->getCode(); |
|
| 553 | - throw new OCSException($e->getHint(), $code); |
|
| 554 | - } catch (\Exception $e) { |
|
| 555 | - throw new OCSForbiddenException($e->getMessage(), $e); |
|
| 556 | - } |
|
| 557 | - |
|
| 558 | - $output = $this->formatShare($share); |
|
| 559 | - |
|
| 560 | - return new DataResponse($output); |
|
| 561 | - } |
|
| 562 | - |
|
| 563 | - /** |
|
| 564 | - * @param \OCP\Files\File|\OCP\Files\Folder $node |
|
| 565 | - * @param boolean $includeTags |
|
| 566 | - * @return DataResponse |
|
| 567 | - */ |
|
| 568 | - private function getSharedWithMe($node = null, bool $includeTags): DataResponse { |
|
| 569 | - |
|
| 570 | - $userShares = $this->shareManager->getSharedWith($this->currentUser, Share::SHARE_TYPE_USER, $node, -1, 0); |
|
| 571 | - $groupShares = $this->shareManager->getSharedWith($this->currentUser, Share::SHARE_TYPE_GROUP, $node, -1, 0); |
|
| 572 | - $circleShares = $this->shareManager->getSharedWith($this->currentUser, Share::SHARE_TYPE_CIRCLE, $node, -1, 0); |
|
| 573 | - $roomShares = $this->shareManager->getSharedWith($this->currentUser, Share::SHARE_TYPE_ROOM, $node, -1, 0); |
|
| 574 | - |
|
| 575 | - $shares = array_merge($userShares, $groupShares, $circleShares, $roomShares); |
|
| 576 | - |
|
| 577 | - $shares = array_filter($shares, function (IShare $share) { |
|
| 578 | - return $share->getShareOwner() !== $this->currentUser; |
|
| 579 | - }); |
|
| 580 | - |
|
| 581 | - $formatted = []; |
|
| 582 | - foreach ($shares as $share) { |
|
| 583 | - if ($this->canAccessShare($share)) { |
|
| 584 | - try { |
|
| 585 | - $formatted[] = $this->formatShare($share); |
|
| 586 | - } catch (NotFoundException $e) { |
|
| 587 | - // Ignore this share |
|
| 588 | - } |
|
| 589 | - } |
|
| 590 | - } |
|
| 591 | - |
|
| 592 | - if ($includeTags) { |
|
| 593 | - $formatted = Helper::populateTags($formatted, 'file_source', \OC::$server->getTagManager()); |
|
| 594 | - } |
|
| 595 | - |
|
| 596 | - return new DataResponse($formatted); |
|
| 597 | - } |
|
| 598 | - |
|
| 599 | - /** |
|
| 600 | - * @param \OCP\Files\Folder $folder |
|
| 601 | - * @return DataResponse |
|
| 602 | - * @throws OCSBadRequestException |
|
| 603 | - */ |
|
| 604 | - private function getSharesInDir(Node $folder): DataResponse { |
|
| 605 | - if (!($folder instanceof \OCP\Files\Folder)) { |
|
| 606 | - throw new OCSBadRequestException($this->l->t('Not a directory')); |
|
| 607 | - } |
|
| 608 | - |
|
| 609 | - $nodes = $folder->getDirectoryListing(); |
|
| 610 | - /** @var \OCP\Share\IShare[] $shares */ |
|
| 611 | - $shares = []; |
|
| 612 | - foreach ($nodes as $node) { |
|
| 613 | - $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_USER, $node, false, -1, 0)); |
|
| 614 | - $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_GROUP, $node, false, -1, 0)); |
|
| 615 | - $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_LINK, $node, false, -1, 0)); |
|
| 616 | - if($this->shareManager->shareProviderExists(Share::SHARE_TYPE_EMAIL)) { |
|
| 617 | - $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_EMAIL, $node, false, -1, 0)); |
|
| 618 | - } |
|
| 619 | - if ($this->shareManager->outgoingServer2ServerSharesAllowed()) { |
|
| 620 | - $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_REMOTE, $node, false, -1, 0)); |
|
| 621 | - } |
|
| 622 | - $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_ROOM, $node, false, -1, 0)); |
|
| 623 | - } |
|
| 624 | - |
|
| 625 | - $formatted = []; |
|
| 626 | - foreach ($shares as $share) { |
|
| 627 | - try { |
|
| 628 | - $formatted[] = $this->formatShare($share); |
|
| 629 | - } catch (NotFoundException $e) { |
|
| 630 | - //Ignore this share |
|
| 631 | - } |
|
| 632 | - } |
|
| 633 | - |
|
| 634 | - return new DataResponse($formatted); |
|
| 635 | - } |
|
| 636 | - |
|
| 637 | - /** |
|
| 638 | - * The getShares function. |
|
| 639 | - * |
|
| 640 | - * @NoAdminRequired |
|
| 641 | - * |
|
| 642 | - * @param string $shared_with_me |
|
| 643 | - * @param string $reshares |
|
| 644 | - * @param string $subfiles |
|
| 645 | - * @param string $path |
|
| 646 | - * |
|
| 647 | - * - Get shares by the current user |
|
| 648 | - * - Get shares by the current user and reshares (?reshares=true) |
|
| 649 | - * - Get shares with the current user (?shared_with_me=true) |
|
| 650 | - * - Get shares for a specific path (?path=...) |
|
| 651 | - * - Get all shares in a folder (?subfiles=true&path=..) |
|
| 652 | - * |
|
| 653 | - * @return DataResponse |
|
| 654 | - * @throws OCSNotFoundException |
|
| 655 | - */ |
|
| 656 | - public function getShares( |
|
| 657 | - string $shared_with_me = 'false', |
|
| 658 | - string $reshares = 'false', |
|
| 659 | - string $subfiles = 'false', |
|
| 660 | - string $path = null, |
|
| 661 | - string $include_tags = 'false' |
|
| 662 | - ): DataResponse { |
|
| 663 | - |
|
| 664 | - if ($path !== null) { |
|
| 665 | - $userFolder = $this->rootFolder->getUserFolder($this->currentUser); |
|
| 666 | - try { |
|
| 667 | - $path = $userFolder->get($path); |
|
| 668 | - $this->lock($path); |
|
| 669 | - } catch (\OCP\Files\NotFoundException $e) { |
|
| 670 | - throw new OCSNotFoundException($this->l->t('Wrong path, file/folder doesn\'t exist')); |
|
| 671 | - } catch (LockedException $e) { |
|
| 672 | - throw new OCSNotFoundException($this->l->t('Could not lock path')); |
|
| 673 | - } |
|
| 674 | - } |
|
| 675 | - |
|
| 676 | - $include_tags = $include_tags === 'true'; |
|
| 677 | - |
|
| 678 | - if ($shared_with_me === 'true') { |
|
| 679 | - $result = $this->getSharedWithMe($path, $include_tags); |
|
| 680 | - return $result; |
|
| 681 | - } |
|
| 682 | - |
|
| 683 | - if ($subfiles === 'true') { |
|
| 684 | - $result = $this->getSharesInDir($path); |
|
| 685 | - return $result; |
|
| 686 | - } |
|
| 687 | - |
|
| 688 | - if ($reshares === 'true') { |
|
| 689 | - $reshares = true; |
|
| 690 | - } else { |
|
| 691 | - $reshares = false; |
|
| 692 | - } |
|
| 693 | - |
|
| 694 | - // Get all shares |
|
| 695 | - $userShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_USER, $path, $reshares, -1, 0); |
|
| 696 | - $groupShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_GROUP, $path, $reshares, -1, 0); |
|
| 697 | - $linkShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_LINK, $path, $reshares, -1, 0); |
|
| 698 | - if ($this->shareManager->shareProviderExists(Share::SHARE_TYPE_EMAIL)) { |
|
| 699 | - $mailShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_EMAIL, $path, $reshares, -1, 0); |
|
| 700 | - } else { |
|
| 701 | - $mailShares = []; |
|
| 702 | - } |
|
| 703 | - if ($this->shareManager->shareProviderExists(Share::SHARE_TYPE_CIRCLE)) { |
|
| 704 | - $circleShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_CIRCLE, $path, $reshares, -1, 0); |
|
| 705 | - } else { |
|
| 706 | - $circleShares = []; |
|
| 707 | - } |
|
| 708 | - $roomShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_ROOM, $path, $reshares, -1, 0); |
|
| 709 | - |
|
| 710 | - $shares = array_merge($userShares, $groupShares, $linkShares, $mailShares, $circleShares, $roomShares); |
|
| 711 | - |
|
| 712 | - if ($this->shareManager->outgoingServer2ServerSharesAllowed()) { |
|
| 713 | - $federatedShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_REMOTE, $path, $reshares, -1, 0); |
|
| 714 | - $shares = array_merge($shares, $federatedShares); |
|
| 715 | - } |
|
| 716 | - |
|
| 717 | - if ($this->shareManager->outgoingServer2ServerGroupSharesAllowed()) { |
|
| 718 | - $federatedShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_REMOTE_GROUP, $path, $reshares, -1, 0); |
|
| 719 | - $shares = array_merge($shares, $federatedShares); |
|
| 720 | - } |
|
| 721 | - |
|
| 722 | - $formatted = []; |
|
| 723 | - foreach ($shares as $share) { |
|
| 724 | - try { |
|
| 725 | - $formatted[] = $this->formatShare($share, $path); |
|
| 726 | - } catch (NotFoundException $e) { |
|
| 727 | - //Ignore share |
|
| 728 | - } |
|
| 729 | - } |
|
| 730 | - |
|
| 731 | - if ($include_tags) { |
|
| 732 | - $formatted = Helper::populateTags($formatted, 'file_source', \OC::$server->getTagManager()); |
|
| 733 | - } |
|
| 734 | - |
|
| 735 | - return new DataResponse($formatted); |
|
| 736 | - } |
|
| 737 | - |
|
| 738 | - /** |
|
| 739 | - * @NoAdminRequired |
|
| 740 | - * |
|
| 741 | - * @param string $id |
|
| 742 | - * @param int $permissions |
|
| 743 | - * @param string $password |
|
| 744 | - * @param string $sendPasswordByTalk |
|
| 745 | - * @param string $publicUpload |
|
| 746 | - * @param string $expireDate |
|
| 747 | - * @param string $note |
|
| 748 | - * @return DataResponse |
|
| 749 | - * @throws LockedException |
|
| 750 | - * @throws NotFoundException |
|
| 751 | - * @throws OCSBadRequestException |
|
| 752 | - * @throws OCSForbiddenException |
|
| 753 | - * @throws OCSNotFoundException |
|
| 754 | - */ |
|
| 755 | - public function updateShare( |
|
| 756 | - string $id, |
|
| 757 | - int $permissions = null, |
|
| 758 | - string $password = null, |
|
| 759 | - string $sendPasswordByTalk = null, |
|
| 760 | - string $publicUpload = null, |
|
| 761 | - string $expireDate = null, |
|
| 762 | - string $note = null |
|
| 763 | - ): DataResponse { |
|
| 764 | - try { |
|
| 765 | - $share = $this->getShareById($id); |
|
| 766 | - } catch (ShareNotFound $e) { |
|
| 767 | - throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist')); |
|
| 768 | - } |
|
| 769 | - |
|
| 770 | - $this->lock($share->getNode()); |
|
| 771 | - |
|
| 772 | - if (!$this->canAccessShare($share, false)) { |
|
| 773 | - throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist')); |
|
| 774 | - } |
|
| 775 | - |
|
| 776 | - if ($permissions === null && $password === null && $sendPasswordByTalk === null && $publicUpload === null && $expireDate === null && $note === null) { |
|
| 777 | - throw new OCSBadRequestException($this->l->t('Wrong or no update parameter given')); |
|
| 778 | - } |
|
| 779 | - |
|
| 780 | - if($note !== null) { |
|
| 781 | - $share->setNote($note); |
|
| 782 | - } |
|
| 783 | - |
|
| 784 | - /* |
|
| 453 | + $existingShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_LINK, $path, false, 1, 0); |
|
| 454 | + if (!empty($existingShares)) { |
|
| 455 | + return new DataResponse($this->formatShare($existingShares[0])); |
|
| 456 | + } |
|
| 457 | + |
|
| 458 | + if ($publicUpload === 'true') { |
|
| 459 | + // Check if public upload is allowed |
|
| 460 | + if (!$this->shareManager->shareApiLinkAllowPublicUpload()) { |
|
| 461 | + throw new OCSForbiddenException($this->l->t('Public upload disabled by the administrator')); |
|
| 462 | + } |
|
| 463 | + |
|
| 464 | + // Public upload can only be set for folders |
|
| 465 | + if ($path instanceof \OCP\Files\File) { |
|
| 466 | + throw new OCSNotFoundException($this->l->t('Public upload is only possible for publicly shared folders')); |
|
| 467 | + } |
|
| 468 | + |
|
| 469 | + $share->setPermissions( |
|
| 470 | + Constants::PERMISSION_READ | |
|
| 471 | + Constants::PERMISSION_CREATE | |
|
| 472 | + Constants::PERMISSION_UPDATE | |
|
| 473 | + Constants::PERMISSION_DELETE |
|
| 474 | + ); |
|
| 475 | + } else { |
|
| 476 | + $share->setPermissions(Constants::PERMISSION_READ); |
|
| 477 | + } |
|
| 478 | + |
|
| 479 | + // Set password |
|
| 480 | + if ($password !== '') { |
|
| 481 | + $share->setPassword($password); |
|
| 482 | + } |
|
| 483 | + |
|
| 484 | + //Expire date |
|
| 485 | + if ($expireDate !== '') { |
|
| 486 | + try { |
|
| 487 | + $expireDate = $this->parseDate($expireDate); |
|
| 488 | + $share->setExpirationDate($expireDate); |
|
| 489 | + } catch (\Exception $e) { |
|
| 490 | + throw new OCSNotFoundException($this->l->t('Invalid date, date format must be YYYY-MM-DD')); |
|
| 491 | + } |
|
| 492 | + } |
|
| 493 | + |
|
| 494 | + } else if ($shareType === Share::SHARE_TYPE_REMOTE) { |
|
| 495 | + if (!$this->shareManager->outgoingServer2ServerSharesAllowed()) { |
|
| 496 | + throw new OCSForbiddenException($this->l->t('Sharing %s failed because the back end does not allow shares from type %s', [$path->getPath(), $shareType])); |
|
| 497 | + } |
|
| 498 | + |
|
| 499 | + $share->setSharedWith($shareWith); |
|
| 500 | + $share->setPermissions($permissions); |
|
| 501 | + } else if ($shareType === Share::SHARE_TYPE_REMOTE_GROUP) { |
|
| 502 | + if (!$this->shareManager->outgoingServer2ServerGroupSharesAllowed()) { |
|
| 503 | + throw new OCSForbiddenException($this->l->t('Sharing %s failed because the back end does not allow shares from type %s', [$path->getPath(), $shareType])); |
|
| 504 | + } |
|
| 505 | + |
|
| 506 | + $share->setSharedWith($shareWith); |
|
| 507 | + $share->setPermissions($permissions); |
|
| 508 | + } else if ($shareType === Share::SHARE_TYPE_EMAIL) { |
|
| 509 | + if ($share->getNodeType() === 'file') { |
|
| 510 | + $share->setPermissions(Constants::PERMISSION_READ); |
|
| 511 | + } else { |
|
| 512 | + $share->setPermissions($permissions); |
|
| 513 | + } |
|
| 514 | + $share->setSharedWith($shareWith); |
|
| 515 | + |
|
| 516 | + if ($sendPasswordByTalk === 'true') { |
|
| 517 | + if (!$this->appManager->isEnabledForUser('spreed')) { |
|
| 518 | + throw new OCSForbiddenException($this->l->t('Sharing %s sending the password by Nextcloud Talk failed because Nextcloud Talk is not enabled', [$path->getPath()])); |
|
| 519 | + } |
|
| 520 | + |
|
| 521 | + $share->setSendPasswordByTalk(true); |
|
| 522 | + } |
|
| 523 | + } else if ($shareType === Share::SHARE_TYPE_CIRCLE) { |
|
| 524 | + if (!\OC::$server->getAppManager()->isEnabledForUser('circles') || !class_exists('\OCA\Circles\ShareByCircleProvider')) { |
|
| 525 | + throw new OCSNotFoundException($this->l->t('You cannot share to a Circle if the app is not enabled')); |
|
| 526 | + } |
|
| 527 | + |
|
| 528 | + $circle = \OCA\Circles\Api\v1\Circles::detailsCircle($shareWith); |
|
| 529 | + |
|
| 530 | + // Valid circle is required to share |
|
| 531 | + if ($circle === null) { |
|
| 532 | + throw new OCSNotFoundException($this->l->t('Please specify a valid circle')); |
|
| 533 | + } |
|
| 534 | + $share->setSharedWith($shareWith); |
|
| 535 | + $share->setPermissions($permissions); |
|
| 536 | + } else if ($shareType === Share::SHARE_TYPE_ROOM) { |
|
| 537 | + try { |
|
| 538 | + $this->getRoomShareHelper()->createShare($share, $shareWith, $permissions, $expireDate); |
|
| 539 | + } catch (QueryException $e) { |
|
| 540 | + throw new OCSForbiddenException($this->l->t('Sharing %s failed because the back end does not support room shares', [$path->getPath()])); |
|
| 541 | + } |
|
| 542 | + } else { |
|
| 543 | + throw new OCSBadRequestException($this->l->t('Unknown share type')); |
|
| 544 | + } |
|
| 545 | + |
|
| 546 | + $share->setShareType($shareType); |
|
| 547 | + $share->setSharedBy($this->currentUser); |
|
| 548 | + |
|
| 549 | + try { |
|
| 550 | + $share = $this->shareManager->createShare($share); |
|
| 551 | + } catch (GenericShareException $e) { |
|
| 552 | + $code = $e->getCode() === 0 ? 403 : $e->getCode(); |
|
| 553 | + throw new OCSException($e->getHint(), $code); |
|
| 554 | + } catch (\Exception $e) { |
|
| 555 | + throw new OCSForbiddenException($e->getMessage(), $e); |
|
| 556 | + } |
|
| 557 | + |
|
| 558 | + $output = $this->formatShare($share); |
|
| 559 | + |
|
| 560 | + return new DataResponse($output); |
|
| 561 | + } |
|
| 562 | + |
|
| 563 | + /** |
|
| 564 | + * @param \OCP\Files\File|\OCP\Files\Folder $node |
|
| 565 | + * @param boolean $includeTags |
|
| 566 | + * @return DataResponse |
|
| 567 | + */ |
|
| 568 | + private function getSharedWithMe($node = null, bool $includeTags): DataResponse { |
|
| 569 | + |
|
| 570 | + $userShares = $this->shareManager->getSharedWith($this->currentUser, Share::SHARE_TYPE_USER, $node, -1, 0); |
|
| 571 | + $groupShares = $this->shareManager->getSharedWith($this->currentUser, Share::SHARE_TYPE_GROUP, $node, -1, 0); |
|
| 572 | + $circleShares = $this->shareManager->getSharedWith($this->currentUser, Share::SHARE_TYPE_CIRCLE, $node, -1, 0); |
|
| 573 | + $roomShares = $this->shareManager->getSharedWith($this->currentUser, Share::SHARE_TYPE_ROOM, $node, -1, 0); |
|
| 574 | + |
|
| 575 | + $shares = array_merge($userShares, $groupShares, $circleShares, $roomShares); |
|
| 576 | + |
|
| 577 | + $shares = array_filter($shares, function (IShare $share) { |
|
| 578 | + return $share->getShareOwner() !== $this->currentUser; |
|
| 579 | + }); |
|
| 580 | + |
|
| 581 | + $formatted = []; |
|
| 582 | + foreach ($shares as $share) { |
|
| 583 | + if ($this->canAccessShare($share)) { |
|
| 584 | + try { |
|
| 585 | + $formatted[] = $this->formatShare($share); |
|
| 586 | + } catch (NotFoundException $e) { |
|
| 587 | + // Ignore this share |
|
| 588 | + } |
|
| 589 | + } |
|
| 590 | + } |
|
| 591 | + |
|
| 592 | + if ($includeTags) { |
|
| 593 | + $formatted = Helper::populateTags($formatted, 'file_source', \OC::$server->getTagManager()); |
|
| 594 | + } |
|
| 595 | + |
|
| 596 | + return new DataResponse($formatted); |
|
| 597 | + } |
|
| 598 | + |
|
| 599 | + /** |
|
| 600 | + * @param \OCP\Files\Folder $folder |
|
| 601 | + * @return DataResponse |
|
| 602 | + * @throws OCSBadRequestException |
|
| 603 | + */ |
|
| 604 | + private function getSharesInDir(Node $folder): DataResponse { |
|
| 605 | + if (!($folder instanceof \OCP\Files\Folder)) { |
|
| 606 | + throw new OCSBadRequestException($this->l->t('Not a directory')); |
|
| 607 | + } |
|
| 608 | + |
|
| 609 | + $nodes = $folder->getDirectoryListing(); |
|
| 610 | + /** @var \OCP\Share\IShare[] $shares */ |
|
| 611 | + $shares = []; |
|
| 612 | + foreach ($nodes as $node) { |
|
| 613 | + $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_USER, $node, false, -1, 0)); |
|
| 614 | + $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_GROUP, $node, false, -1, 0)); |
|
| 615 | + $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_LINK, $node, false, -1, 0)); |
|
| 616 | + if($this->shareManager->shareProviderExists(Share::SHARE_TYPE_EMAIL)) { |
|
| 617 | + $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_EMAIL, $node, false, -1, 0)); |
|
| 618 | + } |
|
| 619 | + if ($this->shareManager->outgoingServer2ServerSharesAllowed()) { |
|
| 620 | + $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_REMOTE, $node, false, -1, 0)); |
|
| 621 | + } |
|
| 622 | + $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_ROOM, $node, false, -1, 0)); |
|
| 623 | + } |
|
| 624 | + |
|
| 625 | + $formatted = []; |
|
| 626 | + foreach ($shares as $share) { |
|
| 627 | + try { |
|
| 628 | + $formatted[] = $this->formatShare($share); |
|
| 629 | + } catch (NotFoundException $e) { |
|
| 630 | + //Ignore this share |
|
| 631 | + } |
|
| 632 | + } |
|
| 633 | + |
|
| 634 | + return new DataResponse($formatted); |
|
| 635 | + } |
|
| 636 | + |
|
| 637 | + /** |
|
| 638 | + * The getShares function. |
|
| 639 | + * |
|
| 640 | + * @NoAdminRequired |
|
| 641 | + * |
|
| 642 | + * @param string $shared_with_me |
|
| 643 | + * @param string $reshares |
|
| 644 | + * @param string $subfiles |
|
| 645 | + * @param string $path |
|
| 646 | + * |
|
| 647 | + * - Get shares by the current user |
|
| 648 | + * - Get shares by the current user and reshares (?reshares=true) |
|
| 649 | + * - Get shares with the current user (?shared_with_me=true) |
|
| 650 | + * - Get shares for a specific path (?path=...) |
|
| 651 | + * - Get all shares in a folder (?subfiles=true&path=..) |
|
| 652 | + * |
|
| 653 | + * @return DataResponse |
|
| 654 | + * @throws OCSNotFoundException |
|
| 655 | + */ |
|
| 656 | + public function getShares( |
|
| 657 | + string $shared_with_me = 'false', |
|
| 658 | + string $reshares = 'false', |
|
| 659 | + string $subfiles = 'false', |
|
| 660 | + string $path = null, |
|
| 661 | + string $include_tags = 'false' |
|
| 662 | + ): DataResponse { |
|
| 663 | + |
|
| 664 | + if ($path !== null) { |
|
| 665 | + $userFolder = $this->rootFolder->getUserFolder($this->currentUser); |
|
| 666 | + try { |
|
| 667 | + $path = $userFolder->get($path); |
|
| 668 | + $this->lock($path); |
|
| 669 | + } catch (\OCP\Files\NotFoundException $e) { |
|
| 670 | + throw new OCSNotFoundException($this->l->t('Wrong path, file/folder doesn\'t exist')); |
|
| 671 | + } catch (LockedException $e) { |
|
| 672 | + throw new OCSNotFoundException($this->l->t('Could not lock path')); |
|
| 673 | + } |
|
| 674 | + } |
|
| 675 | + |
|
| 676 | + $include_tags = $include_tags === 'true'; |
|
| 677 | + |
|
| 678 | + if ($shared_with_me === 'true') { |
|
| 679 | + $result = $this->getSharedWithMe($path, $include_tags); |
|
| 680 | + return $result; |
|
| 681 | + } |
|
| 682 | + |
|
| 683 | + if ($subfiles === 'true') { |
|
| 684 | + $result = $this->getSharesInDir($path); |
|
| 685 | + return $result; |
|
| 686 | + } |
|
| 687 | + |
|
| 688 | + if ($reshares === 'true') { |
|
| 689 | + $reshares = true; |
|
| 690 | + } else { |
|
| 691 | + $reshares = false; |
|
| 692 | + } |
|
| 693 | + |
|
| 694 | + // Get all shares |
|
| 695 | + $userShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_USER, $path, $reshares, -1, 0); |
|
| 696 | + $groupShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_GROUP, $path, $reshares, -1, 0); |
|
| 697 | + $linkShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_LINK, $path, $reshares, -1, 0); |
|
| 698 | + if ($this->shareManager->shareProviderExists(Share::SHARE_TYPE_EMAIL)) { |
|
| 699 | + $mailShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_EMAIL, $path, $reshares, -1, 0); |
|
| 700 | + } else { |
|
| 701 | + $mailShares = []; |
|
| 702 | + } |
|
| 703 | + if ($this->shareManager->shareProviderExists(Share::SHARE_TYPE_CIRCLE)) { |
|
| 704 | + $circleShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_CIRCLE, $path, $reshares, -1, 0); |
|
| 705 | + } else { |
|
| 706 | + $circleShares = []; |
|
| 707 | + } |
|
| 708 | + $roomShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_ROOM, $path, $reshares, -1, 0); |
|
| 709 | + |
|
| 710 | + $shares = array_merge($userShares, $groupShares, $linkShares, $mailShares, $circleShares, $roomShares); |
|
| 711 | + |
|
| 712 | + if ($this->shareManager->outgoingServer2ServerSharesAllowed()) { |
|
| 713 | + $federatedShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_REMOTE, $path, $reshares, -1, 0); |
|
| 714 | + $shares = array_merge($shares, $federatedShares); |
|
| 715 | + } |
|
| 716 | + |
|
| 717 | + if ($this->shareManager->outgoingServer2ServerGroupSharesAllowed()) { |
|
| 718 | + $federatedShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_REMOTE_GROUP, $path, $reshares, -1, 0); |
|
| 719 | + $shares = array_merge($shares, $federatedShares); |
|
| 720 | + } |
|
| 721 | + |
|
| 722 | + $formatted = []; |
|
| 723 | + foreach ($shares as $share) { |
|
| 724 | + try { |
|
| 725 | + $formatted[] = $this->formatShare($share, $path); |
|
| 726 | + } catch (NotFoundException $e) { |
|
| 727 | + //Ignore share |
|
| 728 | + } |
|
| 729 | + } |
|
| 730 | + |
|
| 731 | + if ($include_tags) { |
|
| 732 | + $formatted = Helper::populateTags($formatted, 'file_source', \OC::$server->getTagManager()); |
|
| 733 | + } |
|
| 734 | + |
|
| 735 | + return new DataResponse($formatted); |
|
| 736 | + } |
|
| 737 | + |
|
| 738 | + /** |
|
| 739 | + * @NoAdminRequired |
|
| 740 | + * |
|
| 741 | + * @param string $id |
|
| 742 | + * @param int $permissions |
|
| 743 | + * @param string $password |
|
| 744 | + * @param string $sendPasswordByTalk |
|
| 745 | + * @param string $publicUpload |
|
| 746 | + * @param string $expireDate |
|
| 747 | + * @param string $note |
|
| 748 | + * @return DataResponse |
|
| 749 | + * @throws LockedException |
|
| 750 | + * @throws NotFoundException |
|
| 751 | + * @throws OCSBadRequestException |
|
| 752 | + * @throws OCSForbiddenException |
|
| 753 | + * @throws OCSNotFoundException |
|
| 754 | + */ |
|
| 755 | + public function updateShare( |
|
| 756 | + string $id, |
|
| 757 | + int $permissions = null, |
|
| 758 | + string $password = null, |
|
| 759 | + string $sendPasswordByTalk = null, |
|
| 760 | + string $publicUpload = null, |
|
| 761 | + string $expireDate = null, |
|
| 762 | + string $note = null |
|
| 763 | + ): DataResponse { |
|
| 764 | + try { |
|
| 765 | + $share = $this->getShareById($id); |
|
| 766 | + } catch (ShareNotFound $e) { |
|
| 767 | + throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist')); |
|
| 768 | + } |
|
| 769 | + |
|
| 770 | + $this->lock($share->getNode()); |
|
| 771 | + |
|
| 772 | + if (!$this->canAccessShare($share, false)) { |
|
| 773 | + throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist')); |
|
| 774 | + } |
|
| 775 | + |
|
| 776 | + if ($permissions === null && $password === null && $sendPasswordByTalk === null && $publicUpload === null && $expireDate === null && $note === null) { |
|
| 777 | + throw new OCSBadRequestException($this->l->t('Wrong or no update parameter given')); |
|
| 778 | + } |
|
| 779 | + |
|
| 780 | + if($note !== null) { |
|
| 781 | + $share->setNote($note); |
|
| 782 | + } |
|
| 783 | + |
|
| 784 | + /* |
|
| 785 | 785 | * expirationdate, password and publicUpload only make sense for link shares |
| 786 | 786 | */ |
| 787 | - if ($share->getShareType() === Share::SHARE_TYPE_LINK) { |
|
| 788 | - |
|
| 789 | - $newPermissions = null; |
|
| 790 | - if ($publicUpload === 'true') { |
|
| 791 | - $newPermissions = Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE; |
|
| 792 | - } else if ($publicUpload === 'false') { |
|
| 793 | - $newPermissions = Constants::PERMISSION_READ; |
|
| 794 | - } |
|
| 795 | - |
|
| 796 | - if ($permissions !== null) { |
|
| 797 | - $newPermissions = (int)$permissions; |
|
| 798 | - $newPermissions = $newPermissions & ~Constants::PERMISSION_SHARE; |
|
| 799 | - } |
|
| 800 | - |
|
| 801 | - if ($newPermissions !== null && |
|
| 802 | - !in_array($newPermissions, [ |
|
| 803 | - Constants::PERMISSION_READ, |
|
| 804 | - Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE, // legacy |
|
| 805 | - Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE, // correct |
|
| 806 | - Constants::PERMISSION_CREATE, // hidden file list |
|
| 807 | - Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE, // allow to edit single files |
|
| 808 | - ], true) |
|
| 809 | - ) { |
|
| 810 | - throw new OCSBadRequestException($this->l->t('Can\'t change permissions for public share links')); |
|
| 811 | - } |
|
| 812 | - |
|
| 813 | - if ( |
|
| 814 | - // legacy |
|
| 815 | - $newPermissions === (Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE) || |
|
| 816 | - // correct |
|
| 817 | - $newPermissions === (Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE) |
|
| 818 | - ) { |
|
| 819 | - if (!$this->shareManager->shareApiLinkAllowPublicUpload()) { |
|
| 820 | - throw new OCSForbiddenException($this->l->t('Public upload disabled by the administrator')); |
|
| 821 | - } |
|
| 822 | - |
|
| 823 | - if (!($share->getNode() instanceof \OCP\Files\Folder)) { |
|
| 824 | - throw new OCSBadRequestException($this->l->t('Public upload is only possible for publicly shared folders')); |
|
| 825 | - } |
|
| 826 | - |
|
| 827 | - // normalize to correct public upload permissions |
|
| 828 | - $newPermissions = Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE; |
|
| 829 | - } |
|
| 830 | - |
|
| 831 | - if ($newPermissions !== null) { |
|
| 832 | - $share->setPermissions($newPermissions); |
|
| 833 | - $permissions = $newPermissions; |
|
| 834 | - } |
|
| 835 | - |
|
| 836 | - if ($expireDate === '') { |
|
| 837 | - $share->setExpirationDate(null); |
|
| 838 | - } else if ($expireDate !== null) { |
|
| 839 | - try { |
|
| 840 | - $expireDate = $this->parseDate($expireDate); |
|
| 841 | - } catch (\Exception $e) { |
|
| 842 | - throw new OCSBadRequestException($e->getMessage(), $e); |
|
| 843 | - } |
|
| 844 | - $share->setExpirationDate($expireDate); |
|
| 845 | - } |
|
| 846 | - |
|
| 847 | - if ($password === '') { |
|
| 848 | - $share->setPassword(null); |
|
| 849 | - } else if ($password !== null) { |
|
| 850 | - $share->setPassword($password); |
|
| 851 | - } |
|
| 852 | - |
|
| 853 | - } else { |
|
| 854 | - if ($permissions !== null) { |
|
| 855 | - $permissions = (int)$permissions; |
|
| 856 | - $share->setPermissions($permissions); |
|
| 857 | - } |
|
| 858 | - |
|
| 859 | - if ($share->getShareType() === Share::SHARE_TYPE_EMAIL) { |
|
| 860 | - if ($password === '') { |
|
| 861 | - $share->setPassword(null); |
|
| 862 | - } else if ($password !== null) { |
|
| 863 | - $share->setPassword($password); |
|
| 864 | - } |
|
| 865 | - |
|
| 866 | - if ($sendPasswordByTalk === 'true') { |
|
| 867 | - if (!$this->appManager->isEnabledForUser('spreed')) { |
|
| 868 | - throw new OCSForbiddenException($this->l->t('Sharing sending the password by Nextcloud Talk failed because Nextcloud Talk is not enabled')); |
|
| 869 | - } |
|
| 870 | - |
|
| 871 | - $share->setSendPasswordByTalk(true); |
|
| 872 | - } else { |
|
| 873 | - $share->setSendPasswordByTalk(false); |
|
| 874 | - } |
|
| 875 | - } |
|
| 876 | - |
|
| 877 | - if ($expireDate === '') { |
|
| 878 | - $share->setExpirationDate(null); |
|
| 879 | - } else if ($expireDate !== null) { |
|
| 880 | - try { |
|
| 881 | - $expireDate = $this->parseDate($expireDate); |
|
| 882 | - } catch (\Exception $e) { |
|
| 883 | - throw new OCSBadRequestException($e->getMessage(), $e); |
|
| 884 | - } |
|
| 885 | - $share->setExpirationDate($expireDate); |
|
| 886 | - } |
|
| 887 | - |
|
| 888 | - } |
|
| 889 | - |
|
| 890 | - if ($permissions !== null && $share->getShareOwner() !== $this->currentUser) { |
|
| 891 | - /* Check if this is an incomming share */ |
|
| 892 | - $incomingShares = $this->shareManager->getSharedWith($this->currentUser, Share::SHARE_TYPE_USER, $share->getNode(), -1, 0); |
|
| 893 | - $incomingShares = array_merge($incomingShares, $this->shareManager->getSharedWith($this->currentUser, Share::SHARE_TYPE_GROUP, $share->getNode(), -1, 0)); |
|
| 894 | - $incomingShares = array_merge($incomingShares, $this->shareManager->getSharedWith($this->currentUser, Share::SHARE_TYPE_ROOM, $share->getNode(), -1, 0)); |
|
| 895 | - |
|
| 896 | - /** @var \OCP\Share\IShare[] $incomingShares */ |
|
| 897 | - if (!empty($incomingShares)) { |
|
| 898 | - $maxPermissions = 0; |
|
| 899 | - foreach ($incomingShares as $incomingShare) { |
|
| 900 | - $maxPermissions |= $incomingShare->getPermissions(); |
|
| 901 | - } |
|
| 902 | - |
|
| 903 | - if ($share->getPermissions() & ~$maxPermissions) { |
|
| 904 | - throw new OCSNotFoundException($this->l->t('Cannot increase permissions')); |
|
| 905 | - } |
|
| 906 | - } |
|
| 907 | - } |
|
| 908 | - |
|
| 909 | - |
|
| 910 | - try { |
|
| 911 | - $share = $this->shareManager->updateShare($share); |
|
| 912 | - } catch (\Exception $e) { |
|
| 913 | - throw new OCSBadRequestException($e->getMessage(), $e); |
|
| 914 | - } |
|
| 915 | - |
|
| 916 | - return new DataResponse($this->formatShare($share)); |
|
| 917 | - } |
|
| 918 | - |
|
| 919 | - /** |
|
| 920 | - * @suppress PhanUndeclaredClassMethod |
|
| 921 | - */ |
|
| 922 | - protected function canAccessShare(\OCP\Share\IShare $share, bool $checkGroups = true): bool { |
|
| 923 | - // A file with permissions 0 can't be accessed by us. So Don't show it |
|
| 924 | - if ($share->getPermissions() === 0) { |
|
| 925 | - return false; |
|
| 926 | - } |
|
| 927 | - |
|
| 928 | - // Owner of the file and the sharer of the file can always get share |
|
| 929 | - if ($share->getShareOwner() === $this->currentUser || |
|
| 930 | - $share->getSharedBy() === $this->currentUser |
|
| 931 | - ) { |
|
| 932 | - return true; |
|
| 933 | - } |
|
| 934 | - |
|
| 935 | - // If the share is shared with you (or a group you are a member of) |
|
| 936 | - if ($share->getShareType() === Share::SHARE_TYPE_USER && |
|
| 937 | - $share->getSharedWith() === $this->currentUser |
|
| 938 | - ) { |
|
| 939 | - return true; |
|
| 940 | - } |
|
| 941 | - |
|
| 942 | - if ($checkGroups && $share->getShareType() === Share::SHARE_TYPE_GROUP) { |
|
| 943 | - $sharedWith = $this->groupManager->get($share->getSharedWith()); |
|
| 944 | - $user = $this->userManager->get($this->currentUser); |
|
| 945 | - if ($user !== null && $sharedWith !== null && $sharedWith->inGroup($user)) { |
|
| 946 | - return true; |
|
| 947 | - } |
|
| 948 | - } |
|
| 949 | - |
|
| 950 | - if ($share->getShareType() === Share::SHARE_TYPE_CIRCLE) { |
|
| 951 | - // TODO: have a sanity check like above? |
|
| 952 | - return true; |
|
| 953 | - } |
|
| 954 | - |
|
| 955 | - if ($share->getShareType() === Share::SHARE_TYPE_ROOM) { |
|
| 956 | - try { |
|
| 957 | - return $this->getRoomShareHelper()->canAccessShare($share, $this->currentUser); |
|
| 958 | - } catch (QueryException $e) { |
|
| 959 | - return false; |
|
| 960 | - } |
|
| 961 | - } |
|
| 962 | - |
|
| 963 | - return false; |
|
| 964 | - } |
|
| 965 | - |
|
| 966 | - /** |
|
| 967 | - * Make sure that the passed date is valid ISO 8601 |
|
| 968 | - * So YYYY-MM-DD |
|
| 969 | - * If not throw an exception |
|
| 970 | - * |
|
| 971 | - * @param string $expireDate |
|
| 972 | - * |
|
| 973 | - * @throws \Exception |
|
| 974 | - * @return \DateTime |
|
| 975 | - */ |
|
| 976 | - private function parseDate(string $expireDate): \DateTime { |
|
| 977 | - try { |
|
| 978 | - $date = new \DateTime($expireDate); |
|
| 979 | - } catch (\Exception $e) { |
|
| 980 | - throw new \Exception('Invalid date. Format must be YYYY-MM-DD'); |
|
| 981 | - } |
|
| 982 | - |
|
| 983 | - if ($date === false) { |
|
| 984 | - throw new \Exception('Invalid date. Format must be YYYY-MM-DD'); |
|
| 985 | - } |
|
| 986 | - |
|
| 987 | - $date->setTime(0, 0, 0); |
|
| 988 | - |
|
| 989 | - return $date; |
|
| 990 | - } |
|
| 991 | - |
|
| 992 | - /** |
|
| 993 | - * Since we have multiple providers but the OCS Share API v1 does |
|
| 994 | - * not support this we need to check all backends. |
|
| 995 | - * |
|
| 996 | - * @param string $id |
|
| 997 | - * @return \OCP\Share\IShare |
|
| 998 | - * @throws ShareNotFound |
|
| 999 | - */ |
|
| 1000 | - private function getShareById(string $id): IShare { |
|
| 1001 | - $share = null; |
|
| 1002 | - |
|
| 1003 | - // First check if it is an internal share. |
|
| 1004 | - try { |
|
| 1005 | - $share = $this->shareManager->getShareById('ocinternal:' . $id, $this->currentUser); |
|
| 1006 | - return $share; |
|
| 1007 | - } catch (ShareNotFound $e) { |
|
| 1008 | - // Do nothing, just try the other share type |
|
| 1009 | - } |
|
| 1010 | - |
|
| 1011 | - |
|
| 1012 | - try { |
|
| 1013 | - if ($this->shareManager->shareProviderExists(Share::SHARE_TYPE_CIRCLE)) { |
|
| 1014 | - $share = $this->shareManager->getShareById('ocCircleShare:' . $id, $this->currentUser); |
|
| 1015 | - return $share; |
|
| 1016 | - } |
|
| 1017 | - } catch (ShareNotFound $e) { |
|
| 1018 | - // Do nothing, just try the other share type |
|
| 1019 | - } |
|
| 1020 | - |
|
| 1021 | - try { |
|
| 1022 | - if ($this->shareManager->shareProviderExists(Share::SHARE_TYPE_EMAIL)) { |
|
| 1023 | - $share = $this->shareManager->getShareById('ocMailShare:' . $id, $this->currentUser); |
|
| 1024 | - return $share; |
|
| 1025 | - } |
|
| 1026 | - } catch (ShareNotFound $e) { |
|
| 1027 | - // Do nothing, just try the other share type |
|
| 1028 | - } |
|
| 1029 | - |
|
| 1030 | - try { |
|
| 1031 | - $share = $this->shareManager->getShareById('ocRoomShare:' . $id, $this->currentUser); |
|
| 1032 | - return $share; |
|
| 1033 | - } catch (ShareNotFound $e) { |
|
| 1034 | - // Do nothing, just try the other share type |
|
| 1035 | - } |
|
| 1036 | - |
|
| 1037 | - if (!$this->shareManager->outgoingServer2ServerSharesAllowed()) { |
|
| 1038 | - throw new ShareNotFound(); |
|
| 1039 | - } |
|
| 1040 | - $share = $this->shareManager->getShareById('ocFederatedSharing:' . $id, $this->currentUser); |
|
| 1041 | - |
|
| 1042 | - return $share; |
|
| 1043 | - } |
|
| 1044 | - |
|
| 1045 | - /** |
|
| 1046 | - * Lock a Node |
|
| 1047 | - * |
|
| 1048 | - * @param \OCP\Files\Node $node |
|
| 1049 | - * @throws LockedException |
|
| 1050 | - */ |
|
| 1051 | - private function lock(\OCP\Files\Node $node) { |
|
| 1052 | - $node->lock(ILockingProvider::LOCK_SHARED); |
|
| 1053 | - $this->lockedNode = $node; |
|
| 1054 | - } |
|
| 1055 | - |
|
| 1056 | - /** |
|
| 1057 | - * Cleanup the remaining locks |
|
| 1058 | - * @throws @LockedException |
|
| 1059 | - */ |
|
| 1060 | - public function cleanup() { |
|
| 1061 | - if ($this->lockedNode !== null) { |
|
| 1062 | - $this->lockedNode->unlock(ILockingProvider::LOCK_SHARED); |
|
| 1063 | - } |
|
| 1064 | - } |
|
| 1065 | - |
|
| 1066 | - /** |
|
| 1067 | - * Returns the helper of ShareAPIController for room shares. |
|
| 1068 | - * |
|
| 1069 | - * If the Talk application is not enabled or the helper is not available |
|
| 1070 | - * a QueryException is thrown instead. |
|
| 1071 | - * |
|
| 1072 | - * @return \OCA\Spreed\Share\Helper\ShareAPIController |
|
| 1073 | - * @throws QueryException |
|
| 1074 | - */ |
|
| 1075 | - private function getRoomShareHelper() { |
|
| 1076 | - if (!$this->appManager->isEnabledForUser('spreed')) { |
|
| 1077 | - throw new QueryException(); |
|
| 1078 | - } |
|
| 1079 | - |
|
| 1080 | - return $this->serverContainer->query('\OCA\Spreed\Share\Helper\ShareAPIController'); |
|
| 1081 | - } |
|
| 787 | + if ($share->getShareType() === Share::SHARE_TYPE_LINK) { |
|
| 788 | + |
|
| 789 | + $newPermissions = null; |
|
| 790 | + if ($publicUpload === 'true') { |
|
| 791 | + $newPermissions = Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE; |
|
| 792 | + } else if ($publicUpload === 'false') { |
|
| 793 | + $newPermissions = Constants::PERMISSION_READ; |
|
| 794 | + } |
|
| 795 | + |
|
| 796 | + if ($permissions !== null) { |
|
| 797 | + $newPermissions = (int)$permissions; |
|
| 798 | + $newPermissions = $newPermissions & ~Constants::PERMISSION_SHARE; |
|
| 799 | + } |
|
| 800 | + |
|
| 801 | + if ($newPermissions !== null && |
|
| 802 | + !in_array($newPermissions, [ |
|
| 803 | + Constants::PERMISSION_READ, |
|
| 804 | + Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE, // legacy |
|
| 805 | + Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE, // correct |
|
| 806 | + Constants::PERMISSION_CREATE, // hidden file list |
|
| 807 | + Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE, // allow to edit single files |
|
| 808 | + ], true) |
|
| 809 | + ) { |
|
| 810 | + throw new OCSBadRequestException($this->l->t('Can\'t change permissions for public share links')); |
|
| 811 | + } |
|
| 812 | + |
|
| 813 | + if ( |
|
| 814 | + // legacy |
|
| 815 | + $newPermissions === (Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE) || |
|
| 816 | + // correct |
|
| 817 | + $newPermissions === (Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE) |
|
| 818 | + ) { |
|
| 819 | + if (!$this->shareManager->shareApiLinkAllowPublicUpload()) { |
|
| 820 | + throw new OCSForbiddenException($this->l->t('Public upload disabled by the administrator')); |
|
| 821 | + } |
|
| 822 | + |
|
| 823 | + if (!($share->getNode() instanceof \OCP\Files\Folder)) { |
|
| 824 | + throw new OCSBadRequestException($this->l->t('Public upload is only possible for publicly shared folders')); |
|
| 825 | + } |
|
| 826 | + |
|
| 827 | + // normalize to correct public upload permissions |
|
| 828 | + $newPermissions = Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE; |
|
| 829 | + } |
|
| 830 | + |
|
| 831 | + if ($newPermissions !== null) { |
|
| 832 | + $share->setPermissions($newPermissions); |
|
| 833 | + $permissions = $newPermissions; |
|
| 834 | + } |
|
| 835 | + |
|
| 836 | + if ($expireDate === '') { |
|
| 837 | + $share->setExpirationDate(null); |
|
| 838 | + } else if ($expireDate !== null) { |
|
| 839 | + try { |
|
| 840 | + $expireDate = $this->parseDate($expireDate); |
|
| 841 | + } catch (\Exception $e) { |
|
| 842 | + throw new OCSBadRequestException($e->getMessage(), $e); |
|
| 843 | + } |
|
| 844 | + $share->setExpirationDate($expireDate); |
|
| 845 | + } |
|
| 846 | + |
|
| 847 | + if ($password === '') { |
|
| 848 | + $share->setPassword(null); |
|
| 849 | + } else if ($password !== null) { |
|
| 850 | + $share->setPassword($password); |
|
| 851 | + } |
|
| 852 | + |
|
| 853 | + } else { |
|
| 854 | + if ($permissions !== null) { |
|
| 855 | + $permissions = (int)$permissions; |
|
| 856 | + $share->setPermissions($permissions); |
|
| 857 | + } |
|
| 858 | + |
|
| 859 | + if ($share->getShareType() === Share::SHARE_TYPE_EMAIL) { |
|
| 860 | + if ($password === '') { |
|
| 861 | + $share->setPassword(null); |
|
| 862 | + } else if ($password !== null) { |
|
| 863 | + $share->setPassword($password); |
|
| 864 | + } |
|
| 865 | + |
|
| 866 | + if ($sendPasswordByTalk === 'true') { |
|
| 867 | + if (!$this->appManager->isEnabledForUser('spreed')) { |
|
| 868 | + throw new OCSForbiddenException($this->l->t('Sharing sending the password by Nextcloud Talk failed because Nextcloud Talk is not enabled')); |
|
| 869 | + } |
|
| 870 | + |
|
| 871 | + $share->setSendPasswordByTalk(true); |
|
| 872 | + } else { |
|
| 873 | + $share->setSendPasswordByTalk(false); |
|
| 874 | + } |
|
| 875 | + } |
|
| 876 | + |
|
| 877 | + if ($expireDate === '') { |
|
| 878 | + $share->setExpirationDate(null); |
|
| 879 | + } else if ($expireDate !== null) { |
|
| 880 | + try { |
|
| 881 | + $expireDate = $this->parseDate($expireDate); |
|
| 882 | + } catch (\Exception $e) { |
|
| 883 | + throw new OCSBadRequestException($e->getMessage(), $e); |
|
| 884 | + } |
|
| 885 | + $share->setExpirationDate($expireDate); |
|
| 886 | + } |
|
| 887 | + |
|
| 888 | + } |
|
| 889 | + |
|
| 890 | + if ($permissions !== null && $share->getShareOwner() !== $this->currentUser) { |
|
| 891 | + /* Check if this is an incomming share */ |
|
| 892 | + $incomingShares = $this->shareManager->getSharedWith($this->currentUser, Share::SHARE_TYPE_USER, $share->getNode(), -1, 0); |
|
| 893 | + $incomingShares = array_merge($incomingShares, $this->shareManager->getSharedWith($this->currentUser, Share::SHARE_TYPE_GROUP, $share->getNode(), -1, 0)); |
|
| 894 | + $incomingShares = array_merge($incomingShares, $this->shareManager->getSharedWith($this->currentUser, Share::SHARE_TYPE_ROOM, $share->getNode(), -1, 0)); |
|
| 895 | + |
|
| 896 | + /** @var \OCP\Share\IShare[] $incomingShares */ |
|
| 897 | + if (!empty($incomingShares)) { |
|
| 898 | + $maxPermissions = 0; |
|
| 899 | + foreach ($incomingShares as $incomingShare) { |
|
| 900 | + $maxPermissions |= $incomingShare->getPermissions(); |
|
| 901 | + } |
|
| 902 | + |
|
| 903 | + if ($share->getPermissions() & ~$maxPermissions) { |
|
| 904 | + throw new OCSNotFoundException($this->l->t('Cannot increase permissions')); |
|
| 905 | + } |
|
| 906 | + } |
|
| 907 | + } |
|
| 908 | + |
|
| 909 | + |
|
| 910 | + try { |
|
| 911 | + $share = $this->shareManager->updateShare($share); |
|
| 912 | + } catch (\Exception $e) { |
|
| 913 | + throw new OCSBadRequestException($e->getMessage(), $e); |
|
| 914 | + } |
|
| 915 | + |
|
| 916 | + return new DataResponse($this->formatShare($share)); |
|
| 917 | + } |
|
| 918 | + |
|
| 919 | + /** |
|
| 920 | + * @suppress PhanUndeclaredClassMethod |
|
| 921 | + */ |
|
| 922 | + protected function canAccessShare(\OCP\Share\IShare $share, bool $checkGroups = true): bool { |
|
| 923 | + // A file with permissions 0 can't be accessed by us. So Don't show it |
|
| 924 | + if ($share->getPermissions() === 0) { |
|
| 925 | + return false; |
|
| 926 | + } |
|
| 927 | + |
|
| 928 | + // Owner of the file and the sharer of the file can always get share |
|
| 929 | + if ($share->getShareOwner() === $this->currentUser || |
|
| 930 | + $share->getSharedBy() === $this->currentUser |
|
| 931 | + ) { |
|
| 932 | + return true; |
|
| 933 | + } |
|
| 934 | + |
|
| 935 | + // If the share is shared with you (or a group you are a member of) |
|
| 936 | + if ($share->getShareType() === Share::SHARE_TYPE_USER && |
|
| 937 | + $share->getSharedWith() === $this->currentUser |
|
| 938 | + ) { |
|
| 939 | + return true; |
|
| 940 | + } |
|
| 941 | + |
|
| 942 | + if ($checkGroups && $share->getShareType() === Share::SHARE_TYPE_GROUP) { |
|
| 943 | + $sharedWith = $this->groupManager->get($share->getSharedWith()); |
|
| 944 | + $user = $this->userManager->get($this->currentUser); |
|
| 945 | + if ($user !== null && $sharedWith !== null && $sharedWith->inGroup($user)) { |
|
| 946 | + return true; |
|
| 947 | + } |
|
| 948 | + } |
|
| 949 | + |
|
| 950 | + if ($share->getShareType() === Share::SHARE_TYPE_CIRCLE) { |
|
| 951 | + // TODO: have a sanity check like above? |
|
| 952 | + return true; |
|
| 953 | + } |
|
| 954 | + |
|
| 955 | + if ($share->getShareType() === Share::SHARE_TYPE_ROOM) { |
|
| 956 | + try { |
|
| 957 | + return $this->getRoomShareHelper()->canAccessShare($share, $this->currentUser); |
|
| 958 | + } catch (QueryException $e) { |
|
| 959 | + return false; |
|
| 960 | + } |
|
| 961 | + } |
|
| 962 | + |
|
| 963 | + return false; |
|
| 964 | + } |
|
| 965 | + |
|
| 966 | + /** |
|
| 967 | + * Make sure that the passed date is valid ISO 8601 |
|
| 968 | + * So YYYY-MM-DD |
|
| 969 | + * If not throw an exception |
|
| 970 | + * |
|
| 971 | + * @param string $expireDate |
|
| 972 | + * |
|
| 973 | + * @throws \Exception |
|
| 974 | + * @return \DateTime |
|
| 975 | + */ |
|
| 976 | + private function parseDate(string $expireDate): \DateTime { |
|
| 977 | + try { |
|
| 978 | + $date = new \DateTime($expireDate); |
|
| 979 | + } catch (\Exception $e) { |
|
| 980 | + throw new \Exception('Invalid date. Format must be YYYY-MM-DD'); |
|
| 981 | + } |
|
| 982 | + |
|
| 983 | + if ($date === false) { |
|
| 984 | + throw new \Exception('Invalid date. Format must be YYYY-MM-DD'); |
|
| 985 | + } |
|
| 986 | + |
|
| 987 | + $date->setTime(0, 0, 0); |
|
| 988 | + |
|
| 989 | + return $date; |
|
| 990 | + } |
|
| 991 | + |
|
| 992 | + /** |
|
| 993 | + * Since we have multiple providers but the OCS Share API v1 does |
|
| 994 | + * not support this we need to check all backends. |
|
| 995 | + * |
|
| 996 | + * @param string $id |
|
| 997 | + * @return \OCP\Share\IShare |
|
| 998 | + * @throws ShareNotFound |
|
| 999 | + */ |
|
| 1000 | + private function getShareById(string $id): IShare { |
|
| 1001 | + $share = null; |
|
| 1002 | + |
|
| 1003 | + // First check if it is an internal share. |
|
| 1004 | + try { |
|
| 1005 | + $share = $this->shareManager->getShareById('ocinternal:' . $id, $this->currentUser); |
|
| 1006 | + return $share; |
|
| 1007 | + } catch (ShareNotFound $e) { |
|
| 1008 | + // Do nothing, just try the other share type |
|
| 1009 | + } |
|
| 1010 | + |
|
| 1011 | + |
|
| 1012 | + try { |
|
| 1013 | + if ($this->shareManager->shareProviderExists(Share::SHARE_TYPE_CIRCLE)) { |
|
| 1014 | + $share = $this->shareManager->getShareById('ocCircleShare:' . $id, $this->currentUser); |
|
| 1015 | + return $share; |
|
| 1016 | + } |
|
| 1017 | + } catch (ShareNotFound $e) { |
|
| 1018 | + // Do nothing, just try the other share type |
|
| 1019 | + } |
|
| 1020 | + |
|
| 1021 | + try { |
|
| 1022 | + if ($this->shareManager->shareProviderExists(Share::SHARE_TYPE_EMAIL)) { |
|
| 1023 | + $share = $this->shareManager->getShareById('ocMailShare:' . $id, $this->currentUser); |
|
| 1024 | + return $share; |
|
| 1025 | + } |
|
| 1026 | + } catch (ShareNotFound $e) { |
|
| 1027 | + // Do nothing, just try the other share type |
|
| 1028 | + } |
|
| 1029 | + |
|
| 1030 | + try { |
|
| 1031 | + $share = $this->shareManager->getShareById('ocRoomShare:' . $id, $this->currentUser); |
|
| 1032 | + return $share; |
|
| 1033 | + } catch (ShareNotFound $e) { |
|
| 1034 | + // Do nothing, just try the other share type |
|
| 1035 | + } |
|
| 1036 | + |
|
| 1037 | + if (!$this->shareManager->outgoingServer2ServerSharesAllowed()) { |
|
| 1038 | + throw new ShareNotFound(); |
|
| 1039 | + } |
|
| 1040 | + $share = $this->shareManager->getShareById('ocFederatedSharing:' . $id, $this->currentUser); |
|
| 1041 | + |
|
| 1042 | + return $share; |
|
| 1043 | + } |
|
| 1044 | + |
|
| 1045 | + /** |
|
| 1046 | + * Lock a Node |
|
| 1047 | + * |
|
| 1048 | + * @param \OCP\Files\Node $node |
|
| 1049 | + * @throws LockedException |
|
| 1050 | + */ |
|
| 1051 | + private function lock(\OCP\Files\Node $node) { |
|
| 1052 | + $node->lock(ILockingProvider::LOCK_SHARED); |
|
| 1053 | + $this->lockedNode = $node; |
|
| 1054 | + } |
|
| 1055 | + |
|
| 1056 | + /** |
|
| 1057 | + * Cleanup the remaining locks |
|
| 1058 | + * @throws @LockedException |
|
| 1059 | + */ |
|
| 1060 | + public function cleanup() { |
|
| 1061 | + if ($this->lockedNode !== null) { |
|
| 1062 | + $this->lockedNode->unlock(ILockingProvider::LOCK_SHARED); |
|
| 1063 | + } |
|
| 1064 | + } |
|
| 1065 | + |
|
| 1066 | + /** |
|
| 1067 | + * Returns the helper of ShareAPIController for room shares. |
|
| 1068 | + * |
|
| 1069 | + * If the Talk application is not enabled or the helper is not available |
|
| 1070 | + * a QueryException is thrown instead. |
|
| 1071 | + * |
|
| 1072 | + * @return \OCA\Spreed\Share\Helper\ShareAPIController |
|
| 1073 | + * @throws QueryException |
|
| 1074 | + */ |
|
| 1075 | + private function getRoomShareHelper() { |
|
| 1076 | + if (!$this->appManager->isEnabledForUser('spreed')) { |
|
| 1077 | + throw new QueryException(); |
|
| 1078 | + } |
|
| 1079 | + |
|
| 1080 | + return $this->serverContainer->query('\OCA\Spreed\Share\Helper\ShareAPIController'); |
|
| 1081 | + } |
|
| 1082 | 1082 | } |
@@ -231,14 +231,14 @@ discard block |
||
| 231 | 231 | |
| 232 | 232 | $result['share_with_displayname'] = $share->getSharedWithDisplayName(); |
| 233 | 233 | if (empty($result['share_with_displayname'])) { |
| 234 | - $displayNameLength = ($hasCircleId? strrpos($share->getSharedWith(), ' '): strlen($share->getSharedWith())); |
|
| 234 | + $displayNameLength = ($hasCircleId ? strrpos($share->getSharedWith(), ' ') : strlen($share->getSharedWith())); |
|
| 235 | 235 | $result['share_with_displayname'] = substr($share->getSharedWith(), 0, $displayNameLength); |
| 236 | 236 | } |
| 237 | 237 | |
| 238 | 238 | $result['share_with_avatar'] = $share->getSharedWithAvatar(); |
| 239 | 239 | |
| 240 | - $shareWithStart = ($hasCircleId? strrpos($share->getSharedWith(), '[') + 1: 0); |
|
| 241 | - $shareWithLength = ($hasCircleId? -1: strpos($share->getSharedWith(), ' ')); |
|
| 240 | + $shareWithStart = ($hasCircleId ? strrpos($share->getSharedWith(), '[') + 1 : 0); |
|
| 241 | + $shareWithLength = ($hasCircleId ? -1 : strpos($share->getSharedWith(), ' ')); |
|
| 242 | 242 | $result['share_with'] = substr($share->getSharedWith(), $shareWithStart, $shareWithLength); |
| 243 | 243 | } else if ($share->getShareType() === Share::SHARE_TYPE_ROOM) { |
| 244 | 244 | $result['share_with'] = $share->getSharedWith(); |
@@ -268,7 +268,7 @@ discard block |
||
| 268 | 268 | // FIXME: If we inject the contacts manager it gets initialized bofore any address books are registered |
| 269 | 269 | $result = \OC::$server->getContactsManager()->search($query, [$property]); |
| 270 | 270 | foreach ($result as $r) { |
| 271 | - foreach($r[$property] as $value) { |
|
| 271 | + foreach ($r[$property] as $value) { |
|
| 272 | 272 | if ($value === $query) { |
| 273 | 273 | return $r['FN']; |
| 274 | 274 | } |
@@ -498,7 +498,7 @@ discard block |
||
| 498 | 498 | |
| 499 | 499 | $share->setSharedWith($shareWith); |
| 500 | 500 | $share->setPermissions($permissions); |
| 501 | - } else if ($shareType === Share::SHARE_TYPE_REMOTE_GROUP) { |
|
| 501 | + } else if ($shareType === Share::SHARE_TYPE_REMOTE_GROUP) { |
|
| 502 | 502 | if (!$this->shareManager->outgoingServer2ServerGroupSharesAllowed()) { |
| 503 | 503 | throw new OCSForbiddenException($this->l->t('Sharing %s failed because the back end does not allow shares from type %s', [$path->getPath(), $shareType])); |
| 504 | 504 | } |
@@ -574,7 +574,7 @@ discard block |
||
| 574 | 574 | |
| 575 | 575 | $shares = array_merge($userShares, $groupShares, $circleShares, $roomShares); |
| 576 | 576 | |
| 577 | - $shares = array_filter($shares, function (IShare $share) { |
|
| 577 | + $shares = array_filter($shares, function(IShare $share) { |
|
| 578 | 578 | return $share->getShareOwner() !== $this->currentUser; |
| 579 | 579 | }); |
| 580 | 580 | |
@@ -613,7 +613,7 @@ discard block |
||
| 613 | 613 | $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_USER, $node, false, -1, 0)); |
| 614 | 614 | $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_GROUP, $node, false, -1, 0)); |
| 615 | 615 | $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_LINK, $node, false, -1, 0)); |
| 616 | - if($this->shareManager->shareProviderExists(Share::SHARE_TYPE_EMAIL)) { |
|
| 616 | + if ($this->shareManager->shareProviderExists(Share::SHARE_TYPE_EMAIL)) { |
|
| 617 | 617 | $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_EMAIL, $node, false, -1, 0)); |
| 618 | 618 | } |
| 619 | 619 | if ($this->shareManager->outgoingServer2ServerSharesAllowed()) { |
@@ -777,7 +777,7 @@ discard block |
||
| 777 | 777 | throw new OCSBadRequestException($this->l->t('Wrong or no update parameter given')); |
| 778 | 778 | } |
| 779 | 779 | |
| 780 | - if($note !== null) { |
|
| 780 | + if ($note !== null) { |
|
| 781 | 781 | $share->setNote($note); |
| 782 | 782 | } |
| 783 | 783 | |
@@ -794,7 +794,7 @@ discard block |
||
| 794 | 794 | } |
| 795 | 795 | |
| 796 | 796 | if ($permissions !== null) { |
| 797 | - $newPermissions = (int)$permissions; |
|
| 797 | + $newPermissions = (int) $permissions; |
|
| 798 | 798 | $newPermissions = $newPermissions & ~Constants::PERMISSION_SHARE; |
| 799 | 799 | } |
| 800 | 800 | |
@@ -852,7 +852,7 @@ discard block |
||
| 852 | 852 | |
| 853 | 853 | } else { |
| 854 | 854 | if ($permissions !== null) { |
| 855 | - $permissions = (int)$permissions; |
|
| 855 | + $permissions = (int) $permissions; |
|
| 856 | 856 | $share->setPermissions($permissions); |
| 857 | 857 | } |
| 858 | 858 | |
@@ -1002,7 +1002,7 @@ discard block |
||
| 1002 | 1002 | |
| 1003 | 1003 | // First check if it is an internal share. |
| 1004 | 1004 | try { |
| 1005 | - $share = $this->shareManager->getShareById('ocinternal:' . $id, $this->currentUser); |
|
| 1005 | + $share = $this->shareManager->getShareById('ocinternal:'.$id, $this->currentUser); |
|
| 1006 | 1006 | return $share; |
| 1007 | 1007 | } catch (ShareNotFound $e) { |
| 1008 | 1008 | // Do nothing, just try the other share type |
@@ -1011,7 +1011,7 @@ discard block |
||
| 1011 | 1011 | |
| 1012 | 1012 | try { |
| 1013 | 1013 | if ($this->shareManager->shareProviderExists(Share::SHARE_TYPE_CIRCLE)) { |
| 1014 | - $share = $this->shareManager->getShareById('ocCircleShare:' . $id, $this->currentUser); |
|
| 1014 | + $share = $this->shareManager->getShareById('ocCircleShare:'.$id, $this->currentUser); |
|
| 1015 | 1015 | return $share; |
| 1016 | 1016 | } |
| 1017 | 1017 | } catch (ShareNotFound $e) { |
@@ -1020,7 +1020,7 @@ discard block |
||
| 1020 | 1020 | |
| 1021 | 1021 | try { |
| 1022 | 1022 | if ($this->shareManager->shareProviderExists(Share::SHARE_TYPE_EMAIL)) { |
| 1023 | - $share = $this->shareManager->getShareById('ocMailShare:' . $id, $this->currentUser); |
|
| 1023 | + $share = $this->shareManager->getShareById('ocMailShare:'.$id, $this->currentUser); |
|
| 1024 | 1024 | return $share; |
| 1025 | 1025 | } |
| 1026 | 1026 | } catch (ShareNotFound $e) { |
@@ -1028,7 +1028,7 @@ discard block |
||
| 1028 | 1028 | } |
| 1029 | 1029 | |
| 1030 | 1030 | try { |
| 1031 | - $share = $this->shareManager->getShareById('ocRoomShare:' . $id, $this->currentUser); |
|
| 1031 | + $share = $this->shareManager->getShareById('ocRoomShare:'.$id, $this->currentUser); |
|
| 1032 | 1032 | return $share; |
| 1033 | 1033 | } catch (ShareNotFound $e) { |
| 1034 | 1034 | // Do nothing, just try the other share type |
@@ -1037,7 +1037,7 @@ discard block |
||
| 1037 | 1037 | if (!$this->shareManager->outgoingServer2ServerSharesAllowed()) { |
| 1038 | 1038 | throw new ShareNotFound(); |
| 1039 | 1039 | } |
| 1040 | - $share = $this->shareManager->getShareById('ocFederatedSharing:' . $id, $this->currentUser); |
|
| 1040 | + $share = $this->shareManager->getShareById('ocFederatedSharing:'.$id, $this->currentUser); |
|
| 1041 | 1041 | |
| 1042 | 1042 | return $share; |
| 1043 | 1043 | } |
@@ -35,175 +35,175 @@ |
||
| 35 | 35 | use OCP\Share\IManager; |
| 36 | 36 | |
| 37 | 37 | class MountProvider implements IMountProvider { |
| 38 | - /** |
|
| 39 | - * @var \OCP\IConfig |
|
| 40 | - */ |
|
| 41 | - protected $config; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * @var IManager |
|
| 45 | - */ |
|
| 46 | - protected $shareManager; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * @var ILogger |
|
| 50 | - */ |
|
| 51 | - protected $logger; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * @param \OCP\IConfig $config |
|
| 55 | - * @param IManager $shareManager |
|
| 56 | - * @param ILogger $logger |
|
| 57 | - */ |
|
| 58 | - public function __construct(IConfig $config, IManager $shareManager, ILogger $logger) { |
|
| 59 | - $this->config = $config; |
|
| 60 | - $this->shareManager = $shareManager; |
|
| 61 | - $this->logger = $logger; |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Get all mountpoints applicable for the user and check for shares where we need to update the etags |
|
| 67 | - * |
|
| 68 | - * @param \OCP\IUser $user |
|
| 69 | - * @param \OCP\Files\Storage\IStorageFactory $storageFactory |
|
| 70 | - * @return \OCP\Files\Mount\IMountPoint[] |
|
| 71 | - */ |
|
| 72 | - public function getMountsForUser(IUser $user, IStorageFactory $storageFactory) { |
|
| 73 | - |
|
| 74 | - $shares = $this->shareManager->getSharedWith($user->getUID(), \OCP\Share::SHARE_TYPE_USER, null, -1); |
|
| 75 | - $shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), \OCP\Share::SHARE_TYPE_GROUP, null, -1)); |
|
| 76 | - $shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), \OCP\Share::SHARE_TYPE_CIRCLE, null, -1)); |
|
| 77 | - $shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), \OCP\Share::SHARE_TYPE_ROOM, null, -1)); |
|
| 78 | - |
|
| 79 | - // filter out excluded shares and group shares that includes self |
|
| 80 | - $shares = array_filter($shares, function (\OCP\Share\IShare $share) use ($user) { |
|
| 81 | - return $share->getPermissions() > 0 && $share->getShareOwner() !== $user->getUID(); |
|
| 82 | - }); |
|
| 83 | - |
|
| 84 | - $superShares = $this->buildSuperShares($shares, $user); |
|
| 85 | - |
|
| 86 | - $mounts = []; |
|
| 87 | - foreach ($superShares as $share) { |
|
| 88 | - try { |
|
| 89 | - $mounts[] = new SharedMount( |
|
| 90 | - '\OCA\Files_Sharing\SharedStorage', |
|
| 91 | - $mounts, |
|
| 92 | - [ |
|
| 93 | - 'user' => $user->getUID(), |
|
| 94 | - // parent share |
|
| 95 | - 'superShare' => $share[0], |
|
| 96 | - // children/component of the superShare |
|
| 97 | - 'groupedShares' => $share[1], |
|
| 98 | - ], |
|
| 99 | - $storageFactory |
|
| 100 | - ); |
|
| 101 | - } catch (\Exception $e) { |
|
| 102 | - $this->logger->logException($e); |
|
| 103 | - $this->logger->error('Error while trying to create shared mount'); |
|
| 104 | - } |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - // array_filter removes the null values from the array |
|
| 108 | - return array_filter($mounts); |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Groups shares by path (nodeId) and target path |
|
| 113 | - * |
|
| 114 | - * @param \OCP\Share\IShare[] $shares |
|
| 115 | - * @return \OCP\Share\IShare[][] array of grouped shares, each element in the |
|
| 116 | - * array is a group which itself is an array of shares |
|
| 117 | - */ |
|
| 118 | - private function groupShares(array $shares) { |
|
| 119 | - $tmp = []; |
|
| 120 | - |
|
| 121 | - foreach ($shares as $share) { |
|
| 122 | - if (!isset($tmp[$share->getNodeId()])) { |
|
| 123 | - $tmp[$share->getNodeId()] = []; |
|
| 124 | - } |
|
| 125 | - $tmp[$share->getNodeId()][] = $share; |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - $result = []; |
|
| 129 | - // sort by stime, the super share will be based on the least recent share |
|
| 130 | - foreach ($tmp as &$tmp2) { |
|
| 131 | - @usort($tmp2, function($a, $b) { |
|
| 132 | - if ($a->getShareTime() <= $b->getShareTime()) { |
|
| 133 | - return -1; |
|
| 134 | - } |
|
| 135 | - return 1; |
|
| 136 | - }); |
|
| 137 | - $result[] = $tmp2; |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - return array_values($result); |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - /** |
|
| 144 | - * Build super shares (virtual share) by grouping them by node id and target, |
|
| 145 | - * then for each group compute the super share and return it along with the matching |
|
| 146 | - * grouped shares. The most permissive permissions are used based on the permissions |
|
| 147 | - * of all shares within the group. |
|
| 148 | - * |
|
| 149 | - * @param \OCP\Share\IShare[] $allShares |
|
| 150 | - * @param \OCP\IUser $user user |
|
| 151 | - * @return array Tuple of [superShare, groupedShares] |
|
| 152 | - */ |
|
| 153 | - private function buildSuperShares(array $allShares, \OCP\IUser $user) { |
|
| 154 | - $result = []; |
|
| 155 | - |
|
| 156 | - $groupedShares = $this->groupShares($allShares); |
|
| 157 | - |
|
| 158 | - /** @var \OCP\Share\IShare[] $shares */ |
|
| 159 | - foreach ($groupedShares as $shares) { |
|
| 160 | - if (count($shares) === 0) { |
|
| 161 | - continue; |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - $superShare = $this->shareManager->newShare(); |
|
| 165 | - |
|
| 166 | - // compute super share based on first entry of the group |
|
| 167 | - $superShare->setId($shares[0]->getId()) |
|
| 168 | - ->setShareOwner($shares[0]->getShareOwner()) |
|
| 169 | - ->setNodeId($shares[0]->getNodeId()) |
|
| 170 | - ->setTarget($shares[0]->getTarget()); |
|
| 171 | - |
|
| 172 | - // use most permissive permissions |
|
| 173 | - $permissions = 0; |
|
| 174 | - foreach ($shares as $share) { |
|
| 175 | - $permissions |= $share->getPermissions(); |
|
| 176 | - if ($share->getTarget() !== $superShare->getTarget()) { |
|
| 177 | - // adjust target, for database consistency |
|
| 178 | - $share->setTarget($superShare->getTarget()); |
|
| 179 | - try { |
|
| 180 | - $this->shareManager->moveShare($share, $user->getUID()); |
|
| 181 | - } catch (\InvalidArgumentException $e) { |
|
| 182 | - // ignore as it is not important and we don't want to |
|
| 183 | - // block FS setup |
|
| 184 | - |
|
| 185 | - // the subsequent code anyway only uses the target of the |
|
| 186 | - // super share |
|
| 187 | - |
|
| 188 | - // such issue can usually happen when dealing with |
|
| 189 | - // null groups which usually appear with group backend |
|
| 190 | - // caching inconsistencies |
|
| 191 | - $this->logger->debug( |
|
| 192 | - 'Could not adjust share target for share ' . $share->getId() . ' to make it consistent: ' . $e->getMessage(), |
|
| 193 | - ['app' => 'files_sharing'] |
|
| 194 | - ); |
|
| 195 | - } |
|
| 196 | - } |
|
| 197 | - if (!is_null($share->getNodeCacheEntry())) { |
|
| 198 | - $superShare->setNodeCacheEntry($share->getNodeCacheEntry()); |
|
| 199 | - } |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - $superShare->setPermissions($permissions); |
|
| 203 | - |
|
| 204 | - $result[] = [$superShare, $shares]; |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - return $result; |
|
| 208 | - } |
|
| 38 | + /** |
|
| 39 | + * @var \OCP\IConfig |
|
| 40 | + */ |
|
| 41 | + protected $config; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * @var IManager |
|
| 45 | + */ |
|
| 46 | + protected $shareManager; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * @var ILogger |
|
| 50 | + */ |
|
| 51 | + protected $logger; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * @param \OCP\IConfig $config |
|
| 55 | + * @param IManager $shareManager |
|
| 56 | + * @param ILogger $logger |
|
| 57 | + */ |
|
| 58 | + public function __construct(IConfig $config, IManager $shareManager, ILogger $logger) { |
|
| 59 | + $this->config = $config; |
|
| 60 | + $this->shareManager = $shareManager; |
|
| 61 | + $this->logger = $logger; |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Get all mountpoints applicable for the user and check for shares where we need to update the etags |
|
| 67 | + * |
|
| 68 | + * @param \OCP\IUser $user |
|
| 69 | + * @param \OCP\Files\Storage\IStorageFactory $storageFactory |
|
| 70 | + * @return \OCP\Files\Mount\IMountPoint[] |
|
| 71 | + */ |
|
| 72 | + public function getMountsForUser(IUser $user, IStorageFactory $storageFactory) { |
|
| 73 | + |
|
| 74 | + $shares = $this->shareManager->getSharedWith($user->getUID(), \OCP\Share::SHARE_TYPE_USER, null, -1); |
|
| 75 | + $shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), \OCP\Share::SHARE_TYPE_GROUP, null, -1)); |
|
| 76 | + $shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), \OCP\Share::SHARE_TYPE_CIRCLE, null, -1)); |
|
| 77 | + $shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), \OCP\Share::SHARE_TYPE_ROOM, null, -1)); |
|
| 78 | + |
|
| 79 | + // filter out excluded shares and group shares that includes self |
|
| 80 | + $shares = array_filter($shares, function (\OCP\Share\IShare $share) use ($user) { |
|
| 81 | + return $share->getPermissions() > 0 && $share->getShareOwner() !== $user->getUID(); |
|
| 82 | + }); |
|
| 83 | + |
|
| 84 | + $superShares = $this->buildSuperShares($shares, $user); |
|
| 85 | + |
|
| 86 | + $mounts = []; |
|
| 87 | + foreach ($superShares as $share) { |
|
| 88 | + try { |
|
| 89 | + $mounts[] = new SharedMount( |
|
| 90 | + '\OCA\Files_Sharing\SharedStorage', |
|
| 91 | + $mounts, |
|
| 92 | + [ |
|
| 93 | + 'user' => $user->getUID(), |
|
| 94 | + // parent share |
|
| 95 | + 'superShare' => $share[0], |
|
| 96 | + // children/component of the superShare |
|
| 97 | + 'groupedShares' => $share[1], |
|
| 98 | + ], |
|
| 99 | + $storageFactory |
|
| 100 | + ); |
|
| 101 | + } catch (\Exception $e) { |
|
| 102 | + $this->logger->logException($e); |
|
| 103 | + $this->logger->error('Error while trying to create shared mount'); |
|
| 104 | + } |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + // array_filter removes the null values from the array |
|
| 108 | + return array_filter($mounts); |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Groups shares by path (nodeId) and target path |
|
| 113 | + * |
|
| 114 | + * @param \OCP\Share\IShare[] $shares |
|
| 115 | + * @return \OCP\Share\IShare[][] array of grouped shares, each element in the |
|
| 116 | + * array is a group which itself is an array of shares |
|
| 117 | + */ |
|
| 118 | + private function groupShares(array $shares) { |
|
| 119 | + $tmp = []; |
|
| 120 | + |
|
| 121 | + foreach ($shares as $share) { |
|
| 122 | + if (!isset($tmp[$share->getNodeId()])) { |
|
| 123 | + $tmp[$share->getNodeId()] = []; |
|
| 124 | + } |
|
| 125 | + $tmp[$share->getNodeId()][] = $share; |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + $result = []; |
|
| 129 | + // sort by stime, the super share will be based on the least recent share |
|
| 130 | + foreach ($tmp as &$tmp2) { |
|
| 131 | + @usort($tmp2, function($a, $b) { |
|
| 132 | + if ($a->getShareTime() <= $b->getShareTime()) { |
|
| 133 | + return -1; |
|
| 134 | + } |
|
| 135 | + return 1; |
|
| 136 | + }); |
|
| 137 | + $result[] = $tmp2; |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + return array_values($result); |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + /** |
|
| 144 | + * Build super shares (virtual share) by grouping them by node id and target, |
|
| 145 | + * then for each group compute the super share and return it along with the matching |
|
| 146 | + * grouped shares. The most permissive permissions are used based on the permissions |
|
| 147 | + * of all shares within the group. |
|
| 148 | + * |
|
| 149 | + * @param \OCP\Share\IShare[] $allShares |
|
| 150 | + * @param \OCP\IUser $user user |
|
| 151 | + * @return array Tuple of [superShare, groupedShares] |
|
| 152 | + */ |
|
| 153 | + private function buildSuperShares(array $allShares, \OCP\IUser $user) { |
|
| 154 | + $result = []; |
|
| 155 | + |
|
| 156 | + $groupedShares = $this->groupShares($allShares); |
|
| 157 | + |
|
| 158 | + /** @var \OCP\Share\IShare[] $shares */ |
|
| 159 | + foreach ($groupedShares as $shares) { |
|
| 160 | + if (count($shares) === 0) { |
|
| 161 | + continue; |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + $superShare = $this->shareManager->newShare(); |
|
| 165 | + |
|
| 166 | + // compute super share based on first entry of the group |
|
| 167 | + $superShare->setId($shares[0]->getId()) |
|
| 168 | + ->setShareOwner($shares[0]->getShareOwner()) |
|
| 169 | + ->setNodeId($shares[0]->getNodeId()) |
|
| 170 | + ->setTarget($shares[0]->getTarget()); |
|
| 171 | + |
|
| 172 | + // use most permissive permissions |
|
| 173 | + $permissions = 0; |
|
| 174 | + foreach ($shares as $share) { |
|
| 175 | + $permissions |= $share->getPermissions(); |
|
| 176 | + if ($share->getTarget() !== $superShare->getTarget()) { |
|
| 177 | + // adjust target, for database consistency |
|
| 178 | + $share->setTarget($superShare->getTarget()); |
|
| 179 | + try { |
|
| 180 | + $this->shareManager->moveShare($share, $user->getUID()); |
|
| 181 | + } catch (\InvalidArgumentException $e) { |
|
| 182 | + // ignore as it is not important and we don't want to |
|
| 183 | + // block FS setup |
|
| 184 | + |
|
| 185 | + // the subsequent code anyway only uses the target of the |
|
| 186 | + // super share |
|
| 187 | + |
|
| 188 | + // such issue can usually happen when dealing with |
|
| 189 | + // null groups which usually appear with group backend |
|
| 190 | + // caching inconsistencies |
|
| 191 | + $this->logger->debug( |
|
| 192 | + 'Could not adjust share target for share ' . $share->getId() . ' to make it consistent: ' . $e->getMessage(), |
|
| 193 | + ['app' => 'files_sharing'] |
|
| 194 | + ); |
|
| 195 | + } |
|
| 196 | + } |
|
| 197 | + if (!is_null($share->getNodeCacheEntry())) { |
|
| 198 | + $superShare->setNodeCacheEntry($share->getNodeCacheEntry()); |
|
| 199 | + } |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + $superShare->setPermissions($permissions); |
|
| 203 | + |
|
| 204 | + $result[] = [$superShare, $shares]; |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + return $result; |
|
| 208 | + } |
|
| 209 | 209 | } |
@@ -77,7 +77,7 @@ discard block |
||
| 77 | 77 | $shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), \OCP\Share::SHARE_TYPE_ROOM, null, -1)); |
| 78 | 78 | |
| 79 | 79 | // filter out excluded shares and group shares that includes self |
| 80 | - $shares = array_filter($shares, function (\OCP\Share\IShare $share) use ($user) { |
|
| 80 | + $shares = array_filter($shares, function(\OCP\Share\IShare $share) use ($user) { |
|
| 81 | 81 | return $share->getPermissions() > 0 && $share->getShareOwner() !== $user->getUID(); |
| 82 | 82 | }); |
| 83 | 83 | |
@@ -189,7 +189,7 @@ discard block |
||
| 189 | 189 | // null groups which usually appear with group backend |
| 190 | 190 | // caching inconsistencies |
| 191 | 191 | $this->logger->debug( |
| 192 | - 'Could not adjust share target for share ' . $share->getId() . ' to make it consistent: ' . $e->getMessage(), |
|
| 192 | + 'Could not adjust share target for share '.$share->getId().' to make it consistent: '.$e->getMessage(), |
|
| 193 | 193 | ['app' => 'files_sharing'] |
| 194 | 194 | ); |
| 195 | 195 | } |
@@ -34,188 +34,188 @@ |
||
| 34 | 34 | * @package OCA\AdminAudit\Actions |
| 35 | 35 | */ |
| 36 | 36 | class Sharing extends Action { |
| 37 | - /** |
|
| 38 | - * Logs sharing of data |
|
| 39 | - * |
|
| 40 | - * @param array $params |
|
| 41 | - */ |
|
| 42 | - public function shared(array $params) { |
|
| 43 | - if($params['shareType'] === Share::SHARE_TYPE_LINK) { |
|
| 44 | - $this->log( |
|
| 45 | - 'The %s "%s" with ID "%s" has been shared via link with permissions "%s" (Share ID: %s)', |
|
| 46 | - $params, |
|
| 47 | - [ |
|
| 48 | - 'itemType', |
|
| 49 | - 'itemTarget', |
|
| 50 | - 'itemSource', |
|
| 51 | - 'permissions', |
|
| 52 | - 'id', |
|
| 53 | - ] |
|
| 54 | - ); |
|
| 55 | - } elseif($params['shareType'] === Share::SHARE_TYPE_USER) { |
|
| 56 | - $this->log( |
|
| 57 | - 'The %s "%s" with ID "%s" has been shared to the user "%s" with permissions "%s" (Share ID: %s)', |
|
| 58 | - $params, |
|
| 59 | - [ |
|
| 60 | - 'itemType', |
|
| 61 | - 'itemTarget', |
|
| 62 | - 'itemSource', |
|
| 63 | - 'shareWith', |
|
| 64 | - 'permissions', |
|
| 65 | - 'id', |
|
| 66 | - ] |
|
| 67 | - ); |
|
| 68 | - } elseif($params['shareType'] === Share::SHARE_TYPE_GROUP) { |
|
| 69 | - $this->log( |
|
| 70 | - 'The %s "%s" with ID "%s" has been shared to the group "%s" with permissions "%s" (Share ID: %s)', |
|
| 71 | - $params, |
|
| 72 | - [ |
|
| 73 | - 'itemType', |
|
| 74 | - 'itemTarget', |
|
| 75 | - 'itemSource', |
|
| 76 | - 'shareWith', |
|
| 77 | - 'permissions', |
|
| 78 | - 'id', |
|
| 79 | - ] |
|
| 80 | - ); |
|
| 81 | - } elseif($params['shareType'] === Share::SHARE_TYPE_ROOM) { |
|
| 82 | - $this->log( |
|
| 83 | - 'The %s "%s" with ID "%s" has been shared to the room "%s" with permissions "%s" (Share ID: %s)', |
|
| 84 | - $params, |
|
| 85 | - [ |
|
| 86 | - 'itemType', |
|
| 87 | - 'itemTarget', |
|
| 88 | - 'itemSource', |
|
| 89 | - 'shareWith', |
|
| 90 | - 'permissions', |
|
| 91 | - 'id', |
|
| 92 | - ] |
|
| 93 | - ); |
|
| 94 | - } |
|
| 95 | - } |
|
| 37 | + /** |
|
| 38 | + * Logs sharing of data |
|
| 39 | + * |
|
| 40 | + * @param array $params |
|
| 41 | + */ |
|
| 42 | + public function shared(array $params) { |
|
| 43 | + if($params['shareType'] === Share::SHARE_TYPE_LINK) { |
|
| 44 | + $this->log( |
|
| 45 | + 'The %s "%s" with ID "%s" has been shared via link with permissions "%s" (Share ID: %s)', |
|
| 46 | + $params, |
|
| 47 | + [ |
|
| 48 | + 'itemType', |
|
| 49 | + 'itemTarget', |
|
| 50 | + 'itemSource', |
|
| 51 | + 'permissions', |
|
| 52 | + 'id', |
|
| 53 | + ] |
|
| 54 | + ); |
|
| 55 | + } elseif($params['shareType'] === Share::SHARE_TYPE_USER) { |
|
| 56 | + $this->log( |
|
| 57 | + 'The %s "%s" with ID "%s" has been shared to the user "%s" with permissions "%s" (Share ID: %s)', |
|
| 58 | + $params, |
|
| 59 | + [ |
|
| 60 | + 'itemType', |
|
| 61 | + 'itemTarget', |
|
| 62 | + 'itemSource', |
|
| 63 | + 'shareWith', |
|
| 64 | + 'permissions', |
|
| 65 | + 'id', |
|
| 66 | + ] |
|
| 67 | + ); |
|
| 68 | + } elseif($params['shareType'] === Share::SHARE_TYPE_GROUP) { |
|
| 69 | + $this->log( |
|
| 70 | + 'The %s "%s" with ID "%s" has been shared to the group "%s" with permissions "%s" (Share ID: %s)', |
|
| 71 | + $params, |
|
| 72 | + [ |
|
| 73 | + 'itemType', |
|
| 74 | + 'itemTarget', |
|
| 75 | + 'itemSource', |
|
| 76 | + 'shareWith', |
|
| 77 | + 'permissions', |
|
| 78 | + 'id', |
|
| 79 | + ] |
|
| 80 | + ); |
|
| 81 | + } elseif($params['shareType'] === Share::SHARE_TYPE_ROOM) { |
|
| 82 | + $this->log( |
|
| 83 | + 'The %s "%s" with ID "%s" has been shared to the room "%s" with permissions "%s" (Share ID: %s)', |
|
| 84 | + $params, |
|
| 85 | + [ |
|
| 86 | + 'itemType', |
|
| 87 | + 'itemTarget', |
|
| 88 | + 'itemSource', |
|
| 89 | + 'shareWith', |
|
| 90 | + 'permissions', |
|
| 91 | + 'id', |
|
| 92 | + ] |
|
| 93 | + ); |
|
| 94 | + } |
|
| 95 | + } |
|
| 96 | 96 | |
| 97 | - /** |
|
| 98 | - * Logs unsharing of data |
|
| 99 | - * |
|
| 100 | - * @param array $params |
|
| 101 | - */ |
|
| 102 | - public function unshare(array $params) { |
|
| 103 | - if($params['shareType'] === Share::SHARE_TYPE_LINK) { |
|
| 104 | - $this->log( |
|
| 105 | - 'The %s "%s" with ID "%s" has been unshared (Share ID: %s)', |
|
| 106 | - $params, |
|
| 107 | - [ |
|
| 108 | - 'itemType', |
|
| 109 | - 'fileTarget', |
|
| 110 | - 'itemSource', |
|
| 111 | - 'id', |
|
| 112 | - ] |
|
| 113 | - ); |
|
| 114 | - } elseif($params['shareType'] === Share::SHARE_TYPE_USER) { |
|
| 115 | - $this->log( |
|
| 116 | - 'The %s "%s" with ID "%s" has been unshared from the user "%s" (Share ID: %s)', |
|
| 117 | - $params, |
|
| 118 | - [ |
|
| 119 | - 'itemType', |
|
| 120 | - 'fileTarget', |
|
| 121 | - 'itemSource', |
|
| 122 | - 'shareWith', |
|
| 123 | - 'id', |
|
| 124 | - ] |
|
| 125 | - ); |
|
| 126 | - } elseif($params['shareType'] === Share::SHARE_TYPE_GROUP) { |
|
| 127 | - $this->log( |
|
| 128 | - 'The %s "%s" with ID "%s" has been unshared from the group "%s" (Share ID: %s)', |
|
| 129 | - $params, |
|
| 130 | - [ |
|
| 131 | - 'itemType', |
|
| 132 | - 'fileTarget', |
|
| 133 | - 'itemSource', |
|
| 134 | - 'shareWith', |
|
| 135 | - 'id', |
|
| 136 | - ] |
|
| 137 | - ); |
|
| 138 | - } elseif($params['shareType'] === Share::SHARE_TYPE_ROOM) { |
|
| 139 | - $this->log( |
|
| 140 | - 'The %s "%s" with ID "%s" has been unshared from the room "%s" (Share ID: %s)', |
|
| 141 | - $params, |
|
| 142 | - [ |
|
| 143 | - 'itemType', |
|
| 144 | - 'fileTarget', |
|
| 145 | - 'itemSource', |
|
| 146 | - 'shareWith', |
|
| 147 | - 'id', |
|
| 148 | - ] |
|
| 149 | - ); |
|
| 150 | - } |
|
| 151 | - } |
|
| 97 | + /** |
|
| 98 | + * Logs unsharing of data |
|
| 99 | + * |
|
| 100 | + * @param array $params |
|
| 101 | + */ |
|
| 102 | + public function unshare(array $params) { |
|
| 103 | + if($params['shareType'] === Share::SHARE_TYPE_LINK) { |
|
| 104 | + $this->log( |
|
| 105 | + 'The %s "%s" with ID "%s" has been unshared (Share ID: %s)', |
|
| 106 | + $params, |
|
| 107 | + [ |
|
| 108 | + 'itemType', |
|
| 109 | + 'fileTarget', |
|
| 110 | + 'itemSource', |
|
| 111 | + 'id', |
|
| 112 | + ] |
|
| 113 | + ); |
|
| 114 | + } elseif($params['shareType'] === Share::SHARE_TYPE_USER) { |
|
| 115 | + $this->log( |
|
| 116 | + 'The %s "%s" with ID "%s" has been unshared from the user "%s" (Share ID: %s)', |
|
| 117 | + $params, |
|
| 118 | + [ |
|
| 119 | + 'itemType', |
|
| 120 | + 'fileTarget', |
|
| 121 | + 'itemSource', |
|
| 122 | + 'shareWith', |
|
| 123 | + 'id', |
|
| 124 | + ] |
|
| 125 | + ); |
|
| 126 | + } elseif($params['shareType'] === Share::SHARE_TYPE_GROUP) { |
|
| 127 | + $this->log( |
|
| 128 | + 'The %s "%s" with ID "%s" has been unshared from the group "%s" (Share ID: %s)', |
|
| 129 | + $params, |
|
| 130 | + [ |
|
| 131 | + 'itemType', |
|
| 132 | + 'fileTarget', |
|
| 133 | + 'itemSource', |
|
| 134 | + 'shareWith', |
|
| 135 | + 'id', |
|
| 136 | + ] |
|
| 137 | + ); |
|
| 138 | + } elseif($params['shareType'] === Share::SHARE_TYPE_ROOM) { |
|
| 139 | + $this->log( |
|
| 140 | + 'The %s "%s" with ID "%s" has been unshared from the room "%s" (Share ID: %s)', |
|
| 141 | + $params, |
|
| 142 | + [ |
|
| 143 | + 'itemType', |
|
| 144 | + 'fileTarget', |
|
| 145 | + 'itemSource', |
|
| 146 | + 'shareWith', |
|
| 147 | + 'id', |
|
| 148 | + ] |
|
| 149 | + ); |
|
| 150 | + } |
|
| 151 | + } |
|
| 152 | 152 | |
| 153 | - /** |
|
| 154 | - * Logs the updating of permission changes for shares |
|
| 155 | - * |
|
| 156 | - * @param array $params |
|
| 157 | - */ |
|
| 158 | - public function updatePermissions(array $params) { |
|
| 159 | - $this->log( |
|
| 160 | - 'The permissions of the shared %s "%s" with ID "%s" have been changed to "%s"', |
|
| 161 | - $params, |
|
| 162 | - [ |
|
| 163 | - 'itemType', |
|
| 164 | - 'path', |
|
| 165 | - 'itemSource', |
|
| 166 | - 'permissions', |
|
| 167 | - ] |
|
| 168 | - ); |
|
| 169 | - } |
|
| 153 | + /** |
|
| 154 | + * Logs the updating of permission changes for shares |
|
| 155 | + * |
|
| 156 | + * @param array $params |
|
| 157 | + */ |
|
| 158 | + public function updatePermissions(array $params) { |
|
| 159 | + $this->log( |
|
| 160 | + 'The permissions of the shared %s "%s" with ID "%s" have been changed to "%s"', |
|
| 161 | + $params, |
|
| 162 | + [ |
|
| 163 | + 'itemType', |
|
| 164 | + 'path', |
|
| 165 | + 'itemSource', |
|
| 166 | + 'permissions', |
|
| 167 | + ] |
|
| 168 | + ); |
|
| 169 | + } |
|
| 170 | 170 | |
| 171 | - /** |
|
| 172 | - * Logs the password changes for a share |
|
| 173 | - * |
|
| 174 | - * @param array $params |
|
| 175 | - */ |
|
| 176 | - public function updatePassword(array $params) { |
|
| 177 | - $this->log( |
|
| 178 | - 'The password of the publicly shared %s "%s" with ID "%s" has been changed', |
|
| 179 | - $params, |
|
| 180 | - [ |
|
| 181 | - 'itemType', |
|
| 182 | - 'token', |
|
| 183 | - 'itemSource', |
|
| 184 | - ] |
|
| 185 | - ); |
|
| 186 | - } |
|
| 171 | + /** |
|
| 172 | + * Logs the password changes for a share |
|
| 173 | + * |
|
| 174 | + * @param array $params |
|
| 175 | + */ |
|
| 176 | + public function updatePassword(array $params) { |
|
| 177 | + $this->log( |
|
| 178 | + 'The password of the publicly shared %s "%s" with ID "%s" has been changed', |
|
| 179 | + $params, |
|
| 180 | + [ |
|
| 181 | + 'itemType', |
|
| 182 | + 'token', |
|
| 183 | + 'itemSource', |
|
| 184 | + ] |
|
| 185 | + ); |
|
| 186 | + } |
|
| 187 | 187 | |
| 188 | - /** |
|
| 189 | - * Logs the expiration date changes for a share |
|
| 190 | - * |
|
| 191 | - * @param array $params |
|
| 192 | - */ |
|
| 193 | - public function updateExpirationDate(array $params) { |
|
| 194 | - $this->log( |
|
| 195 | - 'The expiration date of the publicly shared %s with ID "%s" has been changed to "%s"', |
|
| 196 | - $params, |
|
| 197 | - [ |
|
| 198 | - 'itemType', |
|
| 199 | - 'itemSource', |
|
| 200 | - 'date', |
|
| 201 | - ] |
|
| 202 | - ); |
|
| 203 | - } |
|
| 188 | + /** |
|
| 189 | + * Logs the expiration date changes for a share |
|
| 190 | + * |
|
| 191 | + * @param array $params |
|
| 192 | + */ |
|
| 193 | + public function updateExpirationDate(array $params) { |
|
| 194 | + $this->log( |
|
| 195 | + 'The expiration date of the publicly shared %s with ID "%s" has been changed to "%s"', |
|
| 196 | + $params, |
|
| 197 | + [ |
|
| 198 | + 'itemType', |
|
| 199 | + 'itemSource', |
|
| 200 | + 'date', |
|
| 201 | + ] |
|
| 202 | + ); |
|
| 203 | + } |
|
| 204 | 204 | |
| 205 | - /** |
|
| 206 | - * Logs access of shared files |
|
| 207 | - * |
|
| 208 | - * @param array $params |
|
| 209 | - */ |
|
| 210 | - public function shareAccessed(array $params) { |
|
| 211 | - $this->log( |
|
| 212 | - 'The shared %s with the token "%s" by "%s" has been accessed.', |
|
| 213 | - $params, |
|
| 214 | - [ |
|
| 215 | - 'itemType', |
|
| 216 | - 'token', |
|
| 217 | - 'uidOwner', |
|
| 218 | - ] |
|
| 219 | - ); |
|
| 220 | - } |
|
| 205 | + /** |
|
| 206 | + * Logs access of shared files |
|
| 207 | + * |
|
| 208 | + * @param array $params |
|
| 209 | + */ |
|
| 210 | + public function shareAccessed(array $params) { |
|
| 211 | + $this->log( |
|
| 212 | + 'The shared %s with the token "%s" by "%s" has been accessed.', |
|
| 213 | + $params, |
|
| 214 | + [ |
|
| 215 | + 'itemType', |
|
| 216 | + 'token', |
|
| 217 | + 'uidOwner', |
|
| 218 | + ] |
|
| 219 | + ); |
|
| 220 | + } |
|
| 221 | 221 | } |
@@ -40,7 +40,7 @@ discard block |
||
| 40 | 40 | * @param array $params |
| 41 | 41 | */ |
| 42 | 42 | public function shared(array $params) { |
| 43 | - if($params['shareType'] === Share::SHARE_TYPE_LINK) { |
|
| 43 | + if ($params['shareType'] === Share::SHARE_TYPE_LINK) { |
|
| 44 | 44 | $this->log( |
| 45 | 45 | 'The %s "%s" with ID "%s" has been shared via link with permissions "%s" (Share ID: %s)', |
| 46 | 46 | $params, |
@@ -52,7 +52,7 @@ discard block |
||
| 52 | 52 | 'id', |
| 53 | 53 | ] |
| 54 | 54 | ); |
| 55 | - } elseif($params['shareType'] === Share::SHARE_TYPE_USER) { |
|
| 55 | + } elseif ($params['shareType'] === Share::SHARE_TYPE_USER) { |
|
| 56 | 56 | $this->log( |
| 57 | 57 | 'The %s "%s" with ID "%s" has been shared to the user "%s" with permissions "%s" (Share ID: %s)', |
| 58 | 58 | $params, |
@@ -65,7 +65,7 @@ discard block |
||
| 65 | 65 | 'id', |
| 66 | 66 | ] |
| 67 | 67 | ); |
| 68 | - } elseif($params['shareType'] === Share::SHARE_TYPE_GROUP) { |
|
| 68 | + } elseif ($params['shareType'] === Share::SHARE_TYPE_GROUP) { |
|
| 69 | 69 | $this->log( |
| 70 | 70 | 'The %s "%s" with ID "%s" has been shared to the group "%s" with permissions "%s" (Share ID: %s)', |
| 71 | 71 | $params, |
@@ -78,7 +78,7 @@ discard block |
||
| 78 | 78 | 'id', |
| 79 | 79 | ] |
| 80 | 80 | ); |
| 81 | - } elseif($params['shareType'] === Share::SHARE_TYPE_ROOM) { |
|
| 81 | + } elseif ($params['shareType'] === Share::SHARE_TYPE_ROOM) { |
|
| 82 | 82 | $this->log( |
| 83 | 83 | 'The %s "%s" with ID "%s" has been shared to the room "%s" with permissions "%s" (Share ID: %s)', |
| 84 | 84 | $params, |
@@ -100,7 +100,7 @@ discard block |
||
| 100 | 100 | * @param array $params |
| 101 | 101 | */ |
| 102 | 102 | public function unshare(array $params) { |
| 103 | - if($params['shareType'] === Share::SHARE_TYPE_LINK) { |
|
| 103 | + if ($params['shareType'] === Share::SHARE_TYPE_LINK) { |
|
| 104 | 104 | $this->log( |
| 105 | 105 | 'The %s "%s" with ID "%s" has been unshared (Share ID: %s)', |
| 106 | 106 | $params, |
@@ -111,7 +111,7 @@ discard block |
||
| 111 | 111 | 'id', |
| 112 | 112 | ] |
| 113 | 113 | ); |
| 114 | - } elseif($params['shareType'] === Share::SHARE_TYPE_USER) { |
|
| 114 | + } elseif ($params['shareType'] === Share::SHARE_TYPE_USER) { |
|
| 115 | 115 | $this->log( |
| 116 | 116 | 'The %s "%s" with ID "%s" has been unshared from the user "%s" (Share ID: %s)', |
| 117 | 117 | $params, |
@@ -123,7 +123,7 @@ discard block |
||
| 123 | 123 | 'id', |
| 124 | 124 | ] |
| 125 | 125 | ); |
| 126 | - } elseif($params['shareType'] === Share::SHARE_TYPE_GROUP) { |
|
| 126 | + } elseif ($params['shareType'] === Share::SHARE_TYPE_GROUP) { |
|
| 127 | 127 | $this->log( |
| 128 | 128 | 'The %s "%s" with ID "%s" has been unshared from the group "%s" (Share ID: %s)', |
| 129 | 129 | $params, |
@@ -135,7 +135,7 @@ discard block |
||
| 135 | 135 | 'id', |
| 136 | 136 | ] |
| 137 | 137 | ); |
| 138 | - } elseif($params['shareType'] === Share::SHARE_TYPE_ROOM) { |
|
| 138 | + } elseif ($params['shareType'] === Share::SHARE_TYPE_ROOM) { |
|
| 139 | 139 | $this->log( |
| 140 | 140 | 'The %s "%s" with ID "%s" has been unshared from the room "%s" (Share ID: %s)', |
| 141 | 141 | $params, |
@@ -53,244 +53,244 @@ |
||
| 53 | 53 | * @package OCA\Files\Controller |
| 54 | 54 | */ |
| 55 | 55 | class ApiController extends Controller { |
| 56 | - /** @var TagService */ |
|
| 57 | - private $tagService; |
|
| 58 | - /** @var IManager * */ |
|
| 59 | - private $shareManager; |
|
| 60 | - /** @var IPreview */ |
|
| 61 | - private $previewManager; |
|
| 62 | - /** IUserSession */ |
|
| 63 | - private $userSession; |
|
| 64 | - /** IConfig */ |
|
| 65 | - private $config; |
|
| 66 | - /** @var Folder */ |
|
| 67 | - private $userFolder; |
|
| 56 | + /** @var TagService */ |
|
| 57 | + private $tagService; |
|
| 58 | + /** @var IManager * */ |
|
| 59 | + private $shareManager; |
|
| 60 | + /** @var IPreview */ |
|
| 61 | + private $previewManager; |
|
| 62 | + /** IUserSession */ |
|
| 63 | + private $userSession; |
|
| 64 | + /** IConfig */ |
|
| 65 | + private $config; |
|
| 66 | + /** @var Folder */ |
|
| 67 | + private $userFolder; |
|
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * @param string $appName |
|
| 71 | - * @param IRequest $request |
|
| 72 | - * @param IUserSession $userSession |
|
| 73 | - * @param TagService $tagService |
|
| 74 | - * @param IPreview $previewManager |
|
| 75 | - * @param IManager $shareManager |
|
| 76 | - * @param IConfig $config |
|
| 77 | - * @param Folder $userFolder |
|
| 78 | - */ |
|
| 79 | - public function __construct($appName, |
|
| 80 | - IRequest $request, |
|
| 81 | - IUserSession $userSession, |
|
| 82 | - TagService $tagService, |
|
| 83 | - IPreview $previewManager, |
|
| 84 | - IManager $shareManager, |
|
| 85 | - IConfig $config, |
|
| 86 | - Folder $userFolder) { |
|
| 87 | - parent::__construct($appName, $request); |
|
| 88 | - $this->userSession = $userSession; |
|
| 89 | - $this->tagService = $tagService; |
|
| 90 | - $this->previewManager = $previewManager; |
|
| 91 | - $this->shareManager = $shareManager; |
|
| 92 | - $this->config = $config; |
|
| 93 | - $this->userFolder = $userFolder; |
|
| 94 | - } |
|
| 69 | + /** |
|
| 70 | + * @param string $appName |
|
| 71 | + * @param IRequest $request |
|
| 72 | + * @param IUserSession $userSession |
|
| 73 | + * @param TagService $tagService |
|
| 74 | + * @param IPreview $previewManager |
|
| 75 | + * @param IManager $shareManager |
|
| 76 | + * @param IConfig $config |
|
| 77 | + * @param Folder $userFolder |
|
| 78 | + */ |
|
| 79 | + public function __construct($appName, |
|
| 80 | + IRequest $request, |
|
| 81 | + IUserSession $userSession, |
|
| 82 | + TagService $tagService, |
|
| 83 | + IPreview $previewManager, |
|
| 84 | + IManager $shareManager, |
|
| 85 | + IConfig $config, |
|
| 86 | + Folder $userFolder) { |
|
| 87 | + parent::__construct($appName, $request); |
|
| 88 | + $this->userSession = $userSession; |
|
| 89 | + $this->tagService = $tagService; |
|
| 90 | + $this->previewManager = $previewManager; |
|
| 91 | + $this->shareManager = $shareManager; |
|
| 92 | + $this->config = $config; |
|
| 93 | + $this->userFolder = $userFolder; |
|
| 94 | + } |
|
| 95 | 95 | |
| 96 | - /** |
|
| 97 | - * Gets a thumbnail of the specified file |
|
| 98 | - * |
|
| 99 | - * @since API version 1.0 |
|
| 100 | - * |
|
| 101 | - * @NoAdminRequired |
|
| 102 | - * @NoCSRFRequired |
|
| 103 | - * @StrictCookieRequired |
|
| 104 | - * |
|
| 105 | - * @param int $x |
|
| 106 | - * @param int $y |
|
| 107 | - * @param string $file URL-encoded filename |
|
| 108 | - * @return DataResponse|FileDisplayResponse |
|
| 109 | - */ |
|
| 110 | - public function getThumbnail($x, $y, $file) { |
|
| 111 | - if ($x < 1 || $y < 1) { |
|
| 112 | - return new DataResponse(['message' => 'Requested size must be numeric and a positive value.'], Http::STATUS_BAD_REQUEST); |
|
| 113 | - } |
|
| 96 | + /** |
|
| 97 | + * Gets a thumbnail of the specified file |
|
| 98 | + * |
|
| 99 | + * @since API version 1.0 |
|
| 100 | + * |
|
| 101 | + * @NoAdminRequired |
|
| 102 | + * @NoCSRFRequired |
|
| 103 | + * @StrictCookieRequired |
|
| 104 | + * |
|
| 105 | + * @param int $x |
|
| 106 | + * @param int $y |
|
| 107 | + * @param string $file URL-encoded filename |
|
| 108 | + * @return DataResponse|FileDisplayResponse |
|
| 109 | + */ |
|
| 110 | + public function getThumbnail($x, $y, $file) { |
|
| 111 | + if ($x < 1 || $y < 1) { |
|
| 112 | + return new DataResponse(['message' => 'Requested size must be numeric and a positive value.'], Http::STATUS_BAD_REQUEST); |
|
| 113 | + } |
|
| 114 | 114 | |
| 115 | - try { |
|
| 116 | - $file = $this->userFolder->get($file); |
|
| 117 | - if ($file instanceof Folder) { |
|
| 118 | - throw new NotFoundException(); |
|
| 119 | - } |
|
| 115 | + try { |
|
| 116 | + $file = $this->userFolder->get($file); |
|
| 117 | + if ($file instanceof Folder) { |
|
| 118 | + throw new NotFoundException(); |
|
| 119 | + } |
|
| 120 | 120 | |
| 121 | - /** @var File $file */ |
|
| 122 | - $preview = $this->previewManager->getPreview($file, $x, $y, true); |
|
| 121 | + /** @var File $file */ |
|
| 122 | + $preview = $this->previewManager->getPreview($file, $x, $y, true); |
|
| 123 | 123 | |
| 124 | - return new FileDisplayResponse($preview, Http::STATUS_OK, ['Content-Type' => $preview->getMimeType()]); |
|
| 125 | - } catch (NotFoundException $e) { |
|
| 126 | - return new DataResponse(['message' => 'File not found.'], Http::STATUS_NOT_FOUND); |
|
| 127 | - } catch (\Exception $e) { |
|
| 128 | - return new DataResponse([], Http::STATUS_BAD_REQUEST); |
|
| 129 | - } |
|
| 130 | - } |
|
| 124 | + return new FileDisplayResponse($preview, Http::STATUS_OK, ['Content-Type' => $preview->getMimeType()]); |
|
| 125 | + } catch (NotFoundException $e) { |
|
| 126 | + return new DataResponse(['message' => 'File not found.'], Http::STATUS_NOT_FOUND); |
|
| 127 | + } catch (\Exception $e) { |
|
| 128 | + return new DataResponse([], Http::STATUS_BAD_REQUEST); |
|
| 129 | + } |
|
| 130 | + } |
|
| 131 | 131 | |
| 132 | - /** |
|
| 133 | - * Updates the info of the specified file path |
|
| 134 | - * The passed tags are absolute, which means they will |
|
| 135 | - * replace the actual tag selection. |
|
| 136 | - * |
|
| 137 | - * @NoAdminRequired |
|
| 138 | - * |
|
| 139 | - * @param string $path path |
|
| 140 | - * @param array|string $tags array of tags |
|
| 141 | - * @return DataResponse |
|
| 142 | - */ |
|
| 143 | - public function updateFileTags($path, $tags = null) { |
|
| 144 | - $result = []; |
|
| 145 | - // if tags specified or empty array, update tags |
|
| 146 | - if (!is_null($tags)) { |
|
| 147 | - try { |
|
| 148 | - $this->tagService->updateFileTags($path, $tags); |
|
| 149 | - } catch (\OCP\Files\NotFoundException $e) { |
|
| 150 | - return new DataResponse([ |
|
| 151 | - 'message' => $e->getMessage() |
|
| 152 | - ], Http::STATUS_NOT_FOUND); |
|
| 153 | - } catch (\OCP\Files\StorageNotAvailableException $e) { |
|
| 154 | - return new DataResponse([ |
|
| 155 | - 'message' => $e->getMessage() |
|
| 156 | - ], Http::STATUS_SERVICE_UNAVAILABLE); |
|
| 157 | - } catch (\Exception $e) { |
|
| 158 | - return new DataResponse([ |
|
| 159 | - 'message' => $e->getMessage() |
|
| 160 | - ], Http::STATUS_NOT_FOUND); |
|
| 161 | - } |
|
| 162 | - $result['tags'] = $tags; |
|
| 163 | - } |
|
| 164 | - return new DataResponse($result); |
|
| 165 | - } |
|
| 132 | + /** |
|
| 133 | + * Updates the info of the specified file path |
|
| 134 | + * The passed tags are absolute, which means they will |
|
| 135 | + * replace the actual tag selection. |
|
| 136 | + * |
|
| 137 | + * @NoAdminRequired |
|
| 138 | + * |
|
| 139 | + * @param string $path path |
|
| 140 | + * @param array|string $tags array of tags |
|
| 141 | + * @return DataResponse |
|
| 142 | + */ |
|
| 143 | + public function updateFileTags($path, $tags = null) { |
|
| 144 | + $result = []; |
|
| 145 | + // if tags specified or empty array, update tags |
|
| 146 | + if (!is_null($tags)) { |
|
| 147 | + try { |
|
| 148 | + $this->tagService->updateFileTags($path, $tags); |
|
| 149 | + } catch (\OCP\Files\NotFoundException $e) { |
|
| 150 | + return new DataResponse([ |
|
| 151 | + 'message' => $e->getMessage() |
|
| 152 | + ], Http::STATUS_NOT_FOUND); |
|
| 153 | + } catch (\OCP\Files\StorageNotAvailableException $e) { |
|
| 154 | + return new DataResponse([ |
|
| 155 | + 'message' => $e->getMessage() |
|
| 156 | + ], Http::STATUS_SERVICE_UNAVAILABLE); |
|
| 157 | + } catch (\Exception $e) { |
|
| 158 | + return new DataResponse([ |
|
| 159 | + 'message' => $e->getMessage() |
|
| 160 | + ], Http::STATUS_NOT_FOUND); |
|
| 161 | + } |
|
| 162 | + $result['tags'] = $tags; |
|
| 163 | + } |
|
| 164 | + return new DataResponse($result); |
|
| 165 | + } |
|
| 166 | 166 | |
| 167 | - /** |
|
| 168 | - * @param \OCP\Files\Node[] $nodes |
|
| 169 | - * @return array |
|
| 170 | - */ |
|
| 171 | - private function formatNodes(array $nodes) { |
|
| 172 | - return array_values(array_map(function (Node $node) { |
|
| 173 | - /** @var \OC\Files\Node\Node $shareTypes */ |
|
| 174 | - $shareTypes = $this->getShareTypes($node); |
|
| 175 | - $file = \OCA\Files\Helper::formatFileInfo($node->getFileInfo()); |
|
| 176 | - $parts = explode('/', dirname($node->getPath()), 4); |
|
| 177 | - if (isset($parts[3])) { |
|
| 178 | - $file['path'] = '/' . $parts[3]; |
|
| 179 | - } else { |
|
| 180 | - $file['path'] = '/'; |
|
| 181 | - } |
|
| 182 | - if (!empty($shareTypes)) { |
|
| 183 | - $file['shareTypes'] = $shareTypes; |
|
| 184 | - } |
|
| 185 | - return $file; |
|
| 186 | - }, $nodes)); |
|
| 187 | - } |
|
| 167 | + /** |
|
| 168 | + * @param \OCP\Files\Node[] $nodes |
|
| 169 | + * @return array |
|
| 170 | + */ |
|
| 171 | + private function formatNodes(array $nodes) { |
|
| 172 | + return array_values(array_map(function (Node $node) { |
|
| 173 | + /** @var \OC\Files\Node\Node $shareTypes */ |
|
| 174 | + $shareTypes = $this->getShareTypes($node); |
|
| 175 | + $file = \OCA\Files\Helper::formatFileInfo($node->getFileInfo()); |
|
| 176 | + $parts = explode('/', dirname($node->getPath()), 4); |
|
| 177 | + if (isset($parts[3])) { |
|
| 178 | + $file['path'] = '/' . $parts[3]; |
|
| 179 | + } else { |
|
| 180 | + $file['path'] = '/'; |
|
| 181 | + } |
|
| 182 | + if (!empty($shareTypes)) { |
|
| 183 | + $file['shareTypes'] = $shareTypes; |
|
| 184 | + } |
|
| 185 | + return $file; |
|
| 186 | + }, $nodes)); |
|
| 187 | + } |
|
| 188 | 188 | |
| 189 | - /** |
|
| 190 | - * Returns a list of recently modifed files. |
|
| 191 | - * |
|
| 192 | - * @NoAdminRequired |
|
| 193 | - * |
|
| 194 | - * @return DataResponse |
|
| 195 | - */ |
|
| 196 | - public function getRecentFiles() { |
|
| 197 | - $nodes = $this->userFolder->getRecent(100); |
|
| 198 | - $files = $this->formatNodes($nodes); |
|
| 199 | - return new DataResponse(['files' => $files]); |
|
| 200 | - } |
|
| 189 | + /** |
|
| 190 | + * Returns a list of recently modifed files. |
|
| 191 | + * |
|
| 192 | + * @NoAdminRequired |
|
| 193 | + * |
|
| 194 | + * @return DataResponse |
|
| 195 | + */ |
|
| 196 | + public function getRecentFiles() { |
|
| 197 | + $nodes = $this->userFolder->getRecent(100); |
|
| 198 | + $files = $this->formatNodes($nodes); |
|
| 199 | + return new DataResponse(['files' => $files]); |
|
| 200 | + } |
|
| 201 | 201 | |
| 202 | - /** |
|
| 203 | - * Return a list of share types for outgoing shares |
|
| 204 | - * |
|
| 205 | - * @param Node $node file node |
|
| 206 | - * |
|
| 207 | - * @return int[] array of share types |
|
| 208 | - */ |
|
| 209 | - private function getShareTypes(Node $node) { |
|
| 210 | - $userId = $this->userSession->getUser()->getUID(); |
|
| 211 | - $shareTypes = []; |
|
| 212 | - $requestedShareTypes = [ |
|
| 213 | - \OCP\Share::SHARE_TYPE_USER, |
|
| 214 | - \OCP\Share::SHARE_TYPE_GROUP, |
|
| 215 | - \OCP\Share::SHARE_TYPE_LINK, |
|
| 216 | - \OCP\Share::SHARE_TYPE_REMOTE, |
|
| 217 | - \OCP\Share::SHARE_TYPE_EMAIL, |
|
| 218 | - \OCP\Share::SHARE_TYPE_ROOM |
|
| 219 | - ]; |
|
| 220 | - foreach ($requestedShareTypes as $requestedShareType) { |
|
| 221 | - // one of each type is enough to find out about the types |
|
| 222 | - $shares = $this->shareManager->getSharesBy( |
|
| 223 | - $userId, |
|
| 224 | - $requestedShareType, |
|
| 225 | - $node, |
|
| 226 | - false, |
|
| 227 | - 1 |
|
| 228 | - ); |
|
| 229 | - if (!empty($shares)) { |
|
| 230 | - $shareTypes[] = $requestedShareType; |
|
| 231 | - } |
|
| 232 | - } |
|
| 233 | - return $shareTypes; |
|
| 234 | - } |
|
| 202 | + /** |
|
| 203 | + * Return a list of share types for outgoing shares |
|
| 204 | + * |
|
| 205 | + * @param Node $node file node |
|
| 206 | + * |
|
| 207 | + * @return int[] array of share types |
|
| 208 | + */ |
|
| 209 | + private function getShareTypes(Node $node) { |
|
| 210 | + $userId = $this->userSession->getUser()->getUID(); |
|
| 211 | + $shareTypes = []; |
|
| 212 | + $requestedShareTypes = [ |
|
| 213 | + \OCP\Share::SHARE_TYPE_USER, |
|
| 214 | + \OCP\Share::SHARE_TYPE_GROUP, |
|
| 215 | + \OCP\Share::SHARE_TYPE_LINK, |
|
| 216 | + \OCP\Share::SHARE_TYPE_REMOTE, |
|
| 217 | + \OCP\Share::SHARE_TYPE_EMAIL, |
|
| 218 | + \OCP\Share::SHARE_TYPE_ROOM |
|
| 219 | + ]; |
|
| 220 | + foreach ($requestedShareTypes as $requestedShareType) { |
|
| 221 | + // one of each type is enough to find out about the types |
|
| 222 | + $shares = $this->shareManager->getSharesBy( |
|
| 223 | + $userId, |
|
| 224 | + $requestedShareType, |
|
| 225 | + $node, |
|
| 226 | + false, |
|
| 227 | + 1 |
|
| 228 | + ); |
|
| 229 | + if (!empty($shares)) { |
|
| 230 | + $shareTypes[] = $requestedShareType; |
|
| 231 | + } |
|
| 232 | + } |
|
| 233 | + return $shareTypes; |
|
| 234 | + } |
|
| 235 | 235 | |
| 236 | - /** |
|
| 237 | - * Change the default sort mode |
|
| 238 | - * |
|
| 239 | - * @NoAdminRequired |
|
| 240 | - * |
|
| 241 | - * @param string $mode |
|
| 242 | - * @param string $direction |
|
| 243 | - * @return Response |
|
| 244 | - */ |
|
| 245 | - public function updateFileSorting($mode, $direction) { |
|
| 246 | - $allowedMode = ['name', 'size', 'mtime']; |
|
| 247 | - $allowedDirection = ['asc', 'desc']; |
|
| 248 | - if (!in_array($mode, $allowedMode) || !in_array($direction, $allowedDirection)) { |
|
| 249 | - $response = new Response(); |
|
| 250 | - $response->setStatus(Http::STATUS_UNPROCESSABLE_ENTITY); |
|
| 251 | - return $response; |
|
| 252 | - } |
|
| 253 | - $this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', 'file_sorting', $mode); |
|
| 254 | - $this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', 'file_sorting_direction', $direction); |
|
| 255 | - return new Response(); |
|
| 256 | - } |
|
| 236 | + /** |
|
| 237 | + * Change the default sort mode |
|
| 238 | + * |
|
| 239 | + * @NoAdminRequired |
|
| 240 | + * |
|
| 241 | + * @param string $mode |
|
| 242 | + * @param string $direction |
|
| 243 | + * @return Response |
|
| 244 | + */ |
|
| 245 | + public function updateFileSorting($mode, $direction) { |
|
| 246 | + $allowedMode = ['name', 'size', 'mtime']; |
|
| 247 | + $allowedDirection = ['asc', 'desc']; |
|
| 248 | + if (!in_array($mode, $allowedMode) || !in_array($direction, $allowedDirection)) { |
|
| 249 | + $response = new Response(); |
|
| 250 | + $response->setStatus(Http::STATUS_UNPROCESSABLE_ENTITY); |
|
| 251 | + return $response; |
|
| 252 | + } |
|
| 253 | + $this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', 'file_sorting', $mode); |
|
| 254 | + $this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', 'file_sorting_direction', $direction); |
|
| 255 | + return new Response(); |
|
| 256 | + } |
|
| 257 | 257 | |
| 258 | - /** |
|
| 259 | - * Toggle default for showing/hiding hidden files |
|
| 260 | - * |
|
| 261 | - * @NoAdminRequired |
|
| 262 | - * |
|
| 263 | - * @param bool $show |
|
| 264 | - */ |
|
| 265 | - public function showHiddenFiles($show) { |
|
| 266 | - $this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', 'show_hidden', (int)$show); |
|
| 267 | - return new Response(); |
|
| 268 | - } |
|
| 258 | + /** |
|
| 259 | + * Toggle default for showing/hiding hidden files |
|
| 260 | + * |
|
| 261 | + * @NoAdminRequired |
|
| 262 | + * |
|
| 263 | + * @param bool $show |
|
| 264 | + */ |
|
| 265 | + public function showHiddenFiles($show) { |
|
| 266 | + $this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', 'show_hidden', (int)$show); |
|
| 267 | + return new Response(); |
|
| 268 | + } |
|
| 269 | 269 | |
| 270 | - /** |
|
| 271 | - * Toggle default for showing/hiding xxx folder |
|
| 272 | - * |
|
| 273 | - * @NoAdminRequired |
|
| 274 | - * |
|
| 275 | - * @param bool $show |
|
| 276 | - * @param bool $key the key of the folder |
|
| 277 | - * |
|
| 278 | - * @return Response |
|
| 279 | - */ |
|
| 280 | - public function toggleShowFolder(int $show, string $key) { |
|
| 281 | - // ensure the edited key exists |
|
| 282 | - $navItems = \OCA\Files\App::getNavigationManager()->getAll(); |
|
| 283 | - foreach ($navItems as $item) { |
|
| 284 | - // check if data is valid |
|
| 285 | - if (($show === 0 || $show === 1) && isset($item['expandedState']) && $key === $item['expandedState']) { |
|
| 286 | - $this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', $key, (int)$show); |
|
| 287 | - return new Response(); |
|
| 288 | - } |
|
| 289 | - } |
|
| 290 | - $response = new Response(); |
|
| 291 | - $response->setStatus(Http::STATUS_FORBIDDEN); |
|
| 292 | - return $response; |
|
| 293 | - } |
|
| 270 | + /** |
|
| 271 | + * Toggle default for showing/hiding xxx folder |
|
| 272 | + * |
|
| 273 | + * @NoAdminRequired |
|
| 274 | + * |
|
| 275 | + * @param bool $show |
|
| 276 | + * @param bool $key the key of the folder |
|
| 277 | + * |
|
| 278 | + * @return Response |
|
| 279 | + */ |
|
| 280 | + public function toggleShowFolder(int $show, string $key) { |
|
| 281 | + // ensure the edited key exists |
|
| 282 | + $navItems = \OCA\Files\App::getNavigationManager()->getAll(); |
|
| 283 | + foreach ($navItems as $item) { |
|
| 284 | + // check if data is valid |
|
| 285 | + if (($show === 0 || $show === 1) && isset($item['expandedState']) && $key === $item['expandedState']) { |
|
| 286 | + $this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', $key, (int)$show); |
|
| 287 | + return new Response(); |
|
| 288 | + } |
|
| 289 | + } |
|
| 290 | + $response = new Response(); |
|
| 291 | + $response->setStatus(Http::STATUS_FORBIDDEN); |
|
| 292 | + return $response; |
|
| 293 | + } |
|
| 294 | 294 | |
| 295 | 295 | |
| 296 | 296 | } |
@@ -46,249 +46,249 @@ |
||
| 46 | 46 | |
| 47 | 47 | class TransferOwnership extends Command { |
| 48 | 48 | |
| 49 | - /** @var IUserManager $userManager */ |
|
| 50 | - private $userManager; |
|
| 51 | - |
|
| 52 | - /** @var IManager */ |
|
| 53 | - private $shareManager; |
|
| 54 | - |
|
| 55 | - /** @var IMountManager */ |
|
| 56 | - private $mountManager; |
|
| 57 | - |
|
| 58 | - /** @var FileInfo[] */ |
|
| 59 | - private $allFiles = []; |
|
| 60 | - |
|
| 61 | - /** @var FileInfo[] */ |
|
| 62 | - private $encryptedFiles = []; |
|
| 63 | - |
|
| 64 | - /** @var IShare[] */ |
|
| 65 | - private $shares = []; |
|
| 66 | - |
|
| 67 | - /** @var string */ |
|
| 68 | - private $sourceUser; |
|
| 69 | - |
|
| 70 | - /** @var string */ |
|
| 71 | - private $destinationUser; |
|
| 72 | - |
|
| 73 | - /** @var string */ |
|
| 74 | - private $sourcePath; |
|
| 75 | - |
|
| 76 | - /** @var string */ |
|
| 77 | - private $finalTarget; |
|
| 78 | - |
|
| 79 | - public function __construct(IUserManager $userManager, IManager $shareManager, IMountManager $mountManager) { |
|
| 80 | - $this->userManager = $userManager; |
|
| 81 | - $this->shareManager = $shareManager; |
|
| 82 | - $this->mountManager = $mountManager; |
|
| 83 | - parent::__construct(); |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - protected function configure() { |
|
| 87 | - $this |
|
| 88 | - ->setName('files:transfer-ownership') |
|
| 89 | - ->setDescription('All files and folders are moved to another user - shares are moved as well.') |
|
| 90 | - ->addArgument( |
|
| 91 | - 'source-user', |
|
| 92 | - InputArgument::REQUIRED, |
|
| 93 | - 'owner of files which shall be moved' |
|
| 94 | - ) |
|
| 95 | - ->addArgument( |
|
| 96 | - 'destination-user', |
|
| 97 | - InputArgument::REQUIRED, |
|
| 98 | - 'user who will be the new owner of the files' |
|
| 99 | - ) |
|
| 100 | - ->addOption( |
|
| 101 | - 'path', |
|
| 102 | - null, |
|
| 103 | - InputOption::VALUE_REQUIRED, |
|
| 104 | - 'selectively provide the path to transfer. For example --path="folder_name"', |
|
| 105 | - '' |
|
| 106 | - ); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - protected function execute(InputInterface $input, OutputInterface $output) { |
|
| 110 | - $sourceUserObject = $this->userManager->get($input->getArgument('source-user')); |
|
| 111 | - $destinationUserObject = $this->userManager->get($input->getArgument('destination-user')); |
|
| 112 | - |
|
| 113 | - if (!$sourceUserObject instanceof IUser) { |
|
| 114 | - $output->writeln("<error>Unknown source user $this->sourceUser</error>"); |
|
| 115 | - return 1; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - if (!$destinationUserObject instanceof IUser) { |
|
| 119 | - $output->writeln("<error>Unknown destination user $this->destinationUser</error>"); |
|
| 120 | - return 1; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - $this->sourceUser = $sourceUserObject->getUID(); |
|
| 124 | - $this->destinationUser = $destinationUserObject->getUID(); |
|
| 125 | - $sourcePathOption = ltrim($input->getOption('path'), '/'); |
|
| 126 | - $this->sourcePath = rtrim($this->sourceUser . '/files/' . $sourcePathOption, '/'); |
|
| 127 | - |
|
| 128 | - // target user has to be ready |
|
| 129 | - if (!\OC::$server->getEncryptionManager()->isReadyForUser($this->destinationUser)) { |
|
| 130 | - $output->writeln("<error>The target user is not ready to accept files. The user has at least to be logged in once.</error>"); |
|
| 131 | - return 2; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - $date = date('Y-m-d H-i-s'); |
|
| 135 | - $this->finalTarget = "$this->destinationUser/files/transferred from $this->sourceUser on $date"; |
|
| 136 | - |
|
| 137 | - // setup filesystem |
|
| 138 | - Filesystem::initMountPoints($this->sourceUser); |
|
| 139 | - Filesystem::initMountPoints($this->destinationUser); |
|
| 140 | - |
|
| 141 | - $view = new View(); |
|
| 142 | - if (!$view->is_dir($this->sourcePath)) { |
|
| 143 | - $output->writeln("<error>Unknown path provided: $sourcePathOption</error>"); |
|
| 144 | - return 1; |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - // analyse source folder |
|
| 148 | - $this->analyse($output); |
|
| 149 | - |
|
| 150 | - // collect all the shares |
|
| 151 | - $this->collectUsersShares($output); |
|
| 152 | - |
|
| 153 | - // transfer the files |
|
| 154 | - $this->transfer($output); |
|
| 155 | - |
|
| 156 | - // restore the shares |
|
| 157 | - $this->restoreShares($output); |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - private function walkFiles(View $view, $path, \Closure $callBack) { |
|
| 161 | - foreach ($view->getDirectoryContent($path) as $fileInfo) { |
|
| 162 | - if (!$callBack($fileInfo)) { |
|
| 163 | - return; |
|
| 164 | - } |
|
| 165 | - if ($fileInfo->getType() === FileInfo::TYPE_FOLDER) { |
|
| 166 | - $this->walkFiles($view, $fileInfo->getPath(), $callBack); |
|
| 167 | - } |
|
| 168 | - } |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - /** |
|
| 172 | - * @param OutputInterface $output |
|
| 173 | - * @throws \Exception |
|
| 174 | - */ |
|
| 175 | - protected function analyse(OutputInterface $output) { |
|
| 176 | - $view = new View(); |
|
| 177 | - $output->writeln("Analysing files of $this->sourceUser ..."); |
|
| 178 | - $progress = new ProgressBar($output); |
|
| 179 | - $progress->start(); |
|
| 180 | - $self = $this; |
|
| 181 | - |
|
| 182 | - $this->walkFiles($view, $this->sourcePath, |
|
| 183 | - function (FileInfo $fileInfo) use ($progress, $self) { |
|
| 184 | - if ($fileInfo->getType() === FileInfo::TYPE_FOLDER) { |
|
| 185 | - // only analyze into folders from main storage, |
|
| 186 | - if (!$fileInfo->getStorage()->instanceOfStorage(IHomeStorage::class)) { |
|
| 187 | - return false; |
|
| 188 | - } |
|
| 189 | - return true; |
|
| 190 | - } |
|
| 191 | - $progress->advance(); |
|
| 192 | - $this->allFiles[] = $fileInfo; |
|
| 193 | - if ($fileInfo->isEncrypted()) { |
|
| 194 | - $this->encryptedFiles[] = $fileInfo; |
|
| 195 | - } |
|
| 196 | - return true; |
|
| 197 | - }); |
|
| 198 | - $progress->finish(); |
|
| 199 | - $output->writeln(''); |
|
| 200 | - |
|
| 201 | - // no file is allowed to be encrypted |
|
| 202 | - if (!empty($this->encryptedFiles)) { |
|
| 203 | - $output->writeln("<error>Some files are encrypted - please decrypt them first</error>"); |
|
| 204 | - foreach($this->encryptedFiles as $encryptedFile) { |
|
| 205 | - /** @var FileInfo $encryptedFile */ |
|
| 206 | - $output->writeln(" " . $encryptedFile->getPath()); |
|
| 207 | - } |
|
| 208 | - throw new \Exception('Execution terminated.'); |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - /** |
|
| 214 | - * @param OutputInterface $output |
|
| 215 | - */ |
|
| 216 | - private function collectUsersShares(OutputInterface $output) { |
|
| 217 | - $output->writeln("Collecting all share information for files and folder of $this->sourceUser ..."); |
|
| 218 | - |
|
| 219 | - $progress = new ProgressBar($output, count($this->shares)); |
|
| 220 | - foreach([\OCP\Share::SHARE_TYPE_GROUP, \OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_LINK, \OCP\Share::SHARE_TYPE_REMOTE, \OCP\Share::SHARE_TYPE_ROOM] as $shareType) { |
|
| 221 | - $offset = 0; |
|
| 222 | - while (true) { |
|
| 223 | - $sharePage = $this->shareManager->getSharesBy($this->sourceUser, $shareType, null, true, 50, $offset); |
|
| 224 | - $progress->advance(count($sharePage)); |
|
| 225 | - if (empty($sharePage)) { |
|
| 226 | - break; |
|
| 227 | - } |
|
| 228 | - $this->shares = array_merge($this->shares, $sharePage); |
|
| 229 | - $offset += 50; |
|
| 230 | - } |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - $progress->finish(); |
|
| 234 | - $output->writeln(''); |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - /** |
|
| 238 | - * @param OutputInterface $output |
|
| 239 | - */ |
|
| 240 | - protected function transfer(OutputInterface $output) { |
|
| 241 | - $view = new View(); |
|
| 242 | - $output->writeln("Transferring files to $this->finalTarget ..."); |
|
| 243 | - |
|
| 244 | - // This change will help user to transfer the folder specified using --path option. |
|
| 245 | - // Else only the content inside folder is transferred which is not correct. |
|
| 246 | - if($this->sourcePath !== "$this->sourceUser/files") { |
|
| 247 | - $view->mkdir($this->finalTarget); |
|
| 248 | - $this->finalTarget = $this->finalTarget . '/' . basename($this->sourcePath); |
|
| 249 | - } |
|
| 250 | - $view->rename($this->sourcePath, $this->finalTarget); |
|
| 251 | - if (!is_dir("$this->sourceUser/files")) { |
|
| 252 | - // because the files folder is moved away we need to recreate it |
|
| 253 | - $view->mkdir("$this->sourceUser/files"); |
|
| 254 | - } |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - /** |
|
| 258 | - * @param OutputInterface $output |
|
| 259 | - */ |
|
| 260 | - private function restoreShares(OutputInterface $output) { |
|
| 261 | - $output->writeln("Restoring shares ..."); |
|
| 262 | - $progress = new ProgressBar($output, count($this->shares)); |
|
| 263 | - |
|
| 264 | - foreach($this->shares as $share) { |
|
| 265 | - try { |
|
| 266 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER && |
|
| 267 | - $share->getSharedWith() === $this->destinationUser) { |
|
| 268 | - // Unmount the shares before deleting, so we don't try to get the storage later on. |
|
| 269 | - $shareMountPoint = $this->mountManager->find('/' . $this->destinationUser . '/files' . $share->getTarget()); |
|
| 270 | - if ($shareMountPoint) { |
|
| 271 | - $this->mountManager->removeMount($shareMountPoint->getMountPoint()); |
|
| 272 | - } |
|
| 273 | - $this->shareManager->deleteShare($share); |
|
| 274 | - } else { |
|
| 275 | - if ($share->getShareOwner() === $this->sourceUser) { |
|
| 276 | - $share->setShareOwner($this->destinationUser); |
|
| 277 | - } |
|
| 278 | - if ($share->getSharedBy() === $this->sourceUser) { |
|
| 279 | - $share->setSharedBy($this->destinationUser); |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - $this->shareManager->updateShare($share); |
|
| 283 | - } |
|
| 284 | - } catch (\OCP\Files\NotFoundException $e) { |
|
| 285 | - $output->writeln('<error>Share with id ' . $share->getId() . ' points at deleted file, skipping</error>'); |
|
| 286 | - } catch (\Exception $e) { |
|
| 287 | - $output->writeln('<error>Could not restore share with id ' . $share->getId() . ':' . $e->getTraceAsString() . '</error>'); |
|
| 288 | - } |
|
| 289 | - $progress->advance(); |
|
| 290 | - } |
|
| 291 | - $progress->finish(); |
|
| 292 | - $output->writeln(''); |
|
| 293 | - } |
|
| 49 | + /** @var IUserManager $userManager */ |
|
| 50 | + private $userManager; |
|
| 51 | + |
|
| 52 | + /** @var IManager */ |
|
| 53 | + private $shareManager; |
|
| 54 | + |
|
| 55 | + /** @var IMountManager */ |
|
| 56 | + private $mountManager; |
|
| 57 | + |
|
| 58 | + /** @var FileInfo[] */ |
|
| 59 | + private $allFiles = []; |
|
| 60 | + |
|
| 61 | + /** @var FileInfo[] */ |
|
| 62 | + private $encryptedFiles = []; |
|
| 63 | + |
|
| 64 | + /** @var IShare[] */ |
|
| 65 | + private $shares = []; |
|
| 66 | + |
|
| 67 | + /** @var string */ |
|
| 68 | + private $sourceUser; |
|
| 69 | + |
|
| 70 | + /** @var string */ |
|
| 71 | + private $destinationUser; |
|
| 72 | + |
|
| 73 | + /** @var string */ |
|
| 74 | + private $sourcePath; |
|
| 75 | + |
|
| 76 | + /** @var string */ |
|
| 77 | + private $finalTarget; |
|
| 78 | + |
|
| 79 | + public function __construct(IUserManager $userManager, IManager $shareManager, IMountManager $mountManager) { |
|
| 80 | + $this->userManager = $userManager; |
|
| 81 | + $this->shareManager = $shareManager; |
|
| 82 | + $this->mountManager = $mountManager; |
|
| 83 | + parent::__construct(); |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + protected function configure() { |
|
| 87 | + $this |
|
| 88 | + ->setName('files:transfer-ownership') |
|
| 89 | + ->setDescription('All files and folders are moved to another user - shares are moved as well.') |
|
| 90 | + ->addArgument( |
|
| 91 | + 'source-user', |
|
| 92 | + InputArgument::REQUIRED, |
|
| 93 | + 'owner of files which shall be moved' |
|
| 94 | + ) |
|
| 95 | + ->addArgument( |
|
| 96 | + 'destination-user', |
|
| 97 | + InputArgument::REQUIRED, |
|
| 98 | + 'user who will be the new owner of the files' |
|
| 99 | + ) |
|
| 100 | + ->addOption( |
|
| 101 | + 'path', |
|
| 102 | + null, |
|
| 103 | + InputOption::VALUE_REQUIRED, |
|
| 104 | + 'selectively provide the path to transfer. For example --path="folder_name"', |
|
| 105 | + '' |
|
| 106 | + ); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + protected function execute(InputInterface $input, OutputInterface $output) { |
|
| 110 | + $sourceUserObject = $this->userManager->get($input->getArgument('source-user')); |
|
| 111 | + $destinationUserObject = $this->userManager->get($input->getArgument('destination-user')); |
|
| 112 | + |
|
| 113 | + if (!$sourceUserObject instanceof IUser) { |
|
| 114 | + $output->writeln("<error>Unknown source user $this->sourceUser</error>"); |
|
| 115 | + return 1; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + if (!$destinationUserObject instanceof IUser) { |
|
| 119 | + $output->writeln("<error>Unknown destination user $this->destinationUser</error>"); |
|
| 120 | + return 1; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + $this->sourceUser = $sourceUserObject->getUID(); |
|
| 124 | + $this->destinationUser = $destinationUserObject->getUID(); |
|
| 125 | + $sourcePathOption = ltrim($input->getOption('path'), '/'); |
|
| 126 | + $this->sourcePath = rtrim($this->sourceUser . '/files/' . $sourcePathOption, '/'); |
|
| 127 | + |
|
| 128 | + // target user has to be ready |
|
| 129 | + if (!\OC::$server->getEncryptionManager()->isReadyForUser($this->destinationUser)) { |
|
| 130 | + $output->writeln("<error>The target user is not ready to accept files. The user has at least to be logged in once.</error>"); |
|
| 131 | + return 2; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + $date = date('Y-m-d H-i-s'); |
|
| 135 | + $this->finalTarget = "$this->destinationUser/files/transferred from $this->sourceUser on $date"; |
|
| 136 | + |
|
| 137 | + // setup filesystem |
|
| 138 | + Filesystem::initMountPoints($this->sourceUser); |
|
| 139 | + Filesystem::initMountPoints($this->destinationUser); |
|
| 140 | + |
|
| 141 | + $view = new View(); |
|
| 142 | + if (!$view->is_dir($this->sourcePath)) { |
|
| 143 | + $output->writeln("<error>Unknown path provided: $sourcePathOption</error>"); |
|
| 144 | + return 1; |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + // analyse source folder |
|
| 148 | + $this->analyse($output); |
|
| 149 | + |
|
| 150 | + // collect all the shares |
|
| 151 | + $this->collectUsersShares($output); |
|
| 152 | + |
|
| 153 | + // transfer the files |
|
| 154 | + $this->transfer($output); |
|
| 155 | + |
|
| 156 | + // restore the shares |
|
| 157 | + $this->restoreShares($output); |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + private function walkFiles(View $view, $path, \Closure $callBack) { |
|
| 161 | + foreach ($view->getDirectoryContent($path) as $fileInfo) { |
|
| 162 | + if (!$callBack($fileInfo)) { |
|
| 163 | + return; |
|
| 164 | + } |
|
| 165 | + if ($fileInfo->getType() === FileInfo::TYPE_FOLDER) { |
|
| 166 | + $this->walkFiles($view, $fileInfo->getPath(), $callBack); |
|
| 167 | + } |
|
| 168 | + } |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + /** |
|
| 172 | + * @param OutputInterface $output |
|
| 173 | + * @throws \Exception |
|
| 174 | + */ |
|
| 175 | + protected function analyse(OutputInterface $output) { |
|
| 176 | + $view = new View(); |
|
| 177 | + $output->writeln("Analysing files of $this->sourceUser ..."); |
|
| 178 | + $progress = new ProgressBar($output); |
|
| 179 | + $progress->start(); |
|
| 180 | + $self = $this; |
|
| 181 | + |
|
| 182 | + $this->walkFiles($view, $this->sourcePath, |
|
| 183 | + function (FileInfo $fileInfo) use ($progress, $self) { |
|
| 184 | + if ($fileInfo->getType() === FileInfo::TYPE_FOLDER) { |
|
| 185 | + // only analyze into folders from main storage, |
|
| 186 | + if (!$fileInfo->getStorage()->instanceOfStorage(IHomeStorage::class)) { |
|
| 187 | + return false; |
|
| 188 | + } |
|
| 189 | + return true; |
|
| 190 | + } |
|
| 191 | + $progress->advance(); |
|
| 192 | + $this->allFiles[] = $fileInfo; |
|
| 193 | + if ($fileInfo->isEncrypted()) { |
|
| 194 | + $this->encryptedFiles[] = $fileInfo; |
|
| 195 | + } |
|
| 196 | + return true; |
|
| 197 | + }); |
|
| 198 | + $progress->finish(); |
|
| 199 | + $output->writeln(''); |
|
| 200 | + |
|
| 201 | + // no file is allowed to be encrypted |
|
| 202 | + if (!empty($this->encryptedFiles)) { |
|
| 203 | + $output->writeln("<error>Some files are encrypted - please decrypt them first</error>"); |
|
| 204 | + foreach($this->encryptedFiles as $encryptedFile) { |
|
| 205 | + /** @var FileInfo $encryptedFile */ |
|
| 206 | + $output->writeln(" " . $encryptedFile->getPath()); |
|
| 207 | + } |
|
| 208 | + throw new \Exception('Execution terminated.'); |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + /** |
|
| 214 | + * @param OutputInterface $output |
|
| 215 | + */ |
|
| 216 | + private function collectUsersShares(OutputInterface $output) { |
|
| 217 | + $output->writeln("Collecting all share information for files and folder of $this->sourceUser ..."); |
|
| 218 | + |
|
| 219 | + $progress = new ProgressBar($output, count($this->shares)); |
|
| 220 | + foreach([\OCP\Share::SHARE_TYPE_GROUP, \OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_LINK, \OCP\Share::SHARE_TYPE_REMOTE, \OCP\Share::SHARE_TYPE_ROOM] as $shareType) { |
|
| 221 | + $offset = 0; |
|
| 222 | + while (true) { |
|
| 223 | + $sharePage = $this->shareManager->getSharesBy($this->sourceUser, $shareType, null, true, 50, $offset); |
|
| 224 | + $progress->advance(count($sharePage)); |
|
| 225 | + if (empty($sharePage)) { |
|
| 226 | + break; |
|
| 227 | + } |
|
| 228 | + $this->shares = array_merge($this->shares, $sharePage); |
|
| 229 | + $offset += 50; |
|
| 230 | + } |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + $progress->finish(); |
|
| 234 | + $output->writeln(''); |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + /** |
|
| 238 | + * @param OutputInterface $output |
|
| 239 | + */ |
|
| 240 | + protected function transfer(OutputInterface $output) { |
|
| 241 | + $view = new View(); |
|
| 242 | + $output->writeln("Transferring files to $this->finalTarget ..."); |
|
| 243 | + |
|
| 244 | + // This change will help user to transfer the folder specified using --path option. |
|
| 245 | + // Else only the content inside folder is transferred which is not correct. |
|
| 246 | + if($this->sourcePath !== "$this->sourceUser/files") { |
|
| 247 | + $view->mkdir($this->finalTarget); |
|
| 248 | + $this->finalTarget = $this->finalTarget . '/' . basename($this->sourcePath); |
|
| 249 | + } |
|
| 250 | + $view->rename($this->sourcePath, $this->finalTarget); |
|
| 251 | + if (!is_dir("$this->sourceUser/files")) { |
|
| 252 | + // because the files folder is moved away we need to recreate it |
|
| 253 | + $view->mkdir("$this->sourceUser/files"); |
|
| 254 | + } |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + /** |
|
| 258 | + * @param OutputInterface $output |
|
| 259 | + */ |
|
| 260 | + private function restoreShares(OutputInterface $output) { |
|
| 261 | + $output->writeln("Restoring shares ..."); |
|
| 262 | + $progress = new ProgressBar($output, count($this->shares)); |
|
| 263 | + |
|
| 264 | + foreach($this->shares as $share) { |
|
| 265 | + try { |
|
| 266 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER && |
|
| 267 | + $share->getSharedWith() === $this->destinationUser) { |
|
| 268 | + // Unmount the shares before deleting, so we don't try to get the storage later on. |
|
| 269 | + $shareMountPoint = $this->mountManager->find('/' . $this->destinationUser . '/files' . $share->getTarget()); |
|
| 270 | + if ($shareMountPoint) { |
|
| 271 | + $this->mountManager->removeMount($shareMountPoint->getMountPoint()); |
|
| 272 | + } |
|
| 273 | + $this->shareManager->deleteShare($share); |
|
| 274 | + } else { |
|
| 275 | + if ($share->getShareOwner() === $this->sourceUser) { |
|
| 276 | + $share->setShareOwner($this->destinationUser); |
|
| 277 | + } |
|
| 278 | + if ($share->getSharedBy() === $this->sourceUser) { |
|
| 279 | + $share->setSharedBy($this->destinationUser); |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + $this->shareManager->updateShare($share); |
|
| 283 | + } |
|
| 284 | + } catch (\OCP\Files\NotFoundException $e) { |
|
| 285 | + $output->writeln('<error>Share with id ' . $share->getId() . ' points at deleted file, skipping</error>'); |
|
| 286 | + } catch (\Exception $e) { |
|
| 287 | + $output->writeln('<error>Could not restore share with id ' . $share->getId() . ':' . $e->getTraceAsString() . '</error>'); |
|
| 288 | + } |
|
| 289 | + $progress->advance(); |
|
| 290 | + } |
|
| 291 | + $progress->finish(); |
|
| 292 | + $output->writeln(''); |
|
| 293 | + } |
|
| 294 | 294 | } |
@@ -123,7 +123,7 @@ discard block |
||
| 123 | 123 | $this->sourceUser = $sourceUserObject->getUID(); |
| 124 | 124 | $this->destinationUser = $destinationUserObject->getUID(); |
| 125 | 125 | $sourcePathOption = ltrim($input->getOption('path'), '/'); |
| 126 | - $this->sourcePath = rtrim($this->sourceUser . '/files/' . $sourcePathOption, '/'); |
|
| 126 | + $this->sourcePath = rtrim($this->sourceUser.'/files/'.$sourcePathOption, '/'); |
|
| 127 | 127 | |
| 128 | 128 | // target user has to be ready |
| 129 | 129 | if (!\OC::$server->getEncryptionManager()->isReadyForUser($this->destinationUser)) { |
@@ -180,7 +180,7 @@ discard block |
||
| 180 | 180 | $self = $this; |
| 181 | 181 | |
| 182 | 182 | $this->walkFiles($view, $this->sourcePath, |
| 183 | - function (FileInfo $fileInfo) use ($progress, $self) { |
|
| 183 | + function(FileInfo $fileInfo) use ($progress, $self) { |
|
| 184 | 184 | if ($fileInfo->getType() === FileInfo::TYPE_FOLDER) { |
| 185 | 185 | // only analyze into folders from main storage, |
| 186 | 186 | if (!$fileInfo->getStorage()->instanceOfStorage(IHomeStorage::class)) { |
@@ -201,9 +201,9 @@ discard block |
||
| 201 | 201 | // no file is allowed to be encrypted |
| 202 | 202 | if (!empty($this->encryptedFiles)) { |
| 203 | 203 | $output->writeln("<error>Some files are encrypted - please decrypt them first</error>"); |
| 204 | - foreach($this->encryptedFiles as $encryptedFile) { |
|
| 204 | + foreach ($this->encryptedFiles as $encryptedFile) { |
|
| 205 | 205 | /** @var FileInfo $encryptedFile */ |
| 206 | - $output->writeln(" " . $encryptedFile->getPath()); |
|
| 206 | + $output->writeln(" ".$encryptedFile->getPath()); |
|
| 207 | 207 | } |
| 208 | 208 | throw new \Exception('Execution terminated.'); |
| 209 | 209 | } |
@@ -217,7 +217,7 @@ discard block |
||
| 217 | 217 | $output->writeln("Collecting all share information for files and folder of $this->sourceUser ..."); |
| 218 | 218 | |
| 219 | 219 | $progress = new ProgressBar($output, count($this->shares)); |
| 220 | - foreach([\OCP\Share::SHARE_TYPE_GROUP, \OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_LINK, \OCP\Share::SHARE_TYPE_REMOTE, \OCP\Share::SHARE_TYPE_ROOM] as $shareType) { |
|
| 220 | + foreach ([\OCP\Share::SHARE_TYPE_GROUP, \OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_LINK, \OCP\Share::SHARE_TYPE_REMOTE, \OCP\Share::SHARE_TYPE_ROOM] as $shareType) { |
|
| 221 | 221 | $offset = 0; |
| 222 | 222 | while (true) { |
| 223 | 223 | $sharePage = $this->shareManager->getSharesBy($this->sourceUser, $shareType, null, true, 50, $offset); |
@@ -243,9 +243,9 @@ discard block |
||
| 243 | 243 | |
| 244 | 244 | // This change will help user to transfer the folder specified using --path option. |
| 245 | 245 | // Else only the content inside folder is transferred which is not correct. |
| 246 | - if($this->sourcePath !== "$this->sourceUser/files") { |
|
| 246 | + if ($this->sourcePath !== "$this->sourceUser/files") { |
|
| 247 | 247 | $view->mkdir($this->finalTarget); |
| 248 | - $this->finalTarget = $this->finalTarget . '/' . basename($this->sourcePath); |
|
| 248 | + $this->finalTarget = $this->finalTarget.'/'.basename($this->sourcePath); |
|
| 249 | 249 | } |
| 250 | 250 | $view->rename($this->sourcePath, $this->finalTarget); |
| 251 | 251 | if (!is_dir("$this->sourceUser/files")) { |
@@ -261,12 +261,12 @@ discard block |
||
| 261 | 261 | $output->writeln("Restoring shares ..."); |
| 262 | 262 | $progress = new ProgressBar($output, count($this->shares)); |
| 263 | 263 | |
| 264 | - foreach($this->shares as $share) { |
|
| 264 | + foreach ($this->shares as $share) { |
|
| 265 | 265 | try { |
| 266 | 266 | if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER && |
| 267 | 267 | $share->getSharedWith() === $this->destinationUser) { |
| 268 | 268 | // Unmount the shares before deleting, so we don't try to get the storage later on. |
| 269 | - $shareMountPoint = $this->mountManager->find('/' . $this->destinationUser . '/files' . $share->getTarget()); |
|
| 269 | + $shareMountPoint = $this->mountManager->find('/'.$this->destinationUser.'/files'.$share->getTarget()); |
|
| 270 | 270 | if ($shareMountPoint) { |
| 271 | 271 | $this->mountManager->removeMount($shareMountPoint->getMountPoint()); |
| 272 | 272 | } |
@@ -282,9 +282,9 @@ discard block |
||
| 282 | 282 | $this->shareManager->updateShare($share); |
| 283 | 283 | } |
| 284 | 284 | } catch (\OCP\Files\NotFoundException $e) { |
| 285 | - $output->writeln('<error>Share with id ' . $share->getId() . ' points at deleted file, skipping</error>'); |
|
| 285 | + $output->writeln('<error>Share with id '.$share->getId().' points at deleted file, skipping</error>'); |
|
| 286 | 286 | } catch (\Exception $e) { |
| 287 | - $output->writeln('<error>Could not restore share with id ' . $share->getId() . ':' . $e->getTraceAsString() . '</error>'); |
|
| 287 | + $output->writeln('<error>Could not restore share with id '.$share->getId().':'.$e->getTraceAsString().'</error>'); |
|
| 288 | 288 | } |
| 289 | 289 | $progress->advance(); |
| 290 | 290 | } |
@@ -29,30 +29,30 @@ |
||
| 29 | 29 | |
| 30 | 30 | class Constants { |
| 31 | 31 | |
| 32 | - const SHARE_TYPE_USER = 0; |
|
| 33 | - const SHARE_TYPE_GROUP = 1; |
|
| 34 | - // const SHARE_TYPE_USERGROUP = 2; // Internal type used by DefaultShareProvider |
|
| 35 | - const SHARE_TYPE_LINK = 3; |
|
| 36 | - const SHARE_TYPE_EMAIL = 4; |
|
| 37 | - const SHARE_TYPE_CONTACT = 5; // ToDo Check if it is still in use otherwise remove it |
|
| 38 | - const SHARE_TYPE_REMOTE = 6; |
|
| 39 | - const SHARE_TYPE_CIRCLE = 7; |
|
| 40 | - const SHARE_TYPE_GUEST = 8; |
|
| 41 | - const SHARE_TYPE_REMOTE_GROUP = 9; |
|
| 42 | - const SHARE_TYPE_ROOM = 10; |
|
| 43 | - // const SHARE_TYPE_USERROOM = 11; // Internal type used by RoomShareProvider |
|
| 32 | + const SHARE_TYPE_USER = 0; |
|
| 33 | + const SHARE_TYPE_GROUP = 1; |
|
| 34 | + // const SHARE_TYPE_USERGROUP = 2; // Internal type used by DefaultShareProvider |
|
| 35 | + const SHARE_TYPE_LINK = 3; |
|
| 36 | + const SHARE_TYPE_EMAIL = 4; |
|
| 37 | + const SHARE_TYPE_CONTACT = 5; // ToDo Check if it is still in use otherwise remove it |
|
| 38 | + const SHARE_TYPE_REMOTE = 6; |
|
| 39 | + const SHARE_TYPE_CIRCLE = 7; |
|
| 40 | + const SHARE_TYPE_GUEST = 8; |
|
| 41 | + const SHARE_TYPE_REMOTE_GROUP = 9; |
|
| 42 | + const SHARE_TYPE_ROOM = 10; |
|
| 43 | + // const SHARE_TYPE_USERROOM = 11; // Internal type used by RoomShareProvider |
|
| 44 | 44 | |
| 45 | - const FORMAT_NONE = -1; |
|
| 46 | - const FORMAT_STATUSES = -2; |
|
| 47 | - const FORMAT_SOURCES = -3; // ToDo Check if it is still in use otherwise remove it |
|
| 45 | + const FORMAT_NONE = -1; |
|
| 46 | + const FORMAT_STATUSES = -2; |
|
| 47 | + const FORMAT_SOURCES = -3; // ToDo Check if it is still in use otherwise remove it |
|
| 48 | 48 | |
| 49 | - const RESPONSE_FORMAT = 'json'; // default resonse format for ocs calls |
|
| 49 | + const RESPONSE_FORMAT = 'json'; // default resonse format for ocs calls |
|
| 50 | 50 | |
| 51 | - const TOKEN_LENGTH = 15; // old (oc7) length is 32, keep token length in db at least that for compatibility |
|
| 51 | + const TOKEN_LENGTH = 15; // old (oc7) length is 32, keep token length in db at least that for compatibility |
|
| 52 | 52 | |
| 53 | - protected static $shareTypeUserAndGroups = -1; |
|
| 54 | - protected static $shareTypeGroupUserUnique = 2; |
|
| 55 | - protected static $backends = array(); |
|
| 56 | - protected static $backendTypes = array(); |
|
| 57 | - protected static $isResharingAllowed; |
|
| 53 | + protected static $shareTypeUserAndGroups = -1; |
|
| 54 | + protected static $shareTypeGroupUserUnique = 2; |
|
| 55 | + protected static $backends = array(); |
|
| 56 | + protected static $backendTypes = array(); |
|
| 57 | + protected static $isResharingAllowed; |
|
| 58 | 58 | } |