123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- <?php
- namespace OCA\Files_Sharing;
- use OC\Files\View;
- use OCA\Files_Sharing\Event\ShareMountedEvent;
- use OCP\Cache\CappedMemoryCache;
- use OCP\EventDispatcher\IEventDispatcher;
- use OCP\Files\Config\IMountProvider;
- use OCP\Files\Mount\IMountPoint;
- use OCP\Files\Storage\IStorageFactory;
- use OCP\ICacheFactory;
- use OCP\IConfig;
- use OCP\IUser;
- use OCP\Share\IManager;
- use OCP\Share\IShare;
- use Psr\Log\LoggerInterface;
- class MountProvider implements IMountProvider {
-
- public function __construct(
- protected IConfig $config,
- protected IManager $shareManager,
- protected LoggerInterface $logger,
- protected IEventDispatcher $eventDispatcher,
- protected ICacheFactory $cacheFactory,
- ) {
- }
-
- public function getMountsForUser(IUser $user, IStorageFactory $loader) {
- $shares = $this->shareManager->getSharedWith($user->getUID(), IShare::TYPE_USER, null, -1);
- $shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), IShare::TYPE_GROUP, null, -1));
- $shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), IShare::TYPE_CIRCLE, null, -1));
- $shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), IShare::TYPE_ROOM, null, -1));
- $shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), IShare::TYPE_DECK, null, -1));
- $shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), IShare::TYPE_SCIENCEMESH, null, -1));
-
- $shares = array_filter($shares, function (IShare $share) use ($user) {
- return $share->getPermissions() > 0 && $share->getShareOwner() !== $user->getUID();
- });
- $superShares = $this->buildSuperShares($shares, $user);
- $mounts = [];
- $view = new View('/' . $user->getUID() . '/files');
- $ownerViews = [];
- $sharingDisabledForUser = $this->shareManager->sharingDisabledForUser($user->getUID());
-
- $foldersExistCache = new CappedMemoryCache();
- foreach ($superShares as $share) {
- try {
-
- $parentShare = $share[0];
- if ($parentShare->getStatus() !== IShare::STATUS_ACCEPTED &&
- ($parentShare->getShareType() === IShare::TYPE_GROUP ||
- $parentShare->getShareType() === IShare::TYPE_USERGROUP ||
- $parentShare->getShareType() === IShare::TYPE_USER)) {
- continue;
- }
- $owner = $parentShare->getShareOwner();
- if (!isset($ownerViews[$owner])) {
- $ownerViews[$owner] = new View('/' . $parentShare->getShareOwner() . '/files');
- }
- $mount = new SharedMount(
- '\OCA\Files_Sharing\SharedStorage',
- $mounts,
- [
- 'user' => $user->getUID(),
-
- 'superShare' => $parentShare,
-
- 'groupedShares' => $share[1],
- 'ownerView' => $ownerViews[$owner],
- 'sharingDisabledForUser' => $sharingDisabledForUser
- ],
- $loader,
- $view,
- $foldersExistCache,
- $this->eventDispatcher,
- $user,
- $this->cacheFactory->createLocal('share-valid-mountpoint')
- );
- $event = new ShareMountedEvent($mount);
- $this->eventDispatcher->dispatchTyped($event);
- $mounts[$mount->getMountPoint()] = $mount;
- foreach ($event->getAdditionalMounts() as $additionalMount) {
- $mounts[$additionalMount->getMountPoint()] = $additionalMount;
- }
- } catch (\Exception $e) {
- $this->logger->error(
- 'Error while trying to create shared mount',
- [
- 'app' => 'files_sharing',
- 'exception' => $e,
- ],
- );
- }
- }
-
- return array_values(array_filter($mounts));
- }
-
- private function groupShares(array $shares) {
- $tmp = [];
- foreach ($shares as $share) {
- if (!isset($tmp[$share->getNodeId()])) {
- $tmp[$share->getNodeId()] = [];
- }
- $tmp[$share->getNodeId()][] = $share;
- }
- $result = [];
-
- foreach ($tmp as &$tmp2) {
- @usort($tmp2, function ($a, $b) {
- $aTime = $a->getShareTime()->getTimestamp();
- $bTime = $b->getShareTime()->getTimestamp();
- if ($aTime === $bTime) {
- return $a->getId() < $b->getId() ? -1 : 1;
- }
- return $aTime < $bTime ? -1 : 1;
- });
- $result[] = $tmp2;
- }
- return array_values($result);
- }
-
- private function buildSuperShares(array $allShares, IUser $user) {
- $result = [];
- $groupedShares = $this->groupShares($allShares);
-
- foreach ($groupedShares as $shares) {
- if (count($shares) === 0) {
- continue;
- }
- $superShare = $this->shareManager->newShare();
-
- $superShare->setId($shares[0]->getId())
- ->setShareOwner($shares[0]->getShareOwner())
- ->setNodeId($shares[0]->getNodeId())
- ->setShareType($shares[0]->getShareType())
- ->setTarget($shares[0]->getTarget());
-
-
-
-
- $allNotes = implode("\n", array_map(function ($sh) {
- return $sh->getNote();
- }, $shares));
- $superShare->setNote($allNotes);
-
-
-
- $superPermissions = 0;
- $superAttributes = $this->shareManager->newShare()->newAttributes();
- $status = IShare::STATUS_PENDING;
- foreach ($shares as $share) {
- $superPermissions |= $share->getPermissions();
- $status = max($status, $share->getStatus());
-
- $superPermissions |= $share->getPermissions();
-
- $attributes = $share->getAttributes();
- if ($attributes !== null) {
- foreach ($attributes->toArray() as $attribute) {
- if ($superAttributes->getAttribute($attribute['scope'], $attribute['key']) === true) {
-
- continue;
- }
-
- $superAttributes->setAttribute($attribute['scope'], $attribute['key'], $attribute['value']);
- }
- }
-
- if ($share->getTarget() !== $superShare->getTarget()) {
- $share->setTarget($superShare->getTarget());
- try {
- $this->shareManager->moveShare($share, $user->getUID());
- } catch (\InvalidArgumentException $e) {
-
-
-
-
-
-
-
- $this->logger->debug(
- 'Could not adjust share target for share ' . $share->getId() . ' to make it consistent: ' . $e->getMessage(),
- ['app' => 'files_sharing']
- );
- }
- }
- if (!is_null($share->getNodeCacheEntry())) {
- $superShare->setNodeCacheEntry($share->getNodeCacheEntry());
- }
- }
- $superShare->setPermissions($superPermissions);
- $superShare->setStatus($status);
- $superShare->setAttributes($superAttributes);
- $result[] = [$superShare, $shares];
- }
- return $result;
- }
- }
|