1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
<?php
declare(strict_types=1);
namespace OCA\User_LDAP\Migration;
use Closure;
use OC\Hooks\PublicEmitter;
use OCP\DB\Exception;
use OCP\DB\ISchemaWrapper;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\DB\Types;
use OCP\IDBConnection;
use OCP\IUserManager;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;
use Psr\Log\LoggerInterface;
class Version1120Date20210917155206 extends SimpleMigrationStep {
/** @var IDBConnection */
private $dbc;
/** @var IUserManager */
private $userManager;
/** @var LoggerInterface */
private $logger;
public function __construct(IDBConnection $dbc, IUserManager $userManager, LoggerInterface $logger) {
$this->dbc = $dbc;
$this->userManager = $userManager;
$this->logger = $logger;
}
public function getName() {
return 'Adjust LDAP user and group id column lengths to match server lengths';
}
/**
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
* @param array $options
*/
public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
// ensure that there is no user or group id longer than 64char in LDAP table
$this->handleIDs('ldap_group_mapping', false);
$this->handleIDs('ldap_user_mapping', true);
}
/**
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
* @param array $options
* @return null|ISchemaWrapper
*/
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();
$changeSchema = false;
foreach (['ldap_user_mapping', 'ldap_group_mapping'] as $tableName) {
$table = $schema->getTable($tableName);
$column = $table->getColumn('owncloud_name');
if ($column->getLength() > 64) {
$column->setLength(64);
$changeSchema = true;
}
}
return $changeSchema ? $schema : null;
}
protected function handleIDs(string $table, bool $emitHooks) {
$q = $this->getSelectQuery($table);
$u = $this->getUpdateQuery($table);
$r = $q->execute();
while ($row = $r->fetch()) {
$newId = hash('sha256', $row['owncloud_name'], false);
if ($emitHooks) {
$this->emitUnassign($row['owncloud_name'], true);
}
$u->setParameter('uuid', $row['directory_uuid']);
$u->setParameter('newId', $newId);
try {
$u->execute();
if ($emitHooks) {
$this->emitUnassign($row['owncloud_name'], false);
$this->emitAssign($newId);
}
} catch (Exception $e) {
$this->logger->error('Failed to shorten owncloud_name "{oldId}" to "{newId}" (UUID: "{uuid}" of {table})',
[
'app' => 'user_ldap',
'oldId' => $row['owncloud_name'],
'newId' => $newId,
'uuid' => $row['directory_uuid'],
'table' => $table,
'exception' => $e,
]
);
}
}
$r->closeCursor();
}
protected function getSelectQuery(string $table): IQueryBuilder {
$q = $this->dbc->getQueryBuilder();
$q->select('owncloud_name', 'directory_uuid')
->from($table)
->where($q->expr()->like('owncloud_name', $q->createNamedParameter(str_repeat('_', 65) . '%'), Types::STRING));
return $q;
}
protected function getUpdateQuery(string $table): IQueryBuilder {
$q = $this->dbc->getQueryBuilder();
$q->update($table)
->set('owncloud_name', $q->createParameter('newId'))
->where($q->expr()->eq('directory_uuid', $q->createParameter('uuid')));
return $q;
}
protected function emitUnassign(string $oldId, bool $pre): void {
if ($this->userManager instanceof PublicEmitter) {
$this->userManager->emit('\OC\User', $pre ? 'pre' : 'post' . 'UnassignedUserId', [$oldId]);
}
}
protected function emitAssign(string $newId): void {
if ($this->userManager instanceof PublicEmitter) {
$this->userManager->emit('\OC\User', 'assignedUserId', [$newId]);
}
}
}
|