You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Manager.php 39KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Bjoern Schiessle <bjoern@schiessle.org>
  7. * @author Björn Schießle <bjoern@schiessle.org>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Vincent Petry <pvince81@owncloud.com>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OC\Share20;
  28. use OC\Cache\CappedMemoryCache;
  29. use OC\Files\Mount\MoveableMount;
  30. use OC\HintException;
  31. use OC\Share20\Exception\ProviderException;
  32. use OCP\Files\File;
  33. use OCP\Files\Folder;
  34. use OCP\Files\IRootFolder;
  35. use OCP\Files\Mount\IMountManager;
  36. use OCP\Files\Node;
  37. use OCP\Files\NotFoundException;
  38. use OCP\IConfig;
  39. use OCP\IGroupManager;
  40. use OCP\IL10N;
  41. use OCP\ILogger;
  42. use OCP\IUserManager;
  43. use OCP\Security\IHasher;
  44. use OCP\Security\ISecureRandom;
  45. use OCP\Share\Exceptions\GenericShareException;
  46. use OCP\Share\Exceptions\ShareNotFound;
  47. use OCP\Share\IManager;
  48. use OCP\Share\IProviderFactory;
  49. use Symfony\Component\EventDispatcher\EventDispatcher;
  50. use Symfony\Component\EventDispatcher\GenericEvent;
  51. use OCP\Share\IShareProvider;
  52. /**
  53. * This class is the communication hub for all sharing related operations.
  54. */
  55. class Manager implements IManager {
  56. /** @var IProviderFactory */
  57. private $factory;
  58. /** @var ILogger */
  59. private $logger;
  60. /** @var IConfig */
  61. private $config;
  62. /** @var ISecureRandom */
  63. private $secureRandom;
  64. /** @var IHasher */
  65. private $hasher;
  66. /** @var IMountManager */
  67. private $mountManager;
  68. /** @var IGroupManager */
  69. private $groupManager;
  70. /** @var IL10N */
  71. private $l;
  72. /** @var IUserManager */
  73. private $userManager;
  74. /** @var IRootFolder */
  75. private $rootFolder;
  76. /** @var CappedMemoryCache */
  77. private $sharingDisabledForUsersCache;
  78. /** @var EventDispatcher */
  79. private $eventDispatcher;
  80. /**
  81. * Manager constructor.
  82. *
  83. * @param ILogger $logger
  84. * @param IConfig $config
  85. * @param ISecureRandom $secureRandom
  86. * @param IHasher $hasher
  87. * @param IMountManager $mountManager
  88. * @param IGroupManager $groupManager
  89. * @param IL10N $l
  90. * @param IProviderFactory $factory
  91. * @param IUserManager $userManager
  92. * @param IRootFolder $rootFolder
  93. * @param EventDispatcher $eventDispatcher
  94. */
  95. public function __construct(
  96. ILogger $logger,
  97. IConfig $config,
  98. ISecureRandom $secureRandom,
  99. IHasher $hasher,
  100. IMountManager $mountManager,
  101. IGroupManager $groupManager,
  102. IL10N $l,
  103. IProviderFactory $factory,
  104. IUserManager $userManager,
  105. IRootFolder $rootFolder,
  106. EventDispatcher $eventDispatcher
  107. ) {
  108. $this->logger = $logger;
  109. $this->config = $config;
  110. $this->secureRandom = $secureRandom;
  111. $this->hasher = $hasher;
  112. $this->mountManager = $mountManager;
  113. $this->groupManager = $groupManager;
  114. $this->l = $l;
  115. $this->factory = $factory;
  116. $this->userManager = $userManager;
  117. $this->rootFolder = $rootFolder;
  118. $this->eventDispatcher = $eventDispatcher;
  119. $this->sharingDisabledForUsersCache = new CappedMemoryCache();
  120. }
  121. /**
  122. * Convert from a full share id to a tuple (providerId, shareId)
  123. *
  124. * @param string $id
  125. * @return string[]
  126. */
  127. private function splitFullId($id) {
  128. return explode(':', $id, 2);
  129. }
  130. /**
  131. * Verify if a password meets all requirements
  132. *
  133. * @param string $password
  134. * @throws \Exception
  135. */
  136. protected function verifyPassword($password) {
  137. if ($password === null) {
  138. // No password is set, check if this is allowed.
  139. if ($this->shareApiLinkEnforcePassword()) {
  140. throw new \InvalidArgumentException('Passwords are enforced for link shares');
  141. }
  142. return;
  143. }
  144. // Let others verify the password
  145. try {
  146. $event = new GenericEvent($password);
  147. $this->eventDispatcher->dispatch('OCP\PasswordPolicy::validate', $event);
  148. } catch (HintException $e) {
  149. throw new \Exception($e->getHint());
  150. }
  151. }
  152. /**
  153. * Check for generic requirements before creating a share
  154. *
  155. * @param \OCP\Share\IShare $share
  156. * @throws \InvalidArgumentException
  157. * @throws GenericShareException
  158. */
  159. protected function generalCreateChecks(\OCP\Share\IShare $share) {
  160. if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
  161. // We expect a valid user as sharedWith for user shares
  162. if (!$this->userManager->userExists($share->getSharedWith())) {
  163. throw new \InvalidArgumentException('SharedWith is not a valid user');
  164. }
  165. } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
  166. // We expect a valid group as sharedWith for group shares
  167. if (!$this->groupManager->groupExists($share->getSharedWith())) {
  168. throw new \InvalidArgumentException('SharedWith is not a valid group');
  169. }
  170. } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
  171. if ($share->getSharedWith() !== null) {
  172. throw new \InvalidArgumentException('SharedWith should be empty');
  173. }
  174. } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_REMOTE) {
  175. if ($share->getSharedWith() === null) {
  176. throw new \InvalidArgumentException('SharedWith should not be empty');
  177. }
  178. } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) {
  179. if ($share->getSharedWith() === null) {
  180. throw new \InvalidArgumentException('SharedWith should not be empty');
  181. }
  182. } else {
  183. // We can't handle other types yet
  184. throw new \InvalidArgumentException('unkown share type');
  185. }
  186. // Verify the initiator of the share is set
  187. if ($share->getSharedBy() === null) {
  188. throw new \InvalidArgumentException('SharedBy should be set');
  189. }
  190. // Cannot share with yourself
  191. if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER &&
  192. $share->getSharedWith() === $share->getSharedBy()) {
  193. throw new \InvalidArgumentException('Can\'t share with yourself');
  194. }
  195. // The path should be set
  196. if ($share->getNode() === null) {
  197. throw new \InvalidArgumentException('Path should be set');
  198. }
  199. // And it should be a file or a folder
  200. if (!($share->getNode() instanceof \OCP\Files\File) &&
  201. !($share->getNode() instanceof \OCP\Files\Folder)) {
  202. throw new \InvalidArgumentException('Path should be either a file or a folder');
  203. }
  204. // And you can't share your rootfolder
  205. if ($this->userManager->userExists($share->getSharedBy())) {
  206. $sharedPath = $this->rootFolder->getUserFolder($share->getSharedBy())->getPath();
  207. } else {
  208. $sharedPath = $this->rootFolder->getUserFolder($share->getShareOwner())->getPath();
  209. }
  210. if ($sharedPath === $share->getNode()->getPath()) {
  211. throw new \InvalidArgumentException('You can\'t share your root folder');
  212. }
  213. // Check if we actually have share permissions
  214. if (!$share->getNode()->isShareable()) {
  215. $message_t = $this->l->t('You are not allowed to share %s', [$share->getNode()->getPath()]);
  216. throw new GenericShareException($message_t, $message_t, 404);
  217. }
  218. // Permissions should be set
  219. if ($share->getPermissions() === null) {
  220. throw new \InvalidArgumentException('A share requires permissions');
  221. }
  222. /*
  223. * Quick fix for #23536
  224. * Non moveable mount points do not have update and delete permissions
  225. * while we 'most likely' do have that on the storage.
  226. */
  227. $permissions = $share->getNode()->getPermissions();
  228. $mount = $share->getNode()->getMountPoint();
  229. if (!($mount instanceof MoveableMount)) {
  230. $permissions |= \OCP\Constants::PERMISSION_DELETE | \OCP\Constants::PERMISSION_UPDATE;
  231. }
  232. // Check that we do not share with more permissions than we have
  233. if ($share->getPermissions() & ~$permissions) {
  234. $message_t = $this->l->t('Cannot increase permissions of %s', [$share->getNode()->getPath()]);
  235. throw new GenericShareException($message_t, $message_t, 404);
  236. }
  237. // Check that read permissions are always set
  238. // Link shares are allowed to have no read permissions to allow upload to hidden folders
  239. if ($share->getShareType() !== \OCP\Share::SHARE_TYPE_LINK &&
  240. ($share->getPermissions() & \OCP\Constants::PERMISSION_READ) === 0) {
  241. throw new \InvalidArgumentException('Shares need at least read permissions');
  242. }
  243. if ($share->getNode() instanceof \OCP\Files\File) {
  244. if ($share->getPermissions() & \OCP\Constants::PERMISSION_DELETE) {
  245. $message_t = $this->l->t('Files can\'t be shared with delete permissions');
  246. throw new GenericShareException($message_t);
  247. }
  248. if ($share->getPermissions() & \OCP\Constants::PERMISSION_CREATE) {
  249. $message_t = $this->l->t('Files can\'t be shared with create permissions');
  250. throw new GenericShareException($message_t);
  251. }
  252. }
  253. }
  254. /**
  255. * Validate if the expiration date fits the system settings
  256. *
  257. * @param \OCP\Share\IShare $share The share to validate the expiration date of
  258. * @return \OCP\Share\IShare The modified share object
  259. * @throws GenericShareException
  260. * @throws \InvalidArgumentException
  261. * @throws \Exception
  262. */
  263. protected function validateExpirationDate(\OCP\Share\IShare $share) {
  264. $expirationDate = $share->getExpirationDate();
  265. if ($expirationDate !== null) {
  266. //Make sure the expiration date is a date
  267. $expirationDate->setTime(0, 0, 0);
  268. $date = new \DateTime();
  269. $date->setTime(0, 0, 0);
  270. if ($date >= $expirationDate) {
  271. $message = $this->l->t('Expiration date is in the past');
  272. throw new GenericShareException($message, $message, 404);
  273. }
  274. }
  275. // If expiredate is empty set a default one if there is a default
  276. $fullId = null;
  277. try {
  278. $fullId = $share->getFullId();
  279. } catch (\UnexpectedValueException $e) {
  280. // This is a new share
  281. }
  282. if ($fullId === null && $expirationDate === null && $this->shareApiLinkDefaultExpireDate()) {
  283. $expirationDate = new \DateTime();
  284. $expirationDate->setTime(0,0,0);
  285. $expirationDate->add(new \DateInterval('P'.$this->shareApiLinkDefaultExpireDays().'D'));
  286. }
  287. // If we enforce the expiration date check that is does not exceed
  288. if ($this->shareApiLinkDefaultExpireDateEnforced()) {
  289. if ($expirationDate === null) {
  290. throw new \InvalidArgumentException('Expiration date is enforced');
  291. }
  292. $date = new \DateTime();
  293. $date->setTime(0, 0, 0);
  294. $date->add(new \DateInterval('P' . $this->shareApiLinkDefaultExpireDays() . 'D'));
  295. if ($date < $expirationDate) {
  296. $message = $this->l->t('Cannot set expiration date more than %s days in the future', [$this->shareApiLinkDefaultExpireDays()]);
  297. throw new GenericShareException($message, $message, 404);
  298. }
  299. }
  300. $accepted = true;
  301. $message = '';
  302. \OCP\Util::emitHook('\OC\Share', 'verifyExpirationDate', [
  303. 'expirationDate' => &$expirationDate,
  304. 'accepted' => &$accepted,
  305. 'message' => &$message,
  306. 'passwordSet' => $share->getPassword() !== null,
  307. ]);
  308. if (!$accepted) {
  309. throw new \Exception($message);
  310. }
  311. $share->setExpirationDate($expirationDate);
  312. return $share;
  313. }
  314. /**
  315. * Check for pre share requirements for user shares
  316. *
  317. * @param \OCP\Share\IShare $share
  318. * @throws \Exception
  319. */
  320. protected function userCreateChecks(\OCP\Share\IShare $share) {
  321. // Check if we can share with group members only
  322. if ($this->shareWithGroupMembersOnly()) {
  323. $sharedBy = $this->userManager->get($share->getSharedBy());
  324. $sharedWith = $this->userManager->get($share->getSharedWith());
  325. // Verify we can share with this user
  326. $groups = array_intersect(
  327. $this->groupManager->getUserGroupIds($sharedBy),
  328. $this->groupManager->getUserGroupIds($sharedWith)
  329. );
  330. if (empty($groups)) {
  331. throw new \Exception('Only sharing with group members is allowed');
  332. }
  333. }
  334. /*
  335. * TODO: Could be costly, fix
  336. *
  337. * Also this is not what we want in the future.. then we want to squash identical shares.
  338. */
  339. $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_USER);
  340. $existingShares = $provider->getSharesByPath($share->getNode());
  341. foreach($existingShares as $existingShare) {
  342. // Ignore if it is the same share
  343. try {
  344. if ($existingShare->getFullId() === $share->getFullId()) {
  345. continue;
  346. }
  347. } catch (\UnexpectedValueException $e) {
  348. //Shares are not identical
  349. }
  350. // Identical share already existst
  351. if ($existingShare->getSharedWith() === $share->getSharedWith()) {
  352. throw new \Exception('Path already shared with this user');
  353. }
  354. // The share is already shared with this user via a group share
  355. if ($existingShare->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
  356. $group = $this->groupManager->get($existingShare->getSharedWith());
  357. $user = $this->userManager->get($share->getSharedWith());
  358. if ($group->inGroup($user) && $existingShare->getShareOwner() !== $share->getShareOwner()) {
  359. throw new \Exception('Path already shared with this user');
  360. }
  361. }
  362. }
  363. }
  364. /**
  365. * Check for pre share requirements for group shares
  366. *
  367. * @param \OCP\Share\IShare $share
  368. * @throws \Exception
  369. */
  370. protected function groupCreateChecks(\OCP\Share\IShare $share) {
  371. // Verify group shares are allowed
  372. if (!$this->allowGroupSharing()) {
  373. throw new \Exception('Group sharing is now allowed');
  374. }
  375. // Verify if the user can share with this group
  376. if ($this->shareWithGroupMembersOnly()) {
  377. $sharedBy = $this->userManager->get($share->getSharedBy());
  378. $sharedWith = $this->groupManager->get($share->getSharedWith());
  379. if (!$sharedWith->inGroup($sharedBy)) {
  380. throw new \Exception('Only sharing within your own groups is allowed');
  381. }
  382. }
  383. /*
  384. * TODO: Could be costly, fix
  385. *
  386. * Also this is not what we want in the future.. then we want to squash identical shares.
  387. */
  388. $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_GROUP);
  389. $existingShares = $provider->getSharesByPath($share->getNode());
  390. foreach($existingShares as $existingShare) {
  391. try {
  392. if ($existingShare->getFullId() === $share->getFullId()) {
  393. continue;
  394. }
  395. } catch (\UnexpectedValueException $e) {
  396. //It is a new share so just continue
  397. }
  398. if ($existingShare->getSharedWith() === $share->getSharedWith()) {
  399. throw new \Exception('Path already shared with this group');
  400. }
  401. }
  402. }
  403. /**
  404. * Check for pre share requirements for link shares
  405. *
  406. * @param \OCP\Share\IShare $share
  407. * @throws \Exception
  408. */
  409. protected function linkCreateChecks(\OCP\Share\IShare $share) {
  410. // Are link shares allowed?
  411. if (!$this->shareApiAllowLinks()) {
  412. throw new \Exception('Link sharing not allowed');
  413. }
  414. // Link shares by definition can't have share permissions
  415. if ($share->getPermissions() & \OCP\Constants::PERMISSION_SHARE) {
  416. throw new \InvalidArgumentException('Link shares can\'t have reshare permissions');
  417. }
  418. // Check if public upload is allowed
  419. if (!$this->shareApiLinkAllowPublicUpload() &&
  420. ($share->getPermissions() & (\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE))) {
  421. throw new \InvalidArgumentException('Public upload not allowed');
  422. }
  423. }
  424. /**
  425. * To make sure we don't get invisible link shares we set the parent
  426. * of a link if it is a reshare. This is a quick word around
  427. * until we can properly display multiple link shares in the UI
  428. *
  429. * See: https://github.com/owncloud/core/issues/22295
  430. *
  431. * FIXME: Remove once multiple link shares can be properly displayed
  432. *
  433. * @param \OCP\Share\IShare $share
  434. */
  435. protected function setLinkParent(\OCP\Share\IShare $share) {
  436. // No sense in checking if the method is not there.
  437. if (method_exists($share, 'setParent')) {
  438. $storage = $share->getNode()->getStorage();
  439. if ($storage->instanceOfStorage('\OCA\Files_Sharing\ISharedStorage')) {
  440. /** @var \OCA\Files_Sharing\SharedStorage $storage */
  441. $share->setParent($storage->getShareId());
  442. }
  443. };
  444. }
  445. /**
  446. * @param File|Folder $path
  447. */
  448. protected function pathCreateChecks($path) {
  449. // Make sure that we do not share a path that contains a shared mountpoint
  450. if ($path instanceof \OCP\Files\Folder) {
  451. $mounts = $this->mountManager->findIn($path->getPath());
  452. foreach($mounts as $mount) {
  453. if ($mount->getStorage()->instanceOfStorage('\OCA\Files_Sharing\ISharedStorage')) {
  454. throw new \InvalidArgumentException('Path contains files shared with you');
  455. }
  456. }
  457. }
  458. }
  459. /**
  460. * Check if the user that is sharing can actually share
  461. *
  462. * @param \OCP\Share\IShare $share
  463. * @throws \Exception
  464. */
  465. protected function canShare(\OCP\Share\IShare $share) {
  466. if (!$this->shareApiEnabled()) {
  467. throw new \Exception('The share API is disabled');
  468. }
  469. if ($this->sharingDisabledForUser($share->getSharedBy())) {
  470. throw new \Exception('You are not allowed to share');
  471. }
  472. }
  473. /**
  474. * Share a path
  475. *
  476. * @param \OCP\Share\IShare $share
  477. * @return Share The share object
  478. * @throws \Exception
  479. *
  480. * TODO: handle link share permissions or check them
  481. */
  482. public function createShare(\OCP\Share\IShare $share) {
  483. $this->canShare($share);
  484. $this->generalCreateChecks($share);
  485. // Verify if there are any issues with the path
  486. $this->pathCreateChecks($share->getNode());
  487. /*
  488. * On creation of a share the owner is always the owner of the path
  489. * Except for mounted federated shares.
  490. */
  491. $storage = $share->getNode()->getStorage();
  492. if ($storage->instanceOfStorage('OCA\Files_Sharing\External\Storage')) {
  493. $parent = $share->getNode()->getParent();
  494. while($parent->getStorage()->instanceOfStorage('OCA\Files_Sharing\External\Storage')) {
  495. $parent = $parent->getParent();
  496. }
  497. $share->setShareOwner($parent->getOwner()->getUID());
  498. } else {
  499. $share->setShareOwner($share->getNode()->getOwner()->getUID());
  500. }
  501. //Verify share type
  502. if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
  503. $this->userCreateChecks($share);
  504. } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
  505. $this->groupCreateChecks($share);
  506. } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
  507. $this->linkCreateChecks($share);
  508. $this->setLinkParent($share);
  509. /*
  510. * For now ignore a set token.
  511. */
  512. $share->setToken(
  513. $this->secureRandom->generate(
  514. \OC\Share\Constants::TOKEN_LENGTH,
  515. \OCP\Security\ISecureRandom::CHAR_LOWER.
  516. \OCP\Security\ISecureRandom::CHAR_UPPER.
  517. \OCP\Security\ISecureRandom::CHAR_DIGITS
  518. )
  519. );
  520. //Verify the expiration date
  521. $this->validateExpirationDate($share);
  522. //Verify the password
  523. $this->verifyPassword($share->getPassword());
  524. // If a password is set. Hash it!
  525. if ($share->getPassword() !== null) {
  526. $share->setPassword($this->hasher->hash($share->getPassword()));
  527. }
  528. } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) {
  529. $share->setToken(
  530. $this->secureRandom->generate(
  531. \OC\Share\Constants::TOKEN_LENGTH,
  532. \OCP\Security\ISecureRandom::CHAR_LOWER.
  533. \OCP\Security\ISecureRandom::CHAR_UPPER.
  534. \OCP\Security\ISecureRandom::CHAR_DIGITS
  535. )
  536. );
  537. }
  538. // Cannot share with the owner
  539. if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER &&
  540. $share->getSharedWith() === $share->getShareOwner()) {
  541. throw new \InvalidArgumentException('Can\'t share with the share owner');
  542. }
  543. // Generate the target
  544. $target = $this->config->getSystemValue('share_folder', '/') .'/'. $share->getNode()->getName();
  545. $target = \OC\Files\Filesystem::normalizePath($target);
  546. $share->setTarget($target);
  547. // Pre share hook
  548. $run = true;
  549. $error = '';
  550. $preHookData = [
  551. 'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder',
  552. 'itemSource' => $share->getNode()->getId(),
  553. 'shareType' => $share->getShareType(),
  554. 'uidOwner' => $share->getSharedBy(),
  555. 'permissions' => $share->getPermissions(),
  556. 'fileSource' => $share->getNode()->getId(),
  557. 'expiration' => $share->getExpirationDate(),
  558. 'token' => $share->getToken(),
  559. 'itemTarget' => $share->getTarget(),
  560. 'shareWith' => $share->getSharedWith(),
  561. 'run' => &$run,
  562. 'error' => &$error,
  563. ];
  564. \OC_Hook::emit('OCP\Share', 'pre_shared', $preHookData);
  565. if ($run === false) {
  566. throw new \Exception($error);
  567. }
  568. $oldShare = $share;
  569. $provider = $this->factory->getProviderForType($share->getShareType());
  570. $share = $provider->create($share);
  571. //reuse the node we already have
  572. $share->setNode($oldShare->getNode());
  573. // Post share hook
  574. $postHookData = [
  575. 'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder',
  576. 'itemSource' => $share->getNode()->getId(),
  577. 'shareType' => $share->getShareType(),
  578. 'uidOwner' => $share->getSharedBy(),
  579. 'permissions' => $share->getPermissions(),
  580. 'fileSource' => $share->getNode()->getId(),
  581. 'expiration' => $share->getExpirationDate(),
  582. 'token' => $share->getToken(),
  583. 'id' => $share->getId(),
  584. 'shareWith' => $share->getSharedWith(),
  585. 'itemTarget' => $share->getTarget(),
  586. 'fileTarget' => $share->getTarget(),
  587. ];
  588. \OC_Hook::emit('OCP\Share', 'post_shared', $postHookData);
  589. return $share;
  590. }
  591. /**
  592. * Update a share
  593. *
  594. * @param \OCP\Share\IShare $share
  595. * @return \OCP\Share\IShare The share object
  596. * @throws \InvalidArgumentException
  597. */
  598. public function updateShare(\OCP\Share\IShare $share) {
  599. $expirationDateUpdated = false;
  600. $this->canShare($share);
  601. try {
  602. $originalShare = $this->getShareById($share->getFullId());
  603. } catch (\UnexpectedValueException $e) {
  604. throw new \InvalidArgumentException('Share does not have a full id');
  605. }
  606. // We can't change the share type!
  607. if ($share->getShareType() !== $originalShare->getShareType()) {
  608. throw new \InvalidArgumentException('Can\'t change share type');
  609. }
  610. // We can only change the recipient on user shares
  611. if ($share->getSharedWith() !== $originalShare->getSharedWith() &&
  612. $share->getShareType() !== \OCP\Share::SHARE_TYPE_USER) {
  613. throw new \InvalidArgumentException('Can only update recipient on user shares');
  614. }
  615. // Cannot share with the owner
  616. if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER &&
  617. $share->getSharedWith() === $share->getShareOwner()) {
  618. throw new \InvalidArgumentException('Can\'t share with the share owner');
  619. }
  620. $this->generalCreateChecks($share);
  621. if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
  622. $this->userCreateChecks($share);
  623. } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
  624. $this->groupCreateChecks($share);
  625. } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
  626. $this->linkCreateChecks($share);
  627. // Password updated.
  628. if ($share->getPassword() !== $originalShare->getPassword()) {
  629. //Verify the password
  630. $this->verifyPassword($share->getPassword());
  631. // If a password is set. Hash it!
  632. if ($share->getPassword() !== null) {
  633. $share->setPassword($this->hasher->hash($share->getPassword()));
  634. }
  635. }
  636. if ($share->getExpirationDate() != $originalShare->getExpirationDate()) {
  637. //Verify the expiration date
  638. $this->validateExpirationDate($share);
  639. $expirationDateUpdated = true;
  640. }
  641. }
  642. $this->pathCreateChecks($share->getNode());
  643. // Now update the share!
  644. $provider = $this->factory->getProviderForType($share->getShareType());
  645. $share = $provider->update($share);
  646. if ($expirationDateUpdated === true) {
  647. \OC_Hook::emit('OCP\Share', 'post_set_expiration_date', [
  648. 'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder',
  649. 'itemSource' => $share->getNode()->getId(),
  650. 'date' => $share->getExpirationDate(),
  651. 'uidOwner' => $share->getSharedBy(),
  652. ]);
  653. }
  654. if ($share->getPassword() !== $originalShare->getPassword()) {
  655. \OC_Hook::emit('OCP\Share', 'post_update_password', [
  656. 'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder',
  657. 'itemSource' => $share->getNode()->getId(),
  658. 'uidOwner' => $share->getSharedBy(),
  659. 'token' => $share->getToken(),
  660. 'disabled' => is_null($share->getPassword()),
  661. ]);
  662. }
  663. if ($share->getPermissions() !== $originalShare->getPermissions()) {
  664. if ($this->userManager->userExists($share->getShareOwner())) {
  665. $userFolder = $this->rootFolder->getUserFolder($share->getShareOwner());
  666. } else {
  667. $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy());
  668. }
  669. \OC_Hook::emit('OCP\Share', 'post_update_permissions', array(
  670. 'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder',
  671. 'itemSource' => $share->getNode()->getId(),
  672. 'shareType' => $share->getShareType(),
  673. 'shareWith' => $share->getSharedWith(),
  674. 'uidOwner' => $share->getSharedBy(),
  675. 'permissions' => $share->getPermissions(),
  676. 'path' => $userFolder->getRelativePath($share->getNode()->getPath()),
  677. ));
  678. }
  679. return $share;
  680. }
  681. /**
  682. * Delete all the children of this share
  683. * FIXME: remove once https://github.com/owncloud/core/pull/21660 is in
  684. *
  685. * @param \OCP\Share\IShare $share
  686. * @return \OCP\Share\IShare[] List of deleted shares
  687. */
  688. protected function deleteChildren(\OCP\Share\IShare $share) {
  689. $deletedShares = [];
  690. $provider = $this->factory->getProviderForType($share->getShareType());
  691. foreach ($provider->getChildren($share) as $child) {
  692. $deletedChildren = $this->deleteChildren($child);
  693. $deletedShares = array_merge($deletedShares, $deletedChildren);
  694. $provider->delete($child);
  695. $deletedShares[] = $child;
  696. }
  697. return $deletedShares;
  698. }
  699. /**
  700. * Delete a share
  701. *
  702. * @param \OCP\Share\IShare $share
  703. * @throws ShareNotFound
  704. * @throws \InvalidArgumentException
  705. */
  706. public function deleteShare(\OCP\Share\IShare $share) {
  707. try {
  708. $share->getFullId();
  709. } catch (\UnexpectedValueException $e) {
  710. throw new \InvalidArgumentException('Share does not have a full id');
  711. }
  712. $formatHookParams = function(\OCP\Share\IShare $share) {
  713. // Prepare hook
  714. $shareType = $share->getShareType();
  715. $sharedWith = '';
  716. if ($shareType === \OCP\Share::SHARE_TYPE_USER) {
  717. $sharedWith = $share->getSharedWith();
  718. } else if ($shareType === \OCP\Share::SHARE_TYPE_GROUP) {
  719. $sharedWith = $share->getSharedWith();
  720. } else if ($shareType === \OCP\Share::SHARE_TYPE_REMOTE) {
  721. $sharedWith = $share->getSharedWith();
  722. }
  723. $hookParams = [
  724. 'id' => $share->getId(),
  725. 'itemType' => $share->getNodeType(),
  726. 'itemSource' => $share->getNodeId(),
  727. 'shareType' => $shareType,
  728. 'shareWith' => $sharedWith,
  729. 'itemparent' => method_exists($share, 'getParent') ? $share->getParent() : '',
  730. 'uidOwner' => $share->getSharedBy(),
  731. 'fileSource' => $share->getNodeId(),
  732. 'fileTarget' => $share->getTarget()
  733. ];
  734. return $hookParams;
  735. };
  736. $hookParams = $formatHookParams($share);
  737. // Emit pre-hook
  738. \OC_Hook::emit('OCP\Share', 'pre_unshare', $hookParams);
  739. // Get all children and delete them as well
  740. $deletedShares = $this->deleteChildren($share);
  741. // Do the actual delete
  742. $provider = $this->factory->getProviderForType($share->getShareType());
  743. $provider->delete($share);
  744. // All the deleted shares caused by this delete
  745. $deletedShares[] = $share;
  746. //Format hook info
  747. $formattedDeletedShares = array_map(function($share) use ($formatHookParams) {
  748. return $formatHookParams($share);
  749. }, $deletedShares);
  750. $hookParams['deletedShares'] = $formattedDeletedShares;
  751. // Emit post hook
  752. \OC_Hook::emit('OCP\Share', 'post_unshare', $hookParams);
  753. }
  754. /**
  755. * Unshare a file as the recipient.
  756. * This can be different from a regular delete for example when one of
  757. * the users in a groups deletes that share. But the provider should
  758. * handle this.
  759. *
  760. * @param \OCP\Share\IShare $share
  761. * @param string $recipientId
  762. */
  763. public function deleteFromSelf(\OCP\Share\IShare $share, $recipientId) {
  764. list($providerId, ) = $this->splitFullId($share->getFullId());
  765. $provider = $this->factory->getProvider($providerId);
  766. $provider->deleteFromSelf($share, $recipientId);
  767. }
  768. /**
  769. * @inheritdoc
  770. */
  771. public function moveShare(\OCP\Share\IShare $share, $recipientId) {
  772. if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
  773. throw new \InvalidArgumentException('Can\'t change target of link share');
  774. }
  775. if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER && $share->getSharedWith() !== $recipientId) {
  776. throw new \InvalidArgumentException('Invalid recipient');
  777. }
  778. if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
  779. $sharedWith = $this->groupManager->get($share->getSharedWith());
  780. $recipient = $this->userManager->get($recipientId);
  781. if (!$sharedWith->inGroup($recipient)) {
  782. throw new \InvalidArgumentException('Invalid recipient');
  783. }
  784. }
  785. list($providerId, ) = $this->splitFullId($share->getFullId());
  786. $provider = $this->factory->getProvider($providerId);
  787. $provider->move($share, $recipientId);
  788. }
  789. public function getSharesInFolder($userId, Folder $node, $reshares = false) {
  790. $providers = $this->factory->getAllProviders();
  791. return array_reduce($providers, function($shares, IShareProvider $provider) use ($userId, $node, $reshares) {
  792. $newShares = $provider->getSharesInFolder($userId, $node, $reshares);
  793. foreach ($newShares as $fid => $data) {
  794. if (!isset($shares[$fid])) {
  795. $shares[$fid] = [];
  796. }
  797. $shares[$fid] = array_merge($shares[$fid], $data);
  798. }
  799. return $shares;
  800. }, []);
  801. }
  802. /**
  803. * @inheritdoc
  804. */
  805. public function getSharesBy($userId, $shareType, $path = null, $reshares = false, $limit = 50, $offset = 0) {
  806. if ($path !== null &&
  807. !($path instanceof \OCP\Files\File) &&
  808. !($path instanceof \OCP\Files\Folder)) {
  809. throw new \InvalidArgumentException('invalid path');
  810. }
  811. try {
  812. $provider = $this->factory->getProviderForType($shareType);
  813. } catch (ProviderException $e) {
  814. return [];
  815. }
  816. $shares = $provider->getSharesBy($userId, $shareType, $path, $reshares, $limit, $offset);
  817. /*
  818. * Work around so we don't return expired shares but still follow
  819. * proper pagination.
  820. */
  821. if ($shareType === \OCP\Share::SHARE_TYPE_LINK) {
  822. $shares2 = [];
  823. $today = new \DateTime();
  824. while(true) {
  825. $added = 0;
  826. foreach ($shares as $share) {
  827. // Check if the share is expired and if so delete it
  828. if ($share->getExpirationDate() !== null &&
  829. $share->getExpirationDate() <= $today
  830. ) {
  831. try {
  832. $this->deleteShare($share);
  833. } catch (NotFoundException $e) {
  834. //Ignore since this basically means the share is deleted
  835. }
  836. continue;
  837. }
  838. $added++;
  839. $shares2[] = $share;
  840. if (count($shares2) === $limit) {
  841. break;
  842. }
  843. }
  844. if (count($shares2) === $limit) {
  845. break;
  846. }
  847. // If there was no limit on the select we are done
  848. if ($limit === -1) {
  849. break;
  850. }
  851. $offset += $added;
  852. // Fetch again $limit shares
  853. $shares = $provider->getSharesBy($userId, $shareType, $path, $reshares, $limit, $offset);
  854. // No more shares means we are done
  855. if (empty($shares)) {
  856. break;
  857. }
  858. }
  859. $shares = $shares2;
  860. }
  861. return $shares;
  862. }
  863. /**
  864. * @inheritdoc
  865. */
  866. public function getSharedWith($userId, $shareType, $node = null, $limit = 50, $offset = 0) {
  867. try {
  868. $provider = $this->factory->getProviderForType($shareType);
  869. } catch (ProviderException $e) {
  870. return [];
  871. }
  872. return $provider->getSharedWith($userId, $shareType, $node, $limit, $offset);
  873. }
  874. /**
  875. * @inheritdoc
  876. */
  877. public function getShareById($id, $recipient = null) {
  878. if ($id === null) {
  879. throw new ShareNotFound();
  880. }
  881. list($providerId, $id) = $this->splitFullId($id);
  882. try {
  883. $provider = $this->factory->getProvider($providerId);
  884. } catch (ProviderException $e) {
  885. throw new ShareNotFound();
  886. }
  887. $share = $provider->getShareById($id, $recipient);
  888. // Validate link shares expiration date
  889. if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK &&
  890. $share->getExpirationDate() !== null &&
  891. $share->getExpirationDate() <= new \DateTime()) {
  892. $this->deleteShare($share);
  893. throw new ShareNotFound();
  894. }
  895. return $share;
  896. }
  897. /**
  898. * Get all the shares for a given path
  899. *
  900. * @param \OCP\Files\Node $path
  901. * @param int $page
  902. * @param int $perPage
  903. *
  904. * @return Share[]
  905. */
  906. public function getSharesByPath(\OCP\Files\Node $path, $page=0, $perPage=50) {
  907. return [];
  908. }
  909. /**
  910. * Get the share by token possible with password
  911. *
  912. * @param string $token
  913. * @return Share
  914. *
  915. * @throws ShareNotFound
  916. */
  917. public function getShareByToken($token) {
  918. $share = null;
  919. try {
  920. if($this->shareApiAllowLinks()) {
  921. $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_LINK);
  922. $share = $provider->getShareByToken($token);
  923. }
  924. } catch (ProviderException $e) {
  925. } catch (ShareNotFound $e) {
  926. }
  927. // If it is not a link share try to fetch a federated share by token
  928. if ($share === null) {
  929. try {
  930. $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_REMOTE);
  931. $share = $provider->getShareByToken($token);
  932. } catch (ProviderException $e) {
  933. } catch (ShareNotFound $e) {
  934. }
  935. }
  936. // If it is not a link share try to fetch a mail share by token
  937. if ($share === null && $this->shareProviderExists(\OCP\Share::SHARE_TYPE_EMAIL)) {
  938. try {
  939. $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_EMAIL);
  940. $share = $provider->getShareByToken($token);
  941. } catch (ProviderException $e) {
  942. } catch (ShareNotFound $e) {
  943. }
  944. }
  945. if ($share === null) {
  946. throw new ShareNotFound();
  947. }
  948. if ($share->getExpirationDate() !== null &&
  949. $share->getExpirationDate() <= new \DateTime()) {
  950. $this->deleteShare($share);
  951. throw new ShareNotFound();
  952. }
  953. /*
  954. * Reduce the permissions for link shares if public upload is not enabled
  955. */
  956. if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK &&
  957. !$this->shareApiLinkAllowPublicUpload()) {
  958. $share->setPermissions($share->getPermissions() & ~(\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE));
  959. }
  960. return $share;
  961. }
  962. /**
  963. * Verify the password of a public share
  964. *
  965. * @param \OCP\Share\IShare $share
  966. * @param string $password
  967. * @return bool
  968. */
  969. public function checkPassword(\OCP\Share\IShare $share, $password) {
  970. if ($share->getShareType() !== \OCP\Share::SHARE_TYPE_LINK) {
  971. //TODO maybe exception?
  972. return false;
  973. }
  974. if ($password === null || $share->getPassword() === null) {
  975. return false;
  976. }
  977. $newHash = '';
  978. if (!$this->hasher->verify($password, $share->getPassword(), $newHash)) {
  979. return false;
  980. }
  981. if (!empty($newHash)) {
  982. $share->setPassword($newHash);
  983. $provider = $this->factory->getProviderForType($share->getShareType());
  984. $provider->update($share);
  985. }
  986. return true;
  987. }
  988. /**
  989. * @inheritdoc
  990. */
  991. public function userDeleted($uid) {
  992. $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];
  993. foreach ($types as $type) {
  994. try {
  995. $provider = $this->factory->getProviderForType($type);
  996. } catch (ProviderException $e) {
  997. continue;
  998. }
  999. $provider->userDeleted($uid, $type);
  1000. }
  1001. }
  1002. /**
  1003. * @inheritdoc
  1004. */
  1005. public function groupDeleted($gid) {
  1006. $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_GROUP);
  1007. $provider->groupDeleted($gid);
  1008. }
  1009. /**
  1010. * @inheritdoc
  1011. */
  1012. public function userDeletedFromGroup($uid, $gid) {
  1013. $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_GROUP);
  1014. $provider->userDeletedFromGroup($uid, $gid);
  1015. }
  1016. /**
  1017. * Get access list to a path. This means
  1018. * all the users and groups that can access a given path.
  1019. *
  1020. * Consider:
  1021. * -root
  1022. * |-folder1
  1023. * |-folder2
  1024. * |-fileA
  1025. *
  1026. * fileA is shared with user1
  1027. * folder2 is shared with group2
  1028. * folder1 is shared with user2
  1029. *
  1030. * Then the access list will to '/folder1/folder2/fileA' is:
  1031. * [
  1032. * 'users' => ['user1', 'user2'],
  1033. * 'groups' => ['group2']
  1034. * ]
  1035. *
  1036. * This is required for encryption
  1037. *
  1038. * @param \OCP\Files\Node $path
  1039. */
  1040. public function getAccessList(\OCP\Files\Node $path) {
  1041. }
  1042. /**
  1043. * Create a new share
  1044. * @return \OCP\Share\IShare;
  1045. */
  1046. public function newShare() {
  1047. return new \OC\Share20\Share($this->rootFolder, $this->userManager);
  1048. }
  1049. /**
  1050. * Is the share API enabled
  1051. *
  1052. * @return bool
  1053. */
  1054. public function shareApiEnabled() {
  1055. return $this->config->getAppValue('core', 'shareapi_enabled', 'yes') === 'yes';
  1056. }
  1057. /**
  1058. * Is public link sharing enabled
  1059. *
  1060. * @return bool
  1061. */
  1062. public function shareApiAllowLinks() {
  1063. return $this->config->getAppValue('core', 'shareapi_allow_links', 'yes') === 'yes';
  1064. }
  1065. /**
  1066. * Is password on public link requires
  1067. *
  1068. * @return bool
  1069. */
  1070. public function shareApiLinkEnforcePassword() {
  1071. return $this->config->getAppValue('core', 'shareapi_enforce_links_password', 'no') === 'yes';
  1072. }
  1073. /**
  1074. * Is default expire date enabled
  1075. *
  1076. * @return bool
  1077. */
  1078. public function shareApiLinkDefaultExpireDate() {
  1079. return $this->config->getAppValue('core', 'shareapi_default_expire_date', 'no') === 'yes';
  1080. }
  1081. /**
  1082. * Is default expire date enforced
  1083. *`
  1084. * @return bool
  1085. */
  1086. public function shareApiLinkDefaultExpireDateEnforced() {
  1087. return $this->shareApiLinkDefaultExpireDate() &&
  1088. $this->config->getAppValue('core', 'shareapi_enforce_expire_date', 'no') === 'yes';
  1089. }
  1090. /**
  1091. * Number of default expire days
  1092. *shareApiLinkAllowPublicUpload
  1093. * @return int
  1094. */
  1095. public function shareApiLinkDefaultExpireDays() {
  1096. return (int)$this->config->getAppValue('core', 'shareapi_expire_after_n_days', '7');
  1097. }
  1098. /**
  1099. * Allow public upload on link shares
  1100. *
  1101. * @return bool
  1102. */
  1103. public function shareApiLinkAllowPublicUpload() {
  1104. return $this->config->getAppValue('core', 'shareapi_allow_public_upload', 'yes') === 'yes';
  1105. }
  1106. /**
  1107. * check if user can only share with group members
  1108. * @return bool
  1109. */
  1110. public function shareWithGroupMembersOnly() {
  1111. return $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes';
  1112. }
  1113. /**
  1114. * Check if users can share with groups
  1115. * @return bool
  1116. */
  1117. public function allowGroupSharing() {
  1118. return $this->config->getAppValue('core', 'shareapi_allow_group_sharing', 'yes') === 'yes';
  1119. }
  1120. /**
  1121. * Copied from \OC_Util::isSharingDisabledForUser
  1122. *
  1123. * TODO: Deprecate fuction from OC_Util
  1124. *
  1125. * @param string $userId
  1126. * @return bool
  1127. */
  1128. public function sharingDisabledForUser($userId) {
  1129. if ($userId === null) {
  1130. return false;
  1131. }
  1132. if (isset($this->sharingDisabledForUsersCache[$userId])) {
  1133. return $this->sharingDisabledForUsersCache[$userId];
  1134. }
  1135. if ($this->config->getAppValue('core', 'shareapi_exclude_groups', 'no') === 'yes') {
  1136. $groupsList = $this->config->getAppValue('core', 'shareapi_exclude_groups_list', '');
  1137. $excludedGroups = json_decode($groupsList);
  1138. if (is_null($excludedGroups)) {
  1139. $excludedGroups = explode(',', $groupsList);
  1140. $newValue = json_encode($excludedGroups);
  1141. $this->config->setAppValue('core', 'shareapi_exclude_groups_list', $newValue);
  1142. }
  1143. $user = $this->userManager->get($userId);
  1144. $usersGroups = $this->groupManager->getUserGroupIds($user);
  1145. if (!empty($usersGroups)) {
  1146. $remainingGroups = array_diff($usersGroups, $excludedGroups);
  1147. // if the user is only in groups which are disabled for sharing then
  1148. // sharing is also disabled for the user
  1149. if (empty($remainingGroups)) {
  1150. $this->sharingDisabledForUsersCache[$userId] = true;
  1151. return true;
  1152. }
  1153. }
  1154. }
  1155. $this->sharingDisabledForUsersCache[$userId] = false;
  1156. return false;
  1157. }
  1158. /**
  1159. * @inheritdoc
  1160. */
  1161. public function outgoingServer2ServerSharesAllowed() {
  1162. return $this->config->getAppValue('files_sharing', 'outgoing_server2server_share_enabled', 'yes') === 'yes';
  1163. }
  1164. /**
  1165. * @inheritdoc
  1166. */
  1167. public function shareProviderExists($shareType) {
  1168. try {
  1169. $this->factory->getProviderForType($shareType);
  1170. } catch (ProviderException $e) {
  1171. return false;
  1172. }
  1173. return true;
  1174. }
  1175. }