Completed
Push — master ( 879e11...10930c )
by Lukas
20:02 queued 06:34
created

SetPasswordColumn::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Joas Schilling <[email protected]>
4
 *
5
 * @author Joas Schilling <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Files_Sharing\Migration;
25
26
use OCP\IConfig;
27
use OCP\IDBConnection;
28
use OCP\Migration\IOutput;
29
use OCP\Migration\IRepairStep;
30
use OCP\Share;
31
32
/**
33
 * Class SetPasswordColumn
34
 *
35
 * @package OCA\Files_Sharing\Migration
36
 */
37
class SetPasswordColumn implements IRepairStep {
38
39
	/** @var IDBConnection */
40
	private $connection;
41
42
	/** @var  IConfig */
43
	private $config;
44
45
46
	public function __construct(IDBConnection $connection, IConfig $config) {
47
		$this->connection = $connection;
48
		$this->config = $config;
49
	}
50
51
	/**
52
	 * Returns the step's name
53
	 *
54
	 * @return string
55
	 * @since 9.1.0
56
	 */
57
	public function getName() {
58
		return 'Copy the share password into the dedicated column';
59
	}
60
61
	/**
62
	 * @param IOutput $output
63
	 */
64
	public function run(IOutput $output) {
65
		if (!$this->shouldRun()) {
66
			return;
67
		}
68
69
		$query = $this->connection->getQueryBuilder();
70
		$query
71
			->update('share')
72
			->set('password', 'share_with')
73
			->where($query->expr()->eq('share_type', $query->createNamedParameter(Share::SHARE_TYPE_LINK)))
74
			->andWhere($query->expr()->isNotNull('share_with'));
75
		$result = $query->execute();
76
77
		if ($result === 0) {
78
			// No link updated, no need to run the second query
79
			return;
80
		}
81
82
		$clearQuery = $this->connection->getQueryBuilder();
83
		$clearQuery
84
			->update('share')
85
			->set('share_with', $clearQuery->createNamedParameter(null))
86
			->where($clearQuery->expr()->eq('share_type', $clearQuery->createNamedParameter(Share::SHARE_TYPE_LINK)));
87
88
		$clearQuery->execute();
89
90
	}
91
92
	protected function shouldRun() {
93
		$appVersion = $this->config->getAppValue('files_sharing', 'installed_version', '0.0.0');
94
		return version_compare($appVersion, '1.4.0', '<');
95
	}
96
97
}
98