|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
/** |
|
5
|
|
|
* @copyright Copyright (c) 2020 Arthur Schiwon <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* @author Arthur Schiwon <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* @license GNU AGPL version 3 or any later version |
|
10
|
|
|
* |
|
11
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
12
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
13
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
14
|
|
|
* License, or (at your option) any later version. |
|
15
|
|
|
* |
|
16
|
|
|
* This program is distributed in the hope that it will be useful, |
|
17
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
19
|
|
|
* GNU Affero General Public License for more details. |
|
20
|
|
|
* |
|
21
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
22
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
23
|
|
|
* |
|
24
|
|
|
*/ |
|
25
|
|
|
|
|
26
|
|
|
namespace OC\Repair; |
|
27
|
|
|
|
|
28
|
|
|
use OCP\DB\Exception; |
|
29
|
|
|
use OCP\IConfig; |
|
30
|
|
|
use OCP\IDBConnection; |
|
31
|
|
|
use OCP\IGroupManager; |
|
32
|
|
|
use OCP\Migration\IOutput; |
|
33
|
|
|
use OCP\Migration\IRepairStep; |
|
34
|
|
|
use Psr\Log\LoggerInterface; |
|
35
|
|
|
use function strlen; |
|
36
|
|
|
use function substr; |
|
37
|
|
|
use function urldecode; |
|
38
|
|
|
use function urlencode; |
|
39
|
|
|
|
|
40
|
|
|
class RepairDavShares implements IRepairStep { |
|
41
|
|
|
protected const GROUP_PRINCIPAL_PREFIX = 'principals/groups/'; |
|
42
|
|
|
|
|
43
|
|
|
/** @var IConfig */ |
|
44
|
|
|
private $config; |
|
45
|
|
|
/** @var IDBConnection */ |
|
46
|
|
|
private $dbc; |
|
47
|
|
|
/** @var IGroupManager */ |
|
48
|
|
|
private $groupManager; |
|
49
|
|
|
/** @var LoggerInterface */ |
|
50
|
|
|
private $logger; |
|
51
|
|
|
/** @var bool */ |
|
52
|
|
|
private $hintInvalidShares = false; |
|
53
|
|
|
|
|
54
|
|
|
public function __construct( |
|
55
|
|
|
IConfig $config, |
|
56
|
|
|
IDBConnection $dbc, |
|
57
|
|
|
IGroupManager $groupManager, |
|
58
|
|
|
LoggerInterface $logger |
|
59
|
|
|
) { |
|
60
|
|
|
$this->config = $config; |
|
61
|
|
|
$this->dbc = $dbc; |
|
62
|
|
|
$this->groupManager = $groupManager; |
|
63
|
|
|
$this->logger = $logger; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @inheritDoc |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getName() { |
|
70
|
|
|
return 'Repair DAV shares'; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
protected function repairUnencodedGroupShares() { |
|
74
|
|
|
$qb = $this->dbc->getQueryBuilder(); |
|
75
|
|
|
$qb->select(['id', 'principaluri']) |
|
76
|
|
|
->from('dav_shares') |
|
77
|
|
|
->where($qb->expr()->like('principaluri', $qb->createNamedParameter(self::GROUP_PRINCIPAL_PREFIX . '%'))); |
|
78
|
|
|
|
|
79
|
|
|
$updateQuery = $this->dbc->getQueryBuilder(); |
|
80
|
|
|
$updateQuery->update('dav_shares') |
|
81
|
|
|
->set('principaluri', $updateQuery->createParameter('updatedPrincipalUri')) |
|
82
|
|
|
->where($updateQuery->expr()->eq('id', $updateQuery->createParameter('shareId'))); |
|
83
|
|
|
|
|
84
|
|
|
$statement = $qb->execute(); |
|
85
|
|
|
while ($share = $statement->fetch()) { |
|
86
|
|
|
$gid = substr($share['principaluri'], strlen(self::GROUP_PRINCIPAL_PREFIX)); |
|
87
|
|
|
$decodedGid = urldecode($gid); |
|
88
|
|
|
$encodedGid = urlencode($gid); |
|
89
|
|
|
if ($gid === $encodedGid |
|
90
|
|
|
|| !$this->groupManager->groupExists($gid) |
|
91
|
|
|
|| ($gid !== $decodedGid && $this->groupManager->groupExists($decodedGid)) |
|
92
|
|
|
) { |
|
93
|
|
|
$this->hintInvalidShares = $this->hintInvalidShares || $gid !== $encodedGid; |
|
94
|
|
|
continue; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
// Repair when |
|
98
|
|
|
// + the group name needs encoding |
|
99
|
|
|
// + AND it is not encoded yet |
|
100
|
|
|
// + AND there are no ambivalent groups |
|
101
|
|
|
|
|
102
|
|
|
try { |
|
103
|
|
|
$fixedPrincipal = self::GROUP_PRINCIPAL_PREFIX . $encodedGid; |
|
104
|
|
|
$logParameters = [ |
|
105
|
|
|
'app' => 'core', |
|
106
|
|
|
'id' => $share['id'], |
|
107
|
|
|
'old' => $share['principaluri'], |
|
108
|
|
|
'new' => $fixedPrincipal, |
|
109
|
|
|
]; |
|
110
|
|
|
$updateQuery |
|
111
|
|
|
->setParameter('updatedPrincipalUri', $fixedPrincipal) |
|
112
|
|
|
->setParameter('shareId', $share['id']) |
|
113
|
|
|
->execute(); |
|
114
|
|
|
$this->logger->info('Repaired principal for dav share {id} from {old} to {new}', $logParameters); |
|
115
|
|
|
} catch (Exception $e) { |
|
116
|
|
|
$logParameters['message'] = $e->getMessage(); |
|
117
|
|
|
$logParameters['exception'] = $e; |
|
118
|
|
|
$this->logger->info('Could not repair principal for dav share {id} from {old} to {new}: {message}', $logParameters); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
return true; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @inheritDoc |
|
126
|
|
|
*/ |
|
127
|
|
|
public function run(IOutput $output) { |
|
128
|
|
|
$versionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0'); |
|
129
|
|
|
if (version_compare($versionFromBeforeUpdate, '20.0.7', '<') |
|
130
|
|
|
&& $this->repairUnencodedGroupShares() |
|
131
|
|
|
) { |
|
132
|
|
|
$output->info('Repaired DAV group shares'); |
|
133
|
|
|
if ($this->hintInvalidShares) { |
|
134
|
|
|
$output->info('Invalid shares might be left in the database, running "occ dav:remove-invalid-shares" can remove them.'); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|