2017-05-11 15:04:53 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com>
|
|
|
|
*
|
2020-04-29 12:57:22 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2017-05-11 15:04:53 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2020-12-16 16:54:15 +03:00
|
|
|
* @author Julius Härtl <jus@bitgrid.net>
|
2017-05-11 15:04:53 +03:00
|
|
|
*
|
|
|
|
* @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
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2017-05-11 15:04:53 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\Repair\Owncloud;
|
|
|
|
|
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
|
|
|
use OCP\IConfig;
|
|
|
|
use OCP\IDBConnection;
|
|
|
|
use OCP\Migration\IOutput;
|
|
|
|
use OCP\Migration\IRepairStep;
|
|
|
|
use OCP\PreConditionNotMetException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copies the email address from the accounts table to the preference table,
|
|
|
|
* before the data structure is changed and the information is gone
|
|
|
|
*/
|
|
|
|
class SaveAccountsTableData implements IRepairStep {
|
2020-04-10 17:54:27 +03:00
|
|
|
public const BATCH_SIZE = 75;
|
2017-05-11 15:04:53 +03:00
|
|
|
|
|
|
|
/** @var IDBConnection */
|
|
|
|
protected $db;
|
|
|
|
|
|
|
|
/** @var IConfig */
|
|
|
|
protected $config;
|
|
|
|
|
2019-07-11 13:11:07 +03:00
|
|
|
protected $hasForeignKeyOnPersistentLocks = false;
|
|
|
|
|
2017-05-11 15:04:53 +03:00
|
|
|
/**
|
|
|
|
* @param IDBConnection $db
|
|
|
|
* @param IConfig $config
|
|
|
|
*/
|
|
|
|
public function __construct(IDBConnection $db, IConfig $config) {
|
|
|
|
$this->db = $db;
|
|
|
|
$this->config = $config;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getName() {
|
|
|
|
return 'Copy data from accounts table when migrating from ownCloud';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param IOutput $output
|
|
|
|
*/
|
|
|
|
public function run(IOutput $output) {
|
|
|
|
if (!$this->shouldRun()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$offset = 0;
|
|
|
|
$numUsers = $this->runStep($offset);
|
|
|
|
|
|
|
|
while ($numUsers === self::BATCH_SIZE) {
|
|
|
|
$offset += $numUsers;
|
|
|
|
$numUsers = $this->runStep($offset);
|
|
|
|
}
|
|
|
|
|
2020-09-25 10:12:26 +03:00
|
|
|
// oc_persistent_locks will be removed later on anyways so we can just drop and ignore any foreign key constraints here
|
|
|
|
$tableName = $this->config->getSystemValue('dbtableprefix', 'oc_') . 'persistent_locks';
|
|
|
|
$schema = $this->db->createSchema();
|
|
|
|
$table = $schema->getTable($tableName);
|
|
|
|
foreach ($table->getForeignKeys() as $foreignKey) {
|
|
|
|
$table->removeForeignKey($foreignKey->getName());
|
|
|
|
}
|
|
|
|
$this->db->migrateToSchema($schema);
|
|
|
|
|
2017-05-15 10:57:41 +03:00
|
|
|
// Remove the table
|
2019-07-11 13:11:07 +03:00
|
|
|
if ($this->hasForeignKeyOnPersistentLocks) {
|
|
|
|
$this->db->dropTable('persistent_locks');
|
|
|
|
}
|
2017-05-15 10:57:41 +03:00
|
|
|
$this->db->dropTable('accounts');
|
2017-05-11 15:04:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function shouldRun() {
|
2017-09-08 12:37:22 +03:00
|
|
|
$schema = $this->db->createSchema();
|
2019-07-11 13:11:07 +03:00
|
|
|
$prefix = $this->config->getSystemValue('dbtableprefix', 'oc_');
|
2017-09-08 12:37:22 +03:00
|
|
|
|
2019-07-11 13:11:07 +03:00
|
|
|
$tableName = $prefix . 'accounts';
|
2017-09-08 12:37:22 +03:00
|
|
|
if (!$schema->hasTable($tableName)) {
|
2017-07-12 16:35:24 +03:00
|
|
|
return false;
|
2017-05-11 15:04:53 +03:00
|
|
|
}
|
2017-09-08 12:37:22 +03:00
|
|
|
|
|
|
|
$table = $schema->getTable($tableName);
|
2019-07-11 13:11:07 +03:00
|
|
|
if (!$table->hasColumn('user_id')) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($schema->hasTable($prefix . 'persistent_locks')) {
|
|
|
|
$locksTable = $schema->getTable($prefix . 'persistent_locks');
|
|
|
|
$foreignKeys = $locksTable->getForeignKeys();
|
|
|
|
foreach ($foreignKeys as $foreignKey) {
|
|
|
|
if ($tableName === $foreignKey->getForeignTableName()) {
|
|
|
|
$this->hasForeignKeyOnPersistentLocks = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2017-05-11 15:04:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $offset
|
|
|
|
* @return int Number of copied users
|
|
|
|
*/
|
|
|
|
protected function runStep($offset) {
|
|
|
|
$query = $this->db->getQueryBuilder();
|
|
|
|
$query->select('*')
|
|
|
|
->from('accounts')
|
|
|
|
->orderBy('id')
|
|
|
|
->setMaxResults(self::BATCH_SIZE);
|
|
|
|
|
|
|
|
if ($offset > 0) {
|
|
|
|
$query->setFirstResult($offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = $query->execute();
|
|
|
|
|
|
|
|
$update = $this->db->getQueryBuilder();
|
|
|
|
$update->update('users')
|
|
|
|
->set('displayname', $update->createParameter('displayname'))
|
|
|
|
->where($update->expr()->eq('uid', $update->createParameter('userid')));
|
|
|
|
|
|
|
|
$updatedUsers = 0;
|
|
|
|
while ($row = $result->fetch()) {
|
|
|
|
try {
|
|
|
|
$this->migrateUserInfo($update, $row);
|
|
|
|
} catch (PreConditionNotMetException $e) {
|
|
|
|
// Ignore and continue
|
|
|
|
} catch (\UnexpectedValueException $e) {
|
|
|
|
// Ignore and continue
|
|
|
|
}
|
|
|
|
$updatedUsers++;
|
|
|
|
}
|
|
|
|
$result->closeCursor();
|
|
|
|
|
|
|
|
return $updatedUsers;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param IQueryBuilder $update
|
|
|
|
* @param array $userdata
|
|
|
|
* @throws PreConditionNotMetException
|
|
|
|
* @throws \UnexpectedValueException
|
|
|
|
*/
|
|
|
|
protected function migrateUserInfo(IQueryBuilder $update, $userdata) {
|
2017-05-15 13:37:16 +03:00
|
|
|
$state = (int) $userdata['state'];
|
|
|
|
if ($state === 3) {
|
2017-05-11 15:04:53 +03:00
|
|
|
// Deleted user, ignore
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($userdata['email'] !== null) {
|
|
|
|
$this->config->setUserValue($userdata['user_id'], 'settings', 'email', $userdata['email']);
|
|
|
|
}
|
|
|
|
if ($userdata['quota'] !== null) {
|
|
|
|
$this->config->setUserValue($userdata['user_id'], 'files', 'quota', $userdata['quota']);
|
|
|
|
}
|
|
|
|
if ($userdata['last_login'] !== null) {
|
|
|
|
$this->config->setUserValue($userdata['user_id'], 'login', 'lastLogin', $userdata['last_login']);
|
|
|
|
}
|
2017-05-15 13:37:16 +03:00
|
|
|
if ($state === 1) {
|
2017-05-11 15:04:53 +03:00
|
|
|
$this->config->setUserValue($userdata['user_id'], 'core', 'enabled', 'true');
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif ($state === 2) {
|
2017-05-11 15:04:53 +03:00
|
|
|
$this->config->setUserValue($userdata['user_id'], 'core', 'enabled', 'false');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($userdata['display_name'] !== null) {
|
|
|
|
$update->setParameter('displayname', $userdata['display_name'])
|
|
|
|
->setParameter('userid', $userdata['user_id']);
|
|
|
|
$update->execute();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|