2015-10-30 12:10:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 15:07:57 +00:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2017-11-06 14:56:42 +00:00
|
|
|
* @author Andrius <andrius.kulbis@ktu.lt>
|
|
|
|
* @author Bjoern Schiessle <bjoern@schiessle.org>
|
2016-05-26 17:56:05 +00:00
|
|
|
* @author Björn Schießle <bjoern@schiessle.org>
|
2017-11-06 14:56:42 +00:00
|
|
|
* @author Jan-Philipp Litza <jplitza@users.noreply.github.com>
|
2016-07-21 15:07:57 +00:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2017-11-06 14:56:42 +00:00
|
|
|
* @author phisch <git@philippschaffrath.de>
|
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2016-07-21 15:07:57 +00:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2017-11-06 14:56:42 +00:00
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
2015-10-30 12:10:08 +00:00
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* 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, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
namespace OC\Share20;
|
|
|
|
|
2016-11-17 13:18:47 +00:00
|
|
|
use OC\Files\Cache\Cache;
|
2018-07-12 12:55:50 +00:00
|
|
|
use OCP\Defaults;
|
2016-10-31 19:19:00 +00:00
|
|
|
use OCP\Files\Folder;
|
2018-07-12 12:55:50 +00:00
|
|
|
use OCP\IL10N;
|
2018-07-12 16:17:22 +00:00
|
|
|
use OCP\IURLGenerator;
|
2018-07-12 12:55:50 +00:00
|
|
|
use OCP\IUser;
|
|
|
|
use OCP\Mail\IMailer;
|
2018-06-19 07:20:35 +00:00
|
|
|
use OCP\Share\IShare;
|
2018-07-12 12:55:50 +00:00
|
|
|
use OCP\Share\IShareHelper;
|
2016-01-27 11:13:53 +00:00
|
|
|
use OCP\Share\IShareProvider;
|
2016-01-15 20:15:25 +00:00
|
|
|
use OC\Share20\Exception\InvalidShare;
|
2016-01-21 13:31:09 +00:00
|
|
|
use OC\Share20\Exception\ProviderException;
|
2016-02-02 13:07:11 +00:00
|
|
|
use OCP\Share\Exceptions\ShareNotFound;
|
2015-10-30 12:10:08 +00:00
|
|
|
use OC\Share20\Exception\BackendError;
|
2016-01-25 16:17:36 +00:00
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
2016-01-15 20:15:25 +00:00
|
|
|
use OCP\IGroup;
|
2015-11-24 08:58:37 +00:00
|
|
|
use OCP\IGroupManager;
|
|
|
|
use OCP\IUserManager;
|
|
|
|
use OCP\Files\IRootFolder;
|
|
|
|
use OCP\IDBConnection;
|
|
|
|
use OCP\Files\Node;
|
2015-10-30 12:10:08 +00:00
|
|
|
|
2016-01-13 12:02:23 +00:00
|
|
|
/**
|
|
|
|
* Class DefaultShareProvider
|
|
|
|
*
|
|
|
|
* @package OC\Share20
|
|
|
|
*/
|
2015-10-30 12:10:08 +00:00
|
|
|
class DefaultShareProvider implements IShareProvider {
|
|
|
|
|
2016-01-11 15:19:07 +00:00
|
|
|
// Special share type for user modified group shares
|
|
|
|
const SHARE_TYPE_USERGROUP = 2;
|
|
|
|
|
2015-11-24 08:58:37 +00:00
|
|
|
/** @var IDBConnection */
|
2015-10-30 12:10:08 +00:00
|
|
|
private $dbConn;
|
|
|
|
|
2015-11-24 08:58:37 +00:00
|
|
|
/** @var IUserManager */
|
2015-10-30 12:10:08 +00:00
|
|
|
private $userManager;
|
|
|
|
|
2015-11-24 08:58:37 +00:00
|
|
|
/** @var IGroupManager */
|
2015-10-30 12:10:08 +00:00
|
|
|
private $groupManager;
|
|
|
|
|
2015-11-24 08:58:37 +00:00
|
|
|
/** @var IRootFolder */
|
2015-11-23 16:10:58 +00:00
|
|
|
private $rootFolder;
|
2015-10-30 12:10:08 +00:00
|
|
|
|
2018-07-12 12:55:50 +00:00
|
|
|
/** @var IMailer */
|
|
|
|
private $mailer;
|
|
|
|
|
|
|
|
/** @var Defaults */
|
|
|
|
private $defaults;
|
|
|
|
|
|
|
|
/** @var IL10N */
|
|
|
|
private $l;
|
|
|
|
|
2018-07-12 16:17:22 +00:00
|
|
|
/** @var IURLGenerator */
|
|
|
|
private $urlGenerator;
|
|
|
|
|
2015-11-24 08:58:37 +00:00
|
|
|
/**
|
|
|
|
* DefaultShareProvider constructor.
|
|
|
|
*
|
|
|
|
* @param IDBConnection $connection
|
|
|
|
* @param IUserManager $userManager
|
|
|
|
* @param IGroupManager $groupManager
|
|
|
|
* @param IRootFolder $rootFolder
|
2018-07-12 12:55:50 +00:00
|
|
|
* @param IMailer $mailer ;
|
|
|
|
* @param Defaults $defaults
|
|
|
|
* @param IL10N $l
|
2018-07-12 16:17:22 +00:00
|
|
|
* @param IURLGenerator $urlGenerator
|
2015-11-24 08:58:37 +00:00
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
IDBConnection $connection,
|
|
|
|
IUserManager $userManager,
|
|
|
|
IGroupManager $groupManager,
|
2018-07-12 12:55:50 +00:00
|
|
|
IRootFolder $rootFolder,
|
|
|
|
IMailer $mailer,
|
|
|
|
Defaults $defaults,
|
2018-07-12 16:17:22 +00:00
|
|
|
IL10N $l,
|
|
|
|
IURLGenerator $urlGenerator) {
|
2015-10-30 12:10:08 +00:00
|
|
|
$this->dbConn = $connection;
|
|
|
|
$this->userManager = $userManager;
|
|
|
|
$this->groupManager = $groupManager;
|
2015-11-23 16:10:58 +00:00
|
|
|
$this->rootFolder = $rootFolder;
|
2018-07-12 12:55:50 +00:00
|
|
|
$this->mailer = $mailer;
|
|
|
|
$this->defaults = $defaults;
|
|
|
|
$this->l = $l;
|
2018-07-12 16:17:22 +00:00
|
|
|
$this->urlGenerator = $urlGenerator;
|
2015-10-30 12:10:08 +00:00
|
|
|
}
|
|
|
|
|
2016-01-13 12:02:23 +00:00
|
|
|
/**
|
|
|
|
* Return the identifier of this provider.
|
|
|
|
*
|
|
|
|
* @return string Containing only [a-zA-Z0-9]
|
|
|
|
*/
|
|
|
|
public function identifier() {
|
|
|
|
return 'ocinternal';
|
|
|
|
}
|
|
|
|
|
2015-10-30 12:10:08 +00:00
|
|
|
/**
|
|
|
|
* Share a path
|
2015-12-15 08:53:16 +00:00
|
|
|
*
|
2016-01-27 11:13:53 +00:00
|
|
|
* @param \OCP\Share\IShare $share
|
|
|
|
* @return \OCP\Share\IShare The share object
|
2015-12-15 08:53:16 +00:00
|
|
|
* @throws ShareNotFound
|
|
|
|
* @throws \Exception
|
2015-10-30 12:10:08 +00:00
|
|
|
*/
|
2016-01-27 11:13:53 +00:00
|
|
|
public function create(\OCP\Share\IShare $share) {
|
2015-12-15 08:53:16 +00:00
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
|
|
|
|
$qb->insert('share');
|
|
|
|
$qb->setValue('share_type', $qb->createNamedParameter($share->getShareType()));
|
|
|
|
|
|
|
|
if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
|
|
|
|
//Set the UID of the user we share with
|
2016-02-02 15:57:50 +00:00
|
|
|
$qb->setValue('share_with', $qb->createNamedParameter($share->getSharedWith()));
|
2019-08-21 14:15:23 +00:00
|
|
|
$qb->setValue('accepted', $qb->createNamedParameter(IShare::STATUS_PENDING));
|
2015-12-15 08:53:16 +00:00
|
|
|
} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
|
|
|
|
//Set the GID of the group we share with
|
2016-02-02 15:57:50 +00:00
|
|
|
$qb->setValue('share_with', $qb->createNamedParameter($share->getSharedWith()));
|
2015-12-15 08:53:16 +00:00
|
|
|
} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
|
2018-10-16 08:31:38 +00:00
|
|
|
//set label for public link
|
|
|
|
$qb->setValue('label', $qb->createNamedParameter($share->getLabel()));
|
2015-12-15 08:53:16 +00:00
|
|
|
//Set the token of the share
|
|
|
|
$qb->setValue('token', $qb->createNamedParameter($share->getToken()));
|
|
|
|
|
|
|
|
//If a password is set store it
|
|
|
|
if ($share->getPassword() !== null) {
|
2017-03-30 15:03:04 +00:00
|
|
|
$qb->setValue('password', $qb->createNamedParameter($share->getPassword()));
|
2015-12-15 08:53:16 +00:00
|
|
|
}
|
|
|
|
|
2018-10-12 18:15:40 +00:00
|
|
|
$qb->setValue('password_by_talk', $qb->createNamedParameter($share->getSendPasswordByTalk(), IQueryBuilder::PARAM_BOOL));
|
|
|
|
|
2015-12-15 08:53:16 +00:00
|
|
|
//If an expiration date is set store it
|
|
|
|
if ($share->getExpirationDate() !== null) {
|
|
|
|
$qb->setValue('expiration', $qb->createNamedParameter($share->getExpirationDate(), 'datetime'));
|
|
|
|
}
|
2016-02-11 10:55:09 +00:00
|
|
|
|
|
|
|
if (method_exists($share, 'getParent')) {
|
|
|
|
$qb->setValue('parent', $qb->createNamedParameter($share->getParent()));
|
|
|
|
}
|
2015-12-15 08:53:16 +00:00
|
|
|
} else {
|
|
|
|
throw new \Exception('invalid share type!');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set what is shares
|
|
|
|
$qb->setValue('item_type', $qb->createParameter('itemType'));
|
2016-01-27 19:51:26 +00:00
|
|
|
if ($share->getNode() instanceof \OCP\Files\File) {
|
2015-12-15 08:53:16 +00:00
|
|
|
$qb->setParameter('itemType', 'file');
|
|
|
|
} else {
|
|
|
|
$qb->setParameter('itemType', 'folder');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the file id
|
2016-01-27 19:51:26 +00:00
|
|
|
$qb->setValue('item_source', $qb->createNamedParameter($share->getNode()->getId()));
|
|
|
|
$qb->setValue('file_source', $qb->createNamedParameter($share->getNode()->getId()));
|
2015-12-15 08:53:16 +00:00
|
|
|
|
|
|
|
// set the permissions
|
|
|
|
$qb->setValue('permissions', $qb->createNamedParameter($share->getPermissions()));
|
|
|
|
|
|
|
|
// Set who created this share
|
2016-02-02 15:57:50 +00:00
|
|
|
$qb->setValue('uid_initiator', $qb->createNamedParameter($share->getSharedBy()));
|
2015-12-15 08:53:16 +00:00
|
|
|
|
|
|
|
// Set who is the owner of this file/folder (and this the owner of the share)
|
2016-02-02 15:57:50 +00:00
|
|
|
$qb->setValue('uid_owner', $qb->createNamedParameter($share->getShareOwner()));
|
2015-12-15 08:53:16 +00:00
|
|
|
|
|
|
|
// Set the file target
|
|
|
|
$qb->setValue('file_target', $qb->createNamedParameter($share->getTarget()));
|
|
|
|
|
|
|
|
// Set the time this share was created
|
|
|
|
$qb->setValue('stime', $qb->createNamedParameter(time()));
|
|
|
|
|
|
|
|
// insert the data and fetch the id of the share
|
|
|
|
$this->dbConn->beginTransaction();
|
|
|
|
$qb->execute();
|
|
|
|
$id = $this->dbConn->lastInsertId('*PREFIX*share');
|
|
|
|
|
|
|
|
// Now fetch the inserted share and create a complete share object
|
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
$qb->select('*')
|
2016-01-15 20:15:25 +00:00
|
|
|
->from('share')
|
2015-12-15 08:53:16 +00:00
|
|
|
->where($qb->expr()->eq('id', $qb->createNamedParameter($id)));
|
|
|
|
|
|
|
|
$cursor = $qb->execute();
|
|
|
|
$data = $cursor->fetch();
|
2016-11-23 11:27:06 +00:00
|
|
|
$this->dbConn->commit();
|
2015-12-15 08:53:16 +00:00
|
|
|
$cursor->closeCursor();
|
|
|
|
|
|
|
|
if ($data === false) {
|
|
|
|
throw new ShareNotFound();
|
|
|
|
}
|
|
|
|
|
2017-09-10 22:56:20 +00:00
|
|
|
$mailSendValue = $share->getMailSend();
|
|
|
|
$data['mail_send'] = ($mailSendValue === null) ? true : $mailSendValue;
|
|
|
|
|
2015-12-15 08:53:16 +00:00
|
|
|
$share = $this->createShare($data);
|
|
|
|
return $share;
|
2015-10-30 12:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update a share
|
|
|
|
*
|
2016-01-27 11:13:53 +00:00
|
|
|
* @param \OCP\Share\IShare $share
|
|
|
|
* @return \OCP\Share\IShare The share object
|
2018-10-16 08:31:38 +00:00
|
|
|
* @throws ShareNotFound
|
|
|
|
* @throws \OCP\Files\InvalidPathException
|
|
|
|
* @throws \OCP\Files\NotFoundException
|
2015-10-30 12:10:08 +00:00
|
|
|
*/
|
2016-01-27 11:13:53 +00:00
|
|
|
public function update(\OCP\Share\IShare $share) {
|
2018-07-12 12:55:50 +00:00
|
|
|
|
|
|
|
$originalShare = $this->getShareById($share->getId());
|
|
|
|
|
2016-01-22 13:52:20 +00:00
|
|
|
if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
|
|
|
|
/*
|
|
|
|
* We allow updating the recipient on user shares.
|
|
|
|
*/
|
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
$qb->update('share')
|
|
|
|
->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
|
2016-02-02 15:57:50 +00:00
|
|
|
->set('share_with', $qb->createNamedParameter($share->getSharedWith()))
|
|
|
|
->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
|
|
|
|
->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
|
2016-01-22 13:52:20 +00:00
|
|
|
->set('permissions', $qb->createNamedParameter($share->getPermissions()))
|
2016-01-27 19:51:26 +00:00
|
|
|
->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
|
|
|
|
->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
|
2017-03-29 14:50:23 +00:00
|
|
|
->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
|
2018-07-12 12:55:50 +00:00
|
|
|
->set('note', $qb->createNamedParameter($share->getNote()))
|
2019-08-22 01:17:17 +00:00
|
|
|
->set('accepted', $qb->createNamedParameter($share->getStatus()))
|
2016-01-22 13:52:20 +00:00
|
|
|
->execute();
|
|
|
|
} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
|
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
$qb->update('share')
|
|
|
|
->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
|
2016-02-02 15:57:50 +00:00
|
|
|
->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
|
|
|
|
->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
|
2016-01-22 13:52:20 +00:00
|
|
|
->set('permissions', $qb->createNamedParameter($share->getPermissions()))
|
2016-01-27 19:51:26 +00:00
|
|
|
->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
|
|
|
|
->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
|
2017-03-29 14:50:23 +00:00
|
|
|
->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
|
2018-07-12 12:55:50 +00:00
|
|
|
->set('note', $qb->createNamedParameter($share->getNote()))
|
2016-01-22 13:52:20 +00:00
|
|
|
->execute();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Update all user defined group shares
|
|
|
|
*/
|
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
$qb->update('share')
|
|
|
|
->where($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))
|
2018-12-04 19:55:14 +00:00
|
|
|
->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)))
|
2016-02-02 15:57:50 +00:00
|
|
|
->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
|
|
|
|
->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
|
2016-01-27 19:51:26 +00:00
|
|
|
->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
|
|
|
|
->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
|
2017-03-29 14:50:23 +00:00
|
|
|
->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
|
2018-07-12 12:55:50 +00:00
|
|
|
->set('note', $qb->createNamedParameter($share->getNote()))
|
2016-01-22 13:52:20 +00:00
|
|
|
->execute();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now update the permissions for all children that have not set it to 0
|
|
|
|
*/
|
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
$qb->update('share')
|
|
|
|
->where($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))
|
|
|
|
->andWhere($qb->expr()->neq('permissions', $qb->createNamedParameter(0)))
|
|
|
|
->set('permissions', $qb->createNamedParameter($share->getPermissions()))
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
|
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
$qb->update('share')
|
|
|
|
->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
|
2017-03-28 13:11:07 +00:00
|
|
|
->set('password', $qb->createNamedParameter($share->getPassword()))
|
2018-10-12 18:15:40 +00:00
|
|
|
->set('password_by_talk', $qb->createNamedParameter($share->getSendPasswordByTalk(), IQueryBuilder::PARAM_BOOL))
|
2016-02-02 15:57:50 +00:00
|
|
|
->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
|
|
|
|
->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
|
2016-01-22 13:52:20 +00:00
|
|
|
->set('permissions', $qb->createNamedParameter($share->getPermissions()))
|
2016-01-27 19:51:26 +00:00
|
|
|
->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
|
|
|
|
->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
|
2016-01-22 13:52:20 +00:00
|
|
|
->set('token', $qb->createNamedParameter($share->getToken()))
|
2016-01-26 13:29:30 +00:00
|
|
|
->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
|
2018-07-12 12:55:50 +00:00
|
|
|
->set('note', $qb->createNamedParameter($share->getNote()))
|
2018-10-17 08:50:58 +00:00
|
|
|
->set('label', $qb->createNamedParameter($share->getLabel()))
|
2018-10-18 10:38:07 +00:00
|
|
|
->set('hide_download', $qb->createNamedParameter($share->getHideDownload() ? 1 : 0), IQueryBuilder::PARAM_INT)
|
2016-01-22 13:52:20 +00:00
|
|
|
->execute();
|
|
|
|
}
|
|
|
|
|
2018-07-12 12:55:50 +00:00
|
|
|
if ($originalShare->getNote() !== $share->getNote() && $share->getNote() !== '') {
|
|
|
|
$this->propagateNote($share);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-26 13:29:30 +00:00
|
|
|
return $share;
|
2015-10-30 12:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-10 13:14:49 +00:00
|
|
|
* Get all children of this share
|
2016-01-29 10:41:28 +00:00
|
|
|
* FIXME: remove once https://github.com/owncloud/core/pull/21660 is in
|
2015-10-30 12:10:08 +00:00
|
|
|
*
|
2016-01-27 11:13:53 +00:00
|
|
|
* @param \OCP\Share\IShare $parent
|
2016-02-02 15:57:50 +00:00
|
|
|
* @return \OCP\Share\IShare[]
|
2015-10-30 12:10:08 +00:00
|
|
|
*/
|
2016-01-27 11:13:53 +00:00
|
|
|
public function getChildren(\OCP\Share\IShare $parent) {
|
2015-10-30 12:10:08 +00:00
|
|
|
$children = [];
|
|
|
|
|
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
$qb->select('*')
|
|
|
|
->from('share')
|
2016-01-11 14:58:32 +00:00
|
|
|
->where($qb->expr()->eq('parent', $qb->createNamedParameter($parent->getId())))
|
2016-01-11 15:19:07 +00:00
|
|
|
->andWhere(
|
|
|
|
$qb->expr()->in(
|
|
|
|
'share_type',
|
2016-01-29 10:41:28 +00:00
|
|
|
$qb->createNamedParameter([
|
|
|
|
\OCP\Share::SHARE_TYPE_USER,
|
|
|
|
\OCP\Share::SHARE_TYPE_GROUP,
|
|
|
|
\OCP\Share::SHARE_TYPE_LINK,
|
|
|
|
], IQueryBuilder::PARAM_INT_ARRAY)
|
2016-01-11 15:19:07 +00:00
|
|
|
)
|
|
|
|
)
|
2016-03-09 10:19:57 +00:00
|
|
|
->andWhere($qb->expr()->orX(
|
|
|
|
$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
|
|
|
|
$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
|
|
|
|
))
|
2015-11-10 13:14:49 +00:00
|
|
|
->orderBy('id');
|
2015-10-30 12:10:08 +00:00
|
|
|
|
|
|
|
$cursor = $qb->execute();
|
|
|
|
while($data = $cursor->fetch()) {
|
|
|
|
$children[] = $this->createShare($data);
|
|
|
|
}
|
|
|
|
$cursor->closeCursor();
|
|
|
|
|
|
|
|
return $children;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a share
|
|
|
|
*
|
2016-01-27 11:13:53 +00:00
|
|
|
* @param \OCP\Share\IShare $share
|
2015-10-30 12:10:08 +00:00
|
|
|
*/
|
2016-01-27 11:13:53 +00:00
|
|
|
public function delete(\OCP\Share\IShare $share) {
|
2015-11-02 20:06:55 +00:00
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
2015-10-30 12:10:08 +00:00
|
|
|
$qb->delete('share')
|
2016-01-11 14:58:32 +00:00
|
|
|
->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())));
|
2016-01-28 19:35:46 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If the share is a group share delete all possible
|
|
|
|
* user defined groups shares.
|
|
|
|
*/
|
|
|
|
if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
|
|
|
|
$qb->orWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())));
|
2015-10-30 12:10:08 +00:00
|
|
|
}
|
2016-01-28 19:35:46 +00:00
|
|
|
|
|
|
|
$qb->execute();
|
2015-10-30 12:10:08 +00:00
|
|
|
}
|
|
|
|
|
2016-01-21 13:31:09 +00:00
|
|
|
/**
|
|
|
|
* Unshare a share from the recipient. If this is a group share
|
|
|
|
* this means we need a special entry in the share db.
|
|
|
|
*
|
2016-01-27 11:13:53 +00:00
|
|
|
* @param \OCP\Share\IShare $share
|
2016-02-02 15:57:50 +00:00
|
|
|
* @param string $recipient UserId of recipient
|
2016-01-21 13:31:09 +00:00
|
|
|
* @throws BackendError
|
|
|
|
* @throws ProviderException
|
|
|
|
*/
|
2016-02-02 15:57:50 +00:00
|
|
|
public function deleteFromSelf(\OCP\Share\IShare $share, $recipient) {
|
2016-01-21 13:31:09 +00:00
|
|
|
if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
|
|
|
|
|
2016-02-02 15:57:50 +00:00
|
|
|
$group = $this->groupManager->get($share->getSharedWith());
|
|
|
|
$user = $this->userManager->get($recipient);
|
2016-01-21 13:31:09 +00:00
|
|
|
|
2017-01-19 14:02:46 +00:00
|
|
|
if (is_null($group)) {
|
|
|
|
throw new ProviderException('Group "' . $share->getSharedWith() . '" does not exist');
|
|
|
|
}
|
|
|
|
|
2016-02-02 15:57:50 +00:00
|
|
|
if (!$group->inGroup($user)) {
|
2016-01-21 13:31:09 +00:00
|
|
|
throw new ProviderException('Recipient not in receiving group');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to fetch user specific share
|
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
$stmt = $qb->select('*')
|
|
|
|
->from('share')
|
|
|
|
->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)))
|
2016-02-02 15:57:50 +00:00
|
|
|
->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($recipient)))
|
2016-01-21 13:31:09 +00:00
|
|
|
->andWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))
|
2016-03-09 10:19:57 +00:00
|
|
|
->andWhere($qb->expr()->orX(
|
|
|
|
$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
|
|
|
|
$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
|
|
|
|
))
|
2016-01-21 13:31:09 +00:00
|
|
|
->execute();
|
|
|
|
|
|
|
|
$data = $stmt->fetch();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if there already is a user specific group share.
|
|
|
|
* If there is update it (if required).
|
|
|
|
*/
|
|
|
|
if ($data === false) {
|
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
|
2016-12-22 20:44:21 +00:00
|
|
|
$type = $share->getNodeType();
|
2016-01-21 13:31:09 +00:00
|
|
|
|
|
|
|
//Insert new share
|
|
|
|
$qb->insert('share')
|
|
|
|
->values([
|
|
|
|
'share_type' => $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP),
|
2016-02-02 15:57:50 +00:00
|
|
|
'share_with' => $qb->createNamedParameter($recipient),
|
|
|
|
'uid_owner' => $qb->createNamedParameter($share->getShareOwner()),
|
|
|
|
'uid_initiator' => $qb->createNamedParameter($share->getSharedBy()),
|
2016-01-21 13:31:09 +00:00
|
|
|
'parent' => $qb->createNamedParameter($share->getId()),
|
|
|
|
'item_type' => $qb->createNamedParameter($type),
|
2016-12-22 20:44:21 +00:00
|
|
|
'item_source' => $qb->createNamedParameter($share->getNodeId()),
|
|
|
|
'file_source' => $qb->createNamedParameter($share->getNodeId()),
|
2016-01-21 13:31:09 +00:00
|
|
|
'file_target' => $qb->createNamedParameter($share->getTarget()),
|
|
|
|
'permissions' => $qb->createNamedParameter(0),
|
2016-01-27 19:51:26 +00:00
|
|
|
'stime' => $qb->createNamedParameter($share->getShareTime()->getTimestamp()),
|
2016-01-21 13:31:09 +00:00
|
|
|
])->execute();
|
|
|
|
|
|
|
|
} else if ($data['permissions'] !== 0) {
|
|
|
|
|
|
|
|
// Update existing usergroup share
|
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
$qb->update('share')
|
|
|
|
->set('permissions', $qb->createNamedParameter(0))
|
|
|
|
->where($qb->expr()->eq('id', $qb->createNamedParameter($data['id'])))
|
|
|
|
->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
|
|
|
|
|
|
|
|
if ($share->getSharedWith() !== $recipient) {
|
|
|
|
throw new ProviderException('Recipient does not match');
|
|
|
|
}
|
|
|
|
|
|
|
|
// We can just delete user and link shares
|
|
|
|
$this->delete($share);
|
|
|
|
} else {
|
|
|
|
throw new ProviderException('Invalid shareType');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-19 07:20:35 +00:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*
|
|
|
|
* For now this only works for group shares
|
|
|
|
* If this gets implemented for normal shares we have to extend it
|
|
|
|
*/
|
|
|
|
public function restore(IShare $share, string $recipient): IShare {
|
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
$qb->select('permissions')
|
|
|
|
->from('share')
|
|
|
|
->where(
|
|
|
|
$qb->expr()->eq('id', $qb->createNamedParameter($share->getId()))
|
|
|
|
);
|
|
|
|
$cursor = $qb->execute();
|
|
|
|
$data = $cursor->fetch();
|
|
|
|
$cursor->closeCursor();
|
|
|
|
|
|
|
|
$originalPermission = $data['permissions'];
|
|
|
|
|
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
$qb->update('share')
|
|
|
|
->set('permissions', $qb->createNamedParameter($originalPermission))
|
|
|
|
->where(
|
|
|
|
$qb->expr()->eq('parent', $qb->createNamedParameter($share->getParent()))
|
|
|
|
)->andWhere(
|
|
|
|
$qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP))
|
|
|
|
)->andWhere(
|
|
|
|
$qb->expr()->eq('share_with', $qb->createNamedParameter($recipient))
|
|
|
|
);
|
|
|
|
|
|
|
|
$qb->execute();
|
|
|
|
|
|
|
|
return $this->getShareById($share->getId(), $recipient);
|
|
|
|
}
|
|
|
|
|
2016-01-29 09:27:39 +00:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2016-02-02 15:57:50 +00:00
|
|
|
public function move(\OCP\Share\IShare $share, $recipient) {
|
2016-01-29 09:27:39 +00:00
|
|
|
if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
|
|
|
|
// Just update the target
|
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
$qb->update('share')
|
|
|
|
->set('file_target', $qb->createNamedParameter($share->getTarget()))
|
|
|
|
->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
|
|
|
|
|
|
|
|
// Check if there is a usergroup share
|
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
$stmt = $qb->select('id')
|
|
|
|
->from('share')
|
|
|
|
->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)))
|
2016-02-02 15:57:50 +00:00
|
|
|
->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($recipient)))
|
2016-01-29 09:27:39 +00:00
|
|
|
->andWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))
|
2016-03-09 10:19:57 +00:00
|
|
|
->andWhere($qb->expr()->orX(
|
|
|
|
$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
|
|
|
|
$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
|
|
|
|
))
|
2016-01-29 09:27:39 +00:00
|
|
|
->setMaxResults(1)
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
$data = $stmt->fetch();
|
|
|
|
$stmt->closeCursor();
|
|
|
|
|
|
|
|
if ($data === false) {
|
|
|
|
// No usergroup share yet. Create one.
|
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
$qb->insert('share')
|
|
|
|
->values([
|
|
|
|
'share_type' => $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP),
|
2016-02-02 15:57:50 +00:00
|
|
|
'share_with' => $qb->createNamedParameter($recipient),
|
|
|
|
'uid_owner' => $qb->createNamedParameter($share->getShareOwner()),
|
|
|
|
'uid_initiator' => $qb->createNamedParameter($share->getSharedBy()),
|
2016-01-29 09:27:39 +00:00
|
|
|
'parent' => $qb->createNamedParameter($share->getId()),
|
2018-01-09 18:51:50 +00:00
|
|
|
'item_type' => $qb->createNamedParameter($share->getNodeType()),
|
|
|
|
'item_source' => $qb->createNamedParameter($share->getNodeId()),
|
|
|
|
'file_source' => $qb->createNamedParameter($share->getNodeId()),
|
2016-01-29 09:27:39 +00:00
|
|
|
'file_target' => $qb->createNamedParameter($share->getTarget()),
|
|
|
|
'permissions' => $qb->createNamedParameter($share->getPermissions()),
|
|
|
|
'stime' => $qb->createNamedParameter($share->getShareTime()->getTimestamp()),
|
|
|
|
])->execute();
|
|
|
|
} else {
|
|
|
|
// Already a usergroup share. Update it.
|
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
$qb->update('share')
|
|
|
|
->set('file_target', $qb->createNamedParameter($share->getTarget()))
|
|
|
|
->where($qb->expr()->eq('id', $qb->createNamedParameter($data['id'])))
|
|
|
|
->execute();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $share;
|
|
|
|
}
|
|
|
|
|
2016-10-31 19:19:00 +00:00
|
|
|
public function getSharesInFolder($userId, Folder $node, $reshares) {
|
2016-06-18 20:04:56 +00:00
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
$qb->select('*')
|
|
|
|
->from('share', 's')
|
|
|
|
->andWhere($qb->expr()->orX(
|
|
|
|
$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
|
|
|
|
$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
|
|
|
|
));
|
|
|
|
|
|
|
|
$qb->andWhere($qb->expr()->orX(
|
|
|
|
$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)),
|
2016-11-01 11:15:14 +00:00
|
|
|
$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)),
|
|
|
|
$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK))
|
2016-06-18 20:04:56 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reshares for this user are shares where they are the owner.
|
|
|
|
*/
|
|
|
|
if ($reshares === false) {
|
|
|
|
$qb->andWhere($qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)));
|
|
|
|
} else {
|
|
|
|
$qb->andWhere(
|
|
|
|
$qb->expr()->orX(
|
|
|
|
$qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
|
|
|
|
$qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-04-10 15:59:40 +00:00
|
|
|
$qb->innerJoin('s', 'filecache' ,'f', $qb->expr()->eq('s.file_source', 'f.fileid'));
|
2016-06-18 20:04:56 +00:00
|
|
|
$qb->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($node->getId())));
|
|
|
|
|
|
|
|
$qb->orderBy('id');
|
|
|
|
|
|
|
|
$cursor = $qb->execute();
|
|
|
|
$shares = [];
|
|
|
|
while ($data = $cursor->fetch()) {
|
|
|
|
$shares[$data['fileid']][] = $this->createShare($data);
|
|
|
|
}
|
|
|
|
$cursor->closeCursor();
|
|
|
|
|
|
|
|
return $shares;
|
|
|
|
}
|
|
|
|
|
2015-10-30 12:10:08 +00:00
|
|
|
/**
|
2016-03-11 07:51:07 +00:00
|
|
|
* @inheritdoc
|
2015-10-30 12:10:08 +00:00
|
|
|
*/
|
2016-02-02 15:57:50 +00:00
|
|
|
public function getSharesBy($userId, $shareType, $node, $reshares, $limit, $offset) {
|
2015-12-03 09:51:41 +00:00
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
$qb->select('*')
|
2016-03-09 10:19:57 +00:00
|
|
|
->from('share')
|
|
|
|
->andWhere($qb->expr()->orX(
|
|
|
|
$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
|
|
|
|
$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
|
|
|
|
));
|
2015-12-03 09:51:41 +00:00
|
|
|
|
|
|
|
$qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter($shareType)));
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reshares for this user are shares where they are the owner.
|
|
|
|
*/
|
|
|
|
if ($reshares === false) {
|
2016-03-07 09:40:49 +00:00
|
|
|
$qb->andWhere($qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)));
|
2015-12-03 09:51:41 +00:00
|
|
|
} else {
|
2018-11-01 15:46:38 +00:00
|
|
|
if ($node === null) {
|
2018-10-29 09:03:52 +00:00
|
|
|
$qb->andWhere(
|
|
|
|
$qb->expr()->orX(
|
|
|
|
$qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
|
|
|
|
$qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2015-12-03 09:51:41 +00:00
|
|
|
}
|
|
|
|
|
2016-01-20 13:07:56 +00:00
|
|
|
if ($node !== null) {
|
|
|
|
$qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
|
2015-12-03 09:51:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($limit !== -1) {
|
|
|
|
$qb->setMaxResults($limit);
|
|
|
|
}
|
|
|
|
|
2016-01-19 13:35:16 +00:00
|
|
|
$qb->setFirstResult($offset);
|
2015-12-03 09:51:41 +00:00
|
|
|
$qb->orderBy('id');
|
|
|
|
|
|
|
|
$cursor = $qb->execute();
|
|
|
|
$shares = [];
|
|
|
|
while($data = $cursor->fetch()) {
|
|
|
|
$shares[] = $this->createShare($data);
|
|
|
|
}
|
|
|
|
$cursor->closeCursor();
|
|
|
|
|
|
|
|
return $shares;
|
2015-10-30 12:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-01-29 09:07:28 +00:00
|
|
|
* @inheritdoc
|
2015-10-30 12:10:08 +00:00
|
|
|
*/
|
2016-02-02 15:57:50 +00:00
|
|
|
public function getShareById($id, $recipientId = null) {
|
2015-10-30 12:10:08 +00:00
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
|
|
|
|
$qb->select('*')
|
|
|
|
->from('share')
|
2016-01-11 15:19:07 +00:00
|
|
|
->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
|
|
|
|
->andWhere(
|
|
|
|
$qb->expr()->in(
|
|
|
|
'share_type',
|
2016-01-29 09:07:28 +00:00
|
|
|
$qb->createNamedParameter([
|
|
|
|
\OCP\Share::SHARE_TYPE_USER,
|
|
|
|
\OCP\Share::SHARE_TYPE_GROUP,
|
|
|
|
\OCP\Share::SHARE_TYPE_LINK,
|
|
|
|
], IQueryBuilder::PARAM_INT_ARRAY)
|
2016-01-11 15:19:07 +00:00
|
|
|
)
|
2016-03-09 10:19:57 +00:00
|
|
|
)
|
|
|
|
->andWhere($qb->expr()->orX(
|
|
|
|
$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
|
|
|
|
$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
|
|
|
|
));
|
2016-11-17 13:18:47 +00:00
|
|
|
|
2015-10-30 12:10:08 +00:00
|
|
|
$cursor = $qb->execute();
|
|
|
|
$data = $cursor->fetch();
|
|
|
|
$cursor->closeCursor();
|
|
|
|
|
|
|
|
if ($data === false) {
|
|
|
|
throw new ShareNotFound();
|
|
|
|
}
|
|
|
|
|
2016-01-14 15:44:59 +00:00
|
|
|
try {
|
|
|
|
$share = $this->createShare($data);
|
|
|
|
} catch (InvalidShare $e) {
|
|
|
|
throw new ShareNotFound();
|
|
|
|
}
|
2015-10-30 12:10:08 +00:00
|
|
|
|
2016-01-29 09:07:28 +00:00
|
|
|
// If the recipient is set for a group share resolve to that user
|
2016-02-02 15:57:50 +00:00
|
|
|
if ($recipientId !== null && $share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
|
2016-10-06 12:27:44 +00:00
|
|
|
$share = $this->resolveGroupShares([$share], $recipientId)[0];
|
2016-01-29 09:07:28 +00:00
|
|
|
}
|
|
|
|
|
2015-10-30 12:10:08 +00:00
|
|
|
return $share;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get shares for a given path
|
|
|
|
*
|
|
|
|
* @param \OCP\Files\Node $path
|
2016-02-02 15:57:50 +00:00
|
|
|
* @return \OCP\Share\IShare[]
|
2015-10-30 12:10:08 +00:00
|
|
|
*/
|
2015-12-15 08:53:16 +00:00
|
|
|
public function getSharesByPath(Node $path) {
|
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
|
|
|
|
$cursor = $qb->select('*')
|
|
|
|
->from('share')
|
|
|
|
->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($path->getId())))
|
|
|
|
->andWhere(
|
|
|
|
$qb->expr()->orX(
|
|
|
|
$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)),
|
|
|
|
$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP))
|
|
|
|
)
|
2016-03-09 10:19:57 +00:00
|
|
|
)
|
|
|
|
->andWhere($qb->expr()->orX(
|
|
|
|
$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
|
|
|
|
$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
|
|
|
|
))
|
|
|
|
->execute();
|
2015-12-15 08:53:16 +00:00
|
|
|
|
|
|
|
$shares = [];
|
|
|
|
while($data = $cursor->fetch()) {
|
|
|
|
$shares[] = $this->createShare($data);
|
|
|
|
}
|
|
|
|
$cursor->closeCursor();
|
|
|
|
|
|
|
|
return $shares;
|
2015-10-30 12:10:08 +00:00
|
|
|
}
|
|
|
|
|
2016-09-02 09:23:55 +00:00
|
|
|
/**
|
|
|
|
* Returns whether the given database result can be interpreted as
|
|
|
|
* a share with accessible file (not trashed, not deleted)
|
|
|
|
*/
|
|
|
|
private function isAccessibleResult($data) {
|
|
|
|
// exclude shares leading to deleted file entries
|
|
|
|
if ($data['fileid'] === null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// exclude shares leading to trashbin on home storages
|
|
|
|
$pathSections = explode('/', $data['path'], 2);
|
|
|
|
// FIXME: would not detect rare md5'd home storage case properly
|
2017-03-28 13:11:07 +00:00
|
|
|
if ($pathSections[0] !== 'files'
|
2017-03-09 12:36:20 +00:00
|
|
|
&& in_array(explode(':', $data['storage_string_id'], 2)[0], array('home', 'object'))) {
|
2016-09-02 09:23:55 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-10-30 12:10:08 +00:00
|
|
|
/**
|
2016-01-29 14:26:04 +00:00
|
|
|
* @inheritdoc
|
2015-10-30 12:10:08 +00:00
|
|
|
*/
|
2016-02-02 15:57:50 +00:00
|
|
|
public function getSharedWith($userId, $shareType, $node, $limit, $offset) {
|
2016-01-19 12:55:51 +00:00
|
|
|
/** @var Share[] $shares */
|
2015-12-03 09:51:41 +00:00
|
|
|
$shares = [];
|
|
|
|
|
|
|
|
if ($shareType === \OCP\Share::SHARE_TYPE_USER) {
|
2016-01-19 12:55:51 +00:00
|
|
|
//Get shares directly with this user
|
2015-12-03 09:51:41 +00:00
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
2016-11-17 13:18:47 +00:00
|
|
|
$qb->select('s.*',
|
|
|
|
'f.fileid', 'f.path', 'f.permissions AS f_permissions', 'f.storage', 'f.path_hash',
|
|
|
|
'f.parent AS f_parent', 'f.name', 'f.mimetype', 'f.mimepart', 'f.size', 'f.mtime', 'f.storage_mtime',
|
|
|
|
'f.encrypted', 'f.unencrypted_size', 'f.etag', 'f.checksum'
|
|
|
|
)
|
2016-09-02 09:23:55 +00:00
|
|
|
->selectAlias('st.id', 'storage_string_id')
|
|
|
|
->from('share', 's')
|
|
|
|
->leftJoin('s', 'filecache', 'f', $qb->expr()->eq('s.file_source', 'f.fileid'))
|
|
|
|
->leftJoin('f', 'storages', 'st', $qb->expr()->eq('f.storage', 'st.numeric_id'));
|
2015-12-03 09:51:41 +00:00
|
|
|
|
|
|
|
// Order by id
|
2016-09-02 09:23:55 +00:00
|
|
|
$qb->orderBy('s.id');
|
2015-12-03 09:51:41 +00:00
|
|
|
|
|
|
|
// Set limit and offset
|
|
|
|
if ($limit !== -1) {
|
|
|
|
$qb->setMaxResults($limit);
|
|
|
|
}
|
|
|
|
$qb->setFirstResult($offset);
|
|
|
|
|
2016-03-09 10:19:57 +00:00
|
|
|
$qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)))
|
|
|
|
->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId)))
|
|
|
|
->andWhere($qb->expr()->orX(
|
|
|
|
$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
|
|
|
|
$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
|
|
|
|
));
|
2015-12-03 09:51:41 +00:00
|
|
|
|
2016-01-29 14:26:04 +00:00
|
|
|
// Filter by node if provided
|
|
|
|
if ($node !== null) {
|
|
|
|
$qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
|
|
|
|
}
|
|
|
|
|
2015-12-03 09:51:41 +00:00
|
|
|
$cursor = $qb->execute();
|
|
|
|
|
|
|
|
while($data = $cursor->fetch()) {
|
2016-09-02 09:23:55 +00:00
|
|
|
if ($this->isAccessibleResult($data)) {
|
|
|
|
$shares[] = $this->createShare($data);
|
|
|
|
}
|
2015-12-03 09:51:41 +00:00
|
|
|
}
|
|
|
|
$cursor->closeCursor();
|
|
|
|
|
|
|
|
} else if ($shareType === \OCP\Share::SHARE_TYPE_GROUP) {
|
2016-02-02 15:57:50 +00:00
|
|
|
$user = $this->userManager->get($userId);
|
2016-01-19 12:55:51 +00:00
|
|
|
$allGroups = $this->groupManager->getUserGroups($user);
|
|
|
|
|
2016-01-26 11:58:41 +00:00
|
|
|
/** @var Share[] $shares2 */
|
|
|
|
$shares2 = [];
|
|
|
|
|
2016-01-19 12:55:51 +00:00
|
|
|
$start = 0;
|
|
|
|
while(true) {
|
|
|
|
$groups = array_slice($allGroups, $start, 100);
|
|
|
|
$start += 100;
|
|
|
|
|
|
|
|
if ($groups === []) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
2016-11-17 22:30:50 +00:00
|
|
|
$qb->select('s.*',
|
|
|
|
'f.fileid', 'f.path', 'f.permissions AS f_permissions', 'f.storage', 'f.path_hash',
|
|
|
|
'f.parent AS f_parent', 'f.name', 'f.mimetype', 'f.mimepart', 'f.size', 'f.mtime', 'f.storage_mtime',
|
|
|
|
'f.encrypted', 'f.unencrypted_size', 'f.etag', 'f.checksum'
|
|
|
|
)
|
2016-09-02 09:23:55 +00:00
|
|
|
->selectAlias('st.id', 'storage_string_id')
|
|
|
|
->from('share', 's')
|
|
|
|
->leftJoin('s', 'filecache', 'f', $qb->expr()->eq('s.file_source', 'f.fileid'))
|
|
|
|
->leftJoin('f', 'storages', 'st', $qb->expr()->eq('f.storage', 'st.numeric_id'))
|
|
|
|
->orderBy('s.id')
|
2016-01-19 12:55:51 +00:00
|
|
|
->setFirstResult(0);
|
|
|
|
|
|
|
|
if ($limit !== -1) {
|
2016-01-20 13:07:56 +00:00
|
|
|
$qb->setMaxResults($limit - count($shares));
|
2016-01-19 12:55:51 +00:00
|
|
|
}
|
|
|
|
|
2016-01-29 14:26:04 +00:00
|
|
|
// Filter by node if provided
|
|
|
|
if ($node !== null) {
|
|
|
|
$qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
|
|
|
|
}
|
|
|
|
|
2017-06-12 13:46:19 +00:00
|
|
|
|
|
|
|
$groups = array_filter($groups, function($group) { return $group instanceof IGroup; });
|
2016-01-19 12:55:51 +00:00
|
|
|
$groups = array_map(function(IGroup $group) { return $group->getGID(); }, $groups);
|
|
|
|
|
2016-03-09 10:19:57 +00:00
|
|
|
$qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)))
|
|
|
|
->andWhere($qb->expr()->in('share_with', $qb->createNamedParameter(
|
|
|
|
$groups,
|
|
|
|
IQueryBuilder::PARAM_STR_ARRAY
|
|
|
|
)))
|
|
|
|
->andWhere($qb->expr()->orX(
|
|
|
|
$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
|
|
|
|
$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
|
|
|
|
));
|
2016-01-19 12:55:51 +00:00
|
|
|
|
|
|
|
$cursor = $qb->execute();
|
|
|
|
while($data = $cursor->fetch()) {
|
2016-01-20 13:07:56 +00:00
|
|
|
if ($offset > 0) {
|
|
|
|
$offset--;
|
|
|
|
continue;
|
|
|
|
}
|
2016-09-02 09:23:55 +00:00
|
|
|
|
|
|
|
if ($this->isAccessibleResult($data)) {
|
|
|
|
$shares2[] = $this->createShare($data);
|
|
|
|
}
|
2016-01-19 12:55:51 +00:00
|
|
|
}
|
|
|
|
$cursor->closeCursor();
|
|
|
|
}
|
2016-01-20 13:07:56 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Resolve all group shares to user specific shares
|
|
|
|
*/
|
2016-10-06 12:27:44 +00:00
|
|
|
$shares = $this->resolveGroupShares($shares2, $userId);
|
2015-12-03 09:51:41 +00:00
|
|
|
} else {
|
2016-01-20 13:17:25 +00:00
|
|
|
throw new BackendError('Invalid backend');
|
2015-12-03 09:51:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return $shares;
|
2015-10-30 12:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-01-14 15:44:59 +00:00
|
|
|
* Get a share by token
|
2015-10-30 12:10:08 +00:00
|
|
|
*
|
|
|
|
* @param string $token
|
2016-02-02 15:57:50 +00:00
|
|
|
* @return \OCP\Share\IShare
|
2016-01-14 15:44:59 +00:00
|
|
|
* @throws ShareNotFound
|
2015-10-30 12:10:08 +00:00
|
|
|
*/
|
2016-01-14 15:44:59 +00:00
|
|
|
public function getShareByToken($token) {
|
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
|
|
|
|
$cursor = $qb->select('*')
|
|
|
|
->from('share')
|
|
|
|
->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK)))
|
|
|
|
->andWhere($qb->expr()->eq('token', $qb->createNamedParameter($token)))
|
2016-03-09 10:19:57 +00:00
|
|
|
->andWhere($qb->expr()->orX(
|
|
|
|
$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
|
|
|
|
$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
|
|
|
|
))
|
2016-01-14 15:44:59 +00:00
|
|
|
->execute();
|
|
|
|
|
|
|
|
$data = $cursor->fetch();
|
|
|
|
|
|
|
|
if ($data === false) {
|
|
|
|
throw new ShareNotFound();
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$share = $this->createShare($data);
|
|
|
|
} catch (InvalidShare $e) {
|
|
|
|
throw new ShareNotFound();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $share;
|
2015-10-30 12:10:08 +00:00
|
|
|
}
|
2016-11-17 13:18:47 +00:00
|
|
|
|
2015-10-30 12:10:08 +00:00
|
|
|
/**
|
|
|
|
* Create a share object from an database row
|
|
|
|
*
|
|
|
|
* @param mixed[] $data
|
2016-01-27 19:51:26 +00:00
|
|
|
* @return \OCP\Share\IShare
|
2016-01-15 20:15:25 +00:00
|
|
|
* @throws InvalidShare
|
2015-10-30 12:10:08 +00:00
|
|
|
*/
|
|
|
|
private function createShare($data) {
|
2016-05-11 18:48:27 +00:00
|
|
|
$share = new Share($this->rootFolder, $this->userManager);
|
2015-10-30 12:10:08 +00:00
|
|
|
$share->setId((int)$data['id'])
|
|
|
|
->setShareType((int)$data['share_type'])
|
2015-11-02 20:06:55 +00:00
|
|
|
->setPermissions((int)$data['permissions'])
|
2015-11-06 11:05:19 +00:00
|
|
|
->setTarget($data['file_target'])
|
2018-07-12 14:09:05 +00:00
|
|
|
->setNote($data['note'])
|
2018-10-16 08:31:38 +00:00
|
|
|
->setMailSend((bool)$data['mail_send'])
|
2019-08-21 14:15:23 +00:00
|
|
|
->setStatus((int)$data['accepted'])
|
2018-10-16 08:31:38 +00:00
|
|
|
->setLabel($data['label']);
|
2015-10-30 12:10:08 +00:00
|
|
|
|
2016-01-27 19:51:26 +00:00
|
|
|
$shareTime = new \DateTime();
|
|
|
|
$shareTime->setTimestamp((int)$data['stime']);
|
|
|
|
$share->setShareTime($shareTime);
|
|
|
|
|
2015-10-30 12:10:08 +00:00
|
|
|
if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
|
2016-02-02 15:57:50 +00:00
|
|
|
$share->setSharedWith($data['share_with']);
|
2019-07-03 09:31:25 +00:00
|
|
|
$user = $this->userManager->get($data['share_with']);
|
|
|
|
if ($user !== null) {
|
|
|
|
$share->setSharedWithDisplayName($user->getDisplayName());
|
|
|
|
}
|
2015-10-30 12:10:08 +00:00
|
|
|
} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
|
2016-02-02 15:57:50 +00:00
|
|
|
$share->setSharedWith($data['share_with']);
|
2015-10-30 12:10:08 +00:00
|
|
|
} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
|
2017-03-28 13:11:07 +00:00
|
|
|
$share->setPassword($data['password']);
|
2018-10-12 18:15:40 +00:00
|
|
|
$share->setSendPasswordByTalk((bool)$data['password_by_talk']);
|
2015-10-30 12:10:08 +00:00
|
|
|
$share->setToken($data['token']);
|
|
|
|
}
|
|
|
|
|
2016-03-07 09:40:49 +00:00
|
|
|
$share->setSharedBy($data['uid_initiator']);
|
|
|
|
$share->setShareOwner($data['uid_owner']);
|
2015-11-23 16:10:58 +00:00
|
|
|
|
2016-02-04 11:51:23 +00:00
|
|
|
$share->setNodeId((int)$data['file_source']);
|
2016-02-04 13:28:09 +00:00
|
|
|
$share->setNodeType($data['item_type']);
|
2015-10-30 12:10:08 +00:00
|
|
|
|
|
|
|
if ($data['expiration'] !== null) {
|
|
|
|
$expiration = \DateTime::createFromFormat('Y-m-d H:i:s', $data['expiration']);
|
|
|
|
$share->setExpirationDate($expiration);
|
|
|
|
}
|
|
|
|
|
2016-11-17 13:18:47 +00:00
|
|
|
if (isset($data['f_permissions'])) {
|
|
|
|
$entryData = $data;
|
|
|
|
$entryData['permissions'] = $entryData['f_permissions'];
|
2017-07-23 19:03:26 +00:00
|
|
|
$entryData['parent'] = $entryData['f_parent'];
|
2016-11-17 13:18:47 +00:00
|
|
|
$share->setNodeCacheEntry(Cache::cacheEntryFromData($entryData,
|
|
|
|
\OC::$server->getMimeTypeLoader()));
|
|
|
|
}
|
|
|
|
|
2016-01-13 12:02:23 +00:00
|
|
|
$share->setProviderId($this->identifier());
|
2018-10-18 10:38:07 +00:00
|
|
|
$share->setHideDownload((int)$data['hide_download'] === 1);
|
2016-01-13 12:02:23 +00:00
|
|
|
|
2015-10-30 12:10:08 +00:00
|
|
|
return $share;
|
|
|
|
}
|
|
|
|
|
2016-01-19 12:55:51 +00:00
|
|
|
/**
|
2016-10-06 12:27:44 +00:00
|
|
|
* @param Share[] $shares
|
|
|
|
* @param $userId
|
|
|
|
* @return Share[] The updates shares if no update is found for a share return the original
|
2016-01-19 12:55:51 +00:00
|
|
|
*/
|
2016-10-06 12:27:44 +00:00
|
|
|
private function resolveGroupShares($shares, $userId) {
|
|
|
|
$result = [];
|
2016-01-19 12:55:51 +00:00
|
|
|
|
2016-10-06 12:27:44 +00:00
|
|
|
$start = 0;
|
|
|
|
while(true) {
|
|
|
|
/** @var Share[] $shareSlice */
|
|
|
|
$shareSlice = array_slice($shares, $start, 100);
|
|
|
|
$start += 100;
|
|
|
|
|
|
|
|
if ($shareSlice === []) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var int[] $ids */
|
|
|
|
$ids = [];
|
|
|
|
/** @var Share[] $shareMap */
|
|
|
|
$shareMap = [];
|
|
|
|
|
|
|
|
foreach ($shareSlice as $share) {
|
|
|
|
$ids[] = (int)$share->getId();
|
|
|
|
$shareMap[$share->getId()] = $share;
|
|
|
|
}
|
2016-01-19 12:55:51 +00:00
|
|
|
|
2016-10-06 12:27:44 +00:00
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
|
|
|
|
$query = $qb->select('*')
|
|
|
|
->from('share')
|
|
|
|
->where($qb->expr()->in('parent', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)))
|
|
|
|
->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId)))
|
|
|
|
->andWhere($qb->expr()->orX(
|
|
|
|
$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
|
|
|
|
$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
|
|
|
|
));
|
2016-01-19 12:55:51 +00:00
|
|
|
|
2016-10-06 12:27:44 +00:00
|
|
|
$stmt = $query->execute();
|
|
|
|
|
|
|
|
while($data = $stmt->fetch()) {
|
|
|
|
$shareMap[$data['parent']]->setPermissions((int)$data['permissions']);
|
2019-08-21 14:15:23 +00:00
|
|
|
$shareMap[$data['parent']]->setStatus((int)$data['accepted']);
|
2016-10-06 12:27:44 +00:00
|
|
|
$shareMap[$data['parent']]->setTarget($data['file_target']);
|
2018-06-19 07:20:35 +00:00
|
|
|
$shareMap[$data['parent']]->setParent($data['parent']);
|
2016-10-06 12:27:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$stmt->closeCursor();
|
|
|
|
|
|
|
|
foreach ($shareMap as $share) {
|
|
|
|
$result[] = $share;
|
|
|
|
}
|
2016-01-19 12:55:51 +00:00
|
|
|
}
|
|
|
|
|
2016-10-06 12:27:44 +00:00
|
|
|
return $result;
|
2016-01-19 12:55:51 +00:00
|
|
|
}
|
|
|
|
|
2016-04-04 10:28:19 +00:00
|
|
|
/**
|
|
|
|
* A user is deleted from the system
|
|
|
|
* So clean up the relevant shares.
|
|
|
|
*
|
|
|
|
* @param string $uid
|
|
|
|
* @param int $shareType
|
|
|
|
*/
|
|
|
|
public function userDeleted($uid, $shareType) {
|
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
|
|
|
|
$qb->delete('share');
|
|
|
|
|
|
|
|
if ($shareType === \OCP\Share::SHARE_TYPE_USER) {
|
|
|
|
/*
|
|
|
|
* Delete all user shares that are owned by this user
|
|
|
|
* or that are received by this user
|
|
|
|
*/
|
|
|
|
|
|
|
|
$qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)));
|
|
|
|
|
|
|
|
$qb->andWhere(
|
|
|
|
$qb->expr()->orX(
|
|
|
|
$qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid)),
|
|
|
|
$qb->expr()->eq('share_with', $qb->createNamedParameter($uid))
|
|
|
|
)
|
|
|
|
);
|
|
|
|
} else if ($shareType === \OCP\Share::SHARE_TYPE_GROUP) {
|
|
|
|
/*
|
|
|
|
* Delete all group shares that are owned by this user
|
|
|
|
* Or special user group shares that are received by this user
|
|
|
|
*/
|
|
|
|
$qb->where(
|
|
|
|
$qb->expr()->andX(
|
|
|
|
$qb->expr()->orX(
|
|
|
|
$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)),
|
|
|
|
$qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP))
|
|
|
|
),
|
|
|
|
$qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid))
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$qb->orWhere(
|
|
|
|
$qb->expr()->andX(
|
|
|
|
$qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)),
|
|
|
|
$qb->expr()->eq('share_with', $qb->createNamedParameter($uid))
|
|
|
|
)
|
|
|
|
);
|
|
|
|
} else if ($shareType === \OCP\Share::SHARE_TYPE_LINK) {
|
|
|
|
/*
|
|
|
|
* Delete all link shares owned by this user.
|
|
|
|
* And all link shares initiated by this user (until #22327 is in)
|
|
|
|
*/
|
|
|
|
$qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK)));
|
|
|
|
|
|
|
|
$qb->andWhere(
|
|
|
|
$qb->expr()->orX(
|
|
|
|
$qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid)),
|
|
|
|
$qb->expr()->eq('uid_initiator', $qb->createNamedParameter($uid))
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$qb->execute();
|
|
|
|
}
|
2016-04-12 07:46:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete all shares received by this group. As well as any custom group
|
|
|
|
* shares for group members.
|
|
|
|
*
|
|
|
|
* @param string $gid
|
|
|
|
*/
|
|
|
|
public function groupDeleted($gid) {
|
|
|
|
/*
|
|
|
|
* First delete all custom group shares for group members
|
|
|
|
*/
|
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
$qb->select('id')
|
|
|
|
->from('share')
|
|
|
|
->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)))
|
|
|
|
->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($gid)));
|
|
|
|
|
|
|
|
$cursor = $qb->execute();
|
|
|
|
$ids = [];
|
|
|
|
while($row = $cursor->fetch()) {
|
|
|
|
$ids[] = (int)$row['id'];
|
|
|
|
}
|
|
|
|
$cursor->closeCursor();
|
|
|
|
|
|
|
|
if (!empty($ids)) {
|
|
|
|
$chunks = array_chunk($ids, 100);
|
|
|
|
foreach ($chunks as $chunk) {
|
|
|
|
$qb->delete('share')
|
|
|
|
->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)))
|
|
|
|
->andWhere($qb->expr()->in('parent', $qb->createNamedParameter($chunk, IQueryBuilder::PARAM_INT_ARRAY)));
|
|
|
|
$qb->execute();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now delete all the group shares
|
|
|
|
*/
|
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
$qb->delete('share')
|
|
|
|
->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)))
|
|
|
|
->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($gid)));
|
|
|
|
$qb->execute();
|
|
|
|
}
|
2016-04-13 13:00:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete custom group shares to this group for this user
|
|
|
|
*
|
|
|
|
* @param string $uid
|
|
|
|
* @param string $gid
|
|
|
|
*/
|
|
|
|
public function userDeletedFromGroup($uid, $gid) {
|
|
|
|
/*
|
|
|
|
* Get all group shares
|
|
|
|
*/
|
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
$qb->select('id')
|
|
|
|
->from('share')
|
|
|
|
->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)))
|
|
|
|
->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($gid)));
|
|
|
|
|
|
|
|
$cursor = $qb->execute();
|
|
|
|
$ids = [];
|
|
|
|
while($row = $cursor->fetch()) {
|
|
|
|
$ids[] = (int)$row['id'];
|
|
|
|
}
|
|
|
|
$cursor->closeCursor();
|
|
|
|
|
|
|
|
if (!empty($ids)) {
|
|
|
|
$chunks = array_chunk($ids, 100);
|
|
|
|
foreach ($chunks as $chunk) {
|
|
|
|
/*
|
|
|
|
* Delete all special shares wit this users for the found group shares
|
|
|
|
*/
|
|
|
|
$qb->delete('share')
|
|
|
|
->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)))
|
|
|
|
->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($uid)))
|
|
|
|
->andWhere($qb->expr()->in('parent', $qb->createNamedParameter($chunk, IQueryBuilder::PARAM_INT_ARRAY)));
|
|
|
|
$qb->execute();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-22 20:44:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
public function getAccessList($nodes, $currentAccess) {
|
|
|
|
$ids = [];
|
|
|
|
foreach ($nodes as $node) {
|
|
|
|
$ids[] = $node->getId();
|
|
|
|
}
|
|
|
|
|
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
|
|
|
|
$or = $qb->expr()->orX(
|
|
|
|
$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)),
|
|
|
|
$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)),
|
|
|
|
$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK))
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($currentAccess) {
|
|
|
|
$or->add($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)));
|
|
|
|
}
|
|
|
|
|
2017-03-27 14:06:31 +00:00
|
|
|
$qb->select('id', 'parent', 'share_type', 'share_with', 'file_source', 'file_target', 'permissions')
|
2016-12-22 20:44:21 +00:00
|
|
|
->from('share')
|
|
|
|
->where(
|
|
|
|
$or
|
|
|
|
)
|
|
|
|
->andWhere($qb->expr()->in('file_source', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)))
|
|
|
|
->andWhere($qb->expr()->orX(
|
|
|
|
$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
|
|
|
|
$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
|
|
|
|
));
|
|
|
|
$cursor = $qb->execute();
|
|
|
|
|
|
|
|
$users = [];
|
|
|
|
$link = false;
|
|
|
|
while($row = $cursor->fetch()) {
|
|
|
|
$type = (int)$row['share_type'];
|
|
|
|
if ($type === \OCP\Share::SHARE_TYPE_USER) {
|
|
|
|
$uid = $row['share_with'];
|
2017-03-27 14:06:31 +00:00
|
|
|
$users[$uid] = isset($users[$uid]) ? $users[$uid] : [];
|
|
|
|
$users[$uid][$row['id']] = $row;
|
2016-12-22 20:44:21 +00:00
|
|
|
} else if ($type === \OCP\Share::SHARE_TYPE_GROUP) {
|
|
|
|
$gid = $row['share_with'];
|
|
|
|
$group = $this->groupManager->get($gid);
|
|
|
|
|
Fix 500 Internal Server Error on writing
In some not yet completely determined configurations, the following error could occur while writing a file:
Error: Call to a member function getUsers() on null
/var/www/nextcloud/lib/private/Share20/Manager.php - line 1277: OC\Share20\DefaultShareProvider->getAccessList(Array, true)
/var/www/nextcloud/lib/private/Share20/ShareHelper.php - line 51: OC\Share20\Manager->getAccessList(Object(OC\Files\Node\Folder), true, true)
/var/www/nextcloud/apps/activity/lib/FilesHooks.php - line 616: OC\Share20\ShareHelper->getPathsForAccessList(Object(OC\Files\Node\File))
/var/www/nextcloud/apps/activity/lib/FilesHooks.php - line 196: OCA\Activity\FilesHooks->getUserPathsFromPath('/path/to/file', 'user')
/var/www/nextcloud/apps/activity/lib/FilesHooks.php - line 157: OCA\Activity\FilesHooks->addNotificationsForFileAction('/path/to/file', 'file_changed', 'changed_self', 'changed_by')
/var/www/nextcloud/apps/activity/lib/FilesHooksStatic.php - line 55: OCA\Activity\FilesHooks->fileUpdate('/path/to/file')
/var/www/nextcloud/lib/private/legacy/hook.php - line 106: OCA\Activity\FilesHooksStatic fileUpdate(Array)
/var/www/nextcloud/lib/private/Files/View.php - line 1245: OC_Hook emit('OC_Filesystem', 'post_update', Array)
/var/www/nextcloud/lib/private/Files/View.php - line 1173: OC\Files\View->runHooks(Array, '/path/to/file', true)
/var/www/nextcloud/lib/private/Files/View.php - line 679: OC\Files\View->basicOperation('file_put_conten...', '/path/to/file', Array, '<?xml version="...')
/var/www/nextcloud/lib/private/Files/Node/File.php - line 64: OC\Files\View->file_put_contents('/path/to/file', '<?xml version="...')
[...]
Signed-off-by: Jan-Philipp Litza <janphilipp@litza.de>
2017-06-16 09:38:22 +00:00
|
|
|
if ($group === null) {
|
2016-12-22 20:44:21 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$userList = $group->getUsers();
|
|
|
|
foreach ($userList as $user) {
|
2017-03-27 14:06:31 +00:00
|
|
|
$uid = $user->getUID();
|
|
|
|
$users[$uid] = isset($users[$uid]) ? $users[$uid] : [];
|
|
|
|
$users[$uid][$row['id']] = $row;
|
2016-12-22 20:44:21 +00:00
|
|
|
}
|
|
|
|
} else if ($type === \OCP\Share::SHARE_TYPE_LINK) {
|
|
|
|
$link = true;
|
2017-04-10 14:22:12 +00:00
|
|
|
} else if ($type === self::SHARE_TYPE_USERGROUP && $currentAccess === true) {
|
|
|
|
$uid = $row['share_with'];
|
|
|
|
$users[$uid] = isset($users[$uid]) ? $users[$uid] : [];
|
|
|
|
$users[$uid][$row['id']] = $row;
|
2016-12-22 20:44:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$cursor->closeCursor();
|
|
|
|
|
2017-04-10 14:22:12 +00:00
|
|
|
if ($currentAccess === true) {
|
2017-04-11 10:40:36 +00:00
|
|
|
$users = array_map([$this, 'filterSharesOfUser'], $users);
|
2017-04-10 14:22:12 +00:00
|
|
|
$users = array_filter($users);
|
2017-04-11 10:40:36 +00:00
|
|
|
} else {
|
|
|
|
$users = array_keys($users);
|
2017-04-10 14:22:12 +00:00
|
|
|
}
|
2016-12-22 20:44:21 +00:00
|
|
|
|
2017-03-27 14:06:31 +00:00
|
|
|
return ['users' => $users, 'public' => $link];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* For each user the path with the fewest slashes is returned
|
|
|
|
* @param array $shares
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function filterSharesOfUser(array $shares) {
|
|
|
|
// Group shares when the user has a share exception
|
|
|
|
foreach ($shares as $id => $share) {
|
|
|
|
$type = (int) $share['share_type'];
|
|
|
|
$permissions = (int) $share['permissions'];
|
|
|
|
|
|
|
|
if ($type === self::SHARE_TYPE_USERGROUP) {
|
|
|
|
unset($shares[$share['parent']]);
|
|
|
|
|
|
|
|
if ($permissions === 0) {
|
|
|
|
unset($shares[$id]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$best = [];
|
|
|
|
$bestDepth = 0;
|
|
|
|
foreach ($shares as $id => $share) {
|
|
|
|
$depth = substr_count($share['file_target'], '/');
|
|
|
|
if (empty($best) || $depth < $bestDepth) {
|
|
|
|
$bestDepth = $depth;
|
|
|
|
$best = [
|
|
|
|
'node_id' => $share['file_source'],
|
|
|
|
'node_path' => $share['file_target'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $best;
|
2016-12-22 20:44:21 +00:00
|
|
|
}
|
2018-07-12 12:55:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* propagate notes to the recipients
|
|
|
|
*
|
|
|
|
* @param IShare $share
|
|
|
|
* @throws \OCP\Files\NotFoundException
|
|
|
|
*/
|
|
|
|
private function propagateNote(IShare $share) {
|
|
|
|
if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
|
|
|
|
$user = $this->userManager->get($share->getSharedWith());
|
|
|
|
$this->sendNote([$user], $share);
|
|
|
|
} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
|
|
|
|
$group = $this->groupManager->get($share->getSharedWith());
|
|
|
|
$groupMembers = $group->getUsers();
|
|
|
|
$this->sendNote($groupMembers, $share);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* send note by mail
|
|
|
|
*
|
|
|
|
* @param array $recipients
|
|
|
|
* @param IShare $share
|
|
|
|
* @throws \OCP\Files\NotFoundException
|
|
|
|
*/
|
|
|
|
private function sendNote(array $recipients, IShare $share) {
|
|
|
|
|
|
|
|
$toList = [];
|
|
|
|
|
|
|
|
foreach ($recipients as $recipient) {
|
|
|
|
/** @var IUser $recipient */
|
|
|
|
$email = $recipient->getEMailAddress();
|
|
|
|
if ($email) {
|
|
|
|
$toList[$email] = $recipient->getDisplayName();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($toList)) {
|
|
|
|
|
|
|
|
$filename = $share->getNode()->getName();
|
|
|
|
$initiator = $share->getSharedBy();
|
|
|
|
$note = $share->getNote();
|
|
|
|
|
|
|
|
$initiatorUser = $this->userManager->get($initiator);
|
|
|
|
$initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator;
|
|
|
|
$initiatorEmailAddress = ($initiatorUser instanceof IUser) ? $initiatorUser->getEMailAddress() : null;
|
2018-07-13 10:09:15 +00:00
|
|
|
$plainHeading = $this->l->t('%1$s shared »%2$s« with you and wants to add:', [$initiatorDisplayName, $filename]);
|
|
|
|
$htmlHeading = $this->l->t('%1$s shared »%2$s« with you and wants to add', [$initiatorDisplayName, $filename]);
|
2018-07-12 12:55:50 +00:00
|
|
|
$message = $this->mailer->createMessage();
|
|
|
|
|
2018-07-12 14:05:24 +00:00
|
|
|
$emailTemplate = $this->mailer->createEMailTemplate('defaultShareProvider.sendNote');
|
2018-07-12 12:55:50 +00:00
|
|
|
|
|
|
|
$emailTemplate->setSubject($this->l->t('»%s« added a note to a file shared with you', [$initiatorDisplayName]));
|
|
|
|
$emailTemplate->addHeader();
|
2018-07-12 16:17:22 +00:00
|
|
|
$emailTemplate->addHeading($htmlHeading, $plainHeading);
|
2018-07-12 14:05:24 +00:00
|
|
|
$emailTemplate->addBodyText(htmlspecialchars($note), $note);
|
2018-07-12 17:32:35 +00:00
|
|
|
|
|
|
|
$link = $this->urlGenerator->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $share->getNode()->getId()]);
|
|
|
|
$emailTemplate->addBodyButton(
|
|
|
|
$this->l->t('Open »%s«', [$filename]),
|
|
|
|
$link
|
|
|
|
);
|
|
|
|
|
2018-07-12 12:55:50 +00:00
|
|
|
|
|
|
|
// The "From" contains the sharers name
|
|
|
|
$instanceName = $this->defaults->getName();
|
|
|
|
$senderName = $this->l->t(
|
2018-07-12 15:07:11 +00:00
|
|
|
'%1$s via %2$s',
|
2018-07-12 12:55:50 +00:00
|
|
|
[
|
|
|
|
$initiatorDisplayName,
|
|
|
|
$instanceName
|
|
|
|
]
|
|
|
|
);
|
|
|
|
$message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]);
|
|
|
|
if ($initiatorEmailAddress !== null) {
|
|
|
|
$message->setReplyTo([$initiatorEmailAddress => $initiatorDisplayName]);
|
|
|
|
$emailTemplate->addFooter($instanceName . ' - ' . $this->defaults->getSlogan());
|
|
|
|
} else {
|
|
|
|
$emailTemplate->addFooter();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($toList) === 1) {
|
|
|
|
$message->setTo($toList);
|
|
|
|
} else {
|
|
|
|
$message->setTo([]);
|
|
|
|
$message->setBcc($toList);
|
|
|
|
}
|
|
|
|
$message->useTemplate($emailTemplate);
|
|
|
|
$this->mailer->send($message);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-10-29 15:56:06 +00:00
|
|
|
|
|
|
|
public function getAllShares(): iterable {
|
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
|
|
|
|
$qb->select('*')
|
|
|
|
->from('share')
|
|
|
|
->where(
|
|
|
|
$qb->expr()->orX(
|
|
|
|
$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share\IShare::TYPE_USER)),
|
|
|
|
$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share\IShare::TYPE_GROUP)),
|
|
|
|
$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share\IShare::TYPE_LINK))
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$cursor = $qb->execute();
|
|
|
|
while($data = $cursor->fetch()) {
|
|
|
|
try {
|
|
|
|
$share = $this->createShare($data);
|
|
|
|
} catch (InvalidShare $e) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
yield $share;
|
|
|
|
}
|
|
|
|
$cursor->closeCursor();
|
|
|
|
}
|
2016-01-25 16:17:36 +00:00
|
|
|
}
|