2015-09-17 17:01:11 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 18:07:57 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2020-04-29 12:57:22 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2016-07-21 18:07:57 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2020-12-16 16:54:15 +03:00
|
|
|
* @author Vincent Petry <vincent@nextcloud.com>
|
2015-09-17 17:01:11 +03: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,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-09-17 17:01:11 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\Repair;
|
|
|
|
|
2016-04-22 16:35:39 +03:00
|
|
|
use OCP\Migration\IOutput;
|
|
|
|
use OCP\Migration\IRepairStep;
|
2015-09-17 17:01:11 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Repairs shares with invalid data
|
|
|
|
*/
|
2016-04-22 16:35:39 +03:00
|
|
|
class RepairInvalidShares implements IRepairStep {
|
2020-04-10 17:54:27 +03:00
|
|
|
public const CHUNK_SIZE = 200;
|
2016-02-10 14:01:55 +03:00
|
|
|
|
2016-04-22 16:35:39 +03:00
|
|
|
/** @var \OCP\IConfig */
|
2015-09-17 17:01:11 +03:00
|
|
|
protected $config;
|
|
|
|
|
2016-04-22 16:35:39 +03:00
|
|
|
/** @var \OCP\IDBConnection */
|
2015-09-17 17:01:11 +03:00
|
|
|
protected $connection;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \OCP\IConfig $config
|
|
|
|
* @param \OCP\IDBConnection $connection
|
|
|
|
*/
|
|
|
|
public function __construct($config, $connection) {
|
|
|
|
$this->connection = $connection;
|
|
|
|
$this->config = $config;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getName() {
|
|
|
|
return 'Repair invalid shares';
|
|
|
|
}
|
|
|
|
|
2016-11-07 14:25:05 +03:00
|
|
|
/**
|
|
|
|
* Adjust file share permissions
|
|
|
|
*/
|
|
|
|
private function adjustFileSharePermissions(IOutput $out) {
|
|
|
|
$mask = \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE;
|
|
|
|
$builder = $this->connection->getQueryBuilder();
|
|
|
|
|
2017-01-05 13:45:43 +03:00
|
|
|
$permsFunc = $builder->expr()->bitwiseAnd('permissions', $mask);
|
2016-11-07 14:25:05 +03:00
|
|
|
$builder
|
|
|
|
->update('share')
|
|
|
|
->set('permissions', $permsFunc)
|
|
|
|
->where($builder->expr()->eq('item_type', $builder->expr()->literal('file')))
|
|
|
|
->andWhere($builder->expr()->neq('permissions', $permsFunc));
|
|
|
|
|
|
|
|
$updatedEntries = $builder->execute();
|
|
|
|
if ($updatedEntries > 0) {
|
|
|
|
$out->info('Fixed file share permissions for ' . $updatedEntries . ' shares');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-19 16:39:39 +03:00
|
|
|
/**
|
|
|
|
* Remove shares where the parent share does not exist anymore
|
|
|
|
*/
|
2016-04-22 16:35:39 +03:00
|
|
|
private function removeSharesNonExistingParent(IOutput $out) {
|
2015-10-19 16:39:39 +03:00
|
|
|
$deletedEntries = 0;
|
|
|
|
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$query->select('s1.parent')
|
|
|
|
->from('share', 's1')
|
|
|
|
->where($query->expr()->isNotNull('s1.parent'))
|
|
|
|
->andWhere($query->expr()->isNull('s2.id'))
|
|
|
|
->leftJoin('s1', 'share', 's2', $query->expr()->eq('s1.parent', 's2.id'))
|
2016-02-10 14:01:55 +03:00
|
|
|
->groupBy('s1.parent')
|
|
|
|
->setMaxResults(self::CHUNK_SIZE);
|
2015-10-19 16:39:39 +03:00
|
|
|
|
|
|
|
$deleteQuery = $this->connection->getQueryBuilder();
|
|
|
|
$deleteQuery->delete('share')
|
2016-02-10 14:01:55 +03:00
|
|
|
->where($deleteQuery->expr()->eq('parent', $deleteQuery->createParameter('parent')));
|
|
|
|
|
|
|
|
$deletedInLastChunk = self::CHUNK_SIZE;
|
|
|
|
while ($deletedInLastChunk === self::CHUNK_SIZE) {
|
|
|
|
$deletedInLastChunk = 0;
|
|
|
|
$result = $query->execute();
|
|
|
|
while ($row = $result->fetch()) {
|
|
|
|
$deletedInLastChunk++;
|
|
|
|
$deletedEntries += $deleteQuery->setParameter('parent', (int) $row['parent'])
|
|
|
|
->execute();
|
|
|
|
}
|
|
|
|
$result->closeCursor();
|
2015-10-19 16:39:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($deletedEntries) {
|
2016-04-22 16:35:39 +03:00
|
|
|
$out->info('Removed ' . $deletedEntries . ' shares where the parent did not exist');
|
2015-10-19 16:39:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-22 16:35:39 +03:00
|
|
|
public function run(IOutput $out) {
|
2015-09-17 17:01:11 +03:00
|
|
|
$ocVersionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0');
|
2016-12-22 12:20:22 +03:00
|
|
|
if (version_compare($ocVersionFromBeforeUpdate, '12.0.0.11', '<')) {
|
2016-11-07 14:25:05 +03:00
|
|
|
$this->adjustFileSharePermissions($out);
|
|
|
|
}
|
2015-10-19 16:39:39 +03:00
|
|
|
|
2016-04-22 16:35:39 +03:00
|
|
|
$this->removeSharesNonExistingParent($out);
|
2015-09-17 17:01:11 +03:00
|
|
|
}
|
|
|
|
}
|