123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- <?php
- namespace OCA\Files_Sharing;
- use OC\Cache\CappedMemoryCache;
- use OC\Files\View;
- use OCP\Files\Config\IMountProvider;
- use OCP\Files\Storage\IStorageFactory;
- use OCP\IConfig;
- use OCP\ILogger;
- use OCP\IUser;
- use OCP\Share\IManager;
- use OCP\Share\IShare;
- class MountProvider implements IMountProvider {
-
- protected $config;
-
- protected $shareManager;
-
- protected $logger;
-
- public function __construct(IConfig $config, IManager $shareManager, ILogger $logger) {
- $this->config = $config;
- $this->shareManager = $shareManager;
- $this->logger = $logger;
- }
-
- 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_filter($shares, function (\OCP\Share\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
- );
- $mounts[$mount->getMountPoint()] = $mount;
- } catch (\Exception $e) {
- $this->logger->logException($e);
- $this->logger->error('Error while trying to create shared mount');
- }
- }
-
- 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, \OCP\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());
-
- $permissions = 0;
- $status = IShare::STATUS_PENDING;
- foreach ($shares as $share) {
- $permissions |= $share->getPermissions();
- $status = max($status, $share->getStatus());
- 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($permissions)
- ->setStatus($status);
- $result[] = [$superShare, $shares];
- }
- return $result;
- }
- }
|