2018-04-30 12:49:24 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2018 Bjoern Schiessle <bjoern@schiessle.org>
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\FederatedFileSharing\OCM;
|
|
|
|
|
|
|
|
use OC\AppFramework\Http;
|
2018-05-29 17:21:13 +03:00
|
|
|
use OC\Files\Filesystem;
|
2018-04-30 12:49:24 +03:00
|
|
|
use OCA\Files_Sharing\Activity\Providers\RemoteShares;
|
|
|
|
use OCA\FederatedFileSharing\AddressHandler;
|
|
|
|
use OCA\FederatedFileSharing\FederatedShareProvider;
|
|
|
|
use OCP\Activity\IManager as IActivityManager;
|
|
|
|
use OCP\App\IAppManager;
|
2018-06-04 13:16:03 +03:00
|
|
|
use OCP\Constants;
|
2018-05-09 18:06:35 +03:00
|
|
|
use OCP\Federation\Exceptions\ActionNotSupportedException;
|
2018-05-29 17:21:13 +03:00
|
|
|
use OCP\Federation\Exceptions\AuthenticationFailedException;
|
|
|
|
use OCP\Federation\Exceptions\BadRequestException;
|
2018-04-30 12:49:24 +03:00
|
|
|
use OCP\Federation\Exceptions\ProviderCouldNotAddShareException;
|
2018-05-30 18:44:08 +03:00
|
|
|
use OCP\Federation\ICloudFederationFactory;
|
2018-04-30 12:49:24 +03:00
|
|
|
use OCP\Federation\ICloudFederationProvider;
|
2018-05-30 18:44:08 +03:00
|
|
|
use OCP\Federation\ICloudFederationProviderManager;
|
2018-04-30 12:49:24 +03:00
|
|
|
use OCP\Federation\ICloudFederationShare;
|
|
|
|
use OCP\Federation\ICloudIdManager;
|
2018-05-29 17:21:13 +03:00
|
|
|
use OCP\Files\NotFoundException;
|
2018-06-04 17:36:37 +03:00
|
|
|
use OCP\IDBConnection;
|
2018-06-13 15:19:59 +03:00
|
|
|
use OCP\IGroupManager;
|
2018-04-30 12:49:24 +03:00
|
|
|
use OCP\ILogger;
|
|
|
|
use OCP\IURLGenerator;
|
|
|
|
use OCP\IUserManager;
|
|
|
|
use OCP\Notification\IManager as INotificationManager;
|
2018-06-13 15:19:59 +03:00
|
|
|
use OCP\Share;
|
2018-05-29 17:21:13 +03:00
|
|
|
use OCP\Share\Exceptions\ShareNotFound;
|
|
|
|
use OCP\Share\IShare;
|
|
|
|
use OCP\Util;
|
2018-04-30 12:49:24 +03:00
|
|
|
|
|
|
|
class CloudFederationProviderFiles implements ICloudFederationProvider {
|
|
|
|
|
|
|
|
/** @var IAppManager */
|
|
|
|
private $appManager;
|
|
|
|
|
|
|
|
/** @var FederatedShareProvider */
|
|
|
|
private $federatedShareProvider;
|
|
|
|
|
|
|
|
/** @var AddressHandler */
|
|
|
|
private $addressHandler;
|
|
|
|
|
|
|
|
/** @var ILogger */
|
|
|
|
private $logger;
|
|
|
|
|
|
|
|
/** @var IUserManager */
|
|
|
|
private $userManager;
|
|
|
|
|
|
|
|
/** @var ICloudIdManager */
|
|
|
|
private $cloudIdManager;
|
|
|
|
|
|
|
|
/** @var IActivityManager */
|
|
|
|
private $activityManager;
|
|
|
|
|
|
|
|
/** @var INotificationManager */
|
|
|
|
private $notificationManager;
|
|
|
|
|
|
|
|
/** @var IURLGenerator */
|
|
|
|
private $urlGenerator;
|
|
|
|
|
2018-05-30 18:44:08 +03:00
|
|
|
/** @var ICloudFederationFactory */
|
|
|
|
private $cloudFederationFactory;
|
|
|
|
|
|
|
|
/** @var ICloudFederationProviderManager */
|
|
|
|
private $cloudFederationProviderManager;
|
|
|
|
|
2018-06-04 17:36:37 +03:00
|
|
|
/** @var IDBConnection */
|
|
|
|
private $connection;
|
|
|
|
|
2018-06-13 15:19:59 +03:00
|
|
|
/** @var IGroupManager */
|
|
|
|
private $groupManager;
|
|
|
|
|
2018-04-30 12:49:24 +03:00
|
|
|
/**
|
|
|
|
* CloudFederationProvider constructor.
|
|
|
|
*
|
|
|
|
* @param IAppManager $appManager
|
|
|
|
* @param FederatedShareProvider $federatedShareProvider
|
|
|
|
* @param AddressHandler $addressHandler
|
|
|
|
* @param ILogger $logger
|
|
|
|
* @param IUserManager $userManager
|
|
|
|
* @param ICloudIdManager $cloudIdManager
|
|
|
|
* @param IActivityManager $activityManager
|
|
|
|
* @param INotificationManager $notificationManager
|
|
|
|
* @param IURLGenerator $urlGenerator
|
2018-05-30 18:44:08 +03:00
|
|
|
* @param ICloudFederationFactory $cloudFederationFactory
|
|
|
|
* @param ICloudFederationProviderManager $cloudFederationProviderManager
|
2018-06-04 17:36:37 +03:00
|
|
|
* @param IDBConnection $connection
|
2018-06-13 15:19:59 +03:00
|
|
|
* @param IGroupManager $groupManager
|
2018-04-30 12:49:24 +03:00
|
|
|
*/
|
|
|
|
public function __construct(IAppManager $appManager,
|
|
|
|
FederatedShareProvider $federatedShareProvider,
|
|
|
|
AddressHandler $addressHandler,
|
|
|
|
ILogger $logger,
|
|
|
|
IUserManager $userManager,
|
|
|
|
ICloudIdManager $cloudIdManager,
|
|
|
|
IActivityManager $activityManager,
|
|
|
|
INotificationManager $notificationManager,
|
2018-05-30 18:44:08 +03:00
|
|
|
IURLGenerator $urlGenerator,
|
|
|
|
ICloudFederationFactory $cloudFederationFactory,
|
2018-06-04 17:36:37 +03:00
|
|
|
ICloudFederationProviderManager $cloudFederationProviderManager,
|
2018-06-13 15:19:59 +03:00
|
|
|
IDBConnection $connection,
|
|
|
|
IGroupManager $groupManager
|
2018-04-30 12:49:24 +03:00
|
|
|
) {
|
|
|
|
$this->appManager = $appManager;
|
|
|
|
$this->federatedShareProvider = $federatedShareProvider;
|
|
|
|
$this->addressHandler = $addressHandler;
|
|
|
|
$this->logger = $logger;
|
|
|
|
$this->userManager = $userManager;
|
|
|
|
$this->cloudIdManager = $cloudIdManager;
|
|
|
|
$this->activityManager = $activityManager;
|
|
|
|
$this->notificationManager = $notificationManager;
|
|
|
|
$this->urlGenerator = $urlGenerator;
|
2018-05-30 18:44:08 +03:00
|
|
|
$this->cloudFederationFactory = $cloudFederationFactory;
|
|
|
|
$this->cloudFederationProviderManager = $cloudFederationProviderManager;
|
2018-06-04 17:36:37 +03:00
|
|
|
$this->connection = $connection;
|
2018-06-13 15:19:59 +03:00
|
|
|
$this->groupManager = $groupManager;
|
2018-04-30 12:49:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getShareType() {
|
|
|
|
return 'file';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* share received from another server
|
|
|
|
*
|
|
|
|
* @param ICloudFederationShare $share
|
|
|
|
* @return string provider specific unique ID of the share
|
|
|
|
*
|
|
|
|
* @throws ProviderCouldNotAddShareException
|
|
|
|
* @throws \OCP\AppFramework\QueryException
|
|
|
|
* @throws \OC\HintException
|
|
|
|
* @since 14.0.0
|
|
|
|
*/
|
|
|
|
public function shareReceived(ICloudFederationShare $share) {
|
|
|
|
|
|
|
|
if (!$this->isS2SEnabled(true)) {
|
|
|
|
throw new ProviderCouldNotAddShareException('Server does not support federated cloud sharing', '', Http::STATUS_SERVICE_UNAVAILABLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
$protocol = $share->getProtocol();
|
|
|
|
if ($protocol['name'] !== 'webdav') {
|
|
|
|
throw new ProviderCouldNotAddShareException('Unsupported protocol for data exchange.', '', Http::STATUS_NOT_IMPLEMENTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
list($ownerUid, $remote) = $this->addressHandler->splitUserRemote($share->getOwner());
|
2018-06-12 21:07:27 +03:00
|
|
|
// for backward compatibility make sure that the remote url stored in the
|
|
|
|
// database ends with a trailing slash
|
|
|
|
if (substr($remote, -1) !== '/') {
|
|
|
|
$remote = $remote . '/';
|
|
|
|
}
|
2018-04-30 12:49:24 +03:00
|
|
|
|
2018-05-28 18:13:19 +03:00
|
|
|
$token = $share->getShareSecret();
|
2018-04-30 12:49:24 +03:00
|
|
|
$name = $share->getResourceName();
|
|
|
|
$owner = $share->getOwnerDisplayName();
|
|
|
|
$sharedBy = $share->getSharedByDisplayName();
|
|
|
|
$shareWith = $share->getShareWith();
|
|
|
|
$remoteId = $share->getProviderId();
|
|
|
|
$sharedByFederatedId = $share->getSharedBy();
|
|
|
|
$ownerFederatedId = $share->getOwner();
|
2018-06-13 15:19:59 +03:00
|
|
|
$shareType = $this->mapShareTypeToNextcloud($share->getShareType());
|
2018-04-30 12:49:24 +03:00
|
|
|
|
|
|
|
// if no explicit information about the person who created the share was send
|
|
|
|
// we assume that the share comes from the owner
|
|
|
|
if ($sharedByFederatedId === null) {
|
|
|
|
$sharedBy = $owner;
|
|
|
|
$sharedByFederatedId = $ownerFederatedId;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($remote && $token && $name && $owner && $remoteId && $shareWith) {
|
|
|
|
|
2018-05-29 17:21:13 +03:00
|
|
|
if (!Util::isValidFileName($name)) {
|
2018-04-30 12:49:24 +03:00
|
|
|
throw new ProviderCouldNotAddShareException('The mountpoint name contains invalid characters.', '', Http::STATUS_BAD_REQUEST);
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME this should be a method in the user management instead
|
2018-06-13 15:19:59 +03:00
|
|
|
if ($shareType === Share::SHARE_TYPE_USER) {
|
|
|
|
$this->logger->debug('shareWith before, ' . $shareWith, ['app' => 'files_sharing']);
|
|
|
|
Util::emitHook(
|
|
|
|
'\OCA\Files_Sharing\API\Server2Server',
|
|
|
|
'preLoginNameUsedAsUserName',
|
|
|
|
array('uid' => &$shareWith)
|
|
|
|
);
|
|
|
|
$this->logger->debug('shareWith after, ' . $shareWith, ['app' => 'files_sharing']);
|
2018-04-30 12:49:24 +03:00
|
|
|
|
2018-06-13 15:19:59 +03:00
|
|
|
if (!$this->userManager->userExists($shareWith)) {
|
|
|
|
throw new ProviderCouldNotAddShareException('User does not exists', '',Http::STATUS_BAD_REQUEST);
|
|
|
|
}
|
|
|
|
|
|
|
|
\OC_Util::setupFS($shareWith);
|
2018-04-30 12:49:24 +03:00
|
|
|
}
|
|
|
|
|
2018-06-13 15:19:59 +03:00
|
|
|
if ($shareType === Share::SHARE_TYPE_GROUP && !$this->groupManager->groupExists($shareWith)) {
|
|
|
|
throw new ProviderCouldNotAddShareException('Group does not exists', '',Http::STATUS_BAD_REQUEST);
|
|
|
|
}
|
2018-04-30 12:49:24 +03:00
|
|
|
|
|
|
|
$externalManager = new \OCA\Files_Sharing\External\Manager(
|
|
|
|
\OC::$server->getDatabaseConnection(),
|
2018-05-29 17:21:13 +03:00
|
|
|
Filesystem::getMountManager(),
|
|
|
|
Filesystem::getLoader(),
|
2018-04-30 12:49:24 +03:00
|
|
|
\OC::$server->getHTTPClientService(),
|
|
|
|
\OC::$server->getNotificationManager(),
|
|
|
|
\OC::$server->query(\OCP\OCS\IDiscoveryService::class),
|
2018-05-04 16:25:02 +03:00
|
|
|
\OC::$server->getCloudFederationProviderManager(),
|
2018-05-09 18:06:35 +03:00
|
|
|
\OC::$server->getCloudFederationFactory(),
|
2018-06-29 17:35:04 +03:00
|
|
|
\OC::$server->getGroupManager(),
|
|
|
|
\OC::$server->getUserManager(),
|
2018-04-30 12:49:24 +03:00
|
|
|
$shareWith
|
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
2018-06-13 15:19:59 +03:00
|
|
|
$externalManager->addShare($remote, $token, '', $name, $owner, $shareType,false, $shareWith, $remoteId);
|
2018-04-30 12:49:24 +03:00
|
|
|
$shareId = \OC::$server->getDatabaseConnection()->lastInsertId('*PREFIX*share_external');
|
|
|
|
|
2018-06-13 15:19:59 +03:00
|
|
|
if ($shareType === Share::SHARE_TYPE_USER) {
|
2018-06-29 12:30:48 +03:00
|
|
|
$event = $this->activityManager->generateEvent();
|
|
|
|
$event->setApp('files_sharing')
|
|
|
|
->setType('remote_share')
|
|
|
|
->setSubject(RemoteShares::SUBJECT_REMOTE_SHARE_RECEIVED, [$ownerFederatedId, trim($name, '/')])
|
|
|
|
->setAffectedUser($shareWith)
|
|
|
|
->setObject('remote_share', (int)$shareId, $name);
|
|
|
|
\OC::$server->getActivityManager()->publish($event);
|
2018-06-13 15:19:59 +03:00
|
|
|
$this->notifyAboutNewShare($shareWith, $shareId, $ownerFederatedId, $sharedByFederatedId, $name);
|
|
|
|
} else {
|
|
|
|
$groupMembers = $this->groupManager->get($shareWith)->getUsers();
|
|
|
|
foreach ($groupMembers as $user) {
|
2018-06-29 12:30:48 +03:00
|
|
|
$event = $this->activityManager->generateEvent();
|
|
|
|
$event->setApp('files_sharing')
|
|
|
|
->setType('remote_share')
|
|
|
|
->setSubject(RemoteShares::SUBJECT_REMOTE_SHARE_RECEIVED, [$ownerFederatedId, trim($name, '/')])
|
|
|
|
->setAffectedUser($user->getUID())
|
|
|
|
->setObject('remote_share', (int)$shareId, $name);
|
|
|
|
\OC::$server->getActivityManager()->publish($event);
|
|
|
|
$this->notifyAboutNewShare($user->getUID(), $shareId, $ownerFederatedId, $sharedByFederatedId, $name);
|
2018-06-13 15:19:59 +03:00
|
|
|
}
|
|
|
|
}
|
2018-04-30 12:49:24 +03:00
|
|
|
return $shareId;
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$this->logger->logException($e, [
|
|
|
|
'message' => 'Server can not add remote share.',
|
2018-06-07 17:36:26 +03:00
|
|
|
'level' => ILogger::ERROR,
|
2018-04-30 12:49:24 +03:00
|
|
|
'app' => 'files_sharing'
|
|
|
|
]);
|
|
|
|
throw new ProviderCouldNotAddShareException('internal server error, was not able to add share from ' . $remote, '', HTTP::STATUS_INTERNAL_SERVER_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new ProviderCouldNotAddShareException('server can not add remote share, missing parameter', '', HTTP::STATUS_BAD_REQUEST);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* notification received from another server
|
|
|
|
*
|
2018-05-09 18:06:35 +03:00
|
|
|
* @param string $notificationType (e.g. SHARE_ACCEPTED)
|
2018-05-28 18:13:19 +03:00
|
|
|
* @param string $providerId id of the share
|
|
|
|
* @param array $notification payload of the notification
|
2018-06-04 13:16:03 +03:00
|
|
|
* @return array data send back to the sender
|
2018-04-30 12:49:24 +03:00
|
|
|
*
|
2018-05-09 18:06:35 +03:00
|
|
|
* @throws ActionNotSupportedException
|
2018-05-29 17:21:13 +03:00
|
|
|
* @throws AuthenticationFailedException
|
|
|
|
* @throws BadRequestException
|
|
|
|
* @throws \OC\HintException
|
2018-04-30 12:49:24 +03:00
|
|
|
* @since 14.0.0
|
|
|
|
*/
|
2018-05-28 18:13:19 +03:00
|
|
|
public function notificationReceived($notificationType, $providerId, array $notification) {
|
2018-05-29 17:21:13 +03:00
|
|
|
|
2018-05-09 18:06:35 +03:00
|
|
|
switch ($notificationType) {
|
2018-05-29 17:21:13 +03:00
|
|
|
case 'SHARE_ACCEPTED':
|
2018-06-04 13:16:03 +03:00
|
|
|
return $this->shareAccepted($providerId, $notification);
|
2018-05-30 18:44:08 +03:00
|
|
|
case 'SHARE_DECLINED':
|
2018-06-04 13:16:03 +03:00
|
|
|
return $this->shareDeclined($providerId, $notification);
|
2018-06-04 17:36:37 +03:00
|
|
|
case 'SHARE_UNSHARED':
|
|
|
|
return $this->unshare($providerId, $notification);
|
2018-06-04 13:16:03 +03:00
|
|
|
case 'REQUEST_RESHARE':
|
|
|
|
return $this->reshareRequested($providerId, $notification);
|
2018-06-04 17:36:37 +03:00
|
|
|
case 'RESHARE_UNDO':
|
|
|
|
return $this->undoReshare($providerId, $notification);
|
2018-06-05 20:43:57 +03:00
|
|
|
case 'RESHARE_CHANGE_PERMISSION':
|
|
|
|
return $this->updateResharePermissions($providerId, $notification);
|
2018-05-09 18:06:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-30 18:44:08 +03:00
|
|
|
throw new BadRequestException([$notificationType]);
|
2018-04-30 12:49:24 +03:00
|
|
|
}
|
|
|
|
|
2018-06-13 15:19:59 +03:00
|
|
|
/**
|
|
|
|
* map OCM share type (strings) to Nextcloud internal share types (integer)
|
|
|
|
*
|
|
|
|
* @param string $shareType
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
private function mapShareTypeToNextcloud($shareType) {
|
|
|
|
$result = Share::SHARE_TYPE_USER;
|
|
|
|
if ($shareType === 'group') {
|
|
|
|
$result = Share::SHARE_TYPE_GROUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* notify user about new federated share
|
|
|
|
*
|
|
|
|
* @param $shareWith
|
|
|
|
* @param $shareId
|
|
|
|
* @param $ownerFederatedId
|
|
|
|
* @param $sharedByFederatedId
|
|
|
|
* @param $name
|
|
|
|
*/
|
|
|
|
private function notifyAboutNewShare($shareWith, $shareId, $ownerFederatedId, $sharedByFederatedId, $name) {
|
|
|
|
$notification = $this->notificationManager->createNotification();
|
|
|
|
$notification->setApp('files_sharing')
|
|
|
|
->setUser($shareWith)
|
|
|
|
->setDateTime(new \DateTime())
|
|
|
|
->setObject('remote_share', $shareId)
|
|
|
|
->setSubject('remote_share', [$ownerFederatedId, $sharedByFederatedId, trim($name, '/')]);
|
|
|
|
|
|
|
|
$declineAction = $notification->createAction();
|
|
|
|
$declineAction->setLabel('decline')
|
|
|
|
->setLink($this->urlGenerator->getAbsoluteURL($this->urlGenerator->linkTo('', 'ocs/v2.php/apps/files_sharing/api/v1/remote_shares/pending/' . $shareId)), 'DELETE');
|
|
|
|
$notification->addAction($declineAction);
|
|
|
|
|
|
|
|
$acceptAction = $notification->createAction();
|
|
|
|
$acceptAction->setLabel('accept')
|
|
|
|
->setLink($this->urlGenerator->getAbsoluteURL($this->urlGenerator->linkTo('', 'ocs/v2.php/apps/files_sharing/api/v1/remote_shares/pending/' . $shareId)), 'POST');
|
|
|
|
$notification->addAction($acceptAction);
|
|
|
|
|
|
|
|
$this->notificationManager->notify($notification);
|
|
|
|
}
|
|
|
|
|
2018-05-29 17:21:13 +03:00
|
|
|
/**
|
2018-06-04 13:16:03 +03:00
|
|
|
* process notification that the recipient accepted a share
|
|
|
|
*
|
2018-05-30 18:44:08 +03:00
|
|
|
* @param string $id
|
|
|
|
* @param array $notification
|
2018-06-04 13:16:03 +03:00
|
|
|
* @return array
|
2018-05-29 17:21:13 +03:00
|
|
|
* @throws ActionNotSupportedException
|
|
|
|
* @throws AuthenticationFailedException
|
|
|
|
* @throws BadRequestException
|
|
|
|
* @throws \OC\HintException
|
|
|
|
*/
|
2018-06-07 17:36:26 +03:00
|
|
|
private function shareAccepted($id, array $notification) {
|
2018-05-29 17:21:13 +03:00
|
|
|
|
2018-05-30 18:44:08 +03:00
|
|
|
if (!$this->isS2SEnabled()) {
|
|
|
|
throw new ActionNotSupportedException('Server does not support federated cloud sharing');
|
2018-05-29 17:21:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($notification['sharedSecret'])) {
|
|
|
|
throw new BadRequestException(['sharedSecret']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$token = $notification['sharedSecret'];
|
|
|
|
|
|
|
|
$share = $this->federatedShareProvider->getShareById($id);
|
|
|
|
|
|
|
|
$this->verifyShare($share, $token);
|
|
|
|
$this->executeAcceptShare($share);
|
|
|
|
if ($share->getShareOwner() !== $share->getSharedBy()) {
|
|
|
|
list(, $remote) = $this->addressHandler->splitUserRemote($share->getSharedBy());
|
|
|
|
$remoteId = $this->federatedShareProvider->getRemoteId($share);
|
|
|
|
$notification = $this->cloudFederationFactory->getCloudFederationNotification();
|
|
|
|
$notification->setMessage(
|
|
|
|
'SHARE_ACCEPTED',
|
|
|
|
'file',
|
|
|
|
$remoteId,
|
|
|
|
[
|
|
|
|
'sharedSecret' => $token,
|
|
|
|
'message' => 'Recipient accepted the re-share'
|
|
|
|
]
|
|
|
|
|
|
|
|
);
|
|
|
|
$this->cloudFederationProviderManager->sendNotification($remote, $notification);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-06-04 13:16:03 +03:00
|
|
|
return [];
|
2018-05-29 17:21:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param IShare $share
|
2018-06-07 15:40:12 +03:00
|
|
|
* @throws ShareNotFound
|
2018-05-29 17:21:13 +03:00
|
|
|
*/
|
|
|
|
protected function executeAcceptShare(IShare $share) {
|
|
|
|
try {
|
|
|
|
$fileId = (int)$share->getNode()->getId();
|
|
|
|
list($file, $link) = $this->getFile($this->getCorrectUid($share), $fileId);
|
|
|
|
} catch (\Exception $e) {
|
2018-06-07 15:40:12 +03:00
|
|
|
throw new ShareNotFound();
|
2018-05-29 17:21:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$event = $this->activityManager->generateEvent();
|
|
|
|
$event->setApp('files_sharing')
|
|
|
|
->setType('remote_share')
|
|
|
|
->setAffectedUser($this->getCorrectUid($share))
|
|
|
|
->setSubject(RemoteShares::SUBJECT_REMOTE_SHARE_ACCEPTED, [$share->getSharedWith(), [$fileId => $file]])
|
|
|
|
->setObject('files', $fileId, $file)
|
|
|
|
->setLink($link);
|
|
|
|
$this->activityManager->publish($event);
|
|
|
|
}
|
|
|
|
|
2018-05-30 18:44:08 +03:00
|
|
|
/**
|
2018-06-04 13:16:03 +03:00
|
|
|
* process notification that the recipient declined a share
|
|
|
|
*
|
2018-05-30 18:44:08 +03:00
|
|
|
* @param string $id
|
|
|
|
* @param array $notification
|
2018-06-04 13:16:03 +03:00
|
|
|
* @return array
|
2018-05-30 18:44:08 +03:00
|
|
|
* @throws ActionNotSupportedException
|
|
|
|
* @throws AuthenticationFailedException
|
|
|
|
* @throws BadRequestException
|
|
|
|
* @throws ShareNotFound
|
|
|
|
* @throws \OC\HintException
|
2018-06-04 13:16:03 +03:00
|
|
|
*
|
2018-05-30 18:44:08 +03:00
|
|
|
*/
|
2018-06-07 17:36:26 +03:00
|
|
|
protected function shareDeclined($id, array $notification) {
|
2018-05-30 18:44:08 +03:00
|
|
|
|
|
|
|
if (!$this->isS2SEnabled()) {
|
|
|
|
throw new ActionNotSupportedException('Server does not support federated cloud sharing');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($notification['sharedSecret'])) {
|
|
|
|
throw new BadRequestException(['sharedSecret']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$token = $notification['sharedSecret'];
|
|
|
|
|
|
|
|
$share = $this->federatedShareProvider->getShareById($id);
|
|
|
|
|
|
|
|
$this->verifyShare($share, $token);
|
|
|
|
|
|
|
|
if ($share->getShareOwner() !== $share->getSharedBy()) {
|
|
|
|
list(, $remote) = $this->addressHandler->splitUserRemote($share->getSharedBy());
|
|
|
|
$remoteId = $this->federatedShareProvider->getRemoteId($share);
|
|
|
|
$notification = $this->cloudFederationFactory->getCloudFederationNotification();
|
|
|
|
$notification->setMessage(
|
|
|
|
'SHARE_DECLINED',
|
|
|
|
'file',
|
|
|
|
$remoteId,
|
|
|
|
[
|
|
|
|
'sharedSecret' => $token,
|
|
|
|
'message' => 'Recipient declined the re-share'
|
|
|
|
]
|
|
|
|
|
|
|
|
);
|
|
|
|
$this->cloudFederationProviderManager->sendNotification($remote, $notification);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->executeDeclineShare($share);
|
|
|
|
|
2018-06-04 13:16:03 +03:00
|
|
|
return [];
|
|
|
|
|
2018-05-30 18:44:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* delete declined share and create a activity
|
|
|
|
*
|
|
|
|
* @param IShare $share
|
2018-06-07 15:40:12 +03:00
|
|
|
* @throws ShareNotFound
|
2018-05-30 18:44:08 +03:00
|
|
|
*/
|
|
|
|
protected function executeDeclineShare(IShare $share) {
|
|
|
|
$this->federatedShareProvider->removeShareFromTable($share);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$fileId = (int)$share->getNode()->getId();
|
|
|
|
list($file, $link) = $this->getFile($this->getCorrectUid($share), $fileId);
|
|
|
|
} catch (\Exception $e) {
|
2018-06-07 15:40:12 +03:00
|
|
|
throw new ShareNotFound();
|
2018-05-30 18:44:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$event = $this->activityManager->generateEvent();
|
|
|
|
$event->setApp('files_sharing')
|
|
|
|
->setType('remote_share')
|
|
|
|
->setAffectedUser($this->getCorrectUid($share))
|
|
|
|
->setSubject(RemoteShares::SUBJECT_REMOTE_SHARE_DECLINED, [$share->getSharedWith(), [$fileId => $file]])
|
|
|
|
->setObject('files', $fileId, $file)
|
|
|
|
->setLink($link);
|
|
|
|
$this->activityManager->publish($event);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-06-04 17:36:37 +03:00
|
|
|
/**
|
|
|
|
* received the notification that the owner unshared a file from you
|
|
|
|
*
|
|
|
|
* @param string $id
|
2018-06-07 17:36:26 +03:00
|
|
|
* @param array $notification
|
2018-06-04 17:36:37 +03:00
|
|
|
* @return array
|
|
|
|
* @throws AuthenticationFailedException
|
|
|
|
* @throws BadRequestException
|
|
|
|
*/
|
2018-06-07 17:36:26 +03:00
|
|
|
private function undoReshare($id, array $notification) {
|
2018-06-04 17:36:37 +03:00
|
|
|
if (!isset($notification['sharedSecret'])) {
|
|
|
|
throw new BadRequestException(['sharedSecret']);
|
|
|
|
}
|
|
|
|
$token = $notification['sharedSecret'];
|
|
|
|
|
|
|
|
$share = $this->federatedShareProvider->getShareById($id);
|
|
|
|
|
|
|
|
$this->verifyShare($share, $token);
|
|
|
|
$this->federatedShareProvider->removeShareFromTable($share);
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2018-06-07 15:40:12 +03:00
|
|
|
/**
|
|
|
|
* unshare file from self
|
|
|
|
*
|
|
|
|
* @param string $id
|
|
|
|
* @param array $notification
|
|
|
|
* @return array
|
|
|
|
* @throws ActionNotSupportedException
|
|
|
|
* @throws BadRequestException
|
|
|
|
*/
|
|
|
|
private function unshare($id, array $notification) {
|
2018-06-05 18:21:51 +03:00
|
|
|
|
2018-06-04 17:36:37 +03:00
|
|
|
if (!$this->isS2SEnabled(true)) {
|
|
|
|
throw new ActionNotSupportedException("incoming shares disabled!");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($notification['sharedSecret'])) {
|
|
|
|
throw new BadRequestException(['sharedSecret']);
|
|
|
|
}
|
|
|
|
$token = $notification['sharedSecret'];
|
|
|
|
|
|
|
|
$qb = $this->connection->getQueryBuilder();
|
|
|
|
$qb->select('*')
|
|
|
|
->from('share_external')
|
|
|
|
->where(
|
|
|
|
$qb->expr()->andX(
|
|
|
|
$qb->expr()->eq('remote_id', $qb->createNamedParameter($id)),
|
|
|
|
$qb->expr()->eq('share_token', $qb->createNamedParameter($token))
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$result = $qb->execute();
|
|
|
|
$share = $result->fetch();
|
|
|
|
$result->closeCursor();
|
|
|
|
|
|
|
|
if ($token && $id && !empty($share)) {
|
|
|
|
|
|
|
|
$remote = $this->cleanupRemote($share['remote']);
|
|
|
|
|
|
|
|
$owner = $this->cloudIdManager->getCloudId($share['owner'], $remote);
|
|
|
|
$mountpoint = $share['mountpoint'];
|
|
|
|
$user = $share['user'];
|
|
|
|
|
|
|
|
$qb = $this->connection->getQueryBuilder();
|
|
|
|
$qb->delete('share_external')
|
|
|
|
->where(
|
|
|
|
$qb->expr()->andX(
|
|
|
|
$qb->expr()->eq('remote_id', $qb->createNamedParameter($id)),
|
|
|
|
$qb->expr()->eq('share_token', $qb->createNamedParameter($token))
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$qb->execute();
|
|
|
|
|
2018-06-29 19:02:12 +03:00
|
|
|
// delete all child in case of a group share
|
|
|
|
$qb = $this->connection->getQueryBuilder();
|
|
|
|
$qb->delete('share_external')
|
|
|
|
->where($qb->expr()->eq('parent', $qb->createNamedParameter((int)$share['id'])));
|
|
|
|
$qb->execute();
|
2018-06-04 17:36:37 +03:00
|
|
|
|
2018-06-29 19:02:12 +03:00
|
|
|
if ((int)$share['share_type'] === Share::SHARE_TYPE_USER) {
|
|
|
|
if ($share['accepted']) {
|
|
|
|
$path = trim($mountpoint, '/');
|
|
|
|
} else {
|
|
|
|
$path = trim($share['name'], '/');
|
|
|
|
}
|
|
|
|
$notification = $this->notificationManager->createNotification();
|
|
|
|
$notification->setApp('files_sharing')
|
|
|
|
->setUser($share['user'])
|
|
|
|
->setObject('remote_share', (int)$share['id']);
|
|
|
|
$this->notificationManager->markProcessed($notification);
|
|
|
|
|
|
|
|
$event = $this->activityManager->generateEvent();
|
|
|
|
$event->setApp('files_sharing')
|
|
|
|
->setType('remote_share')
|
|
|
|
->setSubject(RemoteShares::SUBJECT_REMOTE_SHARE_UNSHARED, [$owner->getId(), $path])
|
|
|
|
->setAffectedUser($user)
|
|
|
|
->setObject('remote_share', (int)$share['id'], $path);
|
|
|
|
\OC::$server->getActivityManager()->publish($event);
|
|
|
|
}
|
2018-06-04 17:36:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
private function cleanupRemote($remote) {
|
|
|
|
$remote = substr($remote, strpos($remote, '://') + 3);
|
|
|
|
|
|
|
|
return rtrim($remote, '/');
|
|
|
|
}
|
|
|
|
|
2018-06-04 13:16:03 +03:00
|
|
|
/**
|
|
|
|
* recipient of a share request to re-share the file with another user
|
|
|
|
*
|
2018-06-07 17:36:26 +03:00
|
|
|
* @param string $id
|
|
|
|
* @param array $notification
|
2018-06-04 13:16:03 +03:00
|
|
|
* @return array
|
|
|
|
* @throws AuthenticationFailedException
|
|
|
|
* @throws BadRequestException
|
|
|
|
* @throws ProviderCouldNotAddShareException
|
|
|
|
* @throws ShareNotFound
|
|
|
|
*/
|
2018-06-07 17:36:26 +03:00
|
|
|
protected function reshareRequested($id, array $notification) {
|
2018-06-04 13:16:03 +03:00
|
|
|
|
|
|
|
if (!isset($notification['sharedSecret'])) {
|
|
|
|
throw new BadRequestException(['sharedSecret']);
|
|
|
|
}
|
|
|
|
$token = $notification['sharedSecret'];
|
|
|
|
|
|
|
|
if (!isset($notification['shareWith'])) {
|
|
|
|
throw new BadRequestException(['shareWith']);
|
|
|
|
}
|
|
|
|
$shareWith = $notification['shareWith'];
|
|
|
|
|
|
|
|
if (!isset($notification['senderId'])) {
|
|
|
|
throw new BadRequestException(['senderId']);
|
|
|
|
}
|
|
|
|
$senderId = $notification['senderId'];
|
|
|
|
|
|
|
|
$share = $this->federatedShareProvider->getShareById($id);
|
|
|
|
// don't allow to share a file back to the owner
|
|
|
|
try {
|
|
|
|
list($user, $remote) = $this->addressHandler->splitUserRemote($shareWith);
|
|
|
|
$owner = $share->getShareOwner();
|
|
|
|
$currentServer = $this->addressHandler->generateRemoteURL();
|
|
|
|
if ($this->addressHandler->compareAddresses($user, $remote, $owner, $currentServer)) {
|
|
|
|
throw new ProviderCouldNotAddShareException('Resharing back to the owner is not allowed: ' . $id);
|
|
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
throw new ProviderCouldNotAddShareException($e->getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->verifyShare($share, $token);
|
|
|
|
|
|
|
|
// check if re-sharing is allowed
|
2018-06-05 20:54:42 +03:00
|
|
|
if ($share->getPermissions() & Constants::PERMISSION_SHARE) {
|
2018-06-04 13:16:03 +03:00
|
|
|
// the recipient of the initial share is now the initiator for the re-share
|
|
|
|
$share->setSharedBy($share->getSharedWith());
|
|
|
|
$share->setSharedWith($shareWith);
|
|
|
|
$result = $this->federatedShareProvider->create($share);
|
|
|
|
$this->federatedShareProvider->storeRemoteId((int)$result->getId(), $senderId);
|
|
|
|
return ['token' => $result->getToken(), 'providerId' => $result->getId()];
|
|
|
|
} else {
|
|
|
|
throw new ProviderCouldNotAddShareException('resharing not allowed for share: ' . $id);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-05-30 18:44:08 +03:00
|
|
|
|
2018-06-05 20:43:57 +03:00
|
|
|
/**
|
|
|
|
* update permission of a re-share so that the share dialog shows the right
|
|
|
|
* permission if the owner or the sender changes the permission
|
|
|
|
*
|
|
|
|
* @param string $id
|
|
|
|
* @param array $notification
|
|
|
|
* @return array
|
|
|
|
* @throws AuthenticationFailedException
|
|
|
|
* @throws BadRequestException
|
|
|
|
*/
|
2018-06-07 17:36:26 +03:00
|
|
|
protected function updateResharePermissions($id, array $notification) {
|
2018-06-05 20:43:57 +03:00
|
|
|
|
|
|
|
if (!isset($notification['sharedSecret'])) {
|
|
|
|
throw new BadRequestException(['sharedSecret']);
|
|
|
|
}
|
|
|
|
$token = $notification['sharedSecret'];
|
|
|
|
|
|
|
|
if (!isset($notification['permission'])) {
|
|
|
|
throw new BadRequestException(['permission']);
|
|
|
|
}
|
|
|
|
$ocmPermissions = $notification['permission'];
|
|
|
|
|
|
|
|
$share = $this->federatedShareProvider->getShareById($id);
|
|
|
|
|
|
|
|
$ncPermission = $this->ocmPermissions2ncPermissions($ocmPermissions);
|
|
|
|
|
|
|
|
$this->verifyShare($share, $token);
|
|
|
|
$this->updatePermissionsInDatabase($share, $ncPermission);
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* translate OCM Permissions to Nextcloud permissions
|
|
|
|
*
|
2018-06-07 17:36:26 +03:00
|
|
|
* @param array $ocmPermissions
|
2018-06-05 20:43:57 +03:00
|
|
|
* @return int
|
|
|
|
* @throws BadRequestException
|
|
|
|
*/
|
2018-06-07 17:36:26 +03:00
|
|
|
protected function ocmPermissions2ncPermissions(array $ocmPermissions) {
|
2018-06-05 20:43:57 +03:00
|
|
|
$ncPermissions = 0;
|
|
|
|
foreach($ocmPermissions as $permission) {
|
|
|
|
switch (strtolower($permission)) {
|
|
|
|
case 'read':
|
|
|
|
$ncPermissions += Constants::PERMISSION_READ;
|
|
|
|
break;
|
|
|
|
case 'write':
|
|
|
|
$ncPermissions += Constants::PERMISSION_CREATE + Constants::PERMISSION_UPDATE;
|
|
|
|
break;
|
|
|
|
case 'share':
|
|
|
|
$ncPermissions += Constants::PERMISSION_SHARE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new BadRequestException(['permission']);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return $ncPermissions;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* update permissions in database
|
|
|
|
*
|
|
|
|
* @param IShare $share
|
|
|
|
* @param int $permissions
|
|
|
|
*/
|
|
|
|
protected function updatePermissionsInDatabase(IShare $share, $permissions) {
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$query->update('share')
|
|
|
|
->where($query->expr()->eq('id', $query->createNamedParameter($share->getId())))
|
|
|
|
->set('permissions', $query->createNamedParameter($permissions))
|
|
|
|
->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-29 17:21:13 +03:00
|
|
|
/**
|
|
|
|
* get file
|
|
|
|
*
|
|
|
|
* @param string $user
|
|
|
|
* @param int $fileSource
|
|
|
|
* @return array with internal path of the file and a absolute link to it
|
|
|
|
*/
|
|
|
|
private function getFile($user, $fileSource) {
|
|
|
|
\OC_Util::setupFS($user);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$file = Filesystem::getPath($fileSource);
|
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
$file = null;
|
|
|
|
}
|
|
|
|
$args = Filesystem::is_dir($file) ? array('dir' => $file) : array('dir' => dirname($file), 'scrollto' => $file);
|
|
|
|
$link = Util::linkToAbsolute('files', 'index.php', $args);
|
|
|
|
|
2018-06-07 17:36:26 +03:00
|
|
|
return [$file, $link];
|
2018-05-29 17:21:13 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* check if we are the initiator or the owner of a re-share and return the correct UID
|
|
|
|
*
|
|
|
|
* @param IShare $share
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getCorrectUid(IShare $share) {
|
|
|
|
if ($this->userManager->userExists($share->getShareOwner())) {
|
|
|
|
return $share->getShareOwner();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $share->getSharedBy();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* check if we got the right share
|
|
|
|
*
|
|
|
|
* @param IShare $share
|
|
|
|
* @param string $token
|
|
|
|
* @return bool
|
|
|
|
* @throws AuthenticationFailedException
|
|
|
|
*/
|
|
|
|
protected function verifyShare(IShare $share, $token) {
|
|
|
|
if (
|
|
|
|
$share->getShareType() === FederatedShareProvider::SHARE_TYPE_REMOTE &&
|
|
|
|
$share->getToken() === $token
|
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new AuthenticationFailedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-04-30 12:49:24 +03:00
|
|
|
/**
|
|
|
|
* check if server-to-server sharing is enabled
|
|
|
|
*
|
|
|
|
* @param bool $incoming
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function isS2SEnabled($incoming = false) {
|
|
|
|
|
|
|
|
$result = $this->appManager->isEnabledForUser('files_sharing');
|
|
|
|
|
|
|
|
if ($incoming) {
|
|
|
|
$result = $result && $this->federatedShareProvider->isIncomingServer2serverShareEnabled();
|
|
|
|
} else {
|
|
|
|
$result = $result && $this->federatedShareProvider->isOutgoingServer2serverShareEnabled();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-13 15:46:23 +03:00
|
|
|
/**
|
|
|
|
* get the supported share types, e.g. "user", "group", etc.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*
|
|
|
|
* @since 14.0.0
|
|
|
|
*/
|
|
|
|
public function getSupportedShareTypes() {
|
2018-06-13 15:19:59 +03:00
|
|
|
return ['user', 'group'];
|
2018-06-13 15:46:23 +03:00
|
|
|
}
|
2018-04-30 12:49:24 +03:00
|
|
|
}
|