@@ -60,1489 +60,1489 @@ |
||
| 60 | 60 | */ |
| 61 | 61 | class Manager implements IManager { |
| 62 | 62 | |
| 63 | - /** @var IProviderFactory */ |
|
| 64 | - private $factory; |
|
| 65 | - /** @var ILogger */ |
|
| 66 | - private $logger; |
|
| 67 | - /** @var IConfig */ |
|
| 68 | - private $config; |
|
| 69 | - /** @var ISecureRandom */ |
|
| 70 | - private $secureRandom; |
|
| 71 | - /** @var IHasher */ |
|
| 72 | - private $hasher; |
|
| 73 | - /** @var IMountManager */ |
|
| 74 | - private $mountManager; |
|
| 75 | - /** @var IGroupManager */ |
|
| 76 | - private $groupManager; |
|
| 77 | - /** @var IL10N */ |
|
| 78 | - private $l; |
|
| 79 | - /** @var IFactory */ |
|
| 80 | - private $l10nFactory; |
|
| 81 | - /** @var IUserManager */ |
|
| 82 | - private $userManager; |
|
| 83 | - /** @var IRootFolder */ |
|
| 84 | - private $rootFolder; |
|
| 85 | - /** @var CappedMemoryCache */ |
|
| 86 | - private $sharingDisabledForUsersCache; |
|
| 87 | - /** @var EventDispatcher */ |
|
| 88 | - private $eventDispatcher; |
|
| 89 | - /** @var LegacyHooks */ |
|
| 90 | - private $legacyHooks; |
|
| 91 | - /** @var IMailer */ |
|
| 92 | - private $mailer; |
|
| 93 | - /** @var IURLGenerator */ |
|
| 94 | - private $urlGenerator; |
|
| 95 | - /** @var \OC_Defaults */ |
|
| 96 | - private $defaults; |
|
| 97 | - |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * Manager constructor. |
|
| 101 | - * |
|
| 102 | - * @param ILogger $logger |
|
| 103 | - * @param IConfig $config |
|
| 104 | - * @param ISecureRandom $secureRandom |
|
| 105 | - * @param IHasher $hasher |
|
| 106 | - * @param IMountManager $mountManager |
|
| 107 | - * @param IGroupManager $groupManager |
|
| 108 | - * @param IL10N $l |
|
| 109 | - * @param IFactory $l10nFactory |
|
| 110 | - * @param IProviderFactory $factory |
|
| 111 | - * @param IUserManager $userManager |
|
| 112 | - * @param IRootFolder $rootFolder |
|
| 113 | - * @param EventDispatcher $eventDispatcher |
|
| 114 | - * @param IMailer $mailer |
|
| 115 | - * @param IURLGenerator $urlGenerator |
|
| 116 | - * @param \OC_Defaults $defaults |
|
| 117 | - */ |
|
| 118 | - public function __construct( |
|
| 119 | - ILogger $logger, |
|
| 120 | - IConfig $config, |
|
| 121 | - ISecureRandom $secureRandom, |
|
| 122 | - IHasher $hasher, |
|
| 123 | - IMountManager $mountManager, |
|
| 124 | - IGroupManager $groupManager, |
|
| 125 | - IL10N $l, |
|
| 126 | - IFactory $l10nFactory, |
|
| 127 | - IProviderFactory $factory, |
|
| 128 | - IUserManager $userManager, |
|
| 129 | - IRootFolder $rootFolder, |
|
| 130 | - EventDispatcher $eventDispatcher, |
|
| 131 | - IMailer $mailer, |
|
| 132 | - IURLGenerator $urlGenerator, |
|
| 133 | - \OC_Defaults $defaults |
|
| 134 | - ) { |
|
| 135 | - $this->logger = $logger; |
|
| 136 | - $this->config = $config; |
|
| 137 | - $this->secureRandom = $secureRandom; |
|
| 138 | - $this->hasher = $hasher; |
|
| 139 | - $this->mountManager = $mountManager; |
|
| 140 | - $this->groupManager = $groupManager; |
|
| 141 | - $this->l = $l; |
|
| 142 | - $this->l10nFactory = $l10nFactory; |
|
| 143 | - $this->factory = $factory; |
|
| 144 | - $this->userManager = $userManager; |
|
| 145 | - $this->rootFolder = $rootFolder; |
|
| 146 | - $this->eventDispatcher = $eventDispatcher; |
|
| 147 | - $this->sharingDisabledForUsersCache = new CappedMemoryCache(); |
|
| 148 | - $this->legacyHooks = new LegacyHooks($this->eventDispatcher); |
|
| 149 | - $this->mailer = $mailer; |
|
| 150 | - $this->urlGenerator = $urlGenerator; |
|
| 151 | - $this->defaults = $defaults; |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * Convert from a full share id to a tuple (providerId, shareId) |
|
| 156 | - * |
|
| 157 | - * @param string $id |
|
| 158 | - * @return string[] |
|
| 159 | - */ |
|
| 160 | - private function splitFullId($id) { |
|
| 161 | - return explode(':', $id, 2); |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * Verify if a password meets all requirements |
|
| 166 | - * |
|
| 167 | - * @param string $password |
|
| 168 | - * @throws \Exception |
|
| 169 | - */ |
|
| 170 | - protected function verifyPassword($password) { |
|
| 171 | - if ($password === null) { |
|
| 172 | - // No password is set, check if this is allowed. |
|
| 173 | - if ($this->shareApiLinkEnforcePassword()) { |
|
| 174 | - throw new \InvalidArgumentException('Passwords are enforced for link shares'); |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - return; |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - // Let others verify the password |
|
| 181 | - try { |
|
| 182 | - $event = new GenericEvent($password); |
|
| 183 | - $this->eventDispatcher->dispatch('OCP\PasswordPolicy::validate', $event); |
|
| 184 | - } catch (HintException $e) { |
|
| 185 | - throw new \Exception($e->getHint()); |
|
| 186 | - } |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * Check for generic requirements before creating a share |
|
| 191 | - * |
|
| 192 | - * @param \OCP\Share\IShare $share |
|
| 193 | - * @throws \InvalidArgumentException |
|
| 194 | - * @throws GenericShareException |
|
| 195 | - * |
|
| 196 | - * @suppress PhanUndeclaredClassMethod |
|
| 197 | - */ |
|
| 198 | - protected function generalCreateChecks(\OCP\Share\IShare $share) { |
|
| 199 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { |
|
| 200 | - // We expect a valid user as sharedWith for user shares |
|
| 201 | - if (!$this->userManager->userExists($share->getSharedWith())) { |
|
| 202 | - throw new \InvalidArgumentException('SharedWith is not a valid user'); |
|
| 203 | - } |
|
| 204 | - } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 205 | - // We expect a valid group as sharedWith for group shares |
|
| 206 | - if (!$this->groupManager->groupExists($share->getSharedWith())) { |
|
| 207 | - throw new \InvalidArgumentException('SharedWith is not a valid group'); |
|
| 208 | - } |
|
| 209 | - } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { |
|
| 210 | - if ($share->getSharedWith() !== null) { |
|
| 211 | - throw new \InvalidArgumentException('SharedWith should be empty'); |
|
| 212 | - } |
|
| 213 | - } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_REMOTE) { |
|
| 214 | - if ($share->getSharedWith() === null) { |
|
| 215 | - throw new \InvalidArgumentException('SharedWith should not be empty'); |
|
| 216 | - } |
|
| 217 | - } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) { |
|
| 218 | - if ($share->getSharedWith() === null) { |
|
| 219 | - throw new \InvalidArgumentException('SharedWith should not be empty'); |
|
| 220 | - } |
|
| 221 | - } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_CIRCLE) { |
|
| 222 | - $circle = \OCA\Circles\Api\v1\Circles::detailsCircle($share->getSharedWith()); |
|
| 223 | - if ($circle === null) { |
|
| 224 | - throw new \InvalidArgumentException('SharedWith is not a valid circle'); |
|
| 225 | - } |
|
| 226 | - } else { |
|
| 227 | - // We can't handle other types yet |
|
| 228 | - throw new \InvalidArgumentException('unknown share type'); |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - // Verify the initiator of the share is set |
|
| 232 | - if ($share->getSharedBy() === null) { |
|
| 233 | - throw new \InvalidArgumentException('SharedBy should be set'); |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - // Cannot share with yourself |
|
| 237 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER && |
|
| 238 | - $share->getSharedWith() === $share->getSharedBy()) { |
|
| 239 | - throw new \InvalidArgumentException('Can’t share with yourself'); |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - // The path should be set |
|
| 243 | - if ($share->getNode() === null) { |
|
| 244 | - throw new \InvalidArgumentException('Path should be set'); |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - // And it should be a file or a folder |
|
| 248 | - if (!($share->getNode() instanceof \OCP\Files\File) && |
|
| 249 | - !($share->getNode() instanceof \OCP\Files\Folder)) { |
|
| 250 | - throw new \InvalidArgumentException('Path should be either a file or a folder'); |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - // And you can't share your rootfolder |
|
| 254 | - if ($this->userManager->userExists($share->getSharedBy())) { |
|
| 255 | - $sharedPath = $this->rootFolder->getUserFolder($share->getSharedBy())->getPath(); |
|
| 256 | - } else { |
|
| 257 | - $sharedPath = $this->rootFolder->getUserFolder($share->getShareOwner())->getPath(); |
|
| 258 | - } |
|
| 259 | - if ($sharedPath === $share->getNode()->getPath()) { |
|
| 260 | - throw new \InvalidArgumentException('You can’t share your root folder'); |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - // Check if we actually have share permissions |
|
| 264 | - if (!$share->getNode()->isShareable()) { |
|
| 265 | - $message_t = $this->l->t('You are not allowed to share %s', [$share->getNode()->getPath()]); |
|
| 266 | - throw new GenericShareException($message_t, $message_t, 404); |
|
| 267 | - } |
|
| 268 | - |
|
| 269 | - // Permissions should be set |
|
| 270 | - if ($share->getPermissions() === null) { |
|
| 271 | - throw new \InvalidArgumentException('A share requires permissions'); |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - /* |
|
| 63 | + /** @var IProviderFactory */ |
|
| 64 | + private $factory; |
|
| 65 | + /** @var ILogger */ |
|
| 66 | + private $logger; |
|
| 67 | + /** @var IConfig */ |
|
| 68 | + private $config; |
|
| 69 | + /** @var ISecureRandom */ |
|
| 70 | + private $secureRandom; |
|
| 71 | + /** @var IHasher */ |
|
| 72 | + private $hasher; |
|
| 73 | + /** @var IMountManager */ |
|
| 74 | + private $mountManager; |
|
| 75 | + /** @var IGroupManager */ |
|
| 76 | + private $groupManager; |
|
| 77 | + /** @var IL10N */ |
|
| 78 | + private $l; |
|
| 79 | + /** @var IFactory */ |
|
| 80 | + private $l10nFactory; |
|
| 81 | + /** @var IUserManager */ |
|
| 82 | + private $userManager; |
|
| 83 | + /** @var IRootFolder */ |
|
| 84 | + private $rootFolder; |
|
| 85 | + /** @var CappedMemoryCache */ |
|
| 86 | + private $sharingDisabledForUsersCache; |
|
| 87 | + /** @var EventDispatcher */ |
|
| 88 | + private $eventDispatcher; |
|
| 89 | + /** @var LegacyHooks */ |
|
| 90 | + private $legacyHooks; |
|
| 91 | + /** @var IMailer */ |
|
| 92 | + private $mailer; |
|
| 93 | + /** @var IURLGenerator */ |
|
| 94 | + private $urlGenerator; |
|
| 95 | + /** @var \OC_Defaults */ |
|
| 96 | + private $defaults; |
|
| 97 | + |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * Manager constructor. |
|
| 101 | + * |
|
| 102 | + * @param ILogger $logger |
|
| 103 | + * @param IConfig $config |
|
| 104 | + * @param ISecureRandom $secureRandom |
|
| 105 | + * @param IHasher $hasher |
|
| 106 | + * @param IMountManager $mountManager |
|
| 107 | + * @param IGroupManager $groupManager |
|
| 108 | + * @param IL10N $l |
|
| 109 | + * @param IFactory $l10nFactory |
|
| 110 | + * @param IProviderFactory $factory |
|
| 111 | + * @param IUserManager $userManager |
|
| 112 | + * @param IRootFolder $rootFolder |
|
| 113 | + * @param EventDispatcher $eventDispatcher |
|
| 114 | + * @param IMailer $mailer |
|
| 115 | + * @param IURLGenerator $urlGenerator |
|
| 116 | + * @param \OC_Defaults $defaults |
|
| 117 | + */ |
|
| 118 | + public function __construct( |
|
| 119 | + ILogger $logger, |
|
| 120 | + IConfig $config, |
|
| 121 | + ISecureRandom $secureRandom, |
|
| 122 | + IHasher $hasher, |
|
| 123 | + IMountManager $mountManager, |
|
| 124 | + IGroupManager $groupManager, |
|
| 125 | + IL10N $l, |
|
| 126 | + IFactory $l10nFactory, |
|
| 127 | + IProviderFactory $factory, |
|
| 128 | + IUserManager $userManager, |
|
| 129 | + IRootFolder $rootFolder, |
|
| 130 | + EventDispatcher $eventDispatcher, |
|
| 131 | + IMailer $mailer, |
|
| 132 | + IURLGenerator $urlGenerator, |
|
| 133 | + \OC_Defaults $defaults |
|
| 134 | + ) { |
|
| 135 | + $this->logger = $logger; |
|
| 136 | + $this->config = $config; |
|
| 137 | + $this->secureRandom = $secureRandom; |
|
| 138 | + $this->hasher = $hasher; |
|
| 139 | + $this->mountManager = $mountManager; |
|
| 140 | + $this->groupManager = $groupManager; |
|
| 141 | + $this->l = $l; |
|
| 142 | + $this->l10nFactory = $l10nFactory; |
|
| 143 | + $this->factory = $factory; |
|
| 144 | + $this->userManager = $userManager; |
|
| 145 | + $this->rootFolder = $rootFolder; |
|
| 146 | + $this->eventDispatcher = $eventDispatcher; |
|
| 147 | + $this->sharingDisabledForUsersCache = new CappedMemoryCache(); |
|
| 148 | + $this->legacyHooks = new LegacyHooks($this->eventDispatcher); |
|
| 149 | + $this->mailer = $mailer; |
|
| 150 | + $this->urlGenerator = $urlGenerator; |
|
| 151 | + $this->defaults = $defaults; |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * Convert from a full share id to a tuple (providerId, shareId) |
|
| 156 | + * |
|
| 157 | + * @param string $id |
|
| 158 | + * @return string[] |
|
| 159 | + */ |
|
| 160 | + private function splitFullId($id) { |
|
| 161 | + return explode(':', $id, 2); |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * Verify if a password meets all requirements |
|
| 166 | + * |
|
| 167 | + * @param string $password |
|
| 168 | + * @throws \Exception |
|
| 169 | + */ |
|
| 170 | + protected function verifyPassword($password) { |
|
| 171 | + if ($password === null) { |
|
| 172 | + // No password is set, check if this is allowed. |
|
| 173 | + if ($this->shareApiLinkEnforcePassword()) { |
|
| 174 | + throw new \InvalidArgumentException('Passwords are enforced for link shares'); |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + return; |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + // Let others verify the password |
|
| 181 | + try { |
|
| 182 | + $event = new GenericEvent($password); |
|
| 183 | + $this->eventDispatcher->dispatch('OCP\PasswordPolicy::validate', $event); |
|
| 184 | + } catch (HintException $e) { |
|
| 185 | + throw new \Exception($e->getHint()); |
|
| 186 | + } |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * Check for generic requirements before creating a share |
|
| 191 | + * |
|
| 192 | + * @param \OCP\Share\IShare $share |
|
| 193 | + * @throws \InvalidArgumentException |
|
| 194 | + * @throws GenericShareException |
|
| 195 | + * |
|
| 196 | + * @suppress PhanUndeclaredClassMethod |
|
| 197 | + */ |
|
| 198 | + protected function generalCreateChecks(\OCP\Share\IShare $share) { |
|
| 199 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { |
|
| 200 | + // We expect a valid user as sharedWith for user shares |
|
| 201 | + if (!$this->userManager->userExists($share->getSharedWith())) { |
|
| 202 | + throw new \InvalidArgumentException('SharedWith is not a valid user'); |
|
| 203 | + } |
|
| 204 | + } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 205 | + // We expect a valid group as sharedWith for group shares |
|
| 206 | + if (!$this->groupManager->groupExists($share->getSharedWith())) { |
|
| 207 | + throw new \InvalidArgumentException('SharedWith is not a valid group'); |
|
| 208 | + } |
|
| 209 | + } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { |
|
| 210 | + if ($share->getSharedWith() !== null) { |
|
| 211 | + throw new \InvalidArgumentException('SharedWith should be empty'); |
|
| 212 | + } |
|
| 213 | + } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_REMOTE) { |
|
| 214 | + if ($share->getSharedWith() === null) { |
|
| 215 | + throw new \InvalidArgumentException('SharedWith should not be empty'); |
|
| 216 | + } |
|
| 217 | + } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) { |
|
| 218 | + if ($share->getSharedWith() === null) { |
|
| 219 | + throw new \InvalidArgumentException('SharedWith should not be empty'); |
|
| 220 | + } |
|
| 221 | + } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_CIRCLE) { |
|
| 222 | + $circle = \OCA\Circles\Api\v1\Circles::detailsCircle($share->getSharedWith()); |
|
| 223 | + if ($circle === null) { |
|
| 224 | + throw new \InvalidArgumentException('SharedWith is not a valid circle'); |
|
| 225 | + } |
|
| 226 | + } else { |
|
| 227 | + // We can't handle other types yet |
|
| 228 | + throw new \InvalidArgumentException('unknown share type'); |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + // Verify the initiator of the share is set |
|
| 232 | + if ($share->getSharedBy() === null) { |
|
| 233 | + throw new \InvalidArgumentException('SharedBy should be set'); |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + // Cannot share with yourself |
|
| 237 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER && |
|
| 238 | + $share->getSharedWith() === $share->getSharedBy()) { |
|
| 239 | + throw new \InvalidArgumentException('Can’t share with yourself'); |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + // The path should be set |
|
| 243 | + if ($share->getNode() === null) { |
|
| 244 | + throw new \InvalidArgumentException('Path should be set'); |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + // And it should be a file or a folder |
|
| 248 | + if (!($share->getNode() instanceof \OCP\Files\File) && |
|
| 249 | + !($share->getNode() instanceof \OCP\Files\Folder)) { |
|
| 250 | + throw new \InvalidArgumentException('Path should be either a file or a folder'); |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + // And you can't share your rootfolder |
|
| 254 | + if ($this->userManager->userExists($share->getSharedBy())) { |
|
| 255 | + $sharedPath = $this->rootFolder->getUserFolder($share->getSharedBy())->getPath(); |
|
| 256 | + } else { |
|
| 257 | + $sharedPath = $this->rootFolder->getUserFolder($share->getShareOwner())->getPath(); |
|
| 258 | + } |
|
| 259 | + if ($sharedPath === $share->getNode()->getPath()) { |
|
| 260 | + throw new \InvalidArgumentException('You can’t share your root folder'); |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + // Check if we actually have share permissions |
|
| 264 | + if (!$share->getNode()->isShareable()) { |
|
| 265 | + $message_t = $this->l->t('You are not allowed to share %s', [$share->getNode()->getPath()]); |
|
| 266 | + throw new GenericShareException($message_t, $message_t, 404); |
|
| 267 | + } |
|
| 268 | + |
|
| 269 | + // Permissions should be set |
|
| 270 | + if ($share->getPermissions() === null) { |
|
| 271 | + throw new \InvalidArgumentException('A share requires permissions'); |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + /* |
|
| 275 | 275 | * Quick fix for #23536 |
| 276 | 276 | * Non moveable mount points do not have update and delete permissions |
| 277 | 277 | * while we 'most likely' do have that on the storage. |
| 278 | 278 | */ |
| 279 | - $permissions = $share->getNode()->getPermissions(); |
|
| 280 | - $mount = $share->getNode()->getMountPoint(); |
|
| 281 | - if (!($mount instanceof MoveableMount)) { |
|
| 282 | - $permissions |= \OCP\Constants::PERMISSION_DELETE | \OCP\Constants::PERMISSION_UPDATE; |
|
| 283 | - } |
|
| 284 | - |
|
| 285 | - // Check that we do not share with more permissions than we have |
|
| 286 | - if ($share->getPermissions() & ~$permissions) { |
|
| 287 | - $message_t = $this->l->t('Can’t increase permissions of %s', [$share->getNode()->getPath()]); |
|
| 288 | - throw new GenericShareException($message_t, $message_t, 404); |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - |
|
| 292 | - // Check that read permissions are always set |
|
| 293 | - // Link shares are allowed to have no read permissions to allow upload to hidden folders |
|
| 294 | - $noReadPermissionRequired = $share->getShareType() === \OCP\Share::SHARE_TYPE_LINK |
|
| 295 | - || $share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL; |
|
| 296 | - if (!$noReadPermissionRequired && |
|
| 297 | - ($share->getPermissions() & \OCP\Constants::PERMISSION_READ) === 0) { |
|
| 298 | - throw new \InvalidArgumentException('Shares need at least read permissions'); |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - if ($share->getNode() instanceof \OCP\Files\File) { |
|
| 302 | - if ($share->getPermissions() & \OCP\Constants::PERMISSION_DELETE) { |
|
| 303 | - $message_t = $this->l->t('Files can’t be shared with delete permissions'); |
|
| 304 | - throw new GenericShareException($message_t); |
|
| 305 | - } |
|
| 306 | - if ($share->getPermissions() & \OCP\Constants::PERMISSION_CREATE) { |
|
| 307 | - $message_t = $this->l->t('Files can’t be shared with create permissions'); |
|
| 308 | - throw new GenericShareException($message_t); |
|
| 309 | - } |
|
| 310 | - } |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - /** |
|
| 314 | - * Validate if the expiration date fits the system settings |
|
| 315 | - * |
|
| 316 | - * @param \OCP\Share\IShare $share The share to validate the expiration date of |
|
| 317 | - * @return \OCP\Share\IShare The modified share object |
|
| 318 | - * @throws GenericShareException |
|
| 319 | - * @throws \InvalidArgumentException |
|
| 320 | - * @throws \Exception |
|
| 321 | - */ |
|
| 322 | - protected function validateExpirationDate(\OCP\Share\IShare $share) { |
|
| 323 | - |
|
| 324 | - $expirationDate = $share->getExpirationDate(); |
|
| 325 | - |
|
| 326 | - if ($expirationDate !== null) { |
|
| 327 | - //Make sure the expiration date is a date |
|
| 328 | - $expirationDate->setTime(0, 0, 0); |
|
| 329 | - |
|
| 330 | - $date = new \DateTime(); |
|
| 331 | - $date->setTime(0, 0, 0); |
|
| 332 | - if ($date >= $expirationDate) { |
|
| 333 | - $message = $this->l->t('Expiration date is in the past'); |
|
| 334 | - throw new GenericShareException($message, $message, 404); |
|
| 335 | - } |
|
| 336 | - } |
|
| 337 | - |
|
| 338 | - // If expiredate is empty set a default one if there is a default |
|
| 339 | - $fullId = null; |
|
| 340 | - try { |
|
| 341 | - $fullId = $share->getFullId(); |
|
| 342 | - } catch (\UnexpectedValueException $e) { |
|
| 343 | - // This is a new share |
|
| 344 | - } |
|
| 345 | - |
|
| 346 | - if ($fullId === null && $expirationDate === null && $this->shareApiLinkDefaultExpireDate()) { |
|
| 347 | - $expirationDate = new \DateTime(); |
|
| 348 | - $expirationDate->setTime(0,0,0); |
|
| 349 | - $expirationDate->add(new \DateInterval('P'.$this->shareApiLinkDefaultExpireDays().'D')); |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - // If we enforce the expiration date check that is does not exceed |
|
| 353 | - if ($this->shareApiLinkDefaultExpireDateEnforced()) { |
|
| 354 | - if ($expirationDate === null) { |
|
| 355 | - throw new \InvalidArgumentException('Expiration date is enforced'); |
|
| 356 | - } |
|
| 357 | - |
|
| 358 | - $date = new \DateTime(); |
|
| 359 | - $date->setTime(0, 0, 0); |
|
| 360 | - $date->add(new \DateInterval('P' . $this->shareApiLinkDefaultExpireDays() . 'D')); |
|
| 361 | - if ($date < $expirationDate) { |
|
| 362 | - $message = $this->l->t('Can’t set expiration date more than %s days in the future', [$this->shareApiLinkDefaultExpireDays()]); |
|
| 363 | - throw new GenericShareException($message, $message, 404); |
|
| 364 | - } |
|
| 365 | - } |
|
| 366 | - |
|
| 367 | - $accepted = true; |
|
| 368 | - $message = ''; |
|
| 369 | - \OCP\Util::emitHook('\OC\Share', 'verifyExpirationDate', [ |
|
| 370 | - 'expirationDate' => &$expirationDate, |
|
| 371 | - 'accepted' => &$accepted, |
|
| 372 | - 'message' => &$message, |
|
| 373 | - 'passwordSet' => $share->getPassword() !== null, |
|
| 374 | - ]); |
|
| 375 | - |
|
| 376 | - if (!$accepted) { |
|
| 377 | - throw new \Exception($message); |
|
| 378 | - } |
|
| 379 | - |
|
| 380 | - $share->setExpirationDate($expirationDate); |
|
| 381 | - |
|
| 382 | - return $share; |
|
| 383 | - } |
|
| 384 | - |
|
| 385 | - /** |
|
| 386 | - * Check for pre share requirements for user shares |
|
| 387 | - * |
|
| 388 | - * @param \OCP\Share\IShare $share |
|
| 389 | - * @throws \Exception |
|
| 390 | - */ |
|
| 391 | - protected function userCreateChecks(\OCP\Share\IShare $share) { |
|
| 392 | - // Check if we can share with group members only |
|
| 393 | - if ($this->shareWithGroupMembersOnly()) { |
|
| 394 | - $sharedBy = $this->userManager->get($share->getSharedBy()); |
|
| 395 | - $sharedWith = $this->userManager->get($share->getSharedWith()); |
|
| 396 | - // Verify we can share with this user |
|
| 397 | - $groups = array_intersect( |
|
| 398 | - $this->groupManager->getUserGroupIds($sharedBy), |
|
| 399 | - $this->groupManager->getUserGroupIds($sharedWith) |
|
| 400 | - ); |
|
| 401 | - if (empty($groups)) { |
|
| 402 | - throw new \Exception('Sharing is only allowed with group members'); |
|
| 403 | - } |
|
| 404 | - } |
|
| 405 | - |
|
| 406 | - /* |
|
| 279 | + $permissions = $share->getNode()->getPermissions(); |
|
| 280 | + $mount = $share->getNode()->getMountPoint(); |
|
| 281 | + if (!($mount instanceof MoveableMount)) { |
|
| 282 | + $permissions |= \OCP\Constants::PERMISSION_DELETE | \OCP\Constants::PERMISSION_UPDATE; |
|
| 283 | + } |
|
| 284 | + |
|
| 285 | + // Check that we do not share with more permissions than we have |
|
| 286 | + if ($share->getPermissions() & ~$permissions) { |
|
| 287 | + $message_t = $this->l->t('Can’t increase permissions of %s', [$share->getNode()->getPath()]); |
|
| 288 | + throw new GenericShareException($message_t, $message_t, 404); |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + |
|
| 292 | + // Check that read permissions are always set |
|
| 293 | + // Link shares are allowed to have no read permissions to allow upload to hidden folders |
|
| 294 | + $noReadPermissionRequired = $share->getShareType() === \OCP\Share::SHARE_TYPE_LINK |
|
| 295 | + || $share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL; |
|
| 296 | + if (!$noReadPermissionRequired && |
|
| 297 | + ($share->getPermissions() & \OCP\Constants::PERMISSION_READ) === 0) { |
|
| 298 | + throw new \InvalidArgumentException('Shares need at least read permissions'); |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + if ($share->getNode() instanceof \OCP\Files\File) { |
|
| 302 | + if ($share->getPermissions() & \OCP\Constants::PERMISSION_DELETE) { |
|
| 303 | + $message_t = $this->l->t('Files can’t be shared with delete permissions'); |
|
| 304 | + throw new GenericShareException($message_t); |
|
| 305 | + } |
|
| 306 | + if ($share->getPermissions() & \OCP\Constants::PERMISSION_CREATE) { |
|
| 307 | + $message_t = $this->l->t('Files can’t be shared with create permissions'); |
|
| 308 | + throw new GenericShareException($message_t); |
|
| 309 | + } |
|
| 310 | + } |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + /** |
|
| 314 | + * Validate if the expiration date fits the system settings |
|
| 315 | + * |
|
| 316 | + * @param \OCP\Share\IShare $share The share to validate the expiration date of |
|
| 317 | + * @return \OCP\Share\IShare The modified share object |
|
| 318 | + * @throws GenericShareException |
|
| 319 | + * @throws \InvalidArgumentException |
|
| 320 | + * @throws \Exception |
|
| 321 | + */ |
|
| 322 | + protected function validateExpirationDate(\OCP\Share\IShare $share) { |
|
| 323 | + |
|
| 324 | + $expirationDate = $share->getExpirationDate(); |
|
| 325 | + |
|
| 326 | + if ($expirationDate !== null) { |
|
| 327 | + //Make sure the expiration date is a date |
|
| 328 | + $expirationDate->setTime(0, 0, 0); |
|
| 329 | + |
|
| 330 | + $date = new \DateTime(); |
|
| 331 | + $date->setTime(0, 0, 0); |
|
| 332 | + if ($date >= $expirationDate) { |
|
| 333 | + $message = $this->l->t('Expiration date is in the past'); |
|
| 334 | + throw new GenericShareException($message, $message, 404); |
|
| 335 | + } |
|
| 336 | + } |
|
| 337 | + |
|
| 338 | + // If expiredate is empty set a default one if there is a default |
|
| 339 | + $fullId = null; |
|
| 340 | + try { |
|
| 341 | + $fullId = $share->getFullId(); |
|
| 342 | + } catch (\UnexpectedValueException $e) { |
|
| 343 | + // This is a new share |
|
| 344 | + } |
|
| 345 | + |
|
| 346 | + if ($fullId === null && $expirationDate === null && $this->shareApiLinkDefaultExpireDate()) { |
|
| 347 | + $expirationDate = new \DateTime(); |
|
| 348 | + $expirationDate->setTime(0,0,0); |
|
| 349 | + $expirationDate->add(new \DateInterval('P'.$this->shareApiLinkDefaultExpireDays().'D')); |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + // If we enforce the expiration date check that is does not exceed |
|
| 353 | + if ($this->shareApiLinkDefaultExpireDateEnforced()) { |
|
| 354 | + if ($expirationDate === null) { |
|
| 355 | + throw new \InvalidArgumentException('Expiration date is enforced'); |
|
| 356 | + } |
|
| 357 | + |
|
| 358 | + $date = new \DateTime(); |
|
| 359 | + $date->setTime(0, 0, 0); |
|
| 360 | + $date->add(new \DateInterval('P' . $this->shareApiLinkDefaultExpireDays() . 'D')); |
|
| 361 | + if ($date < $expirationDate) { |
|
| 362 | + $message = $this->l->t('Can’t set expiration date more than %s days in the future', [$this->shareApiLinkDefaultExpireDays()]); |
|
| 363 | + throw new GenericShareException($message, $message, 404); |
|
| 364 | + } |
|
| 365 | + } |
|
| 366 | + |
|
| 367 | + $accepted = true; |
|
| 368 | + $message = ''; |
|
| 369 | + \OCP\Util::emitHook('\OC\Share', 'verifyExpirationDate', [ |
|
| 370 | + 'expirationDate' => &$expirationDate, |
|
| 371 | + 'accepted' => &$accepted, |
|
| 372 | + 'message' => &$message, |
|
| 373 | + 'passwordSet' => $share->getPassword() !== null, |
|
| 374 | + ]); |
|
| 375 | + |
|
| 376 | + if (!$accepted) { |
|
| 377 | + throw new \Exception($message); |
|
| 378 | + } |
|
| 379 | + |
|
| 380 | + $share->setExpirationDate($expirationDate); |
|
| 381 | + |
|
| 382 | + return $share; |
|
| 383 | + } |
|
| 384 | + |
|
| 385 | + /** |
|
| 386 | + * Check for pre share requirements for user shares |
|
| 387 | + * |
|
| 388 | + * @param \OCP\Share\IShare $share |
|
| 389 | + * @throws \Exception |
|
| 390 | + */ |
|
| 391 | + protected function userCreateChecks(\OCP\Share\IShare $share) { |
|
| 392 | + // Check if we can share with group members only |
|
| 393 | + if ($this->shareWithGroupMembersOnly()) { |
|
| 394 | + $sharedBy = $this->userManager->get($share->getSharedBy()); |
|
| 395 | + $sharedWith = $this->userManager->get($share->getSharedWith()); |
|
| 396 | + // Verify we can share with this user |
|
| 397 | + $groups = array_intersect( |
|
| 398 | + $this->groupManager->getUserGroupIds($sharedBy), |
|
| 399 | + $this->groupManager->getUserGroupIds($sharedWith) |
|
| 400 | + ); |
|
| 401 | + if (empty($groups)) { |
|
| 402 | + throw new \Exception('Sharing is only allowed with group members'); |
|
| 403 | + } |
|
| 404 | + } |
|
| 405 | + |
|
| 406 | + /* |
|
| 407 | 407 | * TODO: Could be costly, fix |
| 408 | 408 | * |
| 409 | 409 | * Also this is not what we want in the future.. then we want to squash identical shares. |
| 410 | 410 | */ |
| 411 | - $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_USER); |
|
| 412 | - $existingShares = $provider->getSharesByPath($share->getNode()); |
|
| 413 | - foreach($existingShares as $existingShare) { |
|
| 414 | - // Ignore if it is the same share |
|
| 415 | - try { |
|
| 416 | - if ($existingShare->getFullId() === $share->getFullId()) { |
|
| 417 | - continue; |
|
| 418 | - } |
|
| 419 | - } catch (\UnexpectedValueException $e) { |
|
| 420 | - //Shares are not identical |
|
| 421 | - } |
|
| 422 | - |
|
| 423 | - // Identical share already existst |
|
| 424 | - if ($existingShare->getSharedWith() === $share->getSharedWith()) { |
|
| 425 | - throw new \Exception('Path is already shared with this user'); |
|
| 426 | - } |
|
| 427 | - |
|
| 428 | - // The share is already shared with this user via a group share |
|
| 429 | - if ($existingShare->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 430 | - $group = $this->groupManager->get($existingShare->getSharedWith()); |
|
| 431 | - if (!is_null($group)) { |
|
| 432 | - $user = $this->userManager->get($share->getSharedWith()); |
|
| 433 | - |
|
| 434 | - if ($group->inGroup($user) && $existingShare->getShareOwner() !== $share->getShareOwner()) { |
|
| 435 | - throw new \Exception('Path is already shared with this user'); |
|
| 436 | - } |
|
| 437 | - } |
|
| 438 | - } |
|
| 439 | - } |
|
| 440 | - } |
|
| 441 | - |
|
| 442 | - /** |
|
| 443 | - * Check for pre share requirements for group shares |
|
| 444 | - * |
|
| 445 | - * @param \OCP\Share\IShare $share |
|
| 446 | - * @throws \Exception |
|
| 447 | - */ |
|
| 448 | - protected function groupCreateChecks(\OCP\Share\IShare $share) { |
|
| 449 | - // Verify group shares are allowed |
|
| 450 | - if (!$this->allowGroupSharing()) { |
|
| 451 | - throw new \Exception('Group sharing is now allowed'); |
|
| 452 | - } |
|
| 453 | - |
|
| 454 | - // Verify if the user can share with this group |
|
| 455 | - if ($this->shareWithGroupMembersOnly()) { |
|
| 456 | - $sharedBy = $this->userManager->get($share->getSharedBy()); |
|
| 457 | - $sharedWith = $this->groupManager->get($share->getSharedWith()); |
|
| 458 | - if (is_null($sharedWith) || !$sharedWith->inGroup($sharedBy)) { |
|
| 459 | - throw new \Exception('Sharing is only allowed within your own groups'); |
|
| 460 | - } |
|
| 461 | - } |
|
| 462 | - |
|
| 463 | - /* |
|
| 411 | + $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_USER); |
|
| 412 | + $existingShares = $provider->getSharesByPath($share->getNode()); |
|
| 413 | + foreach($existingShares as $existingShare) { |
|
| 414 | + // Ignore if it is the same share |
|
| 415 | + try { |
|
| 416 | + if ($existingShare->getFullId() === $share->getFullId()) { |
|
| 417 | + continue; |
|
| 418 | + } |
|
| 419 | + } catch (\UnexpectedValueException $e) { |
|
| 420 | + //Shares are not identical |
|
| 421 | + } |
|
| 422 | + |
|
| 423 | + // Identical share already existst |
|
| 424 | + if ($existingShare->getSharedWith() === $share->getSharedWith()) { |
|
| 425 | + throw new \Exception('Path is already shared with this user'); |
|
| 426 | + } |
|
| 427 | + |
|
| 428 | + // The share is already shared with this user via a group share |
|
| 429 | + if ($existingShare->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 430 | + $group = $this->groupManager->get($existingShare->getSharedWith()); |
|
| 431 | + if (!is_null($group)) { |
|
| 432 | + $user = $this->userManager->get($share->getSharedWith()); |
|
| 433 | + |
|
| 434 | + if ($group->inGroup($user) && $existingShare->getShareOwner() !== $share->getShareOwner()) { |
|
| 435 | + throw new \Exception('Path is already shared with this user'); |
|
| 436 | + } |
|
| 437 | + } |
|
| 438 | + } |
|
| 439 | + } |
|
| 440 | + } |
|
| 441 | + |
|
| 442 | + /** |
|
| 443 | + * Check for pre share requirements for group shares |
|
| 444 | + * |
|
| 445 | + * @param \OCP\Share\IShare $share |
|
| 446 | + * @throws \Exception |
|
| 447 | + */ |
|
| 448 | + protected function groupCreateChecks(\OCP\Share\IShare $share) { |
|
| 449 | + // Verify group shares are allowed |
|
| 450 | + if (!$this->allowGroupSharing()) { |
|
| 451 | + throw new \Exception('Group sharing is now allowed'); |
|
| 452 | + } |
|
| 453 | + |
|
| 454 | + // Verify if the user can share with this group |
|
| 455 | + if ($this->shareWithGroupMembersOnly()) { |
|
| 456 | + $sharedBy = $this->userManager->get($share->getSharedBy()); |
|
| 457 | + $sharedWith = $this->groupManager->get($share->getSharedWith()); |
|
| 458 | + if (is_null($sharedWith) || !$sharedWith->inGroup($sharedBy)) { |
|
| 459 | + throw new \Exception('Sharing is only allowed within your own groups'); |
|
| 460 | + } |
|
| 461 | + } |
|
| 462 | + |
|
| 463 | + /* |
|
| 464 | 464 | * TODO: Could be costly, fix |
| 465 | 465 | * |
| 466 | 466 | * Also this is not what we want in the future.. then we want to squash identical shares. |
| 467 | 467 | */ |
| 468 | - $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_GROUP); |
|
| 469 | - $existingShares = $provider->getSharesByPath($share->getNode()); |
|
| 470 | - foreach($existingShares as $existingShare) { |
|
| 471 | - try { |
|
| 472 | - if ($existingShare->getFullId() === $share->getFullId()) { |
|
| 473 | - continue; |
|
| 474 | - } |
|
| 475 | - } catch (\UnexpectedValueException $e) { |
|
| 476 | - //It is a new share so just continue |
|
| 477 | - } |
|
| 478 | - |
|
| 479 | - if ($existingShare->getSharedWith() === $share->getSharedWith()) { |
|
| 480 | - throw new \Exception('Path is already shared with this group'); |
|
| 481 | - } |
|
| 482 | - } |
|
| 483 | - } |
|
| 484 | - |
|
| 485 | - /** |
|
| 486 | - * Check for pre share requirements for link shares |
|
| 487 | - * |
|
| 488 | - * @param \OCP\Share\IShare $share |
|
| 489 | - * @throws \Exception |
|
| 490 | - */ |
|
| 491 | - protected function linkCreateChecks(\OCP\Share\IShare $share) { |
|
| 492 | - // Are link shares allowed? |
|
| 493 | - if (!$this->shareApiAllowLinks()) { |
|
| 494 | - throw new \Exception('Link sharing is not allowed'); |
|
| 495 | - } |
|
| 496 | - |
|
| 497 | - // Link shares by definition can't have share permissions |
|
| 498 | - if ($share->getPermissions() & \OCP\Constants::PERMISSION_SHARE) { |
|
| 499 | - throw new \InvalidArgumentException('Link shares can’t have reshare permissions'); |
|
| 500 | - } |
|
| 501 | - |
|
| 502 | - // Check if public upload is allowed |
|
| 503 | - if (!$this->shareApiLinkAllowPublicUpload() && |
|
| 504 | - ($share->getPermissions() & (\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE))) { |
|
| 505 | - throw new \InvalidArgumentException('Public upload is not allowed'); |
|
| 506 | - } |
|
| 507 | - } |
|
| 508 | - |
|
| 509 | - /** |
|
| 510 | - * To make sure we don't get invisible link shares we set the parent |
|
| 511 | - * of a link if it is a reshare. This is a quick word around |
|
| 512 | - * until we can properly display multiple link shares in the UI |
|
| 513 | - * |
|
| 514 | - * See: https://github.com/owncloud/core/issues/22295 |
|
| 515 | - * |
|
| 516 | - * FIXME: Remove once multiple link shares can be properly displayed |
|
| 517 | - * |
|
| 518 | - * @param \OCP\Share\IShare $share |
|
| 519 | - */ |
|
| 520 | - protected function setLinkParent(\OCP\Share\IShare $share) { |
|
| 521 | - |
|
| 522 | - // No sense in checking if the method is not there. |
|
| 523 | - if (method_exists($share, 'setParent')) { |
|
| 524 | - $storage = $share->getNode()->getStorage(); |
|
| 525 | - if ($storage->instanceOfStorage('\OCA\Files_Sharing\ISharedStorage')) { |
|
| 526 | - /** @var \OCA\Files_Sharing\SharedStorage $storage */ |
|
| 527 | - $share->setParent($storage->getShareId()); |
|
| 528 | - } |
|
| 529 | - }; |
|
| 530 | - } |
|
| 531 | - |
|
| 532 | - /** |
|
| 533 | - * @param File|Folder $path |
|
| 534 | - */ |
|
| 535 | - protected function pathCreateChecks($path) { |
|
| 536 | - // Make sure that we do not share a path that contains a shared mountpoint |
|
| 537 | - if ($path instanceof \OCP\Files\Folder) { |
|
| 538 | - $mounts = $this->mountManager->findIn($path->getPath()); |
|
| 539 | - foreach($mounts as $mount) { |
|
| 540 | - if ($mount->getStorage()->instanceOfStorage('\OCA\Files_Sharing\ISharedStorage')) { |
|
| 541 | - throw new \InvalidArgumentException('Path contains files shared with you'); |
|
| 542 | - } |
|
| 543 | - } |
|
| 544 | - } |
|
| 545 | - } |
|
| 546 | - |
|
| 547 | - /** |
|
| 548 | - * Check if the user that is sharing can actually share |
|
| 549 | - * |
|
| 550 | - * @param \OCP\Share\IShare $share |
|
| 551 | - * @throws \Exception |
|
| 552 | - */ |
|
| 553 | - protected function canShare(\OCP\Share\IShare $share) { |
|
| 554 | - if (!$this->shareApiEnabled()) { |
|
| 555 | - throw new \Exception('Sharing is disabled'); |
|
| 556 | - } |
|
| 557 | - |
|
| 558 | - if ($this->sharingDisabledForUser($share->getSharedBy())) { |
|
| 559 | - throw new \Exception('Sharing is disabled for you'); |
|
| 560 | - } |
|
| 561 | - } |
|
| 562 | - |
|
| 563 | - /** |
|
| 564 | - * Share a path |
|
| 565 | - * |
|
| 566 | - * @param \OCP\Share\IShare $share |
|
| 567 | - * @return Share The share object |
|
| 568 | - * @throws \Exception |
|
| 569 | - * |
|
| 570 | - * TODO: handle link share permissions or check them |
|
| 571 | - */ |
|
| 572 | - public function createShare(\OCP\Share\IShare $share) { |
|
| 573 | - $this->canShare($share); |
|
| 574 | - |
|
| 575 | - $this->generalCreateChecks($share); |
|
| 576 | - |
|
| 577 | - // Verify if there are any issues with the path |
|
| 578 | - $this->pathCreateChecks($share->getNode()); |
|
| 579 | - |
|
| 580 | - /* |
|
| 468 | + $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_GROUP); |
|
| 469 | + $existingShares = $provider->getSharesByPath($share->getNode()); |
|
| 470 | + foreach($existingShares as $existingShare) { |
|
| 471 | + try { |
|
| 472 | + if ($existingShare->getFullId() === $share->getFullId()) { |
|
| 473 | + continue; |
|
| 474 | + } |
|
| 475 | + } catch (\UnexpectedValueException $e) { |
|
| 476 | + //It is a new share so just continue |
|
| 477 | + } |
|
| 478 | + |
|
| 479 | + if ($existingShare->getSharedWith() === $share->getSharedWith()) { |
|
| 480 | + throw new \Exception('Path is already shared with this group'); |
|
| 481 | + } |
|
| 482 | + } |
|
| 483 | + } |
|
| 484 | + |
|
| 485 | + /** |
|
| 486 | + * Check for pre share requirements for link shares |
|
| 487 | + * |
|
| 488 | + * @param \OCP\Share\IShare $share |
|
| 489 | + * @throws \Exception |
|
| 490 | + */ |
|
| 491 | + protected function linkCreateChecks(\OCP\Share\IShare $share) { |
|
| 492 | + // Are link shares allowed? |
|
| 493 | + if (!$this->shareApiAllowLinks()) { |
|
| 494 | + throw new \Exception('Link sharing is not allowed'); |
|
| 495 | + } |
|
| 496 | + |
|
| 497 | + // Link shares by definition can't have share permissions |
|
| 498 | + if ($share->getPermissions() & \OCP\Constants::PERMISSION_SHARE) { |
|
| 499 | + throw new \InvalidArgumentException('Link shares can’t have reshare permissions'); |
|
| 500 | + } |
|
| 501 | + |
|
| 502 | + // Check if public upload is allowed |
|
| 503 | + if (!$this->shareApiLinkAllowPublicUpload() && |
|
| 504 | + ($share->getPermissions() & (\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE))) { |
|
| 505 | + throw new \InvalidArgumentException('Public upload is not allowed'); |
|
| 506 | + } |
|
| 507 | + } |
|
| 508 | + |
|
| 509 | + /** |
|
| 510 | + * To make sure we don't get invisible link shares we set the parent |
|
| 511 | + * of a link if it is a reshare. This is a quick word around |
|
| 512 | + * until we can properly display multiple link shares in the UI |
|
| 513 | + * |
|
| 514 | + * See: https://github.com/owncloud/core/issues/22295 |
|
| 515 | + * |
|
| 516 | + * FIXME: Remove once multiple link shares can be properly displayed |
|
| 517 | + * |
|
| 518 | + * @param \OCP\Share\IShare $share |
|
| 519 | + */ |
|
| 520 | + protected function setLinkParent(\OCP\Share\IShare $share) { |
|
| 521 | + |
|
| 522 | + // No sense in checking if the method is not there. |
|
| 523 | + if (method_exists($share, 'setParent')) { |
|
| 524 | + $storage = $share->getNode()->getStorage(); |
|
| 525 | + if ($storage->instanceOfStorage('\OCA\Files_Sharing\ISharedStorage')) { |
|
| 526 | + /** @var \OCA\Files_Sharing\SharedStorage $storage */ |
|
| 527 | + $share->setParent($storage->getShareId()); |
|
| 528 | + } |
|
| 529 | + }; |
|
| 530 | + } |
|
| 531 | + |
|
| 532 | + /** |
|
| 533 | + * @param File|Folder $path |
|
| 534 | + */ |
|
| 535 | + protected function pathCreateChecks($path) { |
|
| 536 | + // Make sure that we do not share a path that contains a shared mountpoint |
|
| 537 | + if ($path instanceof \OCP\Files\Folder) { |
|
| 538 | + $mounts = $this->mountManager->findIn($path->getPath()); |
|
| 539 | + foreach($mounts as $mount) { |
|
| 540 | + if ($mount->getStorage()->instanceOfStorage('\OCA\Files_Sharing\ISharedStorage')) { |
|
| 541 | + throw new \InvalidArgumentException('Path contains files shared with you'); |
|
| 542 | + } |
|
| 543 | + } |
|
| 544 | + } |
|
| 545 | + } |
|
| 546 | + |
|
| 547 | + /** |
|
| 548 | + * Check if the user that is sharing can actually share |
|
| 549 | + * |
|
| 550 | + * @param \OCP\Share\IShare $share |
|
| 551 | + * @throws \Exception |
|
| 552 | + */ |
|
| 553 | + protected function canShare(\OCP\Share\IShare $share) { |
|
| 554 | + if (!$this->shareApiEnabled()) { |
|
| 555 | + throw new \Exception('Sharing is disabled'); |
|
| 556 | + } |
|
| 557 | + |
|
| 558 | + if ($this->sharingDisabledForUser($share->getSharedBy())) { |
|
| 559 | + throw new \Exception('Sharing is disabled for you'); |
|
| 560 | + } |
|
| 561 | + } |
|
| 562 | + |
|
| 563 | + /** |
|
| 564 | + * Share a path |
|
| 565 | + * |
|
| 566 | + * @param \OCP\Share\IShare $share |
|
| 567 | + * @return Share The share object |
|
| 568 | + * @throws \Exception |
|
| 569 | + * |
|
| 570 | + * TODO: handle link share permissions or check them |
|
| 571 | + */ |
|
| 572 | + public function createShare(\OCP\Share\IShare $share) { |
|
| 573 | + $this->canShare($share); |
|
| 574 | + |
|
| 575 | + $this->generalCreateChecks($share); |
|
| 576 | + |
|
| 577 | + // Verify if there are any issues with the path |
|
| 578 | + $this->pathCreateChecks($share->getNode()); |
|
| 579 | + |
|
| 580 | + /* |
|
| 581 | 581 | * On creation of a share the owner is always the owner of the path |
| 582 | 582 | * Except for mounted federated shares. |
| 583 | 583 | */ |
| 584 | - $storage = $share->getNode()->getStorage(); |
|
| 585 | - if ($storage->instanceOfStorage('OCA\Files_Sharing\External\Storage')) { |
|
| 586 | - $parent = $share->getNode()->getParent(); |
|
| 587 | - while($parent->getStorage()->instanceOfStorage('OCA\Files_Sharing\External\Storage')) { |
|
| 588 | - $parent = $parent->getParent(); |
|
| 589 | - } |
|
| 590 | - $share->setShareOwner($parent->getOwner()->getUID()); |
|
| 591 | - } else { |
|
| 592 | - $share->setShareOwner($share->getNode()->getOwner()->getUID()); |
|
| 593 | - } |
|
| 594 | - |
|
| 595 | - //Verify share type |
|
| 596 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { |
|
| 597 | - $this->userCreateChecks($share); |
|
| 598 | - } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 599 | - $this->groupCreateChecks($share); |
|
| 600 | - } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { |
|
| 601 | - $this->linkCreateChecks($share); |
|
| 602 | - $this->setLinkParent($share); |
|
| 603 | - |
|
| 604 | - /* |
|
| 584 | + $storage = $share->getNode()->getStorage(); |
|
| 585 | + if ($storage->instanceOfStorage('OCA\Files_Sharing\External\Storage')) { |
|
| 586 | + $parent = $share->getNode()->getParent(); |
|
| 587 | + while($parent->getStorage()->instanceOfStorage('OCA\Files_Sharing\External\Storage')) { |
|
| 588 | + $parent = $parent->getParent(); |
|
| 589 | + } |
|
| 590 | + $share->setShareOwner($parent->getOwner()->getUID()); |
|
| 591 | + } else { |
|
| 592 | + $share->setShareOwner($share->getNode()->getOwner()->getUID()); |
|
| 593 | + } |
|
| 594 | + |
|
| 595 | + //Verify share type |
|
| 596 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { |
|
| 597 | + $this->userCreateChecks($share); |
|
| 598 | + } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 599 | + $this->groupCreateChecks($share); |
|
| 600 | + } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { |
|
| 601 | + $this->linkCreateChecks($share); |
|
| 602 | + $this->setLinkParent($share); |
|
| 603 | + |
|
| 604 | + /* |
|
| 605 | 605 | * For now ignore a set token. |
| 606 | 606 | */ |
| 607 | - $share->setToken( |
|
| 608 | - $this->secureRandom->generate( |
|
| 609 | - \OC\Share\Constants::TOKEN_LENGTH, |
|
| 610 | - \OCP\Security\ISecureRandom::CHAR_HUMAN_READABLE |
|
| 611 | - ) |
|
| 612 | - ); |
|
| 613 | - |
|
| 614 | - //Verify the expiration date |
|
| 615 | - $this->validateExpirationDate($share); |
|
| 616 | - |
|
| 617 | - //Verify the password |
|
| 618 | - $this->verifyPassword($share->getPassword()); |
|
| 619 | - |
|
| 620 | - // If a password is set. Hash it! |
|
| 621 | - if ($share->getPassword() !== null) { |
|
| 622 | - $share->setPassword($this->hasher->hash($share->getPassword())); |
|
| 623 | - } |
|
| 624 | - } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) { |
|
| 625 | - $share->setToken( |
|
| 626 | - $this->secureRandom->generate( |
|
| 627 | - \OC\Share\Constants::TOKEN_LENGTH, |
|
| 628 | - \OCP\Security\ISecureRandom::CHAR_HUMAN_READABLE |
|
| 629 | - ) |
|
| 630 | - ); |
|
| 631 | - } |
|
| 632 | - |
|
| 633 | - // Cannot share with the owner |
|
| 634 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER && |
|
| 635 | - $share->getSharedWith() === $share->getShareOwner()) { |
|
| 636 | - throw new \InvalidArgumentException('Can’t share with the share owner'); |
|
| 637 | - } |
|
| 638 | - |
|
| 639 | - // Generate the target |
|
| 640 | - $target = $this->config->getSystemValue('share_folder', '/') .'/'. $share->getNode()->getName(); |
|
| 641 | - $target = \OC\Files\Filesystem::normalizePath($target); |
|
| 642 | - $share->setTarget($target); |
|
| 643 | - |
|
| 644 | - // Pre share event |
|
| 645 | - $event = new GenericEvent($share); |
|
| 646 | - $a = $this->eventDispatcher->dispatch('OCP\Share::preShare', $event); |
|
| 647 | - if ($event->isPropagationStopped() && $event->hasArgument('error')) { |
|
| 648 | - throw new \Exception($event->getArgument('error')); |
|
| 649 | - } |
|
| 650 | - |
|
| 651 | - $oldShare = $share; |
|
| 652 | - $provider = $this->factory->getProviderForType($share->getShareType()); |
|
| 653 | - $share = $provider->create($share); |
|
| 654 | - //reuse the node we already have |
|
| 655 | - $share->setNode($oldShare->getNode()); |
|
| 656 | - |
|
| 657 | - // Post share event |
|
| 658 | - $event = new GenericEvent($share); |
|
| 659 | - $this->eventDispatcher->dispatch('OCP\Share::postShare', $event); |
|
| 660 | - |
|
| 661 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { |
|
| 662 | - $mailSend = $share->getMailSend(); |
|
| 663 | - if($mailSend === true) { |
|
| 664 | - $user = $this->userManager->get($share->getSharedWith()); |
|
| 665 | - if ($user !== null) { |
|
| 666 | - $emailAddress = $user->getEMailAddress(); |
|
| 667 | - if ($emailAddress !== null && $emailAddress !== '') { |
|
| 668 | - $userLang = $this->config->getUserValue($share->getSharedWith(), 'core', 'lang', null); |
|
| 669 | - $l = $this->l10nFactory->get('lib', $userLang); |
|
| 670 | - $this->sendMailNotification( |
|
| 671 | - $l, |
|
| 672 | - $share->getNode()->getName(), |
|
| 673 | - $this->urlGenerator->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $share->getNode()->getId()]), |
|
| 674 | - $share->getSharedBy(), |
|
| 675 | - $emailAddress, |
|
| 676 | - $share->getExpirationDate() |
|
| 677 | - ); |
|
| 678 | - $this->logger->debug('Send share notification to ' . $emailAddress . ' for share with ID ' . $share->getId(), ['app' => 'share']); |
|
| 679 | - } else { |
|
| 680 | - $this->logger->debug('Share notification not send to ' . $share->getSharedWith() . ' because email address is not set.', ['app' => 'share']); |
|
| 681 | - } |
|
| 682 | - } else { |
|
| 683 | - $this->logger->debug('Share notification not send to ' . $share->getSharedWith() . ' because user could not be found.', ['app' => 'share']); |
|
| 684 | - } |
|
| 685 | - } else { |
|
| 686 | - $this->logger->debug('Share notification not send because mailsend is false.', ['app' => 'share']); |
|
| 687 | - } |
|
| 688 | - } |
|
| 689 | - |
|
| 690 | - return $share; |
|
| 691 | - } |
|
| 692 | - |
|
| 693 | - /** |
|
| 694 | - * @param IL10N $l Language of the recipient |
|
| 695 | - * @param string $filename file/folder name |
|
| 696 | - * @param string $link link to the file/folder |
|
| 697 | - * @param string $initiator user ID of share sender |
|
| 698 | - * @param string $shareWith email address of share receiver |
|
| 699 | - * @param \DateTime|null $expiration |
|
| 700 | - * @throws \Exception If mail couldn't be sent |
|
| 701 | - */ |
|
| 702 | - protected function sendMailNotification(IL10N $l, |
|
| 703 | - $filename, |
|
| 704 | - $link, |
|
| 705 | - $initiator, |
|
| 706 | - $shareWith, |
|
| 707 | - \DateTime $expiration = null) { |
|
| 708 | - $initiatorUser = $this->userManager->get($initiator); |
|
| 709 | - $initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator; |
|
| 710 | - $subject = $l->t('%s shared »%s« with you', array($initiatorDisplayName, $filename)); |
|
| 711 | - |
|
| 712 | - $message = $this->mailer->createMessage(); |
|
| 713 | - |
|
| 714 | - $emailTemplate = $this->mailer->createEMailTemplate('files_sharing.RecipientNotification', [ |
|
| 715 | - 'filename' => $filename, |
|
| 716 | - 'link' => $link, |
|
| 717 | - 'initiator' => $initiatorDisplayName, |
|
| 718 | - 'expiration' => $expiration, |
|
| 719 | - 'shareWith' => $shareWith, |
|
| 720 | - ]); |
|
| 721 | - |
|
| 722 | - $emailTemplate->addHeader(); |
|
| 723 | - $emailTemplate->addHeading($l->t('%s shared »%s« with you', [$initiatorDisplayName, $filename]), false); |
|
| 724 | - $text = $l->t('%s shared »%s« with you.', [$initiatorDisplayName, $filename]); |
|
| 725 | - |
|
| 726 | - $emailTemplate->addBodyText( |
|
| 727 | - $text . ' ' . $l->t('Click the button below to open it.'), |
|
| 728 | - $text |
|
| 729 | - ); |
|
| 730 | - $emailTemplate->addBodyButton( |
|
| 731 | - $l->t('Open »%s«', [$filename]), |
|
| 732 | - $link |
|
| 733 | - ); |
|
| 734 | - |
|
| 735 | - $message->setTo([$shareWith]); |
|
| 736 | - |
|
| 737 | - // The "From" contains the sharers name |
|
| 738 | - $instanceName = $this->defaults->getName(); |
|
| 739 | - $senderName = $l->t( |
|
| 740 | - '%s via %s', |
|
| 741 | - [ |
|
| 742 | - $initiatorDisplayName, |
|
| 743 | - $instanceName |
|
| 744 | - ] |
|
| 745 | - ); |
|
| 746 | - $message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]); |
|
| 747 | - |
|
| 748 | - // The "Reply-To" is set to the sharer if an mail address is configured |
|
| 749 | - // also the default footer contains a "Do not reply" which needs to be adjusted. |
|
| 750 | - $initiatorEmail = $initiatorUser->getEMailAddress(); |
|
| 751 | - if($initiatorEmail !== null) { |
|
| 752 | - $message->setReplyTo([$initiatorEmail => $initiatorDisplayName]); |
|
| 753 | - $emailTemplate->addFooter($instanceName . ' - ' . $this->defaults->getSlogan()); |
|
| 754 | - } else { |
|
| 755 | - $emailTemplate->addFooter(); |
|
| 756 | - } |
|
| 757 | - |
|
| 758 | - $message->setSubject($subject); |
|
| 759 | - $message->setPlainBody($emailTemplate->renderText()); |
|
| 760 | - $message->setHtmlBody($emailTemplate->renderHtml()); |
|
| 761 | - $this->mailer->send($message); |
|
| 762 | - } |
|
| 763 | - |
|
| 764 | - /** |
|
| 765 | - * Update a share |
|
| 766 | - * |
|
| 767 | - * @param \OCP\Share\IShare $share |
|
| 768 | - * @return \OCP\Share\IShare The share object |
|
| 769 | - * @throws \InvalidArgumentException |
|
| 770 | - */ |
|
| 771 | - public function updateShare(\OCP\Share\IShare $share) { |
|
| 772 | - $expirationDateUpdated = false; |
|
| 773 | - |
|
| 774 | - $this->canShare($share); |
|
| 775 | - |
|
| 776 | - try { |
|
| 777 | - $originalShare = $this->getShareById($share->getFullId()); |
|
| 778 | - } catch (\UnexpectedValueException $e) { |
|
| 779 | - throw new \InvalidArgumentException('Share does not have a full id'); |
|
| 780 | - } |
|
| 781 | - |
|
| 782 | - // We can't change the share type! |
|
| 783 | - if ($share->getShareType() !== $originalShare->getShareType()) { |
|
| 784 | - throw new \InvalidArgumentException('Can’t change share type'); |
|
| 785 | - } |
|
| 786 | - |
|
| 787 | - // We can only change the recipient on user shares |
|
| 788 | - if ($share->getSharedWith() !== $originalShare->getSharedWith() && |
|
| 789 | - $share->getShareType() !== \OCP\Share::SHARE_TYPE_USER) { |
|
| 790 | - throw new \InvalidArgumentException('Can only update recipient on user shares'); |
|
| 791 | - } |
|
| 792 | - |
|
| 793 | - // Cannot share with the owner |
|
| 794 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER && |
|
| 795 | - $share->getSharedWith() === $share->getShareOwner()) { |
|
| 796 | - throw new \InvalidArgumentException('Can’t share with the share owner'); |
|
| 797 | - } |
|
| 798 | - |
|
| 799 | - $this->generalCreateChecks($share); |
|
| 800 | - |
|
| 801 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { |
|
| 802 | - $this->userCreateChecks($share); |
|
| 803 | - } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 804 | - $this->groupCreateChecks($share); |
|
| 805 | - } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { |
|
| 806 | - $this->linkCreateChecks($share); |
|
| 807 | - |
|
| 808 | - $this->updateSharePasswordIfNeeded($share, $originalShare); |
|
| 809 | - |
|
| 810 | - if ($share->getExpirationDate() != $originalShare->getExpirationDate()) { |
|
| 811 | - //Verify the expiration date |
|
| 812 | - $this->validateExpirationDate($share); |
|
| 813 | - $expirationDateUpdated = true; |
|
| 814 | - } |
|
| 815 | - } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) { |
|
| 816 | - $plainTextPassword = $share->getPassword(); |
|
| 817 | - if (!$this->updateSharePasswordIfNeeded($share, $originalShare)) { |
|
| 818 | - $plainTextPassword = null; |
|
| 819 | - } |
|
| 820 | - } |
|
| 821 | - |
|
| 822 | - $this->pathCreateChecks($share->getNode()); |
|
| 823 | - |
|
| 824 | - // Now update the share! |
|
| 825 | - $provider = $this->factory->getProviderForType($share->getShareType()); |
|
| 826 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) { |
|
| 827 | - $share = $provider->update($share, $plainTextPassword); |
|
| 828 | - } else { |
|
| 829 | - $share = $provider->update($share); |
|
| 830 | - } |
|
| 831 | - |
|
| 832 | - if ($expirationDateUpdated === true) { |
|
| 833 | - \OC_Hook::emit('OCP\Share', 'post_set_expiration_date', [ |
|
| 834 | - 'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder', |
|
| 835 | - 'itemSource' => $share->getNode()->getId(), |
|
| 836 | - 'date' => $share->getExpirationDate(), |
|
| 837 | - 'uidOwner' => $share->getSharedBy(), |
|
| 838 | - ]); |
|
| 839 | - } |
|
| 840 | - |
|
| 841 | - if ($share->getPassword() !== $originalShare->getPassword()) { |
|
| 842 | - \OC_Hook::emit('OCP\Share', 'post_update_password', [ |
|
| 843 | - 'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder', |
|
| 844 | - 'itemSource' => $share->getNode()->getId(), |
|
| 845 | - 'uidOwner' => $share->getSharedBy(), |
|
| 846 | - 'token' => $share->getToken(), |
|
| 847 | - 'disabled' => is_null($share->getPassword()), |
|
| 848 | - ]); |
|
| 849 | - } |
|
| 850 | - |
|
| 851 | - if ($share->getPermissions() !== $originalShare->getPermissions()) { |
|
| 852 | - if ($this->userManager->userExists($share->getShareOwner())) { |
|
| 853 | - $userFolder = $this->rootFolder->getUserFolder($share->getShareOwner()); |
|
| 854 | - } else { |
|
| 855 | - $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy()); |
|
| 856 | - } |
|
| 857 | - \OC_Hook::emit('OCP\Share', 'post_update_permissions', array( |
|
| 858 | - 'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder', |
|
| 859 | - 'itemSource' => $share->getNode()->getId(), |
|
| 860 | - 'shareType' => $share->getShareType(), |
|
| 861 | - 'shareWith' => $share->getSharedWith(), |
|
| 862 | - 'uidOwner' => $share->getSharedBy(), |
|
| 863 | - 'permissions' => $share->getPermissions(), |
|
| 864 | - 'path' => $userFolder->getRelativePath($share->getNode()->getPath()), |
|
| 865 | - )); |
|
| 866 | - } |
|
| 867 | - |
|
| 868 | - return $share; |
|
| 869 | - } |
|
| 870 | - |
|
| 871 | - /** |
|
| 872 | - * Updates the password of the given share if it is not the same as the |
|
| 873 | - * password of the original share. |
|
| 874 | - * |
|
| 875 | - * @param \OCP\Share\IShare $share the share to update its password. |
|
| 876 | - * @param \OCP\Share\IShare $originalShare the original share to compare its |
|
| 877 | - * password with. |
|
| 878 | - * @return boolean whether the password was updated or not. |
|
| 879 | - */ |
|
| 880 | - private function updateSharePasswordIfNeeded(\OCP\Share\IShare $share, \OCP\Share\IShare $originalShare) { |
|
| 881 | - // Password updated. |
|
| 882 | - if ($share->getPassword() !== $originalShare->getPassword()) { |
|
| 883 | - //Verify the password |
|
| 884 | - $this->verifyPassword($share->getPassword()); |
|
| 885 | - |
|
| 886 | - // If a password is set. Hash it! |
|
| 887 | - if ($share->getPassword() !== null) { |
|
| 888 | - $share->setPassword($this->hasher->hash($share->getPassword())); |
|
| 889 | - |
|
| 890 | - return true; |
|
| 891 | - } |
|
| 892 | - } |
|
| 893 | - |
|
| 894 | - return false; |
|
| 895 | - } |
|
| 896 | - |
|
| 897 | - /** |
|
| 898 | - * Delete all the children of this share |
|
| 899 | - * FIXME: remove once https://github.com/owncloud/core/pull/21660 is in |
|
| 900 | - * |
|
| 901 | - * @param \OCP\Share\IShare $share |
|
| 902 | - * @return \OCP\Share\IShare[] List of deleted shares |
|
| 903 | - */ |
|
| 904 | - protected function deleteChildren(\OCP\Share\IShare $share) { |
|
| 905 | - $deletedShares = []; |
|
| 906 | - |
|
| 907 | - $provider = $this->factory->getProviderForType($share->getShareType()); |
|
| 908 | - |
|
| 909 | - foreach ($provider->getChildren($share) as $child) { |
|
| 910 | - $deletedChildren = $this->deleteChildren($child); |
|
| 911 | - $deletedShares = array_merge($deletedShares, $deletedChildren); |
|
| 912 | - |
|
| 913 | - $provider->delete($child); |
|
| 914 | - $deletedShares[] = $child; |
|
| 915 | - } |
|
| 916 | - |
|
| 917 | - return $deletedShares; |
|
| 918 | - } |
|
| 919 | - |
|
| 920 | - /** |
|
| 921 | - * Delete a share |
|
| 922 | - * |
|
| 923 | - * @param \OCP\Share\IShare $share |
|
| 924 | - * @throws ShareNotFound |
|
| 925 | - * @throws \InvalidArgumentException |
|
| 926 | - */ |
|
| 927 | - public function deleteShare(\OCP\Share\IShare $share) { |
|
| 928 | - |
|
| 929 | - try { |
|
| 930 | - $share->getFullId(); |
|
| 931 | - } catch (\UnexpectedValueException $e) { |
|
| 932 | - throw new \InvalidArgumentException('Share does not have a full id'); |
|
| 933 | - } |
|
| 934 | - |
|
| 935 | - $event = new GenericEvent($share); |
|
| 936 | - $this->eventDispatcher->dispatch('OCP\Share::preUnshare', $event); |
|
| 937 | - |
|
| 938 | - // Get all children and delete them as well |
|
| 939 | - $deletedShares = $this->deleteChildren($share); |
|
| 940 | - |
|
| 941 | - // Do the actual delete |
|
| 942 | - $provider = $this->factory->getProviderForType($share->getShareType()); |
|
| 943 | - $provider->delete($share); |
|
| 944 | - |
|
| 945 | - // All the deleted shares caused by this delete |
|
| 946 | - $deletedShares[] = $share; |
|
| 947 | - |
|
| 948 | - // Emit post hook |
|
| 949 | - $event->setArgument('deletedShares', $deletedShares); |
|
| 950 | - $this->eventDispatcher->dispatch('OCP\Share::postUnshare', $event); |
|
| 951 | - } |
|
| 952 | - |
|
| 953 | - |
|
| 954 | - /** |
|
| 955 | - * Unshare a file as the recipient. |
|
| 956 | - * This can be different from a regular delete for example when one of |
|
| 957 | - * the users in a groups deletes that share. But the provider should |
|
| 958 | - * handle this. |
|
| 959 | - * |
|
| 960 | - * @param \OCP\Share\IShare $share |
|
| 961 | - * @param string $recipientId |
|
| 962 | - */ |
|
| 963 | - public function deleteFromSelf(\OCP\Share\IShare $share, $recipientId) { |
|
| 964 | - list($providerId, ) = $this->splitFullId($share->getFullId()); |
|
| 965 | - $provider = $this->factory->getProvider($providerId); |
|
| 966 | - |
|
| 967 | - $provider->deleteFromSelf($share, $recipientId); |
|
| 968 | - $event = new GenericEvent($share); |
|
| 969 | - $this->eventDispatcher->dispatch('OCP\Share::postUnshareFromSelf', $event); |
|
| 970 | - } |
|
| 971 | - |
|
| 972 | - /** |
|
| 973 | - * @inheritdoc |
|
| 974 | - */ |
|
| 975 | - public function moveShare(\OCP\Share\IShare $share, $recipientId) { |
|
| 976 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { |
|
| 977 | - throw new \InvalidArgumentException('Can’t change target of link share'); |
|
| 978 | - } |
|
| 979 | - |
|
| 980 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER && $share->getSharedWith() !== $recipientId) { |
|
| 981 | - throw new \InvalidArgumentException('Invalid recipient'); |
|
| 982 | - } |
|
| 983 | - |
|
| 984 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 985 | - $sharedWith = $this->groupManager->get($share->getSharedWith()); |
|
| 986 | - if (is_null($sharedWith)) { |
|
| 987 | - throw new \InvalidArgumentException('Group "' . $share->getSharedWith() . '" does not exist'); |
|
| 988 | - } |
|
| 989 | - $recipient = $this->userManager->get($recipientId); |
|
| 990 | - if (!$sharedWith->inGroup($recipient)) { |
|
| 991 | - throw new \InvalidArgumentException('Invalid recipient'); |
|
| 992 | - } |
|
| 993 | - } |
|
| 994 | - |
|
| 995 | - list($providerId, ) = $this->splitFullId($share->getFullId()); |
|
| 996 | - $provider = $this->factory->getProvider($providerId); |
|
| 997 | - |
|
| 998 | - $provider->move($share, $recipientId); |
|
| 999 | - } |
|
| 1000 | - |
|
| 1001 | - public function getSharesInFolder($userId, Folder $node, $reshares = false) { |
|
| 1002 | - $providers = $this->factory->getAllProviders(); |
|
| 1003 | - |
|
| 1004 | - return array_reduce($providers, function($shares, IShareProvider $provider) use ($userId, $node, $reshares) { |
|
| 1005 | - $newShares = $provider->getSharesInFolder($userId, $node, $reshares); |
|
| 1006 | - foreach ($newShares as $fid => $data) { |
|
| 1007 | - if (!isset($shares[$fid])) { |
|
| 1008 | - $shares[$fid] = []; |
|
| 1009 | - } |
|
| 1010 | - |
|
| 1011 | - $shares[$fid] = array_merge($shares[$fid], $data); |
|
| 1012 | - } |
|
| 1013 | - return $shares; |
|
| 1014 | - }, []); |
|
| 1015 | - } |
|
| 1016 | - |
|
| 1017 | - /** |
|
| 1018 | - * @inheritdoc |
|
| 1019 | - */ |
|
| 1020 | - public function getSharesBy($userId, $shareType, $path = null, $reshares = false, $limit = 50, $offset = 0) { |
|
| 1021 | - if ($path !== null && |
|
| 1022 | - !($path instanceof \OCP\Files\File) && |
|
| 1023 | - !($path instanceof \OCP\Files\Folder)) { |
|
| 1024 | - throw new \InvalidArgumentException('invalid path'); |
|
| 1025 | - } |
|
| 1026 | - |
|
| 1027 | - try { |
|
| 1028 | - $provider = $this->factory->getProviderForType($shareType); |
|
| 1029 | - } catch (ProviderException $e) { |
|
| 1030 | - return []; |
|
| 1031 | - } |
|
| 1032 | - |
|
| 1033 | - $shares = $provider->getSharesBy($userId, $shareType, $path, $reshares, $limit, $offset); |
|
| 1034 | - |
|
| 1035 | - /* |
|
| 607 | + $share->setToken( |
|
| 608 | + $this->secureRandom->generate( |
|
| 609 | + \OC\Share\Constants::TOKEN_LENGTH, |
|
| 610 | + \OCP\Security\ISecureRandom::CHAR_HUMAN_READABLE |
|
| 611 | + ) |
|
| 612 | + ); |
|
| 613 | + |
|
| 614 | + //Verify the expiration date |
|
| 615 | + $this->validateExpirationDate($share); |
|
| 616 | + |
|
| 617 | + //Verify the password |
|
| 618 | + $this->verifyPassword($share->getPassword()); |
|
| 619 | + |
|
| 620 | + // If a password is set. Hash it! |
|
| 621 | + if ($share->getPassword() !== null) { |
|
| 622 | + $share->setPassword($this->hasher->hash($share->getPassword())); |
|
| 623 | + } |
|
| 624 | + } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) { |
|
| 625 | + $share->setToken( |
|
| 626 | + $this->secureRandom->generate( |
|
| 627 | + \OC\Share\Constants::TOKEN_LENGTH, |
|
| 628 | + \OCP\Security\ISecureRandom::CHAR_HUMAN_READABLE |
|
| 629 | + ) |
|
| 630 | + ); |
|
| 631 | + } |
|
| 632 | + |
|
| 633 | + // Cannot share with the owner |
|
| 634 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER && |
|
| 635 | + $share->getSharedWith() === $share->getShareOwner()) { |
|
| 636 | + throw new \InvalidArgumentException('Can’t share with the share owner'); |
|
| 637 | + } |
|
| 638 | + |
|
| 639 | + // Generate the target |
|
| 640 | + $target = $this->config->getSystemValue('share_folder', '/') .'/'. $share->getNode()->getName(); |
|
| 641 | + $target = \OC\Files\Filesystem::normalizePath($target); |
|
| 642 | + $share->setTarget($target); |
|
| 643 | + |
|
| 644 | + // Pre share event |
|
| 645 | + $event = new GenericEvent($share); |
|
| 646 | + $a = $this->eventDispatcher->dispatch('OCP\Share::preShare', $event); |
|
| 647 | + if ($event->isPropagationStopped() && $event->hasArgument('error')) { |
|
| 648 | + throw new \Exception($event->getArgument('error')); |
|
| 649 | + } |
|
| 650 | + |
|
| 651 | + $oldShare = $share; |
|
| 652 | + $provider = $this->factory->getProviderForType($share->getShareType()); |
|
| 653 | + $share = $provider->create($share); |
|
| 654 | + //reuse the node we already have |
|
| 655 | + $share->setNode($oldShare->getNode()); |
|
| 656 | + |
|
| 657 | + // Post share event |
|
| 658 | + $event = new GenericEvent($share); |
|
| 659 | + $this->eventDispatcher->dispatch('OCP\Share::postShare', $event); |
|
| 660 | + |
|
| 661 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { |
|
| 662 | + $mailSend = $share->getMailSend(); |
|
| 663 | + if($mailSend === true) { |
|
| 664 | + $user = $this->userManager->get($share->getSharedWith()); |
|
| 665 | + if ($user !== null) { |
|
| 666 | + $emailAddress = $user->getEMailAddress(); |
|
| 667 | + if ($emailAddress !== null && $emailAddress !== '') { |
|
| 668 | + $userLang = $this->config->getUserValue($share->getSharedWith(), 'core', 'lang', null); |
|
| 669 | + $l = $this->l10nFactory->get('lib', $userLang); |
|
| 670 | + $this->sendMailNotification( |
|
| 671 | + $l, |
|
| 672 | + $share->getNode()->getName(), |
|
| 673 | + $this->urlGenerator->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $share->getNode()->getId()]), |
|
| 674 | + $share->getSharedBy(), |
|
| 675 | + $emailAddress, |
|
| 676 | + $share->getExpirationDate() |
|
| 677 | + ); |
|
| 678 | + $this->logger->debug('Send share notification to ' . $emailAddress . ' for share with ID ' . $share->getId(), ['app' => 'share']); |
|
| 679 | + } else { |
|
| 680 | + $this->logger->debug('Share notification not send to ' . $share->getSharedWith() . ' because email address is not set.', ['app' => 'share']); |
|
| 681 | + } |
|
| 682 | + } else { |
|
| 683 | + $this->logger->debug('Share notification not send to ' . $share->getSharedWith() . ' because user could not be found.', ['app' => 'share']); |
|
| 684 | + } |
|
| 685 | + } else { |
|
| 686 | + $this->logger->debug('Share notification not send because mailsend is false.', ['app' => 'share']); |
|
| 687 | + } |
|
| 688 | + } |
|
| 689 | + |
|
| 690 | + return $share; |
|
| 691 | + } |
|
| 692 | + |
|
| 693 | + /** |
|
| 694 | + * @param IL10N $l Language of the recipient |
|
| 695 | + * @param string $filename file/folder name |
|
| 696 | + * @param string $link link to the file/folder |
|
| 697 | + * @param string $initiator user ID of share sender |
|
| 698 | + * @param string $shareWith email address of share receiver |
|
| 699 | + * @param \DateTime|null $expiration |
|
| 700 | + * @throws \Exception If mail couldn't be sent |
|
| 701 | + */ |
|
| 702 | + protected function sendMailNotification(IL10N $l, |
|
| 703 | + $filename, |
|
| 704 | + $link, |
|
| 705 | + $initiator, |
|
| 706 | + $shareWith, |
|
| 707 | + \DateTime $expiration = null) { |
|
| 708 | + $initiatorUser = $this->userManager->get($initiator); |
|
| 709 | + $initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator; |
|
| 710 | + $subject = $l->t('%s shared »%s« with you', array($initiatorDisplayName, $filename)); |
|
| 711 | + |
|
| 712 | + $message = $this->mailer->createMessage(); |
|
| 713 | + |
|
| 714 | + $emailTemplate = $this->mailer->createEMailTemplate('files_sharing.RecipientNotification', [ |
|
| 715 | + 'filename' => $filename, |
|
| 716 | + 'link' => $link, |
|
| 717 | + 'initiator' => $initiatorDisplayName, |
|
| 718 | + 'expiration' => $expiration, |
|
| 719 | + 'shareWith' => $shareWith, |
|
| 720 | + ]); |
|
| 721 | + |
|
| 722 | + $emailTemplate->addHeader(); |
|
| 723 | + $emailTemplate->addHeading($l->t('%s shared »%s« with you', [$initiatorDisplayName, $filename]), false); |
|
| 724 | + $text = $l->t('%s shared »%s« with you.', [$initiatorDisplayName, $filename]); |
|
| 725 | + |
|
| 726 | + $emailTemplate->addBodyText( |
|
| 727 | + $text . ' ' . $l->t('Click the button below to open it.'), |
|
| 728 | + $text |
|
| 729 | + ); |
|
| 730 | + $emailTemplate->addBodyButton( |
|
| 731 | + $l->t('Open »%s«', [$filename]), |
|
| 732 | + $link |
|
| 733 | + ); |
|
| 734 | + |
|
| 735 | + $message->setTo([$shareWith]); |
|
| 736 | + |
|
| 737 | + // The "From" contains the sharers name |
|
| 738 | + $instanceName = $this->defaults->getName(); |
|
| 739 | + $senderName = $l->t( |
|
| 740 | + '%s via %s', |
|
| 741 | + [ |
|
| 742 | + $initiatorDisplayName, |
|
| 743 | + $instanceName |
|
| 744 | + ] |
|
| 745 | + ); |
|
| 746 | + $message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]); |
|
| 747 | + |
|
| 748 | + // The "Reply-To" is set to the sharer if an mail address is configured |
|
| 749 | + // also the default footer contains a "Do not reply" which needs to be adjusted. |
|
| 750 | + $initiatorEmail = $initiatorUser->getEMailAddress(); |
|
| 751 | + if($initiatorEmail !== null) { |
|
| 752 | + $message->setReplyTo([$initiatorEmail => $initiatorDisplayName]); |
|
| 753 | + $emailTemplate->addFooter($instanceName . ' - ' . $this->defaults->getSlogan()); |
|
| 754 | + } else { |
|
| 755 | + $emailTemplate->addFooter(); |
|
| 756 | + } |
|
| 757 | + |
|
| 758 | + $message->setSubject($subject); |
|
| 759 | + $message->setPlainBody($emailTemplate->renderText()); |
|
| 760 | + $message->setHtmlBody($emailTemplate->renderHtml()); |
|
| 761 | + $this->mailer->send($message); |
|
| 762 | + } |
|
| 763 | + |
|
| 764 | + /** |
|
| 765 | + * Update a share |
|
| 766 | + * |
|
| 767 | + * @param \OCP\Share\IShare $share |
|
| 768 | + * @return \OCP\Share\IShare The share object |
|
| 769 | + * @throws \InvalidArgumentException |
|
| 770 | + */ |
|
| 771 | + public function updateShare(\OCP\Share\IShare $share) { |
|
| 772 | + $expirationDateUpdated = false; |
|
| 773 | + |
|
| 774 | + $this->canShare($share); |
|
| 775 | + |
|
| 776 | + try { |
|
| 777 | + $originalShare = $this->getShareById($share->getFullId()); |
|
| 778 | + } catch (\UnexpectedValueException $e) { |
|
| 779 | + throw new \InvalidArgumentException('Share does not have a full id'); |
|
| 780 | + } |
|
| 781 | + |
|
| 782 | + // We can't change the share type! |
|
| 783 | + if ($share->getShareType() !== $originalShare->getShareType()) { |
|
| 784 | + throw new \InvalidArgumentException('Can’t change share type'); |
|
| 785 | + } |
|
| 786 | + |
|
| 787 | + // We can only change the recipient on user shares |
|
| 788 | + if ($share->getSharedWith() !== $originalShare->getSharedWith() && |
|
| 789 | + $share->getShareType() !== \OCP\Share::SHARE_TYPE_USER) { |
|
| 790 | + throw new \InvalidArgumentException('Can only update recipient on user shares'); |
|
| 791 | + } |
|
| 792 | + |
|
| 793 | + // Cannot share with the owner |
|
| 794 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER && |
|
| 795 | + $share->getSharedWith() === $share->getShareOwner()) { |
|
| 796 | + throw new \InvalidArgumentException('Can’t share with the share owner'); |
|
| 797 | + } |
|
| 798 | + |
|
| 799 | + $this->generalCreateChecks($share); |
|
| 800 | + |
|
| 801 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { |
|
| 802 | + $this->userCreateChecks($share); |
|
| 803 | + } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 804 | + $this->groupCreateChecks($share); |
|
| 805 | + } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { |
|
| 806 | + $this->linkCreateChecks($share); |
|
| 807 | + |
|
| 808 | + $this->updateSharePasswordIfNeeded($share, $originalShare); |
|
| 809 | + |
|
| 810 | + if ($share->getExpirationDate() != $originalShare->getExpirationDate()) { |
|
| 811 | + //Verify the expiration date |
|
| 812 | + $this->validateExpirationDate($share); |
|
| 813 | + $expirationDateUpdated = true; |
|
| 814 | + } |
|
| 815 | + } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) { |
|
| 816 | + $plainTextPassword = $share->getPassword(); |
|
| 817 | + if (!$this->updateSharePasswordIfNeeded($share, $originalShare)) { |
|
| 818 | + $plainTextPassword = null; |
|
| 819 | + } |
|
| 820 | + } |
|
| 821 | + |
|
| 822 | + $this->pathCreateChecks($share->getNode()); |
|
| 823 | + |
|
| 824 | + // Now update the share! |
|
| 825 | + $provider = $this->factory->getProviderForType($share->getShareType()); |
|
| 826 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) { |
|
| 827 | + $share = $provider->update($share, $plainTextPassword); |
|
| 828 | + } else { |
|
| 829 | + $share = $provider->update($share); |
|
| 830 | + } |
|
| 831 | + |
|
| 832 | + if ($expirationDateUpdated === true) { |
|
| 833 | + \OC_Hook::emit('OCP\Share', 'post_set_expiration_date', [ |
|
| 834 | + 'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder', |
|
| 835 | + 'itemSource' => $share->getNode()->getId(), |
|
| 836 | + 'date' => $share->getExpirationDate(), |
|
| 837 | + 'uidOwner' => $share->getSharedBy(), |
|
| 838 | + ]); |
|
| 839 | + } |
|
| 840 | + |
|
| 841 | + if ($share->getPassword() !== $originalShare->getPassword()) { |
|
| 842 | + \OC_Hook::emit('OCP\Share', 'post_update_password', [ |
|
| 843 | + 'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder', |
|
| 844 | + 'itemSource' => $share->getNode()->getId(), |
|
| 845 | + 'uidOwner' => $share->getSharedBy(), |
|
| 846 | + 'token' => $share->getToken(), |
|
| 847 | + 'disabled' => is_null($share->getPassword()), |
|
| 848 | + ]); |
|
| 849 | + } |
|
| 850 | + |
|
| 851 | + if ($share->getPermissions() !== $originalShare->getPermissions()) { |
|
| 852 | + if ($this->userManager->userExists($share->getShareOwner())) { |
|
| 853 | + $userFolder = $this->rootFolder->getUserFolder($share->getShareOwner()); |
|
| 854 | + } else { |
|
| 855 | + $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy()); |
|
| 856 | + } |
|
| 857 | + \OC_Hook::emit('OCP\Share', 'post_update_permissions', array( |
|
| 858 | + 'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder', |
|
| 859 | + 'itemSource' => $share->getNode()->getId(), |
|
| 860 | + 'shareType' => $share->getShareType(), |
|
| 861 | + 'shareWith' => $share->getSharedWith(), |
|
| 862 | + 'uidOwner' => $share->getSharedBy(), |
|
| 863 | + 'permissions' => $share->getPermissions(), |
|
| 864 | + 'path' => $userFolder->getRelativePath($share->getNode()->getPath()), |
|
| 865 | + )); |
|
| 866 | + } |
|
| 867 | + |
|
| 868 | + return $share; |
|
| 869 | + } |
|
| 870 | + |
|
| 871 | + /** |
|
| 872 | + * Updates the password of the given share if it is not the same as the |
|
| 873 | + * password of the original share. |
|
| 874 | + * |
|
| 875 | + * @param \OCP\Share\IShare $share the share to update its password. |
|
| 876 | + * @param \OCP\Share\IShare $originalShare the original share to compare its |
|
| 877 | + * password with. |
|
| 878 | + * @return boolean whether the password was updated or not. |
|
| 879 | + */ |
|
| 880 | + private function updateSharePasswordIfNeeded(\OCP\Share\IShare $share, \OCP\Share\IShare $originalShare) { |
|
| 881 | + // Password updated. |
|
| 882 | + if ($share->getPassword() !== $originalShare->getPassword()) { |
|
| 883 | + //Verify the password |
|
| 884 | + $this->verifyPassword($share->getPassword()); |
|
| 885 | + |
|
| 886 | + // If a password is set. Hash it! |
|
| 887 | + if ($share->getPassword() !== null) { |
|
| 888 | + $share->setPassword($this->hasher->hash($share->getPassword())); |
|
| 889 | + |
|
| 890 | + return true; |
|
| 891 | + } |
|
| 892 | + } |
|
| 893 | + |
|
| 894 | + return false; |
|
| 895 | + } |
|
| 896 | + |
|
| 897 | + /** |
|
| 898 | + * Delete all the children of this share |
|
| 899 | + * FIXME: remove once https://github.com/owncloud/core/pull/21660 is in |
|
| 900 | + * |
|
| 901 | + * @param \OCP\Share\IShare $share |
|
| 902 | + * @return \OCP\Share\IShare[] List of deleted shares |
|
| 903 | + */ |
|
| 904 | + protected function deleteChildren(\OCP\Share\IShare $share) { |
|
| 905 | + $deletedShares = []; |
|
| 906 | + |
|
| 907 | + $provider = $this->factory->getProviderForType($share->getShareType()); |
|
| 908 | + |
|
| 909 | + foreach ($provider->getChildren($share) as $child) { |
|
| 910 | + $deletedChildren = $this->deleteChildren($child); |
|
| 911 | + $deletedShares = array_merge($deletedShares, $deletedChildren); |
|
| 912 | + |
|
| 913 | + $provider->delete($child); |
|
| 914 | + $deletedShares[] = $child; |
|
| 915 | + } |
|
| 916 | + |
|
| 917 | + return $deletedShares; |
|
| 918 | + } |
|
| 919 | + |
|
| 920 | + /** |
|
| 921 | + * Delete a share |
|
| 922 | + * |
|
| 923 | + * @param \OCP\Share\IShare $share |
|
| 924 | + * @throws ShareNotFound |
|
| 925 | + * @throws \InvalidArgumentException |
|
| 926 | + */ |
|
| 927 | + public function deleteShare(\OCP\Share\IShare $share) { |
|
| 928 | + |
|
| 929 | + try { |
|
| 930 | + $share->getFullId(); |
|
| 931 | + } catch (\UnexpectedValueException $e) { |
|
| 932 | + throw new \InvalidArgumentException('Share does not have a full id'); |
|
| 933 | + } |
|
| 934 | + |
|
| 935 | + $event = new GenericEvent($share); |
|
| 936 | + $this->eventDispatcher->dispatch('OCP\Share::preUnshare', $event); |
|
| 937 | + |
|
| 938 | + // Get all children and delete them as well |
|
| 939 | + $deletedShares = $this->deleteChildren($share); |
|
| 940 | + |
|
| 941 | + // Do the actual delete |
|
| 942 | + $provider = $this->factory->getProviderForType($share->getShareType()); |
|
| 943 | + $provider->delete($share); |
|
| 944 | + |
|
| 945 | + // All the deleted shares caused by this delete |
|
| 946 | + $deletedShares[] = $share; |
|
| 947 | + |
|
| 948 | + // Emit post hook |
|
| 949 | + $event->setArgument('deletedShares', $deletedShares); |
|
| 950 | + $this->eventDispatcher->dispatch('OCP\Share::postUnshare', $event); |
|
| 951 | + } |
|
| 952 | + |
|
| 953 | + |
|
| 954 | + /** |
|
| 955 | + * Unshare a file as the recipient. |
|
| 956 | + * This can be different from a regular delete for example when one of |
|
| 957 | + * the users in a groups deletes that share. But the provider should |
|
| 958 | + * handle this. |
|
| 959 | + * |
|
| 960 | + * @param \OCP\Share\IShare $share |
|
| 961 | + * @param string $recipientId |
|
| 962 | + */ |
|
| 963 | + public function deleteFromSelf(\OCP\Share\IShare $share, $recipientId) { |
|
| 964 | + list($providerId, ) = $this->splitFullId($share->getFullId()); |
|
| 965 | + $provider = $this->factory->getProvider($providerId); |
|
| 966 | + |
|
| 967 | + $provider->deleteFromSelf($share, $recipientId); |
|
| 968 | + $event = new GenericEvent($share); |
|
| 969 | + $this->eventDispatcher->dispatch('OCP\Share::postUnshareFromSelf', $event); |
|
| 970 | + } |
|
| 971 | + |
|
| 972 | + /** |
|
| 973 | + * @inheritdoc |
|
| 974 | + */ |
|
| 975 | + public function moveShare(\OCP\Share\IShare $share, $recipientId) { |
|
| 976 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { |
|
| 977 | + throw new \InvalidArgumentException('Can’t change target of link share'); |
|
| 978 | + } |
|
| 979 | + |
|
| 980 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER && $share->getSharedWith() !== $recipientId) { |
|
| 981 | + throw new \InvalidArgumentException('Invalid recipient'); |
|
| 982 | + } |
|
| 983 | + |
|
| 984 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 985 | + $sharedWith = $this->groupManager->get($share->getSharedWith()); |
|
| 986 | + if (is_null($sharedWith)) { |
|
| 987 | + throw new \InvalidArgumentException('Group "' . $share->getSharedWith() . '" does not exist'); |
|
| 988 | + } |
|
| 989 | + $recipient = $this->userManager->get($recipientId); |
|
| 990 | + if (!$sharedWith->inGroup($recipient)) { |
|
| 991 | + throw new \InvalidArgumentException('Invalid recipient'); |
|
| 992 | + } |
|
| 993 | + } |
|
| 994 | + |
|
| 995 | + list($providerId, ) = $this->splitFullId($share->getFullId()); |
|
| 996 | + $provider = $this->factory->getProvider($providerId); |
|
| 997 | + |
|
| 998 | + $provider->move($share, $recipientId); |
|
| 999 | + } |
|
| 1000 | + |
|
| 1001 | + public function getSharesInFolder($userId, Folder $node, $reshares = false) { |
|
| 1002 | + $providers = $this->factory->getAllProviders(); |
|
| 1003 | + |
|
| 1004 | + return array_reduce($providers, function($shares, IShareProvider $provider) use ($userId, $node, $reshares) { |
|
| 1005 | + $newShares = $provider->getSharesInFolder($userId, $node, $reshares); |
|
| 1006 | + foreach ($newShares as $fid => $data) { |
|
| 1007 | + if (!isset($shares[$fid])) { |
|
| 1008 | + $shares[$fid] = []; |
|
| 1009 | + } |
|
| 1010 | + |
|
| 1011 | + $shares[$fid] = array_merge($shares[$fid], $data); |
|
| 1012 | + } |
|
| 1013 | + return $shares; |
|
| 1014 | + }, []); |
|
| 1015 | + } |
|
| 1016 | + |
|
| 1017 | + /** |
|
| 1018 | + * @inheritdoc |
|
| 1019 | + */ |
|
| 1020 | + public function getSharesBy($userId, $shareType, $path = null, $reshares = false, $limit = 50, $offset = 0) { |
|
| 1021 | + if ($path !== null && |
|
| 1022 | + !($path instanceof \OCP\Files\File) && |
|
| 1023 | + !($path instanceof \OCP\Files\Folder)) { |
|
| 1024 | + throw new \InvalidArgumentException('invalid path'); |
|
| 1025 | + } |
|
| 1026 | + |
|
| 1027 | + try { |
|
| 1028 | + $provider = $this->factory->getProviderForType($shareType); |
|
| 1029 | + } catch (ProviderException $e) { |
|
| 1030 | + return []; |
|
| 1031 | + } |
|
| 1032 | + |
|
| 1033 | + $shares = $provider->getSharesBy($userId, $shareType, $path, $reshares, $limit, $offset); |
|
| 1034 | + |
|
| 1035 | + /* |
|
| 1036 | 1036 | * Work around so we don't return expired shares but still follow |
| 1037 | 1037 | * proper pagination. |
| 1038 | 1038 | */ |
| 1039 | 1039 | |
| 1040 | - $shares2 = []; |
|
| 1041 | - |
|
| 1042 | - while(true) { |
|
| 1043 | - $added = 0; |
|
| 1044 | - foreach ($shares as $share) { |
|
| 1045 | - |
|
| 1046 | - try { |
|
| 1047 | - $this->checkExpireDate($share); |
|
| 1048 | - } catch (ShareNotFound $e) { |
|
| 1049 | - //Ignore since this basically means the share is deleted |
|
| 1050 | - continue; |
|
| 1051 | - } |
|
| 1052 | - |
|
| 1053 | - $added++; |
|
| 1054 | - $shares2[] = $share; |
|
| 1055 | - |
|
| 1056 | - if (count($shares2) === $limit) { |
|
| 1057 | - break; |
|
| 1058 | - } |
|
| 1059 | - } |
|
| 1060 | - |
|
| 1061 | - if (count($shares2) === $limit) { |
|
| 1062 | - break; |
|
| 1063 | - } |
|
| 1064 | - |
|
| 1065 | - // If there was no limit on the select we are done |
|
| 1066 | - if ($limit === -1) { |
|
| 1067 | - break; |
|
| 1068 | - } |
|
| 1069 | - |
|
| 1070 | - $offset += $added; |
|
| 1071 | - |
|
| 1072 | - // Fetch again $limit shares |
|
| 1073 | - $shares = $provider->getSharesBy($userId, $shareType, $path, $reshares, $limit, $offset); |
|
| 1074 | - |
|
| 1075 | - // No more shares means we are done |
|
| 1076 | - if (empty($shares)) { |
|
| 1077 | - break; |
|
| 1078 | - } |
|
| 1079 | - } |
|
| 1080 | - |
|
| 1081 | - $shares = $shares2; |
|
| 1082 | - |
|
| 1083 | - return $shares; |
|
| 1084 | - } |
|
| 1085 | - |
|
| 1086 | - /** |
|
| 1087 | - * @inheritdoc |
|
| 1088 | - */ |
|
| 1089 | - public function getSharedWith($userId, $shareType, $node = null, $limit = 50, $offset = 0) { |
|
| 1090 | - try { |
|
| 1091 | - $provider = $this->factory->getProviderForType($shareType); |
|
| 1092 | - } catch (ProviderException $e) { |
|
| 1093 | - return []; |
|
| 1094 | - } |
|
| 1095 | - |
|
| 1096 | - $shares = $provider->getSharedWith($userId, $shareType, $node, $limit, $offset); |
|
| 1097 | - |
|
| 1098 | - // remove all shares which are already expired |
|
| 1099 | - foreach ($shares as $key => $share) { |
|
| 1100 | - try { |
|
| 1101 | - $this->checkExpireDate($share); |
|
| 1102 | - } catch (ShareNotFound $e) { |
|
| 1103 | - unset($shares[$key]); |
|
| 1104 | - } |
|
| 1105 | - } |
|
| 1106 | - |
|
| 1107 | - return $shares; |
|
| 1108 | - } |
|
| 1109 | - |
|
| 1110 | - /** |
|
| 1111 | - * @inheritdoc |
|
| 1112 | - */ |
|
| 1113 | - public function getShareById($id, $recipient = null) { |
|
| 1114 | - if ($id === null) { |
|
| 1115 | - throw new ShareNotFound(); |
|
| 1116 | - } |
|
| 1117 | - |
|
| 1118 | - list($providerId, $id) = $this->splitFullId($id); |
|
| 1119 | - |
|
| 1120 | - try { |
|
| 1121 | - $provider = $this->factory->getProvider($providerId); |
|
| 1122 | - } catch (ProviderException $e) { |
|
| 1123 | - throw new ShareNotFound(); |
|
| 1124 | - } |
|
| 1125 | - |
|
| 1126 | - $share = $provider->getShareById($id, $recipient); |
|
| 1127 | - |
|
| 1128 | - $this->checkExpireDate($share); |
|
| 1129 | - |
|
| 1130 | - return $share; |
|
| 1131 | - } |
|
| 1132 | - |
|
| 1133 | - /** |
|
| 1134 | - * Get all the shares for a given path |
|
| 1135 | - * |
|
| 1136 | - * @param \OCP\Files\Node $path |
|
| 1137 | - * @param int $page |
|
| 1138 | - * @param int $perPage |
|
| 1139 | - * |
|
| 1140 | - * @return Share[] |
|
| 1141 | - */ |
|
| 1142 | - public function getSharesByPath(\OCP\Files\Node $path, $page=0, $perPage=50) { |
|
| 1143 | - return []; |
|
| 1144 | - } |
|
| 1145 | - |
|
| 1146 | - /** |
|
| 1147 | - * Get the share by token possible with password |
|
| 1148 | - * |
|
| 1149 | - * @param string $token |
|
| 1150 | - * @return Share |
|
| 1151 | - * |
|
| 1152 | - * @throws ShareNotFound |
|
| 1153 | - */ |
|
| 1154 | - public function getShareByToken($token) { |
|
| 1155 | - $share = null; |
|
| 1156 | - try { |
|
| 1157 | - if($this->shareApiAllowLinks()) { |
|
| 1158 | - $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_LINK); |
|
| 1159 | - $share = $provider->getShareByToken($token); |
|
| 1160 | - } |
|
| 1161 | - } catch (ProviderException $e) { |
|
| 1162 | - } catch (ShareNotFound $e) { |
|
| 1163 | - } |
|
| 1164 | - |
|
| 1165 | - |
|
| 1166 | - // If it is not a link share try to fetch a federated share by token |
|
| 1167 | - if ($share === null) { |
|
| 1168 | - try { |
|
| 1169 | - $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_REMOTE); |
|
| 1170 | - $share = $provider->getShareByToken($token); |
|
| 1171 | - } catch (ProviderException $e) { |
|
| 1172 | - } catch (ShareNotFound $e) { |
|
| 1173 | - } |
|
| 1174 | - } |
|
| 1175 | - |
|
| 1176 | - // If it is not a link share try to fetch a mail share by token |
|
| 1177 | - if ($share === null && $this->shareProviderExists(\OCP\Share::SHARE_TYPE_EMAIL)) { |
|
| 1178 | - try { |
|
| 1179 | - $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_EMAIL); |
|
| 1180 | - $share = $provider->getShareByToken($token); |
|
| 1181 | - } catch (ProviderException $e) { |
|
| 1182 | - } catch (ShareNotFound $e) { |
|
| 1183 | - } |
|
| 1184 | - } |
|
| 1185 | - |
|
| 1186 | - if ($share === null && $this->shareProviderExists(\OCP\Share::SHARE_TYPE_CIRCLE)) { |
|
| 1187 | - try { |
|
| 1188 | - $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_CIRCLE); |
|
| 1189 | - $share = $provider->getShareByToken($token); |
|
| 1190 | - } catch (ProviderException $e) { |
|
| 1191 | - } catch (ShareNotFound $e) { |
|
| 1192 | - } |
|
| 1193 | - } |
|
| 1194 | - |
|
| 1195 | - if ($share === null) { |
|
| 1196 | - throw new ShareNotFound($this->l->t('The requested share does not exist anymore')); |
|
| 1197 | - } |
|
| 1198 | - |
|
| 1199 | - $this->checkExpireDate($share); |
|
| 1200 | - |
|
| 1201 | - /* |
|
| 1040 | + $shares2 = []; |
|
| 1041 | + |
|
| 1042 | + while(true) { |
|
| 1043 | + $added = 0; |
|
| 1044 | + foreach ($shares as $share) { |
|
| 1045 | + |
|
| 1046 | + try { |
|
| 1047 | + $this->checkExpireDate($share); |
|
| 1048 | + } catch (ShareNotFound $e) { |
|
| 1049 | + //Ignore since this basically means the share is deleted |
|
| 1050 | + continue; |
|
| 1051 | + } |
|
| 1052 | + |
|
| 1053 | + $added++; |
|
| 1054 | + $shares2[] = $share; |
|
| 1055 | + |
|
| 1056 | + if (count($shares2) === $limit) { |
|
| 1057 | + break; |
|
| 1058 | + } |
|
| 1059 | + } |
|
| 1060 | + |
|
| 1061 | + if (count($shares2) === $limit) { |
|
| 1062 | + break; |
|
| 1063 | + } |
|
| 1064 | + |
|
| 1065 | + // If there was no limit on the select we are done |
|
| 1066 | + if ($limit === -1) { |
|
| 1067 | + break; |
|
| 1068 | + } |
|
| 1069 | + |
|
| 1070 | + $offset += $added; |
|
| 1071 | + |
|
| 1072 | + // Fetch again $limit shares |
|
| 1073 | + $shares = $provider->getSharesBy($userId, $shareType, $path, $reshares, $limit, $offset); |
|
| 1074 | + |
|
| 1075 | + // No more shares means we are done |
|
| 1076 | + if (empty($shares)) { |
|
| 1077 | + break; |
|
| 1078 | + } |
|
| 1079 | + } |
|
| 1080 | + |
|
| 1081 | + $shares = $shares2; |
|
| 1082 | + |
|
| 1083 | + return $shares; |
|
| 1084 | + } |
|
| 1085 | + |
|
| 1086 | + /** |
|
| 1087 | + * @inheritdoc |
|
| 1088 | + */ |
|
| 1089 | + public function getSharedWith($userId, $shareType, $node = null, $limit = 50, $offset = 0) { |
|
| 1090 | + try { |
|
| 1091 | + $provider = $this->factory->getProviderForType($shareType); |
|
| 1092 | + } catch (ProviderException $e) { |
|
| 1093 | + return []; |
|
| 1094 | + } |
|
| 1095 | + |
|
| 1096 | + $shares = $provider->getSharedWith($userId, $shareType, $node, $limit, $offset); |
|
| 1097 | + |
|
| 1098 | + // remove all shares which are already expired |
|
| 1099 | + foreach ($shares as $key => $share) { |
|
| 1100 | + try { |
|
| 1101 | + $this->checkExpireDate($share); |
|
| 1102 | + } catch (ShareNotFound $e) { |
|
| 1103 | + unset($shares[$key]); |
|
| 1104 | + } |
|
| 1105 | + } |
|
| 1106 | + |
|
| 1107 | + return $shares; |
|
| 1108 | + } |
|
| 1109 | + |
|
| 1110 | + /** |
|
| 1111 | + * @inheritdoc |
|
| 1112 | + */ |
|
| 1113 | + public function getShareById($id, $recipient = null) { |
|
| 1114 | + if ($id === null) { |
|
| 1115 | + throw new ShareNotFound(); |
|
| 1116 | + } |
|
| 1117 | + |
|
| 1118 | + list($providerId, $id) = $this->splitFullId($id); |
|
| 1119 | + |
|
| 1120 | + try { |
|
| 1121 | + $provider = $this->factory->getProvider($providerId); |
|
| 1122 | + } catch (ProviderException $e) { |
|
| 1123 | + throw new ShareNotFound(); |
|
| 1124 | + } |
|
| 1125 | + |
|
| 1126 | + $share = $provider->getShareById($id, $recipient); |
|
| 1127 | + |
|
| 1128 | + $this->checkExpireDate($share); |
|
| 1129 | + |
|
| 1130 | + return $share; |
|
| 1131 | + } |
|
| 1132 | + |
|
| 1133 | + /** |
|
| 1134 | + * Get all the shares for a given path |
|
| 1135 | + * |
|
| 1136 | + * @param \OCP\Files\Node $path |
|
| 1137 | + * @param int $page |
|
| 1138 | + * @param int $perPage |
|
| 1139 | + * |
|
| 1140 | + * @return Share[] |
|
| 1141 | + */ |
|
| 1142 | + public function getSharesByPath(\OCP\Files\Node $path, $page=0, $perPage=50) { |
|
| 1143 | + return []; |
|
| 1144 | + } |
|
| 1145 | + |
|
| 1146 | + /** |
|
| 1147 | + * Get the share by token possible with password |
|
| 1148 | + * |
|
| 1149 | + * @param string $token |
|
| 1150 | + * @return Share |
|
| 1151 | + * |
|
| 1152 | + * @throws ShareNotFound |
|
| 1153 | + */ |
|
| 1154 | + public function getShareByToken($token) { |
|
| 1155 | + $share = null; |
|
| 1156 | + try { |
|
| 1157 | + if($this->shareApiAllowLinks()) { |
|
| 1158 | + $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_LINK); |
|
| 1159 | + $share = $provider->getShareByToken($token); |
|
| 1160 | + } |
|
| 1161 | + } catch (ProviderException $e) { |
|
| 1162 | + } catch (ShareNotFound $e) { |
|
| 1163 | + } |
|
| 1164 | + |
|
| 1165 | + |
|
| 1166 | + // If it is not a link share try to fetch a federated share by token |
|
| 1167 | + if ($share === null) { |
|
| 1168 | + try { |
|
| 1169 | + $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_REMOTE); |
|
| 1170 | + $share = $provider->getShareByToken($token); |
|
| 1171 | + } catch (ProviderException $e) { |
|
| 1172 | + } catch (ShareNotFound $e) { |
|
| 1173 | + } |
|
| 1174 | + } |
|
| 1175 | + |
|
| 1176 | + // If it is not a link share try to fetch a mail share by token |
|
| 1177 | + if ($share === null && $this->shareProviderExists(\OCP\Share::SHARE_TYPE_EMAIL)) { |
|
| 1178 | + try { |
|
| 1179 | + $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_EMAIL); |
|
| 1180 | + $share = $provider->getShareByToken($token); |
|
| 1181 | + } catch (ProviderException $e) { |
|
| 1182 | + } catch (ShareNotFound $e) { |
|
| 1183 | + } |
|
| 1184 | + } |
|
| 1185 | + |
|
| 1186 | + if ($share === null && $this->shareProviderExists(\OCP\Share::SHARE_TYPE_CIRCLE)) { |
|
| 1187 | + try { |
|
| 1188 | + $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_CIRCLE); |
|
| 1189 | + $share = $provider->getShareByToken($token); |
|
| 1190 | + } catch (ProviderException $e) { |
|
| 1191 | + } catch (ShareNotFound $e) { |
|
| 1192 | + } |
|
| 1193 | + } |
|
| 1194 | + |
|
| 1195 | + if ($share === null) { |
|
| 1196 | + throw new ShareNotFound($this->l->t('The requested share does not exist anymore')); |
|
| 1197 | + } |
|
| 1198 | + |
|
| 1199 | + $this->checkExpireDate($share); |
|
| 1200 | + |
|
| 1201 | + /* |
|
| 1202 | 1202 | * Reduce the permissions for link shares if public upload is not enabled |
| 1203 | 1203 | */ |
| 1204 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK && |
|
| 1205 | - !$this->shareApiLinkAllowPublicUpload()) { |
|
| 1206 | - $share->setPermissions($share->getPermissions() & ~(\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE)); |
|
| 1207 | - } |
|
| 1208 | - |
|
| 1209 | - return $share; |
|
| 1210 | - } |
|
| 1211 | - |
|
| 1212 | - protected function checkExpireDate($share) { |
|
| 1213 | - if ($share->getExpirationDate() !== null && |
|
| 1214 | - $share->getExpirationDate() <= new \DateTime()) { |
|
| 1215 | - $this->deleteShare($share); |
|
| 1216 | - throw new ShareNotFound($this->l->t('The requested share does not exist anymore')); |
|
| 1217 | - } |
|
| 1218 | - |
|
| 1219 | - } |
|
| 1220 | - |
|
| 1221 | - /** |
|
| 1222 | - * Verify the password of a public share |
|
| 1223 | - * |
|
| 1224 | - * @param \OCP\Share\IShare $share |
|
| 1225 | - * @param string $password |
|
| 1226 | - * @return bool |
|
| 1227 | - */ |
|
| 1228 | - public function checkPassword(\OCP\Share\IShare $share, $password) { |
|
| 1229 | - $passwordProtected = $share->getShareType() !== \OCP\Share::SHARE_TYPE_LINK |
|
| 1230 | - || $share->getShareType() !== \OCP\Share::SHARE_TYPE_EMAIL; |
|
| 1231 | - if (!$passwordProtected) { |
|
| 1232 | - //TODO maybe exception? |
|
| 1233 | - return false; |
|
| 1234 | - } |
|
| 1235 | - |
|
| 1236 | - if ($password === null || $share->getPassword() === null) { |
|
| 1237 | - return false; |
|
| 1238 | - } |
|
| 1239 | - |
|
| 1240 | - $newHash = ''; |
|
| 1241 | - if (!$this->hasher->verify($password, $share->getPassword(), $newHash)) { |
|
| 1242 | - return false; |
|
| 1243 | - } |
|
| 1244 | - |
|
| 1245 | - if (!empty($newHash)) { |
|
| 1246 | - $share->setPassword($newHash); |
|
| 1247 | - $provider = $this->factory->getProviderForType($share->getShareType()); |
|
| 1248 | - $provider->update($share); |
|
| 1249 | - } |
|
| 1250 | - |
|
| 1251 | - return true; |
|
| 1252 | - } |
|
| 1253 | - |
|
| 1254 | - /** |
|
| 1255 | - * @inheritdoc |
|
| 1256 | - */ |
|
| 1257 | - public function userDeleted($uid) { |
|
| 1258 | - $types = [\OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_GROUP, \OCP\Share::SHARE_TYPE_LINK, \OCP\Share::SHARE_TYPE_REMOTE, \OCP\Share::SHARE_TYPE_EMAIL]; |
|
| 1259 | - |
|
| 1260 | - foreach ($types as $type) { |
|
| 1261 | - try { |
|
| 1262 | - $provider = $this->factory->getProviderForType($type); |
|
| 1263 | - } catch (ProviderException $e) { |
|
| 1264 | - continue; |
|
| 1265 | - } |
|
| 1266 | - $provider->userDeleted($uid, $type); |
|
| 1267 | - } |
|
| 1268 | - } |
|
| 1269 | - |
|
| 1270 | - /** |
|
| 1271 | - * @inheritdoc |
|
| 1272 | - */ |
|
| 1273 | - public function groupDeleted($gid) { |
|
| 1274 | - $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_GROUP); |
|
| 1275 | - $provider->groupDeleted($gid); |
|
| 1276 | - } |
|
| 1277 | - |
|
| 1278 | - /** |
|
| 1279 | - * @inheritdoc |
|
| 1280 | - */ |
|
| 1281 | - public function userDeletedFromGroup($uid, $gid) { |
|
| 1282 | - $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_GROUP); |
|
| 1283 | - $provider->userDeletedFromGroup($uid, $gid); |
|
| 1284 | - } |
|
| 1285 | - |
|
| 1286 | - /** |
|
| 1287 | - * Get access list to a path. This means |
|
| 1288 | - * all the users that can access a given path. |
|
| 1289 | - * |
|
| 1290 | - * Consider: |
|
| 1291 | - * -root |
|
| 1292 | - * |-folder1 (23) |
|
| 1293 | - * |-folder2 (32) |
|
| 1294 | - * |-fileA (42) |
|
| 1295 | - * |
|
| 1296 | - * fileA is shared with user1 and user1@server1 |
|
| 1297 | - * folder2 is shared with group2 (user4 is a member of group2) |
|
| 1298 | - * folder1 is shared with user2 (renamed to "folder (1)") and user2@server2 |
|
| 1299 | - * |
|
| 1300 | - * Then the access list to '/folder1/folder2/fileA' with $currentAccess is: |
|
| 1301 | - * [ |
|
| 1302 | - * users => [ |
|
| 1303 | - * 'user1' => ['node_id' => 42, 'node_path' => '/fileA'], |
|
| 1304 | - * 'user4' => ['node_id' => 32, 'node_path' => '/folder2'], |
|
| 1305 | - * 'user2' => ['node_id' => 23, 'node_path' => '/folder (1)'], |
|
| 1306 | - * ], |
|
| 1307 | - * remote => [ |
|
| 1308 | - * 'user1@server1' => ['node_id' => 42, 'token' => 'SeCr3t'], |
|
| 1309 | - * 'user2@server2' => ['node_id' => 23, 'token' => 'FooBaR'], |
|
| 1310 | - * ], |
|
| 1311 | - * public => bool |
|
| 1312 | - * mail => bool |
|
| 1313 | - * ] |
|
| 1314 | - * |
|
| 1315 | - * The access list to '/folder1/folder2/fileA' **without** $currentAccess is: |
|
| 1316 | - * [ |
|
| 1317 | - * users => ['user1', 'user2', 'user4'], |
|
| 1318 | - * remote => bool, |
|
| 1319 | - * public => bool |
|
| 1320 | - * mail => bool |
|
| 1321 | - * ] |
|
| 1322 | - * |
|
| 1323 | - * This is required for encryption/activity |
|
| 1324 | - * |
|
| 1325 | - * @param \OCP\Files\Node $path |
|
| 1326 | - * @param bool $recursive Should we check all parent folders as well |
|
| 1327 | - * @param bool $currentAccess Should the user have currently access to the file |
|
| 1328 | - * @return array |
|
| 1329 | - */ |
|
| 1330 | - public function getAccessList(\OCP\Files\Node $path, $recursive = true, $currentAccess = false) { |
|
| 1331 | - $owner = $path->getOwner()->getUID(); |
|
| 1332 | - |
|
| 1333 | - if ($currentAccess) { |
|
| 1334 | - $al = ['users' => [], 'remote' => [], 'public' => false]; |
|
| 1335 | - } else { |
|
| 1336 | - $al = ['users' => [], 'remote' => false, 'public' => false]; |
|
| 1337 | - } |
|
| 1338 | - if (!$this->userManager->userExists($owner)) { |
|
| 1339 | - return $al; |
|
| 1340 | - } |
|
| 1341 | - |
|
| 1342 | - //Get node for the owner |
|
| 1343 | - $userFolder = $this->rootFolder->getUserFolder($owner); |
|
| 1344 | - if ($path->getId() !== $userFolder->getId() && !$userFolder->isSubNode($path)) { |
|
| 1345 | - $path = $userFolder->getById($path->getId())[0]; |
|
| 1346 | - } |
|
| 1347 | - |
|
| 1348 | - $providers = $this->factory->getAllProviders(); |
|
| 1349 | - |
|
| 1350 | - /** @var Node[] $nodes */ |
|
| 1351 | - $nodes = []; |
|
| 1352 | - |
|
| 1353 | - |
|
| 1354 | - if ($currentAccess) { |
|
| 1355 | - $ownerPath = $path->getPath(); |
|
| 1356 | - $ownerPath = explode('/', $ownerPath, 4); |
|
| 1357 | - if (count($ownerPath) < 4) { |
|
| 1358 | - $ownerPath = ''; |
|
| 1359 | - } else { |
|
| 1360 | - $ownerPath = $ownerPath[3]; |
|
| 1361 | - } |
|
| 1362 | - $al['users'][$owner] = [ |
|
| 1363 | - 'node_id' => $path->getId(), |
|
| 1364 | - 'node_path' => '/' . $ownerPath, |
|
| 1365 | - ]; |
|
| 1366 | - } else { |
|
| 1367 | - $al['users'][] = $owner; |
|
| 1368 | - } |
|
| 1369 | - |
|
| 1370 | - // Collect all the shares |
|
| 1371 | - while ($path->getPath() !== $userFolder->getPath()) { |
|
| 1372 | - $nodes[] = $path; |
|
| 1373 | - if (!$recursive) { |
|
| 1374 | - break; |
|
| 1375 | - } |
|
| 1376 | - $path = $path->getParent(); |
|
| 1377 | - } |
|
| 1378 | - |
|
| 1379 | - foreach ($providers as $provider) { |
|
| 1380 | - $tmp = $provider->getAccessList($nodes, $currentAccess); |
|
| 1381 | - |
|
| 1382 | - foreach ($tmp as $k => $v) { |
|
| 1383 | - if (isset($al[$k])) { |
|
| 1384 | - if (is_array($al[$k])) { |
|
| 1385 | - $al[$k] = array_merge($al[$k], $v); |
|
| 1386 | - } else { |
|
| 1387 | - $al[$k] = $al[$k] || $v; |
|
| 1388 | - } |
|
| 1389 | - } else { |
|
| 1390 | - $al[$k] = $v; |
|
| 1391 | - } |
|
| 1392 | - } |
|
| 1393 | - } |
|
| 1394 | - |
|
| 1395 | - return $al; |
|
| 1396 | - } |
|
| 1397 | - |
|
| 1398 | - /** |
|
| 1399 | - * Create a new share |
|
| 1400 | - * @return \OCP\Share\IShare; |
|
| 1401 | - */ |
|
| 1402 | - public function newShare() { |
|
| 1403 | - return new \OC\Share20\Share($this->rootFolder, $this->userManager); |
|
| 1404 | - } |
|
| 1405 | - |
|
| 1406 | - /** |
|
| 1407 | - * Is the share API enabled |
|
| 1408 | - * |
|
| 1409 | - * @return bool |
|
| 1410 | - */ |
|
| 1411 | - public function shareApiEnabled() { |
|
| 1412 | - return $this->config->getAppValue('core', 'shareapi_enabled', 'yes') === 'yes'; |
|
| 1413 | - } |
|
| 1414 | - |
|
| 1415 | - /** |
|
| 1416 | - * Is public link sharing enabled |
|
| 1417 | - * |
|
| 1418 | - * @return bool |
|
| 1419 | - */ |
|
| 1420 | - public function shareApiAllowLinks() { |
|
| 1421 | - return $this->config->getAppValue('core', 'shareapi_allow_links', 'yes') === 'yes'; |
|
| 1422 | - } |
|
| 1423 | - |
|
| 1424 | - /** |
|
| 1425 | - * Is password on public link requires |
|
| 1426 | - * |
|
| 1427 | - * @return bool |
|
| 1428 | - */ |
|
| 1429 | - public function shareApiLinkEnforcePassword() { |
|
| 1430 | - return $this->config->getAppValue('core', 'shareapi_enforce_links_password', 'no') === 'yes'; |
|
| 1431 | - } |
|
| 1432 | - |
|
| 1433 | - /** |
|
| 1434 | - * Is default expire date enabled |
|
| 1435 | - * |
|
| 1436 | - * @return bool |
|
| 1437 | - */ |
|
| 1438 | - public function shareApiLinkDefaultExpireDate() { |
|
| 1439 | - return $this->config->getAppValue('core', 'shareapi_default_expire_date', 'no') === 'yes'; |
|
| 1440 | - } |
|
| 1441 | - |
|
| 1442 | - /** |
|
| 1443 | - * Is default expire date enforced |
|
| 1444 | - *` |
|
| 1445 | - * @return bool |
|
| 1446 | - */ |
|
| 1447 | - public function shareApiLinkDefaultExpireDateEnforced() { |
|
| 1448 | - return $this->shareApiLinkDefaultExpireDate() && |
|
| 1449 | - $this->config->getAppValue('core', 'shareapi_enforce_expire_date', 'no') === 'yes'; |
|
| 1450 | - } |
|
| 1451 | - |
|
| 1452 | - /** |
|
| 1453 | - * Number of default expire days |
|
| 1454 | - *shareApiLinkAllowPublicUpload |
|
| 1455 | - * @return int |
|
| 1456 | - */ |
|
| 1457 | - public function shareApiLinkDefaultExpireDays() { |
|
| 1458 | - return (int)$this->config->getAppValue('core', 'shareapi_expire_after_n_days', '7'); |
|
| 1459 | - } |
|
| 1460 | - |
|
| 1461 | - /** |
|
| 1462 | - * Allow public upload on link shares |
|
| 1463 | - * |
|
| 1464 | - * @return bool |
|
| 1465 | - */ |
|
| 1466 | - public function shareApiLinkAllowPublicUpload() { |
|
| 1467 | - return $this->config->getAppValue('core', 'shareapi_allow_public_upload', 'yes') === 'yes'; |
|
| 1468 | - } |
|
| 1469 | - |
|
| 1470 | - /** |
|
| 1471 | - * check if user can only share with group members |
|
| 1472 | - * @return bool |
|
| 1473 | - */ |
|
| 1474 | - public function shareWithGroupMembersOnly() { |
|
| 1475 | - return $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes'; |
|
| 1476 | - } |
|
| 1477 | - |
|
| 1478 | - /** |
|
| 1479 | - * Check if users can share with groups |
|
| 1480 | - * @return bool |
|
| 1481 | - */ |
|
| 1482 | - public function allowGroupSharing() { |
|
| 1483 | - return $this->config->getAppValue('core', 'shareapi_allow_group_sharing', 'yes') === 'yes'; |
|
| 1484 | - } |
|
| 1485 | - |
|
| 1486 | - /** |
|
| 1487 | - * Copied from \OC_Util::isSharingDisabledForUser |
|
| 1488 | - * |
|
| 1489 | - * TODO: Deprecate fuction from OC_Util |
|
| 1490 | - * |
|
| 1491 | - * @param string $userId |
|
| 1492 | - * @return bool |
|
| 1493 | - */ |
|
| 1494 | - public function sharingDisabledForUser($userId) { |
|
| 1495 | - if ($userId === null) { |
|
| 1496 | - return false; |
|
| 1497 | - } |
|
| 1498 | - |
|
| 1499 | - if (isset($this->sharingDisabledForUsersCache[$userId])) { |
|
| 1500 | - return $this->sharingDisabledForUsersCache[$userId]; |
|
| 1501 | - } |
|
| 1502 | - |
|
| 1503 | - if ($this->config->getAppValue('core', 'shareapi_exclude_groups', 'no') === 'yes') { |
|
| 1504 | - $groupsList = $this->config->getAppValue('core', 'shareapi_exclude_groups_list', ''); |
|
| 1505 | - $excludedGroups = json_decode($groupsList); |
|
| 1506 | - if (is_null($excludedGroups)) { |
|
| 1507 | - $excludedGroups = explode(',', $groupsList); |
|
| 1508 | - $newValue = json_encode($excludedGroups); |
|
| 1509 | - $this->config->setAppValue('core', 'shareapi_exclude_groups_list', $newValue); |
|
| 1510 | - } |
|
| 1511 | - $user = $this->userManager->get($userId); |
|
| 1512 | - $usersGroups = $this->groupManager->getUserGroupIds($user); |
|
| 1513 | - if (!empty($usersGroups)) { |
|
| 1514 | - $remainingGroups = array_diff($usersGroups, $excludedGroups); |
|
| 1515 | - // if the user is only in groups which are disabled for sharing then |
|
| 1516 | - // sharing is also disabled for the user |
|
| 1517 | - if (empty($remainingGroups)) { |
|
| 1518 | - $this->sharingDisabledForUsersCache[$userId] = true; |
|
| 1519 | - return true; |
|
| 1520 | - } |
|
| 1521 | - } |
|
| 1522 | - } |
|
| 1523 | - |
|
| 1524 | - $this->sharingDisabledForUsersCache[$userId] = false; |
|
| 1525 | - return false; |
|
| 1526 | - } |
|
| 1527 | - |
|
| 1528 | - /** |
|
| 1529 | - * @inheritdoc |
|
| 1530 | - */ |
|
| 1531 | - public function outgoingServer2ServerSharesAllowed() { |
|
| 1532 | - return $this->config->getAppValue('files_sharing', 'outgoing_server2server_share_enabled', 'yes') === 'yes'; |
|
| 1533 | - } |
|
| 1534 | - |
|
| 1535 | - /** |
|
| 1536 | - * @inheritdoc |
|
| 1537 | - */ |
|
| 1538 | - public function shareProviderExists($shareType) { |
|
| 1539 | - try { |
|
| 1540 | - $this->factory->getProviderForType($shareType); |
|
| 1541 | - } catch (ProviderException $e) { |
|
| 1542 | - return false; |
|
| 1543 | - } |
|
| 1544 | - |
|
| 1545 | - return true; |
|
| 1546 | - } |
|
| 1204 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK && |
|
| 1205 | + !$this->shareApiLinkAllowPublicUpload()) { |
|
| 1206 | + $share->setPermissions($share->getPermissions() & ~(\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE)); |
|
| 1207 | + } |
|
| 1208 | + |
|
| 1209 | + return $share; |
|
| 1210 | + } |
|
| 1211 | + |
|
| 1212 | + protected function checkExpireDate($share) { |
|
| 1213 | + if ($share->getExpirationDate() !== null && |
|
| 1214 | + $share->getExpirationDate() <= new \DateTime()) { |
|
| 1215 | + $this->deleteShare($share); |
|
| 1216 | + throw new ShareNotFound($this->l->t('The requested share does not exist anymore')); |
|
| 1217 | + } |
|
| 1218 | + |
|
| 1219 | + } |
|
| 1220 | + |
|
| 1221 | + /** |
|
| 1222 | + * Verify the password of a public share |
|
| 1223 | + * |
|
| 1224 | + * @param \OCP\Share\IShare $share |
|
| 1225 | + * @param string $password |
|
| 1226 | + * @return bool |
|
| 1227 | + */ |
|
| 1228 | + public function checkPassword(\OCP\Share\IShare $share, $password) { |
|
| 1229 | + $passwordProtected = $share->getShareType() !== \OCP\Share::SHARE_TYPE_LINK |
|
| 1230 | + || $share->getShareType() !== \OCP\Share::SHARE_TYPE_EMAIL; |
|
| 1231 | + if (!$passwordProtected) { |
|
| 1232 | + //TODO maybe exception? |
|
| 1233 | + return false; |
|
| 1234 | + } |
|
| 1235 | + |
|
| 1236 | + if ($password === null || $share->getPassword() === null) { |
|
| 1237 | + return false; |
|
| 1238 | + } |
|
| 1239 | + |
|
| 1240 | + $newHash = ''; |
|
| 1241 | + if (!$this->hasher->verify($password, $share->getPassword(), $newHash)) { |
|
| 1242 | + return false; |
|
| 1243 | + } |
|
| 1244 | + |
|
| 1245 | + if (!empty($newHash)) { |
|
| 1246 | + $share->setPassword($newHash); |
|
| 1247 | + $provider = $this->factory->getProviderForType($share->getShareType()); |
|
| 1248 | + $provider->update($share); |
|
| 1249 | + } |
|
| 1250 | + |
|
| 1251 | + return true; |
|
| 1252 | + } |
|
| 1253 | + |
|
| 1254 | + /** |
|
| 1255 | + * @inheritdoc |
|
| 1256 | + */ |
|
| 1257 | + public function userDeleted($uid) { |
|
| 1258 | + $types = [\OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_GROUP, \OCP\Share::SHARE_TYPE_LINK, \OCP\Share::SHARE_TYPE_REMOTE, \OCP\Share::SHARE_TYPE_EMAIL]; |
|
| 1259 | + |
|
| 1260 | + foreach ($types as $type) { |
|
| 1261 | + try { |
|
| 1262 | + $provider = $this->factory->getProviderForType($type); |
|
| 1263 | + } catch (ProviderException $e) { |
|
| 1264 | + continue; |
|
| 1265 | + } |
|
| 1266 | + $provider->userDeleted($uid, $type); |
|
| 1267 | + } |
|
| 1268 | + } |
|
| 1269 | + |
|
| 1270 | + /** |
|
| 1271 | + * @inheritdoc |
|
| 1272 | + */ |
|
| 1273 | + public function groupDeleted($gid) { |
|
| 1274 | + $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_GROUP); |
|
| 1275 | + $provider->groupDeleted($gid); |
|
| 1276 | + } |
|
| 1277 | + |
|
| 1278 | + /** |
|
| 1279 | + * @inheritdoc |
|
| 1280 | + */ |
|
| 1281 | + public function userDeletedFromGroup($uid, $gid) { |
|
| 1282 | + $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_GROUP); |
|
| 1283 | + $provider->userDeletedFromGroup($uid, $gid); |
|
| 1284 | + } |
|
| 1285 | + |
|
| 1286 | + /** |
|
| 1287 | + * Get access list to a path. This means |
|
| 1288 | + * all the users that can access a given path. |
|
| 1289 | + * |
|
| 1290 | + * Consider: |
|
| 1291 | + * -root |
|
| 1292 | + * |-folder1 (23) |
|
| 1293 | + * |-folder2 (32) |
|
| 1294 | + * |-fileA (42) |
|
| 1295 | + * |
|
| 1296 | + * fileA is shared with user1 and user1@server1 |
|
| 1297 | + * folder2 is shared with group2 (user4 is a member of group2) |
|
| 1298 | + * folder1 is shared with user2 (renamed to "folder (1)") and user2@server2 |
|
| 1299 | + * |
|
| 1300 | + * Then the access list to '/folder1/folder2/fileA' with $currentAccess is: |
|
| 1301 | + * [ |
|
| 1302 | + * users => [ |
|
| 1303 | + * 'user1' => ['node_id' => 42, 'node_path' => '/fileA'], |
|
| 1304 | + * 'user4' => ['node_id' => 32, 'node_path' => '/folder2'], |
|
| 1305 | + * 'user2' => ['node_id' => 23, 'node_path' => '/folder (1)'], |
|
| 1306 | + * ], |
|
| 1307 | + * remote => [ |
|
| 1308 | + * 'user1@server1' => ['node_id' => 42, 'token' => 'SeCr3t'], |
|
| 1309 | + * 'user2@server2' => ['node_id' => 23, 'token' => 'FooBaR'], |
|
| 1310 | + * ], |
|
| 1311 | + * public => bool |
|
| 1312 | + * mail => bool |
|
| 1313 | + * ] |
|
| 1314 | + * |
|
| 1315 | + * The access list to '/folder1/folder2/fileA' **without** $currentAccess is: |
|
| 1316 | + * [ |
|
| 1317 | + * users => ['user1', 'user2', 'user4'], |
|
| 1318 | + * remote => bool, |
|
| 1319 | + * public => bool |
|
| 1320 | + * mail => bool |
|
| 1321 | + * ] |
|
| 1322 | + * |
|
| 1323 | + * This is required for encryption/activity |
|
| 1324 | + * |
|
| 1325 | + * @param \OCP\Files\Node $path |
|
| 1326 | + * @param bool $recursive Should we check all parent folders as well |
|
| 1327 | + * @param bool $currentAccess Should the user have currently access to the file |
|
| 1328 | + * @return array |
|
| 1329 | + */ |
|
| 1330 | + public function getAccessList(\OCP\Files\Node $path, $recursive = true, $currentAccess = false) { |
|
| 1331 | + $owner = $path->getOwner()->getUID(); |
|
| 1332 | + |
|
| 1333 | + if ($currentAccess) { |
|
| 1334 | + $al = ['users' => [], 'remote' => [], 'public' => false]; |
|
| 1335 | + } else { |
|
| 1336 | + $al = ['users' => [], 'remote' => false, 'public' => false]; |
|
| 1337 | + } |
|
| 1338 | + if (!$this->userManager->userExists($owner)) { |
|
| 1339 | + return $al; |
|
| 1340 | + } |
|
| 1341 | + |
|
| 1342 | + //Get node for the owner |
|
| 1343 | + $userFolder = $this->rootFolder->getUserFolder($owner); |
|
| 1344 | + if ($path->getId() !== $userFolder->getId() && !$userFolder->isSubNode($path)) { |
|
| 1345 | + $path = $userFolder->getById($path->getId())[0]; |
|
| 1346 | + } |
|
| 1347 | + |
|
| 1348 | + $providers = $this->factory->getAllProviders(); |
|
| 1349 | + |
|
| 1350 | + /** @var Node[] $nodes */ |
|
| 1351 | + $nodes = []; |
|
| 1352 | + |
|
| 1353 | + |
|
| 1354 | + if ($currentAccess) { |
|
| 1355 | + $ownerPath = $path->getPath(); |
|
| 1356 | + $ownerPath = explode('/', $ownerPath, 4); |
|
| 1357 | + if (count($ownerPath) < 4) { |
|
| 1358 | + $ownerPath = ''; |
|
| 1359 | + } else { |
|
| 1360 | + $ownerPath = $ownerPath[3]; |
|
| 1361 | + } |
|
| 1362 | + $al['users'][$owner] = [ |
|
| 1363 | + 'node_id' => $path->getId(), |
|
| 1364 | + 'node_path' => '/' . $ownerPath, |
|
| 1365 | + ]; |
|
| 1366 | + } else { |
|
| 1367 | + $al['users'][] = $owner; |
|
| 1368 | + } |
|
| 1369 | + |
|
| 1370 | + // Collect all the shares |
|
| 1371 | + while ($path->getPath() !== $userFolder->getPath()) { |
|
| 1372 | + $nodes[] = $path; |
|
| 1373 | + if (!$recursive) { |
|
| 1374 | + break; |
|
| 1375 | + } |
|
| 1376 | + $path = $path->getParent(); |
|
| 1377 | + } |
|
| 1378 | + |
|
| 1379 | + foreach ($providers as $provider) { |
|
| 1380 | + $tmp = $provider->getAccessList($nodes, $currentAccess); |
|
| 1381 | + |
|
| 1382 | + foreach ($tmp as $k => $v) { |
|
| 1383 | + if (isset($al[$k])) { |
|
| 1384 | + if (is_array($al[$k])) { |
|
| 1385 | + $al[$k] = array_merge($al[$k], $v); |
|
| 1386 | + } else { |
|
| 1387 | + $al[$k] = $al[$k] || $v; |
|
| 1388 | + } |
|
| 1389 | + } else { |
|
| 1390 | + $al[$k] = $v; |
|
| 1391 | + } |
|
| 1392 | + } |
|
| 1393 | + } |
|
| 1394 | + |
|
| 1395 | + return $al; |
|
| 1396 | + } |
|
| 1397 | + |
|
| 1398 | + /** |
|
| 1399 | + * Create a new share |
|
| 1400 | + * @return \OCP\Share\IShare; |
|
| 1401 | + */ |
|
| 1402 | + public function newShare() { |
|
| 1403 | + return new \OC\Share20\Share($this->rootFolder, $this->userManager); |
|
| 1404 | + } |
|
| 1405 | + |
|
| 1406 | + /** |
|
| 1407 | + * Is the share API enabled |
|
| 1408 | + * |
|
| 1409 | + * @return bool |
|
| 1410 | + */ |
|
| 1411 | + public function shareApiEnabled() { |
|
| 1412 | + return $this->config->getAppValue('core', 'shareapi_enabled', 'yes') === 'yes'; |
|
| 1413 | + } |
|
| 1414 | + |
|
| 1415 | + /** |
|
| 1416 | + * Is public link sharing enabled |
|
| 1417 | + * |
|
| 1418 | + * @return bool |
|
| 1419 | + */ |
|
| 1420 | + public function shareApiAllowLinks() { |
|
| 1421 | + return $this->config->getAppValue('core', 'shareapi_allow_links', 'yes') === 'yes'; |
|
| 1422 | + } |
|
| 1423 | + |
|
| 1424 | + /** |
|
| 1425 | + * Is password on public link requires |
|
| 1426 | + * |
|
| 1427 | + * @return bool |
|
| 1428 | + */ |
|
| 1429 | + public function shareApiLinkEnforcePassword() { |
|
| 1430 | + return $this->config->getAppValue('core', 'shareapi_enforce_links_password', 'no') === 'yes'; |
|
| 1431 | + } |
|
| 1432 | + |
|
| 1433 | + /** |
|
| 1434 | + * Is default expire date enabled |
|
| 1435 | + * |
|
| 1436 | + * @return bool |
|
| 1437 | + */ |
|
| 1438 | + public function shareApiLinkDefaultExpireDate() { |
|
| 1439 | + return $this->config->getAppValue('core', 'shareapi_default_expire_date', 'no') === 'yes'; |
|
| 1440 | + } |
|
| 1441 | + |
|
| 1442 | + /** |
|
| 1443 | + * Is default expire date enforced |
|
| 1444 | + *` |
|
| 1445 | + * @return bool |
|
| 1446 | + */ |
|
| 1447 | + public function shareApiLinkDefaultExpireDateEnforced() { |
|
| 1448 | + return $this->shareApiLinkDefaultExpireDate() && |
|
| 1449 | + $this->config->getAppValue('core', 'shareapi_enforce_expire_date', 'no') === 'yes'; |
|
| 1450 | + } |
|
| 1451 | + |
|
| 1452 | + /** |
|
| 1453 | + * Number of default expire days |
|
| 1454 | + *shareApiLinkAllowPublicUpload |
|
| 1455 | + * @return int |
|
| 1456 | + */ |
|
| 1457 | + public function shareApiLinkDefaultExpireDays() { |
|
| 1458 | + return (int)$this->config->getAppValue('core', 'shareapi_expire_after_n_days', '7'); |
|
| 1459 | + } |
|
| 1460 | + |
|
| 1461 | + /** |
|
| 1462 | + * Allow public upload on link shares |
|
| 1463 | + * |
|
| 1464 | + * @return bool |
|
| 1465 | + */ |
|
| 1466 | + public function shareApiLinkAllowPublicUpload() { |
|
| 1467 | + return $this->config->getAppValue('core', 'shareapi_allow_public_upload', 'yes') === 'yes'; |
|
| 1468 | + } |
|
| 1469 | + |
|
| 1470 | + /** |
|
| 1471 | + * check if user can only share with group members |
|
| 1472 | + * @return bool |
|
| 1473 | + */ |
|
| 1474 | + public function shareWithGroupMembersOnly() { |
|
| 1475 | + return $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes'; |
|
| 1476 | + } |
|
| 1477 | + |
|
| 1478 | + /** |
|
| 1479 | + * Check if users can share with groups |
|
| 1480 | + * @return bool |
|
| 1481 | + */ |
|
| 1482 | + public function allowGroupSharing() { |
|
| 1483 | + return $this->config->getAppValue('core', 'shareapi_allow_group_sharing', 'yes') === 'yes'; |
|
| 1484 | + } |
|
| 1485 | + |
|
| 1486 | + /** |
|
| 1487 | + * Copied from \OC_Util::isSharingDisabledForUser |
|
| 1488 | + * |
|
| 1489 | + * TODO: Deprecate fuction from OC_Util |
|
| 1490 | + * |
|
| 1491 | + * @param string $userId |
|
| 1492 | + * @return bool |
|
| 1493 | + */ |
|
| 1494 | + public function sharingDisabledForUser($userId) { |
|
| 1495 | + if ($userId === null) { |
|
| 1496 | + return false; |
|
| 1497 | + } |
|
| 1498 | + |
|
| 1499 | + if (isset($this->sharingDisabledForUsersCache[$userId])) { |
|
| 1500 | + return $this->sharingDisabledForUsersCache[$userId]; |
|
| 1501 | + } |
|
| 1502 | + |
|
| 1503 | + if ($this->config->getAppValue('core', 'shareapi_exclude_groups', 'no') === 'yes') { |
|
| 1504 | + $groupsList = $this->config->getAppValue('core', 'shareapi_exclude_groups_list', ''); |
|
| 1505 | + $excludedGroups = json_decode($groupsList); |
|
| 1506 | + if (is_null($excludedGroups)) { |
|
| 1507 | + $excludedGroups = explode(',', $groupsList); |
|
| 1508 | + $newValue = json_encode($excludedGroups); |
|
| 1509 | + $this->config->setAppValue('core', 'shareapi_exclude_groups_list', $newValue); |
|
| 1510 | + } |
|
| 1511 | + $user = $this->userManager->get($userId); |
|
| 1512 | + $usersGroups = $this->groupManager->getUserGroupIds($user); |
|
| 1513 | + if (!empty($usersGroups)) { |
|
| 1514 | + $remainingGroups = array_diff($usersGroups, $excludedGroups); |
|
| 1515 | + // if the user is only in groups which are disabled for sharing then |
|
| 1516 | + // sharing is also disabled for the user |
|
| 1517 | + if (empty($remainingGroups)) { |
|
| 1518 | + $this->sharingDisabledForUsersCache[$userId] = true; |
|
| 1519 | + return true; |
|
| 1520 | + } |
|
| 1521 | + } |
|
| 1522 | + } |
|
| 1523 | + |
|
| 1524 | + $this->sharingDisabledForUsersCache[$userId] = false; |
|
| 1525 | + return false; |
|
| 1526 | + } |
|
| 1527 | + |
|
| 1528 | + /** |
|
| 1529 | + * @inheritdoc |
|
| 1530 | + */ |
|
| 1531 | + public function outgoingServer2ServerSharesAllowed() { |
|
| 1532 | + return $this->config->getAppValue('files_sharing', 'outgoing_server2server_share_enabled', 'yes') === 'yes'; |
|
| 1533 | + } |
|
| 1534 | + |
|
| 1535 | + /** |
|
| 1536 | + * @inheritdoc |
|
| 1537 | + */ |
|
| 1538 | + public function shareProviderExists($shareType) { |
|
| 1539 | + try { |
|
| 1540 | + $this->factory->getProviderForType($shareType); |
|
| 1541 | + } catch (ProviderException $e) { |
|
| 1542 | + return false; |
|
| 1543 | + } |
|
| 1544 | + |
|
| 1545 | + return true; |
|
| 1546 | + } |
|
| 1547 | 1547 | |
| 1548 | 1548 | } |
@@ -345,7 +345,7 @@ discard block |
||
| 345 | 345 | |
| 346 | 346 | if ($fullId === null && $expirationDate === null && $this->shareApiLinkDefaultExpireDate()) { |
| 347 | 347 | $expirationDate = new \DateTime(); |
| 348 | - $expirationDate->setTime(0,0,0); |
|
| 348 | + $expirationDate->setTime(0, 0, 0); |
|
| 349 | 349 | $expirationDate->add(new \DateInterval('P'.$this->shareApiLinkDefaultExpireDays().'D')); |
| 350 | 350 | } |
| 351 | 351 | |
@@ -357,7 +357,7 @@ discard block |
||
| 357 | 357 | |
| 358 | 358 | $date = new \DateTime(); |
| 359 | 359 | $date->setTime(0, 0, 0); |
| 360 | - $date->add(new \DateInterval('P' . $this->shareApiLinkDefaultExpireDays() . 'D')); |
|
| 360 | + $date->add(new \DateInterval('P'.$this->shareApiLinkDefaultExpireDays().'D')); |
|
| 361 | 361 | if ($date < $expirationDate) { |
| 362 | 362 | $message = $this->l->t('Can’t set expiration date more than %s days in the future', [$this->shareApiLinkDefaultExpireDays()]); |
| 363 | 363 | throw new GenericShareException($message, $message, 404); |
@@ -410,7 +410,7 @@ discard block |
||
| 410 | 410 | */ |
| 411 | 411 | $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_USER); |
| 412 | 412 | $existingShares = $provider->getSharesByPath($share->getNode()); |
| 413 | - foreach($existingShares as $existingShare) { |
|
| 413 | + foreach ($existingShares as $existingShare) { |
|
| 414 | 414 | // Ignore if it is the same share |
| 415 | 415 | try { |
| 416 | 416 | if ($existingShare->getFullId() === $share->getFullId()) { |
@@ -467,7 +467,7 @@ discard block |
||
| 467 | 467 | */ |
| 468 | 468 | $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_GROUP); |
| 469 | 469 | $existingShares = $provider->getSharesByPath($share->getNode()); |
| 470 | - foreach($existingShares as $existingShare) { |
|
| 470 | + foreach ($existingShares as $existingShare) { |
|
| 471 | 471 | try { |
| 472 | 472 | if ($existingShare->getFullId() === $share->getFullId()) { |
| 473 | 473 | continue; |
@@ -536,7 +536,7 @@ discard block |
||
| 536 | 536 | // Make sure that we do not share a path that contains a shared mountpoint |
| 537 | 537 | if ($path instanceof \OCP\Files\Folder) { |
| 538 | 538 | $mounts = $this->mountManager->findIn($path->getPath()); |
| 539 | - foreach($mounts as $mount) { |
|
| 539 | + foreach ($mounts as $mount) { |
|
| 540 | 540 | if ($mount->getStorage()->instanceOfStorage('\OCA\Files_Sharing\ISharedStorage')) { |
| 541 | 541 | throw new \InvalidArgumentException('Path contains files shared with you'); |
| 542 | 542 | } |
@@ -584,7 +584,7 @@ discard block |
||
| 584 | 584 | $storage = $share->getNode()->getStorage(); |
| 585 | 585 | if ($storage->instanceOfStorage('OCA\Files_Sharing\External\Storage')) { |
| 586 | 586 | $parent = $share->getNode()->getParent(); |
| 587 | - while($parent->getStorage()->instanceOfStorage('OCA\Files_Sharing\External\Storage')) { |
|
| 587 | + while ($parent->getStorage()->instanceOfStorage('OCA\Files_Sharing\External\Storage')) { |
|
| 588 | 588 | $parent = $parent->getParent(); |
| 589 | 589 | } |
| 590 | 590 | $share->setShareOwner($parent->getOwner()->getUID()); |
@@ -637,7 +637,7 @@ discard block |
||
| 637 | 637 | } |
| 638 | 638 | |
| 639 | 639 | // Generate the target |
| 640 | - $target = $this->config->getSystemValue('share_folder', '/') .'/'. $share->getNode()->getName(); |
|
| 640 | + $target = $this->config->getSystemValue('share_folder', '/').'/'.$share->getNode()->getName(); |
|
| 641 | 641 | $target = \OC\Files\Filesystem::normalizePath($target); |
| 642 | 642 | $share->setTarget($target); |
| 643 | 643 | |
@@ -660,7 +660,7 @@ discard block |
||
| 660 | 660 | |
| 661 | 661 | if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { |
| 662 | 662 | $mailSend = $share->getMailSend(); |
| 663 | - if($mailSend === true) { |
|
| 663 | + if ($mailSend === true) { |
|
| 664 | 664 | $user = $this->userManager->get($share->getSharedWith()); |
| 665 | 665 | if ($user !== null) { |
| 666 | 666 | $emailAddress = $user->getEMailAddress(); |
@@ -675,12 +675,12 @@ discard block |
||
| 675 | 675 | $emailAddress, |
| 676 | 676 | $share->getExpirationDate() |
| 677 | 677 | ); |
| 678 | - $this->logger->debug('Send share notification to ' . $emailAddress . ' for share with ID ' . $share->getId(), ['app' => 'share']); |
|
| 678 | + $this->logger->debug('Send share notification to '.$emailAddress.' for share with ID '.$share->getId(), ['app' => 'share']); |
|
| 679 | 679 | } else { |
| 680 | - $this->logger->debug('Share notification not send to ' . $share->getSharedWith() . ' because email address is not set.', ['app' => 'share']); |
|
| 680 | + $this->logger->debug('Share notification not send to '.$share->getSharedWith().' because email address is not set.', ['app' => 'share']); |
|
| 681 | 681 | } |
| 682 | 682 | } else { |
| 683 | - $this->logger->debug('Share notification not send to ' . $share->getSharedWith() . ' because user could not be found.', ['app' => 'share']); |
|
| 683 | + $this->logger->debug('Share notification not send to '.$share->getSharedWith().' because user could not be found.', ['app' => 'share']); |
|
| 684 | 684 | } |
| 685 | 685 | } else { |
| 686 | 686 | $this->logger->debug('Share notification not send because mailsend is false.', ['app' => 'share']); |
@@ -724,7 +724,7 @@ discard block |
||
| 724 | 724 | $text = $l->t('%s shared »%s« with you.', [$initiatorDisplayName, $filename]); |
| 725 | 725 | |
| 726 | 726 | $emailTemplate->addBodyText( |
| 727 | - $text . ' ' . $l->t('Click the button below to open it.'), |
|
| 727 | + $text.' '.$l->t('Click the button below to open it.'), |
|
| 728 | 728 | $text |
| 729 | 729 | ); |
| 730 | 730 | $emailTemplate->addBodyButton( |
@@ -748,9 +748,9 @@ discard block |
||
| 748 | 748 | // The "Reply-To" is set to the sharer if an mail address is configured |
| 749 | 749 | // also the default footer contains a "Do not reply" which needs to be adjusted. |
| 750 | 750 | $initiatorEmail = $initiatorUser->getEMailAddress(); |
| 751 | - if($initiatorEmail !== null) { |
|
| 751 | + if ($initiatorEmail !== null) { |
|
| 752 | 752 | $message->setReplyTo([$initiatorEmail => $initiatorDisplayName]); |
| 753 | - $emailTemplate->addFooter($instanceName . ' - ' . $this->defaults->getSlogan()); |
|
| 753 | + $emailTemplate->addFooter($instanceName.' - '.$this->defaults->getSlogan()); |
|
| 754 | 754 | } else { |
| 755 | 755 | $emailTemplate->addFooter(); |
| 756 | 756 | } |
@@ -961,7 +961,7 @@ discard block |
||
| 961 | 961 | * @param string $recipientId |
| 962 | 962 | */ |
| 963 | 963 | public function deleteFromSelf(\OCP\Share\IShare $share, $recipientId) { |
| 964 | - list($providerId, ) = $this->splitFullId($share->getFullId()); |
|
| 964 | + list($providerId,) = $this->splitFullId($share->getFullId()); |
|
| 965 | 965 | $provider = $this->factory->getProvider($providerId); |
| 966 | 966 | |
| 967 | 967 | $provider->deleteFromSelf($share, $recipientId); |
@@ -984,7 +984,7 @@ discard block |
||
| 984 | 984 | if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
| 985 | 985 | $sharedWith = $this->groupManager->get($share->getSharedWith()); |
| 986 | 986 | if (is_null($sharedWith)) { |
| 987 | - throw new \InvalidArgumentException('Group "' . $share->getSharedWith() . '" does not exist'); |
|
| 987 | + throw new \InvalidArgumentException('Group "'.$share->getSharedWith().'" does not exist'); |
|
| 988 | 988 | } |
| 989 | 989 | $recipient = $this->userManager->get($recipientId); |
| 990 | 990 | if (!$sharedWith->inGroup($recipient)) { |
@@ -992,7 +992,7 @@ discard block |
||
| 992 | 992 | } |
| 993 | 993 | } |
| 994 | 994 | |
| 995 | - list($providerId, ) = $this->splitFullId($share->getFullId()); |
|
| 995 | + list($providerId,) = $this->splitFullId($share->getFullId()); |
|
| 996 | 996 | $provider = $this->factory->getProvider($providerId); |
| 997 | 997 | |
| 998 | 998 | $provider->move($share, $recipientId); |
@@ -1039,7 +1039,7 @@ discard block |
||
| 1039 | 1039 | |
| 1040 | 1040 | $shares2 = []; |
| 1041 | 1041 | |
| 1042 | - while(true) { |
|
| 1042 | + while (true) { |
|
| 1043 | 1043 | $added = 0; |
| 1044 | 1044 | foreach ($shares as $share) { |
| 1045 | 1045 | |
@@ -1139,7 +1139,7 @@ discard block |
||
| 1139 | 1139 | * |
| 1140 | 1140 | * @return Share[] |
| 1141 | 1141 | */ |
| 1142 | - public function getSharesByPath(\OCP\Files\Node $path, $page=0, $perPage=50) { |
|
| 1142 | + public function getSharesByPath(\OCP\Files\Node $path, $page = 0, $perPage = 50) { |
|
| 1143 | 1143 | return []; |
| 1144 | 1144 | } |
| 1145 | 1145 | |
@@ -1154,7 +1154,7 @@ discard block |
||
| 1154 | 1154 | public function getShareByToken($token) { |
| 1155 | 1155 | $share = null; |
| 1156 | 1156 | try { |
| 1157 | - if($this->shareApiAllowLinks()) { |
|
| 1157 | + if ($this->shareApiAllowLinks()) { |
|
| 1158 | 1158 | $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_LINK); |
| 1159 | 1159 | $share = $provider->getShareByToken($token); |
| 1160 | 1160 | } |
@@ -1361,7 +1361,7 @@ discard block |
||
| 1361 | 1361 | } |
| 1362 | 1362 | $al['users'][$owner] = [ |
| 1363 | 1363 | 'node_id' => $path->getId(), |
| 1364 | - 'node_path' => '/' . $ownerPath, |
|
| 1364 | + 'node_path' => '/'.$ownerPath, |
|
| 1365 | 1365 | ]; |
| 1366 | 1366 | } else { |
| 1367 | 1367 | $al['users'][] = $owner; |
@@ -1455,7 +1455,7 @@ discard block |
||
| 1455 | 1455 | * @return int |
| 1456 | 1456 | */ |
| 1457 | 1457 | public function shareApiLinkDefaultExpireDays() { |
| 1458 | - return (int)$this->config->getAppValue('core', 'shareapi_expire_after_n_days', '7'); |
|
| 1458 | + return (int) $this->config->getAppValue('core', 'shareapi_expire_after_n_days', '7'); |
|
| 1459 | 1459 | } |
| 1460 | 1460 | |
| 1461 | 1461 | /** |
@@ -46,1143 +46,1143 @@ |
||
| 46 | 46 | */ |
| 47 | 47 | class DefaultShareProvider implements IShareProvider { |
| 48 | 48 | |
| 49 | - // Special share type for user modified group shares |
|
| 50 | - const SHARE_TYPE_USERGROUP = 2; |
|
| 51 | - |
|
| 52 | - /** @var IDBConnection */ |
|
| 53 | - private $dbConn; |
|
| 54 | - |
|
| 55 | - /** @var IUserManager */ |
|
| 56 | - private $userManager; |
|
| 57 | - |
|
| 58 | - /** @var IGroupManager */ |
|
| 59 | - private $groupManager; |
|
| 60 | - |
|
| 61 | - /** @var IRootFolder */ |
|
| 62 | - private $rootFolder; |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * DefaultShareProvider constructor. |
|
| 66 | - * |
|
| 67 | - * @param IDBConnection $connection |
|
| 68 | - * @param IUserManager $userManager |
|
| 69 | - * @param IGroupManager $groupManager |
|
| 70 | - * @param IRootFolder $rootFolder |
|
| 71 | - */ |
|
| 72 | - public function __construct( |
|
| 73 | - IDBConnection $connection, |
|
| 74 | - IUserManager $userManager, |
|
| 75 | - IGroupManager $groupManager, |
|
| 76 | - IRootFolder $rootFolder) { |
|
| 77 | - $this->dbConn = $connection; |
|
| 78 | - $this->userManager = $userManager; |
|
| 79 | - $this->groupManager = $groupManager; |
|
| 80 | - $this->rootFolder = $rootFolder; |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * Return the identifier of this provider. |
|
| 85 | - * |
|
| 86 | - * @return string Containing only [a-zA-Z0-9] |
|
| 87 | - */ |
|
| 88 | - public function identifier() { |
|
| 89 | - return 'ocinternal'; |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * Share a path |
|
| 94 | - * |
|
| 95 | - * @param \OCP\Share\IShare $share |
|
| 96 | - * @return \OCP\Share\IShare The share object |
|
| 97 | - * @throws ShareNotFound |
|
| 98 | - * @throws \Exception |
|
| 99 | - */ |
|
| 100 | - public function create(\OCP\Share\IShare $share) { |
|
| 101 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 102 | - |
|
| 103 | - $qb->insert('share'); |
|
| 104 | - $qb->setValue('share_type', $qb->createNamedParameter($share->getShareType())); |
|
| 105 | - |
|
| 106 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { |
|
| 107 | - //Set the UID of the user we share with |
|
| 108 | - $qb->setValue('share_with', $qb->createNamedParameter($share->getSharedWith())); |
|
| 109 | - } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 110 | - //Set the GID of the group we share with |
|
| 111 | - $qb->setValue('share_with', $qb->createNamedParameter($share->getSharedWith())); |
|
| 112 | - } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { |
|
| 113 | - //Set the token of the share |
|
| 114 | - $qb->setValue('token', $qb->createNamedParameter($share->getToken())); |
|
| 115 | - |
|
| 116 | - //If a password is set store it |
|
| 117 | - if ($share->getPassword() !== null) { |
|
| 118 | - $qb->setValue('password', $qb->createNamedParameter($share->getPassword())); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - //If an expiration date is set store it |
|
| 122 | - if ($share->getExpirationDate() !== null) { |
|
| 123 | - $qb->setValue('expiration', $qb->createNamedParameter($share->getExpirationDate(), 'datetime')); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - if (method_exists($share, 'getParent')) { |
|
| 127 | - $qb->setValue('parent', $qb->createNamedParameter($share->getParent())); |
|
| 128 | - } |
|
| 129 | - } else { |
|
| 130 | - throw new \Exception('invalid share type!'); |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - // Set what is shares |
|
| 134 | - $qb->setValue('item_type', $qb->createParameter('itemType')); |
|
| 135 | - if ($share->getNode() instanceof \OCP\Files\File) { |
|
| 136 | - $qb->setParameter('itemType', 'file'); |
|
| 137 | - } else { |
|
| 138 | - $qb->setParameter('itemType', 'folder'); |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - // Set the file id |
|
| 142 | - $qb->setValue('item_source', $qb->createNamedParameter($share->getNode()->getId())); |
|
| 143 | - $qb->setValue('file_source', $qb->createNamedParameter($share->getNode()->getId())); |
|
| 144 | - |
|
| 145 | - // set the permissions |
|
| 146 | - $qb->setValue('permissions', $qb->createNamedParameter($share->getPermissions())); |
|
| 147 | - |
|
| 148 | - // Set who created this share |
|
| 149 | - $qb->setValue('uid_initiator', $qb->createNamedParameter($share->getSharedBy())); |
|
| 150 | - |
|
| 151 | - // Set who is the owner of this file/folder (and this the owner of the share) |
|
| 152 | - $qb->setValue('uid_owner', $qb->createNamedParameter($share->getShareOwner())); |
|
| 153 | - |
|
| 154 | - // Set the file target |
|
| 155 | - $qb->setValue('file_target', $qb->createNamedParameter($share->getTarget())); |
|
| 156 | - |
|
| 157 | - // Set the time this share was created |
|
| 158 | - $qb->setValue('stime', $qb->createNamedParameter(time())); |
|
| 159 | - |
|
| 160 | - // insert the data and fetch the id of the share |
|
| 161 | - $this->dbConn->beginTransaction(); |
|
| 162 | - $qb->execute(); |
|
| 163 | - $id = $this->dbConn->lastInsertId('*PREFIX*share'); |
|
| 164 | - |
|
| 165 | - // Now fetch the inserted share and create a complete share object |
|
| 166 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 167 | - $qb->select('*') |
|
| 168 | - ->from('share') |
|
| 169 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($id))); |
|
| 170 | - |
|
| 171 | - $cursor = $qb->execute(); |
|
| 172 | - $data = $cursor->fetch(); |
|
| 173 | - $this->dbConn->commit(); |
|
| 174 | - $cursor->closeCursor(); |
|
| 175 | - |
|
| 176 | - if ($data === false) { |
|
| 177 | - throw new ShareNotFound(); |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - $mailSendValue = $share->getMailSend(); |
|
| 181 | - $data['mail_send'] = ($mailSendValue === null) ? true : $mailSendValue; |
|
| 182 | - |
|
| 183 | - $share = $this->createShare($data); |
|
| 184 | - return $share; |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - /** |
|
| 188 | - * Update a share |
|
| 189 | - * |
|
| 190 | - * @param \OCP\Share\IShare $share |
|
| 191 | - * @return \OCP\Share\IShare The share object |
|
| 192 | - */ |
|
| 193 | - public function update(\OCP\Share\IShare $share) { |
|
| 194 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { |
|
| 195 | - /* |
|
| 49 | + // Special share type for user modified group shares |
|
| 50 | + const SHARE_TYPE_USERGROUP = 2; |
|
| 51 | + |
|
| 52 | + /** @var IDBConnection */ |
|
| 53 | + private $dbConn; |
|
| 54 | + |
|
| 55 | + /** @var IUserManager */ |
|
| 56 | + private $userManager; |
|
| 57 | + |
|
| 58 | + /** @var IGroupManager */ |
|
| 59 | + private $groupManager; |
|
| 60 | + |
|
| 61 | + /** @var IRootFolder */ |
|
| 62 | + private $rootFolder; |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * DefaultShareProvider constructor. |
|
| 66 | + * |
|
| 67 | + * @param IDBConnection $connection |
|
| 68 | + * @param IUserManager $userManager |
|
| 69 | + * @param IGroupManager $groupManager |
|
| 70 | + * @param IRootFolder $rootFolder |
|
| 71 | + */ |
|
| 72 | + public function __construct( |
|
| 73 | + IDBConnection $connection, |
|
| 74 | + IUserManager $userManager, |
|
| 75 | + IGroupManager $groupManager, |
|
| 76 | + IRootFolder $rootFolder) { |
|
| 77 | + $this->dbConn = $connection; |
|
| 78 | + $this->userManager = $userManager; |
|
| 79 | + $this->groupManager = $groupManager; |
|
| 80 | + $this->rootFolder = $rootFolder; |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * Return the identifier of this provider. |
|
| 85 | + * |
|
| 86 | + * @return string Containing only [a-zA-Z0-9] |
|
| 87 | + */ |
|
| 88 | + public function identifier() { |
|
| 89 | + return 'ocinternal'; |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * Share a path |
|
| 94 | + * |
|
| 95 | + * @param \OCP\Share\IShare $share |
|
| 96 | + * @return \OCP\Share\IShare The share object |
|
| 97 | + * @throws ShareNotFound |
|
| 98 | + * @throws \Exception |
|
| 99 | + */ |
|
| 100 | + public function create(\OCP\Share\IShare $share) { |
|
| 101 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 102 | + |
|
| 103 | + $qb->insert('share'); |
|
| 104 | + $qb->setValue('share_type', $qb->createNamedParameter($share->getShareType())); |
|
| 105 | + |
|
| 106 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { |
|
| 107 | + //Set the UID of the user we share with |
|
| 108 | + $qb->setValue('share_with', $qb->createNamedParameter($share->getSharedWith())); |
|
| 109 | + } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 110 | + //Set the GID of the group we share with |
|
| 111 | + $qb->setValue('share_with', $qb->createNamedParameter($share->getSharedWith())); |
|
| 112 | + } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { |
|
| 113 | + //Set the token of the share |
|
| 114 | + $qb->setValue('token', $qb->createNamedParameter($share->getToken())); |
|
| 115 | + |
|
| 116 | + //If a password is set store it |
|
| 117 | + if ($share->getPassword() !== null) { |
|
| 118 | + $qb->setValue('password', $qb->createNamedParameter($share->getPassword())); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + //If an expiration date is set store it |
|
| 122 | + if ($share->getExpirationDate() !== null) { |
|
| 123 | + $qb->setValue('expiration', $qb->createNamedParameter($share->getExpirationDate(), 'datetime')); |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + if (method_exists($share, 'getParent')) { |
|
| 127 | + $qb->setValue('parent', $qb->createNamedParameter($share->getParent())); |
|
| 128 | + } |
|
| 129 | + } else { |
|
| 130 | + throw new \Exception('invalid share type!'); |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + // Set what is shares |
|
| 134 | + $qb->setValue('item_type', $qb->createParameter('itemType')); |
|
| 135 | + if ($share->getNode() instanceof \OCP\Files\File) { |
|
| 136 | + $qb->setParameter('itemType', 'file'); |
|
| 137 | + } else { |
|
| 138 | + $qb->setParameter('itemType', 'folder'); |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + // Set the file id |
|
| 142 | + $qb->setValue('item_source', $qb->createNamedParameter($share->getNode()->getId())); |
|
| 143 | + $qb->setValue('file_source', $qb->createNamedParameter($share->getNode()->getId())); |
|
| 144 | + |
|
| 145 | + // set the permissions |
|
| 146 | + $qb->setValue('permissions', $qb->createNamedParameter($share->getPermissions())); |
|
| 147 | + |
|
| 148 | + // Set who created this share |
|
| 149 | + $qb->setValue('uid_initiator', $qb->createNamedParameter($share->getSharedBy())); |
|
| 150 | + |
|
| 151 | + // Set who is the owner of this file/folder (and this the owner of the share) |
|
| 152 | + $qb->setValue('uid_owner', $qb->createNamedParameter($share->getShareOwner())); |
|
| 153 | + |
|
| 154 | + // Set the file target |
|
| 155 | + $qb->setValue('file_target', $qb->createNamedParameter($share->getTarget())); |
|
| 156 | + |
|
| 157 | + // Set the time this share was created |
|
| 158 | + $qb->setValue('stime', $qb->createNamedParameter(time())); |
|
| 159 | + |
|
| 160 | + // insert the data and fetch the id of the share |
|
| 161 | + $this->dbConn->beginTransaction(); |
|
| 162 | + $qb->execute(); |
|
| 163 | + $id = $this->dbConn->lastInsertId('*PREFIX*share'); |
|
| 164 | + |
|
| 165 | + // Now fetch the inserted share and create a complete share object |
|
| 166 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 167 | + $qb->select('*') |
|
| 168 | + ->from('share') |
|
| 169 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($id))); |
|
| 170 | + |
|
| 171 | + $cursor = $qb->execute(); |
|
| 172 | + $data = $cursor->fetch(); |
|
| 173 | + $this->dbConn->commit(); |
|
| 174 | + $cursor->closeCursor(); |
|
| 175 | + |
|
| 176 | + if ($data === false) { |
|
| 177 | + throw new ShareNotFound(); |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + $mailSendValue = $share->getMailSend(); |
|
| 181 | + $data['mail_send'] = ($mailSendValue === null) ? true : $mailSendValue; |
|
| 182 | + |
|
| 183 | + $share = $this->createShare($data); |
|
| 184 | + return $share; |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + /** |
|
| 188 | + * Update a share |
|
| 189 | + * |
|
| 190 | + * @param \OCP\Share\IShare $share |
|
| 191 | + * @return \OCP\Share\IShare The share object |
|
| 192 | + */ |
|
| 193 | + public function update(\OCP\Share\IShare $share) { |
|
| 194 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { |
|
| 195 | + /* |
|
| 196 | 196 | * We allow updating the recipient on user shares. |
| 197 | 197 | */ |
| 198 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 199 | - $qb->update('share') |
|
| 200 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId()))) |
|
| 201 | - ->set('share_with', $qb->createNamedParameter($share->getSharedWith())) |
|
| 202 | - ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner())) |
|
| 203 | - ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy())) |
|
| 204 | - ->set('permissions', $qb->createNamedParameter($share->getPermissions())) |
|
| 205 | - ->set('item_source', $qb->createNamedParameter($share->getNode()->getId())) |
|
| 206 | - ->set('file_source', $qb->createNamedParameter($share->getNode()->getId())) |
|
| 207 | - ->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE)) |
|
| 208 | - ->execute(); |
|
| 209 | - } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 210 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 211 | - $qb->update('share') |
|
| 212 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId()))) |
|
| 213 | - ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner())) |
|
| 214 | - ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy())) |
|
| 215 | - ->set('permissions', $qb->createNamedParameter($share->getPermissions())) |
|
| 216 | - ->set('item_source', $qb->createNamedParameter($share->getNode()->getId())) |
|
| 217 | - ->set('file_source', $qb->createNamedParameter($share->getNode()->getId())) |
|
| 218 | - ->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE)) |
|
| 219 | - ->execute(); |
|
| 220 | - |
|
| 221 | - /* |
|
| 198 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 199 | + $qb->update('share') |
|
| 200 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId()))) |
|
| 201 | + ->set('share_with', $qb->createNamedParameter($share->getSharedWith())) |
|
| 202 | + ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner())) |
|
| 203 | + ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy())) |
|
| 204 | + ->set('permissions', $qb->createNamedParameter($share->getPermissions())) |
|
| 205 | + ->set('item_source', $qb->createNamedParameter($share->getNode()->getId())) |
|
| 206 | + ->set('file_source', $qb->createNamedParameter($share->getNode()->getId())) |
|
| 207 | + ->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE)) |
|
| 208 | + ->execute(); |
|
| 209 | + } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 210 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 211 | + $qb->update('share') |
|
| 212 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId()))) |
|
| 213 | + ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner())) |
|
| 214 | + ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy())) |
|
| 215 | + ->set('permissions', $qb->createNamedParameter($share->getPermissions())) |
|
| 216 | + ->set('item_source', $qb->createNamedParameter($share->getNode()->getId())) |
|
| 217 | + ->set('file_source', $qb->createNamedParameter($share->getNode()->getId())) |
|
| 218 | + ->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE)) |
|
| 219 | + ->execute(); |
|
| 220 | + |
|
| 221 | + /* |
|
| 222 | 222 | * Update all user defined group shares |
| 223 | 223 | */ |
| 224 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 225 | - $qb->update('share') |
|
| 226 | - ->where($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId()))) |
|
| 227 | - ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner())) |
|
| 228 | - ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy())) |
|
| 229 | - ->set('item_source', $qb->createNamedParameter($share->getNode()->getId())) |
|
| 230 | - ->set('file_source', $qb->createNamedParameter($share->getNode()->getId())) |
|
| 231 | - ->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE)) |
|
| 232 | - ->execute(); |
|
| 233 | - |
|
| 234 | - /* |
|
| 224 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 225 | + $qb->update('share') |
|
| 226 | + ->where($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId()))) |
|
| 227 | + ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner())) |
|
| 228 | + ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy())) |
|
| 229 | + ->set('item_source', $qb->createNamedParameter($share->getNode()->getId())) |
|
| 230 | + ->set('file_source', $qb->createNamedParameter($share->getNode()->getId())) |
|
| 231 | + ->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE)) |
|
| 232 | + ->execute(); |
|
| 233 | + |
|
| 234 | + /* |
|
| 235 | 235 | * Now update the permissions for all children that have not set it to 0 |
| 236 | 236 | */ |
| 237 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 238 | - $qb->update('share') |
|
| 239 | - ->where($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId()))) |
|
| 240 | - ->andWhere($qb->expr()->neq('permissions', $qb->createNamedParameter(0))) |
|
| 241 | - ->set('permissions', $qb->createNamedParameter($share->getPermissions())) |
|
| 242 | - ->execute(); |
|
| 243 | - |
|
| 244 | - } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { |
|
| 245 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 246 | - $qb->update('share') |
|
| 247 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId()))) |
|
| 248 | - ->set('password', $qb->createNamedParameter($share->getPassword())) |
|
| 249 | - ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner())) |
|
| 250 | - ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy())) |
|
| 251 | - ->set('permissions', $qb->createNamedParameter($share->getPermissions())) |
|
| 252 | - ->set('item_source', $qb->createNamedParameter($share->getNode()->getId())) |
|
| 253 | - ->set('file_source', $qb->createNamedParameter($share->getNode()->getId())) |
|
| 254 | - ->set('token', $qb->createNamedParameter($share->getToken())) |
|
| 255 | - ->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE)) |
|
| 256 | - ->execute(); |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - return $share; |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - /** |
|
| 263 | - * Get all children of this share |
|
| 264 | - * FIXME: remove once https://github.com/owncloud/core/pull/21660 is in |
|
| 265 | - * |
|
| 266 | - * @param \OCP\Share\IShare $parent |
|
| 267 | - * @return \OCP\Share\IShare[] |
|
| 268 | - */ |
|
| 269 | - public function getChildren(\OCP\Share\IShare $parent) { |
|
| 270 | - $children = []; |
|
| 271 | - |
|
| 272 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 273 | - $qb->select('*') |
|
| 274 | - ->from('share') |
|
| 275 | - ->where($qb->expr()->eq('parent', $qb->createNamedParameter($parent->getId()))) |
|
| 276 | - ->andWhere( |
|
| 277 | - $qb->expr()->in( |
|
| 278 | - 'share_type', |
|
| 279 | - $qb->createNamedParameter([ |
|
| 280 | - \OCP\Share::SHARE_TYPE_USER, |
|
| 281 | - \OCP\Share::SHARE_TYPE_GROUP, |
|
| 282 | - \OCP\Share::SHARE_TYPE_LINK, |
|
| 283 | - ], IQueryBuilder::PARAM_INT_ARRAY) |
|
| 284 | - ) |
|
| 285 | - ) |
|
| 286 | - ->andWhere($qb->expr()->orX( |
|
| 287 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 288 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 289 | - )) |
|
| 290 | - ->orderBy('id'); |
|
| 291 | - |
|
| 292 | - $cursor = $qb->execute(); |
|
| 293 | - while($data = $cursor->fetch()) { |
|
| 294 | - $children[] = $this->createShare($data); |
|
| 295 | - } |
|
| 296 | - $cursor->closeCursor(); |
|
| 297 | - |
|
| 298 | - return $children; |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - /** |
|
| 302 | - * Delete a share |
|
| 303 | - * |
|
| 304 | - * @param \OCP\Share\IShare $share |
|
| 305 | - */ |
|
| 306 | - public function delete(\OCP\Share\IShare $share) { |
|
| 307 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 308 | - $qb->delete('share') |
|
| 309 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId()))); |
|
| 310 | - |
|
| 311 | - /* |
|
| 237 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 238 | + $qb->update('share') |
|
| 239 | + ->where($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId()))) |
|
| 240 | + ->andWhere($qb->expr()->neq('permissions', $qb->createNamedParameter(0))) |
|
| 241 | + ->set('permissions', $qb->createNamedParameter($share->getPermissions())) |
|
| 242 | + ->execute(); |
|
| 243 | + |
|
| 244 | + } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { |
|
| 245 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 246 | + $qb->update('share') |
|
| 247 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId()))) |
|
| 248 | + ->set('password', $qb->createNamedParameter($share->getPassword())) |
|
| 249 | + ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner())) |
|
| 250 | + ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy())) |
|
| 251 | + ->set('permissions', $qb->createNamedParameter($share->getPermissions())) |
|
| 252 | + ->set('item_source', $qb->createNamedParameter($share->getNode()->getId())) |
|
| 253 | + ->set('file_source', $qb->createNamedParameter($share->getNode()->getId())) |
|
| 254 | + ->set('token', $qb->createNamedParameter($share->getToken())) |
|
| 255 | + ->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE)) |
|
| 256 | + ->execute(); |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + return $share; |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + /** |
|
| 263 | + * Get all children of this share |
|
| 264 | + * FIXME: remove once https://github.com/owncloud/core/pull/21660 is in |
|
| 265 | + * |
|
| 266 | + * @param \OCP\Share\IShare $parent |
|
| 267 | + * @return \OCP\Share\IShare[] |
|
| 268 | + */ |
|
| 269 | + public function getChildren(\OCP\Share\IShare $parent) { |
|
| 270 | + $children = []; |
|
| 271 | + |
|
| 272 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 273 | + $qb->select('*') |
|
| 274 | + ->from('share') |
|
| 275 | + ->where($qb->expr()->eq('parent', $qb->createNamedParameter($parent->getId()))) |
|
| 276 | + ->andWhere( |
|
| 277 | + $qb->expr()->in( |
|
| 278 | + 'share_type', |
|
| 279 | + $qb->createNamedParameter([ |
|
| 280 | + \OCP\Share::SHARE_TYPE_USER, |
|
| 281 | + \OCP\Share::SHARE_TYPE_GROUP, |
|
| 282 | + \OCP\Share::SHARE_TYPE_LINK, |
|
| 283 | + ], IQueryBuilder::PARAM_INT_ARRAY) |
|
| 284 | + ) |
|
| 285 | + ) |
|
| 286 | + ->andWhere($qb->expr()->orX( |
|
| 287 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 288 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 289 | + )) |
|
| 290 | + ->orderBy('id'); |
|
| 291 | + |
|
| 292 | + $cursor = $qb->execute(); |
|
| 293 | + while($data = $cursor->fetch()) { |
|
| 294 | + $children[] = $this->createShare($data); |
|
| 295 | + } |
|
| 296 | + $cursor->closeCursor(); |
|
| 297 | + |
|
| 298 | + return $children; |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + /** |
|
| 302 | + * Delete a share |
|
| 303 | + * |
|
| 304 | + * @param \OCP\Share\IShare $share |
|
| 305 | + */ |
|
| 306 | + public function delete(\OCP\Share\IShare $share) { |
|
| 307 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 308 | + $qb->delete('share') |
|
| 309 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId()))); |
|
| 310 | + |
|
| 311 | + /* |
|
| 312 | 312 | * If the share is a group share delete all possible |
| 313 | 313 | * user defined groups shares. |
| 314 | 314 | */ |
| 315 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 316 | - $qb->orWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId()))); |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - $qb->execute(); |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - /** |
|
| 323 | - * Unshare a share from the recipient. If this is a group share |
|
| 324 | - * this means we need a special entry in the share db. |
|
| 325 | - * |
|
| 326 | - * @param \OCP\Share\IShare $share |
|
| 327 | - * @param string $recipient UserId of recipient |
|
| 328 | - * @throws BackendError |
|
| 329 | - * @throws ProviderException |
|
| 330 | - */ |
|
| 331 | - public function deleteFromSelf(\OCP\Share\IShare $share, $recipient) { |
|
| 332 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 333 | - |
|
| 334 | - $group = $this->groupManager->get($share->getSharedWith()); |
|
| 335 | - $user = $this->userManager->get($recipient); |
|
| 336 | - |
|
| 337 | - if (is_null($group)) { |
|
| 338 | - throw new ProviderException('Group "' . $share->getSharedWith() . '" does not exist'); |
|
| 339 | - } |
|
| 340 | - |
|
| 341 | - if (!$group->inGroup($user)) { |
|
| 342 | - throw new ProviderException('Recipient not in receiving group'); |
|
| 343 | - } |
|
| 344 | - |
|
| 345 | - // Try to fetch user specific share |
|
| 346 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 347 | - $stmt = $qb->select('*') |
|
| 348 | - ->from('share') |
|
| 349 | - ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP))) |
|
| 350 | - ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($recipient))) |
|
| 351 | - ->andWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId()))) |
|
| 352 | - ->andWhere($qb->expr()->orX( |
|
| 353 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 354 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 355 | - )) |
|
| 356 | - ->execute(); |
|
| 357 | - |
|
| 358 | - $data = $stmt->fetch(); |
|
| 359 | - |
|
| 360 | - /* |
|
| 315 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 316 | + $qb->orWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId()))); |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + $qb->execute(); |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + /** |
|
| 323 | + * Unshare a share from the recipient. If this is a group share |
|
| 324 | + * this means we need a special entry in the share db. |
|
| 325 | + * |
|
| 326 | + * @param \OCP\Share\IShare $share |
|
| 327 | + * @param string $recipient UserId of recipient |
|
| 328 | + * @throws BackendError |
|
| 329 | + * @throws ProviderException |
|
| 330 | + */ |
|
| 331 | + public function deleteFromSelf(\OCP\Share\IShare $share, $recipient) { |
|
| 332 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 333 | + |
|
| 334 | + $group = $this->groupManager->get($share->getSharedWith()); |
|
| 335 | + $user = $this->userManager->get($recipient); |
|
| 336 | + |
|
| 337 | + if (is_null($group)) { |
|
| 338 | + throw new ProviderException('Group "' . $share->getSharedWith() . '" does not exist'); |
|
| 339 | + } |
|
| 340 | + |
|
| 341 | + if (!$group->inGroup($user)) { |
|
| 342 | + throw new ProviderException('Recipient not in receiving group'); |
|
| 343 | + } |
|
| 344 | + |
|
| 345 | + // Try to fetch user specific share |
|
| 346 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 347 | + $stmt = $qb->select('*') |
|
| 348 | + ->from('share') |
|
| 349 | + ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP))) |
|
| 350 | + ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($recipient))) |
|
| 351 | + ->andWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId()))) |
|
| 352 | + ->andWhere($qb->expr()->orX( |
|
| 353 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 354 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 355 | + )) |
|
| 356 | + ->execute(); |
|
| 357 | + |
|
| 358 | + $data = $stmt->fetch(); |
|
| 359 | + |
|
| 360 | + /* |
|
| 361 | 361 | * Check if there already is a user specific group share. |
| 362 | 362 | * If there is update it (if required). |
| 363 | 363 | */ |
| 364 | - if ($data === false) { |
|
| 365 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 366 | - |
|
| 367 | - $type = $share->getNodeType(); |
|
| 368 | - |
|
| 369 | - //Insert new share |
|
| 370 | - $qb->insert('share') |
|
| 371 | - ->values([ |
|
| 372 | - 'share_type' => $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP), |
|
| 373 | - 'share_with' => $qb->createNamedParameter($recipient), |
|
| 374 | - 'uid_owner' => $qb->createNamedParameter($share->getShareOwner()), |
|
| 375 | - 'uid_initiator' => $qb->createNamedParameter($share->getSharedBy()), |
|
| 376 | - 'parent' => $qb->createNamedParameter($share->getId()), |
|
| 377 | - 'item_type' => $qb->createNamedParameter($type), |
|
| 378 | - 'item_source' => $qb->createNamedParameter($share->getNodeId()), |
|
| 379 | - 'file_source' => $qb->createNamedParameter($share->getNodeId()), |
|
| 380 | - 'file_target' => $qb->createNamedParameter($share->getTarget()), |
|
| 381 | - 'permissions' => $qb->createNamedParameter(0), |
|
| 382 | - 'stime' => $qb->createNamedParameter($share->getShareTime()->getTimestamp()), |
|
| 383 | - ])->execute(); |
|
| 384 | - |
|
| 385 | - } else if ($data['permissions'] !== 0) { |
|
| 386 | - |
|
| 387 | - // Update existing usergroup share |
|
| 388 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 389 | - $qb->update('share') |
|
| 390 | - ->set('permissions', $qb->createNamedParameter(0)) |
|
| 391 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($data['id']))) |
|
| 392 | - ->execute(); |
|
| 393 | - } |
|
| 394 | - |
|
| 395 | - } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { |
|
| 396 | - |
|
| 397 | - if ($share->getSharedWith() !== $recipient) { |
|
| 398 | - throw new ProviderException('Recipient does not match'); |
|
| 399 | - } |
|
| 400 | - |
|
| 401 | - // We can just delete user and link shares |
|
| 402 | - $this->delete($share); |
|
| 403 | - } else { |
|
| 404 | - throw new ProviderException('Invalid shareType'); |
|
| 405 | - } |
|
| 406 | - } |
|
| 407 | - |
|
| 408 | - /** |
|
| 409 | - * @inheritdoc |
|
| 410 | - */ |
|
| 411 | - public function move(\OCP\Share\IShare $share, $recipient) { |
|
| 412 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { |
|
| 413 | - // Just update the target |
|
| 414 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 415 | - $qb->update('share') |
|
| 416 | - ->set('file_target', $qb->createNamedParameter($share->getTarget())) |
|
| 417 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId()))) |
|
| 418 | - ->execute(); |
|
| 419 | - |
|
| 420 | - } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 421 | - |
|
| 422 | - // Check if there is a usergroup share |
|
| 423 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 424 | - $stmt = $qb->select('id') |
|
| 425 | - ->from('share') |
|
| 426 | - ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP))) |
|
| 427 | - ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($recipient))) |
|
| 428 | - ->andWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId()))) |
|
| 429 | - ->andWhere($qb->expr()->orX( |
|
| 430 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 431 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 432 | - )) |
|
| 433 | - ->setMaxResults(1) |
|
| 434 | - ->execute(); |
|
| 435 | - |
|
| 436 | - $data = $stmt->fetch(); |
|
| 437 | - $stmt->closeCursor(); |
|
| 438 | - |
|
| 439 | - if ($data === false) { |
|
| 440 | - // No usergroup share yet. Create one. |
|
| 441 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 442 | - $qb->insert('share') |
|
| 443 | - ->values([ |
|
| 444 | - 'share_type' => $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP), |
|
| 445 | - 'share_with' => $qb->createNamedParameter($recipient), |
|
| 446 | - 'uid_owner' => $qb->createNamedParameter($share->getShareOwner()), |
|
| 447 | - 'uid_initiator' => $qb->createNamedParameter($share->getSharedBy()), |
|
| 448 | - 'parent' => $qb->createNamedParameter($share->getId()), |
|
| 449 | - 'item_type' => $qb->createNamedParameter($share->getNode() instanceof File ? 'file' : 'folder'), |
|
| 450 | - 'item_source' => $qb->createNamedParameter($share->getNode()->getId()), |
|
| 451 | - 'file_source' => $qb->createNamedParameter($share->getNode()->getId()), |
|
| 452 | - 'file_target' => $qb->createNamedParameter($share->getTarget()), |
|
| 453 | - 'permissions' => $qb->createNamedParameter($share->getPermissions()), |
|
| 454 | - 'stime' => $qb->createNamedParameter($share->getShareTime()->getTimestamp()), |
|
| 455 | - ])->execute(); |
|
| 456 | - } else { |
|
| 457 | - // Already a usergroup share. Update it. |
|
| 458 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 459 | - $qb->update('share') |
|
| 460 | - ->set('file_target', $qb->createNamedParameter($share->getTarget())) |
|
| 461 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($data['id']))) |
|
| 462 | - ->execute(); |
|
| 463 | - } |
|
| 464 | - } |
|
| 465 | - |
|
| 466 | - return $share; |
|
| 467 | - } |
|
| 468 | - |
|
| 469 | - public function getSharesInFolder($userId, Folder $node, $reshares) { |
|
| 470 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 471 | - $qb->select('*') |
|
| 472 | - ->from('share', 's') |
|
| 473 | - ->andWhere($qb->expr()->orX( |
|
| 474 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 475 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 476 | - )); |
|
| 477 | - |
|
| 478 | - $qb->andWhere($qb->expr()->orX( |
|
| 479 | - $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)), |
|
| 480 | - $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)), |
|
| 481 | - $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK)) |
|
| 482 | - )); |
|
| 483 | - |
|
| 484 | - /** |
|
| 485 | - * Reshares for this user are shares where they are the owner. |
|
| 486 | - */ |
|
| 487 | - if ($reshares === false) { |
|
| 488 | - $qb->andWhere($qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))); |
|
| 489 | - } else { |
|
| 490 | - $qb->andWhere( |
|
| 491 | - $qb->expr()->orX( |
|
| 492 | - $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)), |
|
| 493 | - $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)) |
|
| 494 | - ) |
|
| 495 | - ); |
|
| 496 | - } |
|
| 497 | - |
|
| 498 | - $qb->innerJoin('s', 'filecache' ,'f', $qb->expr()->eq('s.file_source', 'f.fileid')); |
|
| 499 | - $qb->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($node->getId()))); |
|
| 500 | - |
|
| 501 | - $qb->orderBy('id'); |
|
| 502 | - |
|
| 503 | - $cursor = $qb->execute(); |
|
| 504 | - $shares = []; |
|
| 505 | - while ($data = $cursor->fetch()) { |
|
| 506 | - $shares[$data['fileid']][] = $this->createShare($data); |
|
| 507 | - } |
|
| 508 | - $cursor->closeCursor(); |
|
| 509 | - |
|
| 510 | - return $shares; |
|
| 511 | - } |
|
| 512 | - |
|
| 513 | - /** |
|
| 514 | - * @inheritdoc |
|
| 515 | - */ |
|
| 516 | - public function getSharesBy($userId, $shareType, $node, $reshares, $limit, $offset) { |
|
| 517 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 518 | - $qb->select('*') |
|
| 519 | - ->from('share') |
|
| 520 | - ->andWhere($qb->expr()->orX( |
|
| 521 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 522 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 523 | - )); |
|
| 524 | - |
|
| 525 | - $qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter($shareType))); |
|
| 526 | - |
|
| 527 | - /** |
|
| 528 | - * Reshares for this user are shares where they are the owner. |
|
| 529 | - */ |
|
| 530 | - if ($reshares === false) { |
|
| 531 | - $qb->andWhere($qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))); |
|
| 532 | - } else { |
|
| 533 | - $qb->andWhere( |
|
| 534 | - $qb->expr()->orX( |
|
| 535 | - $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)), |
|
| 536 | - $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)) |
|
| 537 | - ) |
|
| 538 | - ); |
|
| 539 | - } |
|
| 540 | - |
|
| 541 | - if ($node !== null) { |
|
| 542 | - $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId()))); |
|
| 543 | - } |
|
| 544 | - |
|
| 545 | - if ($limit !== -1) { |
|
| 546 | - $qb->setMaxResults($limit); |
|
| 547 | - } |
|
| 548 | - |
|
| 549 | - $qb->setFirstResult($offset); |
|
| 550 | - $qb->orderBy('id'); |
|
| 551 | - |
|
| 552 | - $cursor = $qb->execute(); |
|
| 553 | - $shares = []; |
|
| 554 | - while($data = $cursor->fetch()) { |
|
| 555 | - $shares[] = $this->createShare($data); |
|
| 556 | - } |
|
| 557 | - $cursor->closeCursor(); |
|
| 558 | - |
|
| 559 | - return $shares; |
|
| 560 | - } |
|
| 561 | - |
|
| 562 | - /** |
|
| 563 | - * @inheritdoc |
|
| 564 | - */ |
|
| 565 | - public function getShareById($id, $recipientId = null) { |
|
| 566 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 567 | - |
|
| 568 | - $qb->select('*') |
|
| 569 | - ->from('share') |
|
| 570 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($id))) |
|
| 571 | - ->andWhere( |
|
| 572 | - $qb->expr()->in( |
|
| 573 | - 'share_type', |
|
| 574 | - $qb->createNamedParameter([ |
|
| 575 | - \OCP\Share::SHARE_TYPE_USER, |
|
| 576 | - \OCP\Share::SHARE_TYPE_GROUP, |
|
| 577 | - \OCP\Share::SHARE_TYPE_LINK, |
|
| 578 | - ], IQueryBuilder::PARAM_INT_ARRAY) |
|
| 579 | - ) |
|
| 580 | - ) |
|
| 581 | - ->andWhere($qb->expr()->orX( |
|
| 582 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 583 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 584 | - )); |
|
| 585 | - |
|
| 586 | - $cursor = $qb->execute(); |
|
| 587 | - $data = $cursor->fetch(); |
|
| 588 | - $cursor->closeCursor(); |
|
| 589 | - |
|
| 590 | - if ($data === false) { |
|
| 591 | - throw new ShareNotFound(); |
|
| 592 | - } |
|
| 593 | - |
|
| 594 | - try { |
|
| 595 | - $share = $this->createShare($data); |
|
| 596 | - } catch (InvalidShare $e) { |
|
| 597 | - throw new ShareNotFound(); |
|
| 598 | - } |
|
| 599 | - |
|
| 600 | - // If the recipient is set for a group share resolve to that user |
|
| 601 | - if ($recipientId !== null && $share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 602 | - $share = $this->resolveGroupShares([$share], $recipientId)[0]; |
|
| 603 | - } |
|
| 604 | - |
|
| 605 | - return $share; |
|
| 606 | - } |
|
| 607 | - |
|
| 608 | - /** |
|
| 609 | - * Get shares for a given path |
|
| 610 | - * |
|
| 611 | - * @param \OCP\Files\Node $path |
|
| 612 | - * @return \OCP\Share\IShare[] |
|
| 613 | - */ |
|
| 614 | - public function getSharesByPath(Node $path) { |
|
| 615 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 616 | - |
|
| 617 | - $cursor = $qb->select('*') |
|
| 618 | - ->from('share') |
|
| 619 | - ->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($path->getId()))) |
|
| 620 | - ->andWhere( |
|
| 621 | - $qb->expr()->orX( |
|
| 622 | - $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)), |
|
| 623 | - $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)) |
|
| 624 | - ) |
|
| 625 | - ) |
|
| 626 | - ->andWhere($qb->expr()->orX( |
|
| 627 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 628 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 629 | - )) |
|
| 630 | - ->execute(); |
|
| 631 | - |
|
| 632 | - $shares = []; |
|
| 633 | - while($data = $cursor->fetch()) { |
|
| 634 | - $shares[] = $this->createShare($data); |
|
| 635 | - } |
|
| 636 | - $cursor->closeCursor(); |
|
| 637 | - |
|
| 638 | - return $shares; |
|
| 639 | - } |
|
| 640 | - |
|
| 641 | - /** |
|
| 642 | - * Returns whether the given database result can be interpreted as |
|
| 643 | - * a share with accessible file (not trashed, not deleted) |
|
| 644 | - */ |
|
| 645 | - private function isAccessibleResult($data) { |
|
| 646 | - // exclude shares leading to deleted file entries |
|
| 647 | - if ($data['fileid'] === null) { |
|
| 648 | - return false; |
|
| 649 | - } |
|
| 650 | - |
|
| 651 | - // exclude shares leading to trashbin on home storages |
|
| 652 | - $pathSections = explode('/', $data['path'], 2); |
|
| 653 | - // FIXME: would not detect rare md5'd home storage case properly |
|
| 654 | - if ($pathSections[0] !== 'files' |
|
| 655 | - && in_array(explode(':', $data['storage_string_id'], 2)[0], array('home', 'object'))) { |
|
| 656 | - return false; |
|
| 657 | - } |
|
| 658 | - return true; |
|
| 659 | - } |
|
| 660 | - |
|
| 661 | - /** |
|
| 662 | - * @inheritdoc |
|
| 663 | - */ |
|
| 664 | - public function getSharedWith($userId, $shareType, $node, $limit, $offset) { |
|
| 665 | - /** @var Share[] $shares */ |
|
| 666 | - $shares = []; |
|
| 667 | - |
|
| 668 | - if ($shareType === \OCP\Share::SHARE_TYPE_USER) { |
|
| 669 | - //Get shares directly with this user |
|
| 670 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 671 | - $qb->select('s.*', |
|
| 672 | - 'f.fileid', 'f.path', 'f.permissions AS f_permissions', 'f.storage', 'f.path_hash', |
|
| 673 | - 'f.parent AS f_parent', 'f.name', 'f.mimetype', 'f.mimepart', 'f.size', 'f.mtime', 'f.storage_mtime', |
|
| 674 | - 'f.encrypted', 'f.unencrypted_size', 'f.etag', 'f.checksum' |
|
| 675 | - ) |
|
| 676 | - ->selectAlias('st.id', 'storage_string_id') |
|
| 677 | - ->from('share', 's') |
|
| 678 | - ->leftJoin('s', 'filecache', 'f', $qb->expr()->eq('s.file_source', 'f.fileid')) |
|
| 679 | - ->leftJoin('f', 'storages', 'st', $qb->expr()->eq('f.storage', 'st.numeric_id')); |
|
| 680 | - |
|
| 681 | - // Order by id |
|
| 682 | - $qb->orderBy('s.id'); |
|
| 683 | - |
|
| 684 | - // Set limit and offset |
|
| 685 | - if ($limit !== -1) { |
|
| 686 | - $qb->setMaxResults($limit); |
|
| 687 | - } |
|
| 688 | - $qb->setFirstResult($offset); |
|
| 689 | - |
|
| 690 | - $qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER))) |
|
| 691 | - ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId))) |
|
| 692 | - ->andWhere($qb->expr()->orX( |
|
| 693 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 694 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 695 | - )); |
|
| 696 | - |
|
| 697 | - // Filter by node if provided |
|
| 698 | - if ($node !== null) { |
|
| 699 | - $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId()))); |
|
| 700 | - } |
|
| 701 | - |
|
| 702 | - $cursor = $qb->execute(); |
|
| 703 | - |
|
| 704 | - while($data = $cursor->fetch()) { |
|
| 705 | - if ($this->isAccessibleResult($data)) { |
|
| 706 | - $shares[] = $this->createShare($data); |
|
| 707 | - } |
|
| 708 | - } |
|
| 709 | - $cursor->closeCursor(); |
|
| 710 | - |
|
| 711 | - } else if ($shareType === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 712 | - $user = $this->userManager->get($userId); |
|
| 713 | - $allGroups = $this->groupManager->getUserGroups($user); |
|
| 714 | - |
|
| 715 | - /** @var Share[] $shares2 */ |
|
| 716 | - $shares2 = []; |
|
| 717 | - |
|
| 718 | - $start = 0; |
|
| 719 | - while(true) { |
|
| 720 | - $groups = array_slice($allGroups, $start, 100); |
|
| 721 | - $start += 100; |
|
| 722 | - |
|
| 723 | - if ($groups === []) { |
|
| 724 | - break; |
|
| 725 | - } |
|
| 726 | - |
|
| 727 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 728 | - $qb->select('s.*', |
|
| 729 | - 'f.fileid', 'f.path', 'f.permissions AS f_permissions', 'f.storage', 'f.path_hash', |
|
| 730 | - 'f.parent AS f_parent', 'f.name', 'f.mimetype', 'f.mimepart', 'f.size', 'f.mtime', 'f.storage_mtime', |
|
| 731 | - 'f.encrypted', 'f.unencrypted_size', 'f.etag', 'f.checksum' |
|
| 732 | - ) |
|
| 733 | - ->selectAlias('st.id', 'storage_string_id') |
|
| 734 | - ->from('share', 's') |
|
| 735 | - ->leftJoin('s', 'filecache', 'f', $qb->expr()->eq('s.file_source', 'f.fileid')) |
|
| 736 | - ->leftJoin('f', 'storages', 'st', $qb->expr()->eq('f.storage', 'st.numeric_id')) |
|
| 737 | - ->orderBy('s.id') |
|
| 738 | - ->setFirstResult(0); |
|
| 739 | - |
|
| 740 | - if ($limit !== -1) { |
|
| 741 | - $qb->setMaxResults($limit - count($shares)); |
|
| 742 | - } |
|
| 743 | - |
|
| 744 | - // Filter by node if provided |
|
| 745 | - if ($node !== null) { |
|
| 746 | - $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId()))); |
|
| 747 | - } |
|
| 748 | - |
|
| 749 | - |
|
| 750 | - $groups = array_filter($groups, function($group) { return $group instanceof IGroup; }); |
|
| 751 | - $groups = array_map(function(IGroup $group) { return $group->getGID(); }, $groups); |
|
| 752 | - |
|
| 753 | - $qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP))) |
|
| 754 | - ->andWhere($qb->expr()->in('share_with', $qb->createNamedParameter( |
|
| 755 | - $groups, |
|
| 756 | - IQueryBuilder::PARAM_STR_ARRAY |
|
| 757 | - ))) |
|
| 758 | - ->andWhere($qb->expr()->orX( |
|
| 759 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 760 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 761 | - )); |
|
| 762 | - |
|
| 763 | - $cursor = $qb->execute(); |
|
| 764 | - while($data = $cursor->fetch()) { |
|
| 765 | - if ($offset > 0) { |
|
| 766 | - $offset--; |
|
| 767 | - continue; |
|
| 768 | - } |
|
| 769 | - |
|
| 770 | - if ($this->isAccessibleResult($data)) { |
|
| 771 | - $shares2[] = $this->createShare($data); |
|
| 772 | - } |
|
| 773 | - } |
|
| 774 | - $cursor->closeCursor(); |
|
| 775 | - } |
|
| 776 | - |
|
| 777 | - /* |
|
| 364 | + if ($data === false) { |
|
| 365 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 366 | + |
|
| 367 | + $type = $share->getNodeType(); |
|
| 368 | + |
|
| 369 | + //Insert new share |
|
| 370 | + $qb->insert('share') |
|
| 371 | + ->values([ |
|
| 372 | + 'share_type' => $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP), |
|
| 373 | + 'share_with' => $qb->createNamedParameter($recipient), |
|
| 374 | + 'uid_owner' => $qb->createNamedParameter($share->getShareOwner()), |
|
| 375 | + 'uid_initiator' => $qb->createNamedParameter($share->getSharedBy()), |
|
| 376 | + 'parent' => $qb->createNamedParameter($share->getId()), |
|
| 377 | + 'item_type' => $qb->createNamedParameter($type), |
|
| 378 | + 'item_source' => $qb->createNamedParameter($share->getNodeId()), |
|
| 379 | + 'file_source' => $qb->createNamedParameter($share->getNodeId()), |
|
| 380 | + 'file_target' => $qb->createNamedParameter($share->getTarget()), |
|
| 381 | + 'permissions' => $qb->createNamedParameter(0), |
|
| 382 | + 'stime' => $qb->createNamedParameter($share->getShareTime()->getTimestamp()), |
|
| 383 | + ])->execute(); |
|
| 384 | + |
|
| 385 | + } else if ($data['permissions'] !== 0) { |
|
| 386 | + |
|
| 387 | + // Update existing usergroup share |
|
| 388 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 389 | + $qb->update('share') |
|
| 390 | + ->set('permissions', $qb->createNamedParameter(0)) |
|
| 391 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($data['id']))) |
|
| 392 | + ->execute(); |
|
| 393 | + } |
|
| 394 | + |
|
| 395 | + } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { |
|
| 396 | + |
|
| 397 | + if ($share->getSharedWith() !== $recipient) { |
|
| 398 | + throw new ProviderException('Recipient does not match'); |
|
| 399 | + } |
|
| 400 | + |
|
| 401 | + // We can just delete user and link shares |
|
| 402 | + $this->delete($share); |
|
| 403 | + } else { |
|
| 404 | + throw new ProviderException('Invalid shareType'); |
|
| 405 | + } |
|
| 406 | + } |
|
| 407 | + |
|
| 408 | + /** |
|
| 409 | + * @inheritdoc |
|
| 410 | + */ |
|
| 411 | + public function move(\OCP\Share\IShare $share, $recipient) { |
|
| 412 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { |
|
| 413 | + // Just update the target |
|
| 414 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 415 | + $qb->update('share') |
|
| 416 | + ->set('file_target', $qb->createNamedParameter($share->getTarget())) |
|
| 417 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId()))) |
|
| 418 | + ->execute(); |
|
| 419 | + |
|
| 420 | + } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 421 | + |
|
| 422 | + // Check if there is a usergroup share |
|
| 423 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 424 | + $stmt = $qb->select('id') |
|
| 425 | + ->from('share') |
|
| 426 | + ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP))) |
|
| 427 | + ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($recipient))) |
|
| 428 | + ->andWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId()))) |
|
| 429 | + ->andWhere($qb->expr()->orX( |
|
| 430 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 431 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 432 | + )) |
|
| 433 | + ->setMaxResults(1) |
|
| 434 | + ->execute(); |
|
| 435 | + |
|
| 436 | + $data = $stmt->fetch(); |
|
| 437 | + $stmt->closeCursor(); |
|
| 438 | + |
|
| 439 | + if ($data === false) { |
|
| 440 | + // No usergroup share yet. Create one. |
|
| 441 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 442 | + $qb->insert('share') |
|
| 443 | + ->values([ |
|
| 444 | + 'share_type' => $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP), |
|
| 445 | + 'share_with' => $qb->createNamedParameter($recipient), |
|
| 446 | + 'uid_owner' => $qb->createNamedParameter($share->getShareOwner()), |
|
| 447 | + 'uid_initiator' => $qb->createNamedParameter($share->getSharedBy()), |
|
| 448 | + 'parent' => $qb->createNamedParameter($share->getId()), |
|
| 449 | + 'item_type' => $qb->createNamedParameter($share->getNode() instanceof File ? 'file' : 'folder'), |
|
| 450 | + 'item_source' => $qb->createNamedParameter($share->getNode()->getId()), |
|
| 451 | + 'file_source' => $qb->createNamedParameter($share->getNode()->getId()), |
|
| 452 | + 'file_target' => $qb->createNamedParameter($share->getTarget()), |
|
| 453 | + 'permissions' => $qb->createNamedParameter($share->getPermissions()), |
|
| 454 | + 'stime' => $qb->createNamedParameter($share->getShareTime()->getTimestamp()), |
|
| 455 | + ])->execute(); |
|
| 456 | + } else { |
|
| 457 | + // Already a usergroup share. Update it. |
|
| 458 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 459 | + $qb->update('share') |
|
| 460 | + ->set('file_target', $qb->createNamedParameter($share->getTarget())) |
|
| 461 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($data['id']))) |
|
| 462 | + ->execute(); |
|
| 463 | + } |
|
| 464 | + } |
|
| 465 | + |
|
| 466 | + return $share; |
|
| 467 | + } |
|
| 468 | + |
|
| 469 | + public function getSharesInFolder($userId, Folder $node, $reshares) { |
|
| 470 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 471 | + $qb->select('*') |
|
| 472 | + ->from('share', 's') |
|
| 473 | + ->andWhere($qb->expr()->orX( |
|
| 474 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 475 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 476 | + )); |
|
| 477 | + |
|
| 478 | + $qb->andWhere($qb->expr()->orX( |
|
| 479 | + $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)), |
|
| 480 | + $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)), |
|
| 481 | + $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK)) |
|
| 482 | + )); |
|
| 483 | + |
|
| 484 | + /** |
|
| 485 | + * Reshares for this user are shares where they are the owner. |
|
| 486 | + */ |
|
| 487 | + if ($reshares === false) { |
|
| 488 | + $qb->andWhere($qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))); |
|
| 489 | + } else { |
|
| 490 | + $qb->andWhere( |
|
| 491 | + $qb->expr()->orX( |
|
| 492 | + $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)), |
|
| 493 | + $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)) |
|
| 494 | + ) |
|
| 495 | + ); |
|
| 496 | + } |
|
| 497 | + |
|
| 498 | + $qb->innerJoin('s', 'filecache' ,'f', $qb->expr()->eq('s.file_source', 'f.fileid')); |
|
| 499 | + $qb->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($node->getId()))); |
|
| 500 | + |
|
| 501 | + $qb->orderBy('id'); |
|
| 502 | + |
|
| 503 | + $cursor = $qb->execute(); |
|
| 504 | + $shares = []; |
|
| 505 | + while ($data = $cursor->fetch()) { |
|
| 506 | + $shares[$data['fileid']][] = $this->createShare($data); |
|
| 507 | + } |
|
| 508 | + $cursor->closeCursor(); |
|
| 509 | + |
|
| 510 | + return $shares; |
|
| 511 | + } |
|
| 512 | + |
|
| 513 | + /** |
|
| 514 | + * @inheritdoc |
|
| 515 | + */ |
|
| 516 | + public function getSharesBy($userId, $shareType, $node, $reshares, $limit, $offset) { |
|
| 517 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 518 | + $qb->select('*') |
|
| 519 | + ->from('share') |
|
| 520 | + ->andWhere($qb->expr()->orX( |
|
| 521 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 522 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 523 | + )); |
|
| 524 | + |
|
| 525 | + $qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter($shareType))); |
|
| 526 | + |
|
| 527 | + /** |
|
| 528 | + * Reshares for this user are shares where they are the owner. |
|
| 529 | + */ |
|
| 530 | + if ($reshares === false) { |
|
| 531 | + $qb->andWhere($qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))); |
|
| 532 | + } else { |
|
| 533 | + $qb->andWhere( |
|
| 534 | + $qb->expr()->orX( |
|
| 535 | + $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)), |
|
| 536 | + $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)) |
|
| 537 | + ) |
|
| 538 | + ); |
|
| 539 | + } |
|
| 540 | + |
|
| 541 | + if ($node !== null) { |
|
| 542 | + $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId()))); |
|
| 543 | + } |
|
| 544 | + |
|
| 545 | + if ($limit !== -1) { |
|
| 546 | + $qb->setMaxResults($limit); |
|
| 547 | + } |
|
| 548 | + |
|
| 549 | + $qb->setFirstResult($offset); |
|
| 550 | + $qb->orderBy('id'); |
|
| 551 | + |
|
| 552 | + $cursor = $qb->execute(); |
|
| 553 | + $shares = []; |
|
| 554 | + while($data = $cursor->fetch()) { |
|
| 555 | + $shares[] = $this->createShare($data); |
|
| 556 | + } |
|
| 557 | + $cursor->closeCursor(); |
|
| 558 | + |
|
| 559 | + return $shares; |
|
| 560 | + } |
|
| 561 | + |
|
| 562 | + /** |
|
| 563 | + * @inheritdoc |
|
| 564 | + */ |
|
| 565 | + public function getShareById($id, $recipientId = null) { |
|
| 566 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 567 | + |
|
| 568 | + $qb->select('*') |
|
| 569 | + ->from('share') |
|
| 570 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($id))) |
|
| 571 | + ->andWhere( |
|
| 572 | + $qb->expr()->in( |
|
| 573 | + 'share_type', |
|
| 574 | + $qb->createNamedParameter([ |
|
| 575 | + \OCP\Share::SHARE_TYPE_USER, |
|
| 576 | + \OCP\Share::SHARE_TYPE_GROUP, |
|
| 577 | + \OCP\Share::SHARE_TYPE_LINK, |
|
| 578 | + ], IQueryBuilder::PARAM_INT_ARRAY) |
|
| 579 | + ) |
|
| 580 | + ) |
|
| 581 | + ->andWhere($qb->expr()->orX( |
|
| 582 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 583 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 584 | + )); |
|
| 585 | + |
|
| 586 | + $cursor = $qb->execute(); |
|
| 587 | + $data = $cursor->fetch(); |
|
| 588 | + $cursor->closeCursor(); |
|
| 589 | + |
|
| 590 | + if ($data === false) { |
|
| 591 | + throw new ShareNotFound(); |
|
| 592 | + } |
|
| 593 | + |
|
| 594 | + try { |
|
| 595 | + $share = $this->createShare($data); |
|
| 596 | + } catch (InvalidShare $e) { |
|
| 597 | + throw new ShareNotFound(); |
|
| 598 | + } |
|
| 599 | + |
|
| 600 | + // If the recipient is set for a group share resolve to that user |
|
| 601 | + if ($recipientId !== null && $share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 602 | + $share = $this->resolveGroupShares([$share], $recipientId)[0]; |
|
| 603 | + } |
|
| 604 | + |
|
| 605 | + return $share; |
|
| 606 | + } |
|
| 607 | + |
|
| 608 | + /** |
|
| 609 | + * Get shares for a given path |
|
| 610 | + * |
|
| 611 | + * @param \OCP\Files\Node $path |
|
| 612 | + * @return \OCP\Share\IShare[] |
|
| 613 | + */ |
|
| 614 | + public function getSharesByPath(Node $path) { |
|
| 615 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 616 | + |
|
| 617 | + $cursor = $qb->select('*') |
|
| 618 | + ->from('share') |
|
| 619 | + ->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($path->getId()))) |
|
| 620 | + ->andWhere( |
|
| 621 | + $qb->expr()->orX( |
|
| 622 | + $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)), |
|
| 623 | + $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)) |
|
| 624 | + ) |
|
| 625 | + ) |
|
| 626 | + ->andWhere($qb->expr()->orX( |
|
| 627 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 628 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 629 | + )) |
|
| 630 | + ->execute(); |
|
| 631 | + |
|
| 632 | + $shares = []; |
|
| 633 | + while($data = $cursor->fetch()) { |
|
| 634 | + $shares[] = $this->createShare($data); |
|
| 635 | + } |
|
| 636 | + $cursor->closeCursor(); |
|
| 637 | + |
|
| 638 | + return $shares; |
|
| 639 | + } |
|
| 640 | + |
|
| 641 | + /** |
|
| 642 | + * Returns whether the given database result can be interpreted as |
|
| 643 | + * a share with accessible file (not trashed, not deleted) |
|
| 644 | + */ |
|
| 645 | + private function isAccessibleResult($data) { |
|
| 646 | + // exclude shares leading to deleted file entries |
|
| 647 | + if ($data['fileid'] === null) { |
|
| 648 | + return false; |
|
| 649 | + } |
|
| 650 | + |
|
| 651 | + // exclude shares leading to trashbin on home storages |
|
| 652 | + $pathSections = explode('/', $data['path'], 2); |
|
| 653 | + // FIXME: would not detect rare md5'd home storage case properly |
|
| 654 | + if ($pathSections[0] !== 'files' |
|
| 655 | + && in_array(explode(':', $data['storage_string_id'], 2)[0], array('home', 'object'))) { |
|
| 656 | + return false; |
|
| 657 | + } |
|
| 658 | + return true; |
|
| 659 | + } |
|
| 660 | + |
|
| 661 | + /** |
|
| 662 | + * @inheritdoc |
|
| 663 | + */ |
|
| 664 | + public function getSharedWith($userId, $shareType, $node, $limit, $offset) { |
|
| 665 | + /** @var Share[] $shares */ |
|
| 666 | + $shares = []; |
|
| 667 | + |
|
| 668 | + if ($shareType === \OCP\Share::SHARE_TYPE_USER) { |
|
| 669 | + //Get shares directly with this user |
|
| 670 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 671 | + $qb->select('s.*', |
|
| 672 | + 'f.fileid', 'f.path', 'f.permissions AS f_permissions', 'f.storage', 'f.path_hash', |
|
| 673 | + 'f.parent AS f_parent', 'f.name', 'f.mimetype', 'f.mimepart', 'f.size', 'f.mtime', 'f.storage_mtime', |
|
| 674 | + 'f.encrypted', 'f.unencrypted_size', 'f.etag', 'f.checksum' |
|
| 675 | + ) |
|
| 676 | + ->selectAlias('st.id', 'storage_string_id') |
|
| 677 | + ->from('share', 's') |
|
| 678 | + ->leftJoin('s', 'filecache', 'f', $qb->expr()->eq('s.file_source', 'f.fileid')) |
|
| 679 | + ->leftJoin('f', 'storages', 'st', $qb->expr()->eq('f.storage', 'st.numeric_id')); |
|
| 680 | + |
|
| 681 | + // Order by id |
|
| 682 | + $qb->orderBy('s.id'); |
|
| 683 | + |
|
| 684 | + // Set limit and offset |
|
| 685 | + if ($limit !== -1) { |
|
| 686 | + $qb->setMaxResults($limit); |
|
| 687 | + } |
|
| 688 | + $qb->setFirstResult($offset); |
|
| 689 | + |
|
| 690 | + $qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER))) |
|
| 691 | + ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId))) |
|
| 692 | + ->andWhere($qb->expr()->orX( |
|
| 693 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 694 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 695 | + )); |
|
| 696 | + |
|
| 697 | + // Filter by node if provided |
|
| 698 | + if ($node !== null) { |
|
| 699 | + $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId()))); |
|
| 700 | + } |
|
| 701 | + |
|
| 702 | + $cursor = $qb->execute(); |
|
| 703 | + |
|
| 704 | + while($data = $cursor->fetch()) { |
|
| 705 | + if ($this->isAccessibleResult($data)) { |
|
| 706 | + $shares[] = $this->createShare($data); |
|
| 707 | + } |
|
| 708 | + } |
|
| 709 | + $cursor->closeCursor(); |
|
| 710 | + |
|
| 711 | + } else if ($shareType === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 712 | + $user = $this->userManager->get($userId); |
|
| 713 | + $allGroups = $this->groupManager->getUserGroups($user); |
|
| 714 | + |
|
| 715 | + /** @var Share[] $shares2 */ |
|
| 716 | + $shares2 = []; |
|
| 717 | + |
|
| 718 | + $start = 0; |
|
| 719 | + while(true) { |
|
| 720 | + $groups = array_slice($allGroups, $start, 100); |
|
| 721 | + $start += 100; |
|
| 722 | + |
|
| 723 | + if ($groups === []) { |
|
| 724 | + break; |
|
| 725 | + } |
|
| 726 | + |
|
| 727 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 728 | + $qb->select('s.*', |
|
| 729 | + 'f.fileid', 'f.path', 'f.permissions AS f_permissions', 'f.storage', 'f.path_hash', |
|
| 730 | + 'f.parent AS f_parent', 'f.name', 'f.mimetype', 'f.mimepart', 'f.size', 'f.mtime', 'f.storage_mtime', |
|
| 731 | + 'f.encrypted', 'f.unencrypted_size', 'f.etag', 'f.checksum' |
|
| 732 | + ) |
|
| 733 | + ->selectAlias('st.id', 'storage_string_id') |
|
| 734 | + ->from('share', 's') |
|
| 735 | + ->leftJoin('s', 'filecache', 'f', $qb->expr()->eq('s.file_source', 'f.fileid')) |
|
| 736 | + ->leftJoin('f', 'storages', 'st', $qb->expr()->eq('f.storage', 'st.numeric_id')) |
|
| 737 | + ->orderBy('s.id') |
|
| 738 | + ->setFirstResult(0); |
|
| 739 | + |
|
| 740 | + if ($limit !== -1) { |
|
| 741 | + $qb->setMaxResults($limit - count($shares)); |
|
| 742 | + } |
|
| 743 | + |
|
| 744 | + // Filter by node if provided |
|
| 745 | + if ($node !== null) { |
|
| 746 | + $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId()))); |
|
| 747 | + } |
|
| 748 | + |
|
| 749 | + |
|
| 750 | + $groups = array_filter($groups, function($group) { return $group instanceof IGroup; }); |
|
| 751 | + $groups = array_map(function(IGroup $group) { return $group->getGID(); }, $groups); |
|
| 752 | + |
|
| 753 | + $qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP))) |
|
| 754 | + ->andWhere($qb->expr()->in('share_with', $qb->createNamedParameter( |
|
| 755 | + $groups, |
|
| 756 | + IQueryBuilder::PARAM_STR_ARRAY |
|
| 757 | + ))) |
|
| 758 | + ->andWhere($qb->expr()->orX( |
|
| 759 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 760 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 761 | + )); |
|
| 762 | + |
|
| 763 | + $cursor = $qb->execute(); |
|
| 764 | + while($data = $cursor->fetch()) { |
|
| 765 | + if ($offset > 0) { |
|
| 766 | + $offset--; |
|
| 767 | + continue; |
|
| 768 | + } |
|
| 769 | + |
|
| 770 | + if ($this->isAccessibleResult($data)) { |
|
| 771 | + $shares2[] = $this->createShare($data); |
|
| 772 | + } |
|
| 773 | + } |
|
| 774 | + $cursor->closeCursor(); |
|
| 775 | + } |
|
| 776 | + |
|
| 777 | + /* |
|
| 778 | 778 | * Resolve all group shares to user specific shares |
| 779 | 779 | */ |
| 780 | - $shares = $this->resolveGroupShares($shares2, $userId); |
|
| 781 | - } else { |
|
| 782 | - throw new BackendError('Invalid backend'); |
|
| 783 | - } |
|
| 784 | - |
|
| 785 | - |
|
| 786 | - return $shares; |
|
| 787 | - } |
|
| 788 | - |
|
| 789 | - /** |
|
| 790 | - * Get a share by token |
|
| 791 | - * |
|
| 792 | - * @param string $token |
|
| 793 | - * @return \OCP\Share\IShare |
|
| 794 | - * @throws ShareNotFound |
|
| 795 | - */ |
|
| 796 | - public function getShareByToken($token) { |
|
| 797 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 798 | - |
|
| 799 | - $cursor = $qb->select('*') |
|
| 800 | - ->from('share') |
|
| 801 | - ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK))) |
|
| 802 | - ->andWhere($qb->expr()->eq('token', $qb->createNamedParameter($token))) |
|
| 803 | - ->andWhere($qb->expr()->orX( |
|
| 804 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 805 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 806 | - )) |
|
| 807 | - ->execute(); |
|
| 808 | - |
|
| 809 | - $data = $cursor->fetch(); |
|
| 810 | - |
|
| 811 | - if ($data === false) { |
|
| 812 | - throw new ShareNotFound(); |
|
| 813 | - } |
|
| 814 | - |
|
| 815 | - try { |
|
| 816 | - $share = $this->createShare($data); |
|
| 817 | - } catch (InvalidShare $e) { |
|
| 818 | - throw new ShareNotFound(); |
|
| 819 | - } |
|
| 820 | - |
|
| 821 | - return $share; |
|
| 822 | - } |
|
| 823 | - |
|
| 824 | - /** |
|
| 825 | - * Create a share object from an database row |
|
| 826 | - * |
|
| 827 | - * @param mixed[] $data |
|
| 828 | - * @return \OCP\Share\IShare |
|
| 829 | - * @throws InvalidShare |
|
| 830 | - */ |
|
| 831 | - private function createShare($data) { |
|
| 832 | - $share = new Share($this->rootFolder, $this->userManager); |
|
| 833 | - $share->setId((int)$data['id']) |
|
| 834 | - ->setShareType((int)$data['share_type']) |
|
| 835 | - ->setPermissions((int)$data['permissions']) |
|
| 836 | - ->setTarget($data['file_target']) |
|
| 837 | - ->setMailSend((bool)$data['mail_send']); |
|
| 838 | - |
|
| 839 | - $shareTime = new \DateTime(); |
|
| 840 | - $shareTime->setTimestamp((int)$data['stime']); |
|
| 841 | - $share->setShareTime($shareTime); |
|
| 842 | - |
|
| 843 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { |
|
| 844 | - $share->setSharedWith($data['share_with']); |
|
| 845 | - } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 846 | - $share->setSharedWith($data['share_with']); |
|
| 847 | - } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { |
|
| 848 | - $share->setPassword($data['password']); |
|
| 849 | - $share->setToken($data['token']); |
|
| 850 | - } |
|
| 851 | - |
|
| 852 | - $share->setSharedBy($data['uid_initiator']); |
|
| 853 | - $share->setShareOwner($data['uid_owner']); |
|
| 854 | - |
|
| 855 | - $share->setNodeId((int)$data['file_source']); |
|
| 856 | - $share->setNodeType($data['item_type']); |
|
| 857 | - |
|
| 858 | - if ($data['expiration'] !== null) { |
|
| 859 | - $expiration = \DateTime::createFromFormat('Y-m-d H:i:s', $data['expiration']); |
|
| 860 | - $share->setExpirationDate($expiration); |
|
| 861 | - } |
|
| 862 | - |
|
| 863 | - if (isset($data['f_permissions'])) { |
|
| 864 | - $entryData = $data; |
|
| 865 | - $entryData['permissions'] = $entryData['f_permissions']; |
|
| 866 | - $entryData['parent'] = $entryData['f_parent']; |
|
| 867 | - $share->setNodeCacheEntry(Cache::cacheEntryFromData($entryData, |
|
| 868 | - \OC::$server->getMimeTypeLoader())); |
|
| 869 | - } |
|
| 870 | - |
|
| 871 | - $share->setProviderId($this->identifier()); |
|
| 872 | - |
|
| 873 | - return $share; |
|
| 874 | - } |
|
| 875 | - |
|
| 876 | - /** |
|
| 877 | - * @param Share[] $shares |
|
| 878 | - * @param $userId |
|
| 879 | - * @return Share[] The updates shares if no update is found for a share return the original |
|
| 880 | - */ |
|
| 881 | - private function resolveGroupShares($shares, $userId) { |
|
| 882 | - $result = []; |
|
| 883 | - |
|
| 884 | - $start = 0; |
|
| 885 | - while(true) { |
|
| 886 | - /** @var Share[] $shareSlice */ |
|
| 887 | - $shareSlice = array_slice($shares, $start, 100); |
|
| 888 | - $start += 100; |
|
| 889 | - |
|
| 890 | - if ($shareSlice === []) { |
|
| 891 | - break; |
|
| 892 | - } |
|
| 893 | - |
|
| 894 | - /** @var int[] $ids */ |
|
| 895 | - $ids = []; |
|
| 896 | - /** @var Share[] $shareMap */ |
|
| 897 | - $shareMap = []; |
|
| 898 | - |
|
| 899 | - foreach ($shareSlice as $share) { |
|
| 900 | - $ids[] = (int)$share->getId(); |
|
| 901 | - $shareMap[$share->getId()] = $share; |
|
| 902 | - } |
|
| 903 | - |
|
| 904 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 905 | - |
|
| 906 | - $query = $qb->select('*') |
|
| 907 | - ->from('share') |
|
| 908 | - ->where($qb->expr()->in('parent', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY))) |
|
| 909 | - ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId))) |
|
| 910 | - ->andWhere($qb->expr()->orX( |
|
| 911 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 912 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 913 | - )); |
|
| 914 | - |
|
| 915 | - $stmt = $query->execute(); |
|
| 916 | - |
|
| 917 | - while($data = $stmt->fetch()) { |
|
| 918 | - $shareMap[$data['parent']]->setPermissions((int)$data['permissions']); |
|
| 919 | - $shareMap[$data['parent']]->setTarget($data['file_target']); |
|
| 920 | - } |
|
| 921 | - |
|
| 922 | - $stmt->closeCursor(); |
|
| 923 | - |
|
| 924 | - foreach ($shareMap as $share) { |
|
| 925 | - $result[] = $share; |
|
| 926 | - } |
|
| 927 | - } |
|
| 928 | - |
|
| 929 | - return $result; |
|
| 930 | - } |
|
| 931 | - |
|
| 932 | - /** |
|
| 933 | - * A user is deleted from the system |
|
| 934 | - * So clean up the relevant shares. |
|
| 935 | - * |
|
| 936 | - * @param string $uid |
|
| 937 | - * @param int $shareType |
|
| 938 | - */ |
|
| 939 | - public function userDeleted($uid, $shareType) { |
|
| 940 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 941 | - |
|
| 942 | - $qb->delete('share'); |
|
| 943 | - |
|
| 944 | - if ($shareType === \OCP\Share::SHARE_TYPE_USER) { |
|
| 945 | - /* |
|
| 780 | + $shares = $this->resolveGroupShares($shares2, $userId); |
|
| 781 | + } else { |
|
| 782 | + throw new BackendError('Invalid backend'); |
|
| 783 | + } |
|
| 784 | + |
|
| 785 | + |
|
| 786 | + return $shares; |
|
| 787 | + } |
|
| 788 | + |
|
| 789 | + /** |
|
| 790 | + * Get a share by token |
|
| 791 | + * |
|
| 792 | + * @param string $token |
|
| 793 | + * @return \OCP\Share\IShare |
|
| 794 | + * @throws ShareNotFound |
|
| 795 | + */ |
|
| 796 | + public function getShareByToken($token) { |
|
| 797 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 798 | + |
|
| 799 | + $cursor = $qb->select('*') |
|
| 800 | + ->from('share') |
|
| 801 | + ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK))) |
|
| 802 | + ->andWhere($qb->expr()->eq('token', $qb->createNamedParameter($token))) |
|
| 803 | + ->andWhere($qb->expr()->orX( |
|
| 804 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 805 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 806 | + )) |
|
| 807 | + ->execute(); |
|
| 808 | + |
|
| 809 | + $data = $cursor->fetch(); |
|
| 810 | + |
|
| 811 | + if ($data === false) { |
|
| 812 | + throw new ShareNotFound(); |
|
| 813 | + } |
|
| 814 | + |
|
| 815 | + try { |
|
| 816 | + $share = $this->createShare($data); |
|
| 817 | + } catch (InvalidShare $e) { |
|
| 818 | + throw new ShareNotFound(); |
|
| 819 | + } |
|
| 820 | + |
|
| 821 | + return $share; |
|
| 822 | + } |
|
| 823 | + |
|
| 824 | + /** |
|
| 825 | + * Create a share object from an database row |
|
| 826 | + * |
|
| 827 | + * @param mixed[] $data |
|
| 828 | + * @return \OCP\Share\IShare |
|
| 829 | + * @throws InvalidShare |
|
| 830 | + */ |
|
| 831 | + private function createShare($data) { |
|
| 832 | + $share = new Share($this->rootFolder, $this->userManager); |
|
| 833 | + $share->setId((int)$data['id']) |
|
| 834 | + ->setShareType((int)$data['share_type']) |
|
| 835 | + ->setPermissions((int)$data['permissions']) |
|
| 836 | + ->setTarget($data['file_target']) |
|
| 837 | + ->setMailSend((bool)$data['mail_send']); |
|
| 838 | + |
|
| 839 | + $shareTime = new \DateTime(); |
|
| 840 | + $shareTime->setTimestamp((int)$data['stime']); |
|
| 841 | + $share->setShareTime($shareTime); |
|
| 842 | + |
|
| 843 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { |
|
| 844 | + $share->setSharedWith($data['share_with']); |
|
| 845 | + } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 846 | + $share->setSharedWith($data['share_with']); |
|
| 847 | + } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { |
|
| 848 | + $share->setPassword($data['password']); |
|
| 849 | + $share->setToken($data['token']); |
|
| 850 | + } |
|
| 851 | + |
|
| 852 | + $share->setSharedBy($data['uid_initiator']); |
|
| 853 | + $share->setShareOwner($data['uid_owner']); |
|
| 854 | + |
|
| 855 | + $share->setNodeId((int)$data['file_source']); |
|
| 856 | + $share->setNodeType($data['item_type']); |
|
| 857 | + |
|
| 858 | + if ($data['expiration'] !== null) { |
|
| 859 | + $expiration = \DateTime::createFromFormat('Y-m-d H:i:s', $data['expiration']); |
|
| 860 | + $share->setExpirationDate($expiration); |
|
| 861 | + } |
|
| 862 | + |
|
| 863 | + if (isset($data['f_permissions'])) { |
|
| 864 | + $entryData = $data; |
|
| 865 | + $entryData['permissions'] = $entryData['f_permissions']; |
|
| 866 | + $entryData['parent'] = $entryData['f_parent']; |
|
| 867 | + $share->setNodeCacheEntry(Cache::cacheEntryFromData($entryData, |
|
| 868 | + \OC::$server->getMimeTypeLoader())); |
|
| 869 | + } |
|
| 870 | + |
|
| 871 | + $share->setProviderId($this->identifier()); |
|
| 872 | + |
|
| 873 | + return $share; |
|
| 874 | + } |
|
| 875 | + |
|
| 876 | + /** |
|
| 877 | + * @param Share[] $shares |
|
| 878 | + * @param $userId |
|
| 879 | + * @return Share[] The updates shares if no update is found for a share return the original |
|
| 880 | + */ |
|
| 881 | + private function resolveGroupShares($shares, $userId) { |
|
| 882 | + $result = []; |
|
| 883 | + |
|
| 884 | + $start = 0; |
|
| 885 | + while(true) { |
|
| 886 | + /** @var Share[] $shareSlice */ |
|
| 887 | + $shareSlice = array_slice($shares, $start, 100); |
|
| 888 | + $start += 100; |
|
| 889 | + |
|
| 890 | + if ($shareSlice === []) { |
|
| 891 | + break; |
|
| 892 | + } |
|
| 893 | + |
|
| 894 | + /** @var int[] $ids */ |
|
| 895 | + $ids = []; |
|
| 896 | + /** @var Share[] $shareMap */ |
|
| 897 | + $shareMap = []; |
|
| 898 | + |
|
| 899 | + foreach ($shareSlice as $share) { |
|
| 900 | + $ids[] = (int)$share->getId(); |
|
| 901 | + $shareMap[$share->getId()] = $share; |
|
| 902 | + } |
|
| 903 | + |
|
| 904 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 905 | + |
|
| 906 | + $query = $qb->select('*') |
|
| 907 | + ->from('share') |
|
| 908 | + ->where($qb->expr()->in('parent', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY))) |
|
| 909 | + ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId))) |
|
| 910 | + ->andWhere($qb->expr()->orX( |
|
| 911 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 912 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 913 | + )); |
|
| 914 | + |
|
| 915 | + $stmt = $query->execute(); |
|
| 916 | + |
|
| 917 | + while($data = $stmt->fetch()) { |
|
| 918 | + $shareMap[$data['parent']]->setPermissions((int)$data['permissions']); |
|
| 919 | + $shareMap[$data['parent']]->setTarget($data['file_target']); |
|
| 920 | + } |
|
| 921 | + |
|
| 922 | + $stmt->closeCursor(); |
|
| 923 | + |
|
| 924 | + foreach ($shareMap as $share) { |
|
| 925 | + $result[] = $share; |
|
| 926 | + } |
|
| 927 | + } |
|
| 928 | + |
|
| 929 | + return $result; |
|
| 930 | + } |
|
| 931 | + |
|
| 932 | + /** |
|
| 933 | + * A user is deleted from the system |
|
| 934 | + * So clean up the relevant shares. |
|
| 935 | + * |
|
| 936 | + * @param string $uid |
|
| 937 | + * @param int $shareType |
|
| 938 | + */ |
|
| 939 | + public function userDeleted($uid, $shareType) { |
|
| 940 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 941 | + |
|
| 942 | + $qb->delete('share'); |
|
| 943 | + |
|
| 944 | + if ($shareType === \OCP\Share::SHARE_TYPE_USER) { |
|
| 945 | + /* |
|
| 946 | 946 | * Delete all user shares that are owned by this user |
| 947 | 947 | * or that are received by this user |
| 948 | 948 | */ |
| 949 | 949 | |
| 950 | - $qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER))); |
|
| 950 | + $qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER))); |
|
| 951 | 951 | |
| 952 | - $qb->andWhere( |
|
| 953 | - $qb->expr()->orX( |
|
| 954 | - $qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid)), |
|
| 955 | - $qb->expr()->eq('share_with', $qb->createNamedParameter($uid)) |
|
| 956 | - ) |
|
| 957 | - ); |
|
| 958 | - } else if ($shareType === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 959 | - /* |
|
| 952 | + $qb->andWhere( |
|
| 953 | + $qb->expr()->orX( |
|
| 954 | + $qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid)), |
|
| 955 | + $qb->expr()->eq('share_with', $qb->createNamedParameter($uid)) |
|
| 956 | + ) |
|
| 957 | + ); |
|
| 958 | + } else if ($shareType === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 959 | + /* |
|
| 960 | 960 | * Delete all group shares that are owned by this user |
| 961 | 961 | * Or special user group shares that are received by this user |
| 962 | 962 | */ |
| 963 | - $qb->where( |
|
| 964 | - $qb->expr()->andX( |
|
| 965 | - $qb->expr()->orX( |
|
| 966 | - $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)), |
|
| 967 | - $qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)) |
|
| 968 | - ), |
|
| 969 | - $qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid)) |
|
| 970 | - ) |
|
| 971 | - ); |
|
| 972 | - |
|
| 973 | - $qb->orWhere( |
|
| 974 | - $qb->expr()->andX( |
|
| 975 | - $qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)), |
|
| 976 | - $qb->expr()->eq('share_with', $qb->createNamedParameter($uid)) |
|
| 977 | - ) |
|
| 978 | - ); |
|
| 979 | - } else if ($shareType === \OCP\Share::SHARE_TYPE_LINK) { |
|
| 980 | - /* |
|
| 963 | + $qb->where( |
|
| 964 | + $qb->expr()->andX( |
|
| 965 | + $qb->expr()->orX( |
|
| 966 | + $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)), |
|
| 967 | + $qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)) |
|
| 968 | + ), |
|
| 969 | + $qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid)) |
|
| 970 | + ) |
|
| 971 | + ); |
|
| 972 | + |
|
| 973 | + $qb->orWhere( |
|
| 974 | + $qb->expr()->andX( |
|
| 975 | + $qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)), |
|
| 976 | + $qb->expr()->eq('share_with', $qb->createNamedParameter($uid)) |
|
| 977 | + ) |
|
| 978 | + ); |
|
| 979 | + } else if ($shareType === \OCP\Share::SHARE_TYPE_LINK) { |
|
| 980 | + /* |
|
| 981 | 981 | * Delete all link shares owned by this user. |
| 982 | 982 | * And all link shares initiated by this user (until #22327 is in) |
| 983 | 983 | */ |
| 984 | - $qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK))); |
|
| 985 | - |
|
| 986 | - $qb->andWhere( |
|
| 987 | - $qb->expr()->orX( |
|
| 988 | - $qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid)), |
|
| 989 | - $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($uid)) |
|
| 990 | - ) |
|
| 991 | - ); |
|
| 992 | - } |
|
| 993 | - |
|
| 994 | - $qb->execute(); |
|
| 995 | - } |
|
| 996 | - |
|
| 997 | - /** |
|
| 998 | - * Delete all shares received by this group. As well as any custom group |
|
| 999 | - * shares for group members. |
|
| 1000 | - * |
|
| 1001 | - * @param string $gid |
|
| 1002 | - */ |
|
| 1003 | - public function groupDeleted($gid) { |
|
| 1004 | - /* |
|
| 984 | + $qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK))); |
|
| 985 | + |
|
| 986 | + $qb->andWhere( |
|
| 987 | + $qb->expr()->orX( |
|
| 988 | + $qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid)), |
|
| 989 | + $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($uid)) |
|
| 990 | + ) |
|
| 991 | + ); |
|
| 992 | + } |
|
| 993 | + |
|
| 994 | + $qb->execute(); |
|
| 995 | + } |
|
| 996 | + |
|
| 997 | + /** |
|
| 998 | + * Delete all shares received by this group. As well as any custom group |
|
| 999 | + * shares for group members. |
|
| 1000 | + * |
|
| 1001 | + * @param string $gid |
|
| 1002 | + */ |
|
| 1003 | + public function groupDeleted($gid) { |
|
| 1004 | + /* |
|
| 1005 | 1005 | * First delete all custom group shares for group members |
| 1006 | 1006 | */ |
| 1007 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 1008 | - $qb->select('id') |
|
| 1009 | - ->from('share') |
|
| 1010 | - ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP))) |
|
| 1011 | - ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($gid))); |
|
| 1012 | - |
|
| 1013 | - $cursor = $qb->execute(); |
|
| 1014 | - $ids = []; |
|
| 1015 | - while($row = $cursor->fetch()) { |
|
| 1016 | - $ids[] = (int)$row['id']; |
|
| 1017 | - } |
|
| 1018 | - $cursor->closeCursor(); |
|
| 1019 | - |
|
| 1020 | - if (!empty($ids)) { |
|
| 1021 | - $chunks = array_chunk($ids, 100); |
|
| 1022 | - foreach ($chunks as $chunk) { |
|
| 1023 | - $qb->delete('share') |
|
| 1024 | - ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP))) |
|
| 1025 | - ->andWhere($qb->expr()->in('parent', $qb->createNamedParameter($chunk, IQueryBuilder::PARAM_INT_ARRAY))); |
|
| 1026 | - $qb->execute(); |
|
| 1027 | - } |
|
| 1028 | - } |
|
| 1029 | - |
|
| 1030 | - /* |
|
| 1007 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 1008 | + $qb->select('id') |
|
| 1009 | + ->from('share') |
|
| 1010 | + ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP))) |
|
| 1011 | + ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($gid))); |
|
| 1012 | + |
|
| 1013 | + $cursor = $qb->execute(); |
|
| 1014 | + $ids = []; |
|
| 1015 | + while($row = $cursor->fetch()) { |
|
| 1016 | + $ids[] = (int)$row['id']; |
|
| 1017 | + } |
|
| 1018 | + $cursor->closeCursor(); |
|
| 1019 | + |
|
| 1020 | + if (!empty($ids)) { |
|
| 1021 | + $chunks = array_chunk($ids, 100); |
|
| 1022 | + foreach ($chunks as $chunk) { |
|
| 1023 | + $qb->delete('share') |
|
| 1024 | + ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP))) |
|
| 1025 | + ->andWhere($qb->expr()->in('parent', $qb->createNamedParameter($chunk, IQueryBuilder::PARAM_INT_ARRAY))); |
|
| 1026 | + $qb->execute(); |
|
| 1027 | + } |
|
| 1028 | + } |
|
| 1029 | + |
|
| 1030 | + /* |
|
| 1031 | 1031 | * Now delete all the group shares |
| 1032 | 1032 | */ |
| 1033 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 1034 | - $qb->delete('share') |
|
| 1035 | - ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP))) |
|
| 1036 | - ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($gid))); |
|
| 1037 | - $qb->execute(); |
|
| 1038 | - } |
|
| 1039 | - |
|
| 1040 | - /** |
|
| 1041 | - * Delete custom group shares to this group for this user |
|
| 1042 | - * |
|
| 1043 | - * @param string $uid |
|
| 1044 | - * @param string $gid |
|
| 1045 | - */ |
|
| 1046 | - public function userDeletedFromGroup($uid, $gid) { |
|
| 1047 | - /* |
|
| 1033 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 1034 | + $qb->delete('share') |
|
| 1035 | + ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP))) |
|
| 1036 | + ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($gid))); |
|
| 1037 | + $qb->execute(); |
|
| 1038 | + } |
|
| 1039 | + |
|
| 1040 | + /** |
|
| 1041 | + * Delete custom group shares to this group for this user |
|
| 1042 | + * |
|
| 1043 | + * @param string $uid |
|
| 1044 | + * @param string $gid |
|
| 1045 | + */ |
|
| 1046 | + public function userDeletedFromGroup($uid, $gid) { |
|
| 1047 | + /* |
|
| 1048 | 1048 | * Get all group shares |
| 1049 | 1049 | */ |
| 1050 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 1051 | - $qb->select('id') |
|
| 1052 | - ->from('share') |
|
| 1053 | - ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP))) |
|
| 1054 | - ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($gid))); |
|
| 1055 | - |
|
| 1056 | - $cursor = $qb->execute(); |
|
| 1057 | - $ids = []; |
|
| 1058 | - while($row = $cursor->fetch()) { |
|
| 1059 | - $ids[] = (int)$row['id']; |
|
| 1060 | - } |
|
| 1061 | - $cursor->closeCursor(); |
|
| 1062 | - |
|
| 1063 | - if (!empty($ids)) { |
|
| 1064 | - $chunks = array_chunk($ids, 100); |
|
| 1065 | - foreach ($chunks as $chunk) { |
|
| 1066 | - /* |
|
| 1050 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 1051 | + $qb->select('id') |
|
| 1052 | + ->from('share') |
|
| 1053 | + ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP))) |
|
| 1054 | + ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($gid))); |
|
| 1055 | + |
|
| 1056 | + $cursor = $qb->execute(); |
|
| 1057 | + $ids = []; |
|
| 1058 | + while($row = $cursor->fetch()) { |
|
| 1059 | + $ids[] = (int)$row['id']; |
|
| 1060 | + } |
|
| 1061 | + $cursor->closeCursor(); |
|
| 1062 | + |
|
| 1063 | + if (!empty($ids)) { |
|
| 1064 | + $chunks = array_chunk($ids, 100); |
|
| 1065 | + foreach ($chunks as $chunk) { |
|
| 1066 | + /* |
|
| 1067 | 1067 | * Delete all special shares wit this users for the found group shares |
| 1068 | 1068 | */ |
| 1069 | - $qb->delete('share') |
|
| 1070 | - ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP))) |
|
| 1071 | - ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($uid))) |
|
| 1072 | - ->andWhere($qb->expr()->in('parent', $qb->createNamedParameter($chunk, IQueryBuilder::PARAM_INT_ARRAY))); |
|
| 1073 | - $qb->execute(); |
|
| 1074 | - } |
|
| 1075 | - } |
|
| 1076 | - } |
|
| 1077 | - |
|
| 1078 | - /** |
|
| 1079 | - * @inheritdoc |
|
| 1080 | - */ |
|
| 1081 | - public function getAccessList($nodes, $currentAccess) { |
|
| 1082 | - $ids = []; |
|
| 1083 | - foreach ($nodes as $node) { |
|
| 1084 | - $ids[] = $node->getId(); |
|
| 1085 | - } |
|
| 1086 | - |
|
| 1087 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 1088 | - |
|
| 1089 | - $or = $qb->expr()->orX( |
|
| 1090 | - $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)), |
|
| 1091 | - $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)), |
|
| 1092 | - $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK)) |
|
| 1093 | - ); |
|
| 1094 | - |
|
| 1095 | - if ($currentAccess) { |
|
| 1096 | - $or->add($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP))); |
|
| 1097 | - } |
|
| 1098 | - |
|
| 1099 | - $qb->select('id', 'parent', 'share_type', 'share_with', 'file_source', 'file_target', 'permissions') |
|
| 1100 | - ->from('share') |
|
| 1101 | - ->where( |
|
| 1102 | - $or |
|
| 1103 | - ) |
|
| 1104 | - ->andWhere($qb->expr()->in('file_source', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY))) |
|
| 1105 | - ->andWhere($qb->expr()->orX( |
|
| 1106 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 1107 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 1108 | - )); |
|
| 1109 | - $cursor = $qb->execute(); |
|
| 1110 | - |
|
| 1111 | - $users = []; |
|
| 1112 | - $link = false; |
|
| 1113 | - while($row = $cursor->fetch()) { |
|
| 1114 | - $type = (int)$row['share_type']; |
|
| 1115 | - if ($type === \OCP\Share::SHARE_TYPE_USER) { |
|
| 1116 | - $uid = $row['share_with']; |
|
| 1117 | - $users[$uid] = isset($users[$uid]) ? $users[$uid] : []; |
|
| 1118 | - $users[$uid][$row['id']] = $row; |
|
| 1119 | - } else if ($type === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 1120 | - $gid = $row['share_with']; |
|
| 1121 | - $group = $this->groupManager->get($gid); |
|
| 1122 | - |
|
| 1123 | - if ($group === null) { |
|
| 1124 | - continue; |
|
| 1125 | - } |
|
| 1126 | - |
|
| 1127 | - $userList = $group->getUsers(); |
|
| 1128 | - foreach ($userList as $user) { |
|
| 1129 | - $uid = $user->getUID(); |
|
| 1130 | - $users[$uid] = isset($users[$uid]) ? $users[$uid] : []; |
|
| 1131 | - $users[$uid][$row['id']] = $row; |
|
| 1132 | - } |
|
| 1133 | - } else if ($type === \OCP\Share::SHARE_TYPE_LINK) { |
|
| 1134 | - $link = true; |
|
| 1135 | - } else if ($type === self::SHARE_TYPE_USERGROUP && $currentAccess === true) { |
|
| 1136 | - $uid = $row['share_with']; |
|
| 1137 | - $users[$uid] = isset($users[$uid]) ? $users[$uid] : []; |
|
| 1138 | - $users[$uid][$row['id']] = $row; |
|
| 1139 | - } |
|
| 1140 | - } |
|
| 1141 | - $cursor->closeCursor(); |
|
| 1142 | - |
|
| 1143 | - if ($currentAccess === true) { |
|
| 1144 | - $users = array_map([$this, 'filterSharesOfUser'], $users); |
|
| 1145 | - $users = array_filter($users); |
|
| 1146 | - } else { |
|
| 1147 | - $users = array_keys($users); |
|
| 1148 | - } |
|
| 1149 | - |
|
| 1150 | - return ['users' => $users, 'public' => $link]; |
|
| 1151 | - } |
|
| 1152 | - |
|
| 1153 | - /** |
|
| 1154 | - * For each user the path with the fewest slashes is returned |
|
| 1155 | - * @param array $shares |
|
| 1156 | - * @return array |
|
| 1157 | - */ |
|
| 1158 | - protected function filterSharesOfUser(array $shares) { |
|
| 1159 | - // Group shares when the user has a share exception |
|
| 1160 | - foreach ($shares as $id => $share) { |
|
| 1161 | - $type = (int) $share['share_type']; |
|
| 1162 | - $permissions = (int) $share['permissions']; |
|
| 1163 | - |
|
| 1164 | - if ($type === self::SHARE_TYPE_USERGROUP) { |
|
| 1165 | - unset($shares[$share['parent']]); |
|
| 1166 | - |
|
| 1167 | - if ($permissions === 0) { |
|
| 1168 | - unset($shares[$id]); |
|
| 1169 | - } |
|
| 1170 | - } |
|
| 1171 | - } |
|
| 1172 | - |
|
| 1173 | - $best = []; |
|
| 1174 | - $bestDepth = 0; |
|
| 1175 | - foreach ($shares as $id => $share) { |
|
| 1176 | - $depth = substr_count($share['file_target'], '/'); |
|
| 1177 | - if (empty($best) || $depth < $bestDepth) { |
|
| 1178 | - $bestDepth = $depth; |
|
| 1179 | - $best = [ |
|
| 1180 | - 'node_id' => $share['file_source'], |
|
| 1181 | - 'node_path' => $share['file_target'], |
|
| 1182 | - ]; |
|
| 1183 | - } |
|
| 1184 | - } |
|
| 1185 | - |
|
| 1186 | - return $best; |
|
| 1187 | - } |
|
| 1069 | + $qb->delete('share') |
|
| 1070 | + ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP))) |
|
| 1071 | + ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($uid))) |
|
| 1072 | + ->andWhere($qb->expr()->in('parent', $qb->createNamedParameter($chunk, IQueryBuilder::PARAM_INT_ARRAY))); |
|
| 1073 | + $qb->execute(); |
|
| 1074 | + } |
|
| 1075 | + } |
|
| 1076 | + } |
|
| 1077 | + |
|
| 1078 | + /** |
|
| 1079 | + * @inheritdoc |
|
| 1080 | + */ |
|
| 1081 | + public function getAccessList($nodes, $currentAccess) { |
|
| 1082 | + $ids = []; |
|
| 1083 | + foreach ($nodes as $node) { |
|
| 1084 | + $ids[] = $node->getId(); |
|
| 1085 | + } |
|
| 1086 | + |
|
| 1087 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 1088 | + |
|
| 1089 | + $or = $qb->expr()->orX( |
|
| 1090 | + $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)), |
|
| 1091 | + $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)), |
|
| 1092 | + $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK)) |
|
| 1093 | + ); |
|
| 1094 | + |
|
| 1095 | + if ($currentAccess) { |
|
| 1096 | + $or->add($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP))); |
|
| 1097 | + } |
|
| 1098 | + |
|
| 1099 | + $qb->select('id', 'parent', 'share_type', 'share_with', 'file_source', 'file_target', 'permissions') |
|
| 1100 | + ->from('share') |
|
| 1101 | + ->where( |
|
| 1102 | + $or |
|
| 1103 | + ) |
|
| 1104 | + ->andWhere($qb->expr()->in('file_source', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY))) |
|
| 1105 | + ->andWhere($qb->expr()->orX( |
|
| 1106 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 1107 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 1108 | + )); |
|
| 1109 | + $cursor = $qb->execute(); |
|
| 1110 | + |
|
| 1111 | + $users = []; |
|
| 1112 | + $link = false; |
|
| 1113 | + while($row = $cursor->fetch()) { |
|
| 1114 | + $type = (int)$row['share_type']; |
|
| 1115 | + if ($type === \OCP\Share::SHARE_TYPE_USER) { |
|
| 1116 | + $uid = $row['share_with']; |
|
| 1117 | + $users[$uid] = isset($users[$uid]) ? $users[$uid] : []; |
|
| 1118 | + $users[$uid][$row['id']] = $row; |
|
| 1119 | + } else if ($type === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 1120 | + $gid = $row['share_with']; |
|
| 1121 | + $group = $this->groupManager->get($gid); |
|
| 1122 | + |
|
| 1123 | + if ($group === null) { |
|
| 1124 | + continue; |
|
| 1125 | + } |
|
| 1126 | + |
|
| 1127 | + $userList = $group->getUsers(); |
|
| 1128 | + foreach ($userList as $user) { |
|
| 1129 | + $uid = $user->getUID(); |
|
| 1130 | + $users[$uid] = isset($users[$uid]) ? $users[$uid] : []; |
|
| 1131 | + $users[$uid][$row['id']] = $row; |
|
| 1132 | + } |
|
| 1133 | + } else if ($type === \OCP\Share::SHARE_TYPE_LINK) { |
|
| 1134 | + $link = true; |
|
| 1135 | + } else if ($type === self::SHARE_TYPE_USERGROUP && $currentAccess === true) { |
|
| 1136 | + $uid = $row['share_with']; |
|
| 1137 | + $users[$uid] = isset($users[$uid]) ? $users[$uid] : []; |
|
| 1138 | + $users[$uid][$row['id']] = $row; |
|
| 1139 | + } |
|
| 1140 | + } |
|
| 1141 | + $cursor->closeCursor(); |
|
| 1142 | + |
|
| 1143 | + if ($currentAccess === true) { |
|
| 1144 | + $users = array_map([$this, 'filterSharesOfUser'], $users); |
|
| 1145 | + $users = array_filter($users); |
|
| 1146 | + } else { |
|
| 1147 | + $users = array_keys($users); |
|
| 1148 | + } |
|
| 1149 | + |
|
| 1150 | + return ['users' => $users, 'public' => $link]; |
|
| 1151 | + } |
|
| 1152 | + |
|
| 1153 | + /** |
|
| 1154 | + * For each user the path with the fewest slashes is returned |
|
| 1155 | + * @param array $shares |
|
| 1156 | + * @return array |
|
| 1157 | + */ |
|
| 1158 | + protected function filterSharesOfUser(array $shares) { |
|
| 1159 | + // Group shares when the user has a share exception |
|
| 1160 | + foreach ($shares as $id => $share) { |
|
| 1161 | + $type = (int) $share['share_type']; |
|
| 1162 | + $permissions = (int) $share['permissions']; |
|
| 1163 | + |
|
| 1164 | + if ($type === self::SHARE_TYPE_USERGROUP) { |
|
| 1165 | + unset($shares[$share['parent']]); |
|
| 1166 | + |
|
| 1167 | + if ($permissions === 0) { |
|
| 1168 | + unset($shares[$id]); |
|
| 1169 | + } |
|
| 1170 | + } |
|
| 1171 | + } |
|
| 1172 | + |
|
| 1173 | + $best = []; |
|
| 1174 | + $bestDepth = 0; |
|
| 1175 | + foreach ($shares as $id => $share) { |
|
| 1176 | + $depth = substr_count($share['file_target'], '/'); |
|
| 1177 | + if (empty($best) || $depth < $bestDepth) { |
|
| 1178 | + $bestDepth = $depth; |
|
| 1179 | + $best = [ |
|
| 1180 | + 'node_id' => $share['file_source'], |
|
| 1181 | + 'node_path' => $share['file_target'], |
|
| 1182 | + ]; |
|
| 1183 | + } |
|
| 1184 | + } |
|
| 1185 | + |
|
| 1186 | + return $best; |
|
| 1187 | + } |
|
| 1188 | 1188 | } |