@@ -214,7 +214,7 @@ |
||
214 | 214 | |
215 | 215 | while ($nextcloudId = array_shift($idList)) { |
216 | 216 | $update->setParameter('nextcloudId', $nextcloudId); |
217 | - $update->setParameter('invalidatedUuid', 'invalidated_' . \bin2hex(\random_bytes(6))); |
|
217 | + $update->setParameter('invalidatedUuid', 'invalidated_'.\bin2hex(\random_bytes(6))); |
|
218 | 218 | try { |
219 | 219 | $update->executeStatement(); |
220 | 220 | $this->logger->warning( |
@@ -22,245 +22,245 @@ |
||
22 | 22 | |
23 | 23 | class Version1130Date20211102154716 extends SimpleMigrationStep { |
24 | 24 | |
25 | - /** @var string[] */ |
|
26 | - private $hashColumnAddedToTables = []; |
|
25 | + /** @var string[] */ |
|
26 | + private $hashColumnAddedToTables = []; |
|
27 | 27 | |
28 | - public function __construct( |
|
29 | - private IDBConnection $dbc, |
|
30 | - private LoggerInterface $logger, |
|
31 | - ) { |
|
32 | - } |
|
28 | + public function __construct( |
|
29 | + private IDBConnection $dbc, |
|
30 | + private LoggerInterface $logger, |
|
31 | + ) { |
|
32 | + } |
|
33 | 33 | |
34 | - public function getName() { |
|
35 | - return 'Adjust LDAP user and group ldap_dn column lengths and add ldap_dn_hash columns'; |
|
36 | - } |
|
34 | + public function getName() { |
|
35 | + return 'Adjust LDAP user and group ldap_dn column lengths and add ldap_dn_hash columns'; |
|
36 | + } |
|
37 | 37 | |
38 | - public function preSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) { |
|
39 | - foreach (['ldap_user_mapping', 'ldap_group_mapping'] as $tableName) { |
|
40 | - $this->processDuplicateUUIDs($tableName); |
|
41 | - } |
|
38 | + public function preSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) { |
|
39 | + foreach (['ldap_user_mapping', 'ldap_group_mapping'] as $tableName) { |
|
40 | + $this->processDuplicateUUIDs($tableName); |
|
41 | + } |
|
42 | 42 | |
43 | - /** @var ISchemaWrapper $schema */ |
|
44 | - $schema = $schemaClosure(); |
|
45 | - if ($schema->hasTable('ldap_group_mapping_backup')) { |
|
46 | - // Previous upgrades of a broken release might have left an incomplete |
|
47 | - // ldap_group_mapping_backup table. No need to recreate, but it |
|
48 | - // should be empty. |
|
49 | - // TRUNCATE is not available from Query Builder, but faster than DELETE FROM. |
|
50 | - $sql = $this->dbc->getDatabasePlatform()->getTruncateTableSQL('`*PREFIX*ldap_group_mapping_backup`', false); |
|
51 | - $this->dbc->executeStatement($sql); |
|
52 | - } |
|
53 | - } |
|
43 | + /** @var ISchemaWrapper $schema */ |
|
44 | + $schema = $schemaClosure(); |
|
45 | + if ($schema->hasTable('ldap_group_mapping_backup')) { |
|
46 | + // Previous upgrades of a broken release might have left an incomplete |
|
47 | + // ldap_group_mapping_backup table. No need to recreate, but it |
|
48 | + // should be empty. |
|
49 | + // TRUNCATE is not available from Query Builder, but faster than DELETE FROM. |
|
50 | + $sql = $this->dbc->getDatabasePlatform()->getTruncateTableSQL('`*PREFIX*ldap_group_mapping_backup`', false); |
|
51 | + $this->dbc->executeStatement($sql); |
|
52 | + } |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * @param IOutput $output |
|
57 | - * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
58 | - * @param array $options |
|
59 | - * @return null|ISchemaWrapper |
|
60 | - */ |
|
61 | - public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { |
|
62 | - /** @var ISchemaWrapper $schema */ |
|
63 | - $schema = $schemaClosure(); |
|
55 | + /** |
|
56 | + * @param IOutput $output |
|
57 | + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
58 | + * @param array $options |
|
59 | + * @return null|ISchemaWrapper |
|
60 | + */ |
|
61 | + public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { |
|
62 | + /** @var ISchemaWrapper $schema */ |
|
63 | + $schema = $schemaClosure(); |
|
64 | 64 | |
65 | - $changeSchema = false; |
|
66 | - foreach (['ldap_user_mapping', 'ldap_group_mapping'] as $tableName) { |
|
67 | - $table = $schema->getTable($tableName); |
|
68 | - if (!$table->hasColumn('ldap_dn_hash')) { |
|
69 | - $table->addColumn('ldap_dn_hash', Types::STRING, [ |
|
70 | - 'notnull' => false, |
|
71 | - 'length' => 64, |
|
72 | - ]); |
|
73 | - $changeSchema = true; |
|
74 | - $this->hashColumnAddedToTables[] = $tableName; |
|
75 | - } |
|
76 | - $column = $table->getColumn('ldap_dn'); |
|
77 | - if ($tableName === 'ldap_user_mapping') { |
|
78 | - if ($column->getLength() < 4000) { |
|
79 | - $column->setLength(4000); |
|
80 | - $changeSchema = true; |
|
81 | - } |
|
65 | + $changeSchema = false; |
|
66 | + foreach (['ldap_user_mapping', 'ldap_group_mapping'] as $tableName) { |
|
67 | + $table = $schema->getTable($tableName); |
|
68 | + if (!$table->hasColumn('ldap_dn_hash')) { |
|
69 | + $table->addColumn('ldap_dn_hash', Types::STRING, [ |
|
70 | + 'notnull' => false, |
|
71 | + 'length' => 64, |
|
72 | + ]); |
|
73 | + $changeSchema = true; |
|
74 | + $this->hashColumnAddedToTables[] = $tableName; |
|
75 | + } |
|
76 | + $column = $table->getColumn('ldap_dn'); |
|
77 | + if ($tableName === 'ldap_user_mapping') { |
|
78 | + if ($column->getLength() < 4000) { |
|
79 | + $column->setLength(4000); |
|
80 | + $changeSchema = true; |
|
81 | + } |
|
82 | 82 | |
83 | - if ($table->hasIndex('ldap_dn_users')) { |
|
84 | - $table->dropIndex('ldap_dn_users'); |
|
85 | - $changeSchema = true; |
|
86 | - } |
|
87 | - if (!$table->hasIndex('ldap_user_dn_hashes')) { |
|
88 | - $table->addUniqueIndex(['ldap_dn_hash'], 'ldap_user_dn_hashes'); |
|
89 | - $changeSchema = true; |
|
90 | - } |
|
91 | - if (!$table->hasIndex('ldap_user_directory_uuid')) { |
|
92 | - $table->addUniqueIndex(['directory_uuid'], 'ldap_user_directory_uuid'); |
|
93 | - $changeSchema = true; |
|
94 | - } |
|
95 | - } elseif (!$schema->hasTable('ldap_group_mapping_backup')) { |
|
96 | - // We need to copy the table twice to be able to change primary key, prepare the backup table |
|
97 | - $table2 = $schema->createTable('ldap_group_mapping_backup'); |
|
98 | - $table2->addColumn('ldap_dn', Types::STRING, [ |
|
99 | - 'notnull' => true, |
|
100 | - 'length' => 4000, |
|
101 | - 'default' => '', |
|
102 | - ]); |
|
103 | - $table2->addColumn('owncloud_name', Types::STRING, [ |
|
104 | - 'notnull' => true, |
|
105 | - 'length' => 64, |
|
106 | - 'default' => '', |
|
107 | - ]); |
|
108 | - $table2->addColumn('directory_uuid', Types::STRING, [ |
|
109 | - 'notnull' => true, |
|
110 | - 'length' => 255, |
|
111 | - 'default' => '', |
|
112 | - ]); |
|
113 | - $table2->addColumn('ldap_dn_hash', Types::STRING, [ |
|
114 | - 'notnull' => false, |
|
115 | - 'length' => 64, |
|
116 | - ]); |
|
117 | - $table2->setPrimaryKey(['owncloud_name'], 'lgm_backup_primary'); |
|
118 | - $changeSchema = true; |
|
119 | - } |
|
120 | - } |
|
83 | + if ($table->hasIndex('ldap_dn_users')) { |
|
84 | + $table->dropIndex('ldap_dn_users'); |
|
85 | + $changeSchema = true; |
|
86 | + } |
|
87 | + if (!$table->hasIndex('ldap_user_dn_hashes')) { |
|
88 | + $table->addUniqueIndex(['ldap_dn_hash'], 'ldap_user_dn_hashes'); |
|
89 | + $changeSchema = true; |
|
90 | + } |
|
91 | + if (!$table->hasIndex('ldap_user_directory_uuid')) { |
|
92 | + $table->addUniqueIndex(['directory_uuid'], 'ldap_user_directory_uuid'); |
|
93 | + $changeSchema = true; |
|
94 | + } |
|
95 | + } elseif (!$schema->hasTable('ldap_group_mapping_backup')) { |
|
96 | + // We need to copy the table twice to be able to change primary key, prepare the backup table |
|
97 | + $table2 = $schema->createTable('ldap_group_mapping_backup'); |
|
98 | + $table2->addColumn('ldap_dn', Types::STRING, [ |
|
99 | + 'notnull' => true, |
|
100 | + 'length' => 4000, |
|
101 | + 'default' => '', |
|
102 | + ]); |
|
103 | + $table2->addColumn('owncloud_name', Types::STRING, [ |
|
104 | + 'notnull' => true, |
|
105 | + 'length' => 64, |
|
106 | + 'default' => '', |
|
107 | + ]); |
|
108 | + $table2->addColumn('directory_uuid', Types::STRING, [ |
|
109 | + 'notnull' => true, |
|
110 | + 'length' => 255, |
|
111 | + 'default' => '', |
|
112 | + ]); |
|
113 | + $table2->addColumn('ldap_dn_hash', Types::STRING, [ |
|
114 | + 'notnull' => false, |
|
115 | + 'length' => 64, |
|
116 | + ]); |
|
117 | + $table2->setPrimaryKey(['owncloud_name'], 'lgm_backup_primary'); |
|
118 | + $changeSchema = true; |
|
119 | + } |
|
120 | + } |
|
121 | 121 | |
122 | - return $changeSchema ? $schema : null; |
|
123 | - } |
|
122 | + return $changeSchema ? $schema : null; |
|
123 | + } |
|
124 | 124 | |
125 | - /** |
|
126 | - * @param IOutput $output |
|
127 | - * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
128 | - * @param array $options |
|
129 | - */ |
|
130 | - public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) { |
|
131 | - $this->handleDNHashes('ldap_group_mapping'); |
|
132 | - $this->handleDNHashes('ldap_user_mapping'); |
|
133 | - } |
|
125 | + /** |
|
126 | + * @param IOutput $output |
|
127 | + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
128 | + * @param array $options |
|
129 | + */ |
|
130 | + public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) { |
|
131 | + $this->handleDNHashes('ldap_group_mapping'); |
|
132 | + $this->handleDNHashes('ldap_user_mapping'); |
|
133 | + } |
|
134 | 134 | |
135 | - protected function handleDNHashes(string $table): void { |
|
136 | - $select = $this->getSelectQuery($table); |
|
137 | - $update = $this->getUpdateQuery($table); |
|
135 | + protected function handleDNHashes(string $table): void { |
|
136 | + $select = $this->getSelectQuery($table); |
|
137 | + $update = $this->getUpdateQuery($table); |
|
138 | 138 | |
139 | - $result = $select->executeQuery(); |
|
140 | - while ($row = $result->fetch()) { |
|
141 | - $dnHash = hash('sha256', $row['ldap_dn'], false); |
|
142 | - $update->setParameter('name', $row['owncloud_name']); |
|
143 | - $update->setParameter('dn_hash', $dnHash); |
|
144 | - try { |
|
145 | - $update->executeStatement(); |
|
146 | - } catch (Exception $e) { |
|
147 | - $this->logger->error('Failed to add hash "{dnHash}" ("{name}" of {table})', |
|
148 | - [ |
|
149 | - 'app' => 'user_ldap', |
|
150 | - 'name' => $row['owncloud_name'], |
|
151 | - 'dnHash' => $dnHash, |
|
152 | - 'table' => $table, |
|
153 | - 'exception' => $e, |
|
154 | - ] |
|
155 | - ); |
|
156 | - } |
|
157 | - } |
|
158 | - $result->closeCursor(); |
|
159 | - } |
|
139 | + $result = $select->executeQuery(); |
|
140 | + while ($row = $result->fetch()) { |
|
141 | + $dnHash = hash('sha256', $row['ldap_dn'], false); |
|
142 | + $update->setParameter('name', $row['owncloud_name']); |
|
143 | + $update->setParameter('dn_hash', $dnHash); |
|
144 | + try { |
|
145 | + $update->executeStatement(); |
|
146 | + } catch (Exception $e) { |
|
147 | + $this->logger->error('Failed to add hash "{dnHash}" ("{name}" of {table})', |
|
148 | + [ |
|
149 | + 'app' => 'user_ldap', |
|
150 | + 'name' => $row['owncloud_name'], |
|
151 | + 'dnHash' => $dnHash, |
|
152 | + 'table' => $table, |
|
153 | + 'exception' => $e, |
|
154 | + ] |
|
155 | + ); |
|
156 | + } |
|
157 | + } |
|
158 | + $result->closeCursor(); |
|
159 | + } |
|
160 | 160 | |
161 | - protected function getSelectQuery(string $table): IQueryBuilder { |
|
162 | - $qb = $this->dbc->getQueryBuilder(); |
|
163 | - $qb->select('owncloud_name', 'ldap_dn') |
|
164 | - ->from($table); |
|
161 | + protected function getSelectQuery(string $table): IQueryBuilder { |
|
162 | + $qb = $this->dbc->getQueryBuilder(); |
|
163 | + $qb->select('owncloud_name', 'ldap_dn') |
|
164 | + ->from($table); |
|
165 | 165 | |
166 | - // when added we may run into risk that it's read from a DB node |
|
167 | - // where the column is not present. Then the where clause is also |
|
168 | - // not necessary since all rows qualify. |
|
169 | - if (!in_array($table, $this->hashColumnAddedToTables, true)) { |
|
170 | - $qb->where($qb->expr()->isNull('ldap_dn_hash')); |
|
171 | - } |
|
166 | + // when added we may run into risk that it's read from a DB node |
|
167 | + // where the column is not present. Then the where clause is also |
|
168 | + // not necessary since all rows qualify. |
|
169 | + if (!in_array($table, $this->hashColumnAddedToTables, true)) { |
|
170 | + $qb->where($qb->expr()->isNull('ldap_dn_hash')); |
|
171 | + } |
|
172 | 172 | |
173 | - return $qb; |
|
174 | - } |
|
173 | + return $qb; |
|
174 | + } |
|
175 | 175 | |
176 | - protected function getUpdateQuery(string $table): IQueryBuilder { |
|
177 | - $qb = $this->dbc->getQueryBuilder(); |
|
178 | - $qb->update($table) |
|
179 | - ->set('ldap_dn_hash', $qb->createParameter('dn_hash')) |
|
180 | - ->where($qb->expr()->eq('owncloud_name', $qb->createParameter('name'))); |
|
181 | - return $qb; |
|
182 | - } |
|
176 | + protected function getUpdateQuery(string $table): IQueryBuilder { |
|
177 | + $qb = $this->dbc->getQueryBuilder(); |
|
178 | + $qb->update($table) |
|
179 | + ->set('ldap_dn_hash', $qb->createParameter('dn_hash')) |
|
180 | + ->where($qb->expr()->eq('owncloud_name', $qb->createParameter('name'))); |
|
181 | + return $qb; |
|
182 | + } |
|
183 | 183 | |
184 | - /** |
|
185 | - * @throws Exception |
|
186 | - */ |
|
187 | - protected function processDuplicateUUIDs(string $table): void { |
|
188 | - $uuids = $this->getDuplicatedUuids($table); |
|
189 | - $idsWithUuidToInvalidate = []; |
|
190 | - foreach ($uuids as $uuid) { |
|
191 | - array_push($idsWithUuidToInvalidate, ...$this->getNextcloudIdsByUuid($table, $uuid)); |
|
192 | - } |
|
193 | - $this->invalidateUuids($table, $idsWithUuidToInvalidate); |
|
194 | - } |
|
184 | + /** |
|
185 | + * @throws Exception |
|
186 | + */ |
|
187 | + protected function processDuplicateUUIDs(string $table): void { |
|
188 | + $uuids = $this->getDuplicatedUuids($table); |
|
189 | + $idsWithUuidToInvalidate = []; |
|
190 | + foreach ($uuids as $uuid) { |
|
191 | + array_push($idsWithUuidToInvalidate, ...$this->getNextcloudIdsByUuid($table, $uuid)); |
|
192 | + } |
|
193 | + $this->invalidateUuids($table, $idsWithUuidToInvalidate); |
|
194 | + } |
|
195 | 195 | |
196 | - /** |
|
197 | - * @throws Exception |
|
198 | - */ |
|
199 | - protected function invalidateUuids(string $table, array $idList): void { |
|
200 | - $update = $this->dbc->getQueryBuilder(); |
|
201 | - $update->update($table) |
|
202 | - ->set('directory_uuid', $update->createParameter('invalidatedUuid')) |
|
203 | - ->where($update->expr()->eq('owncloud_name', $update->createParameter('nextcloudId'))); |
|
196 | + /** |
|
197 | + * @throws Exception |
|
198 | + */ |
|
199 | + protected function invalidateUuids(string $table, array $idList): void { |
|
200 | + $update = $this->dbc->getQueryBuilder(); |
|
201 | + $update->update($table) |
|
202 | + ->set('directory_uuid', $update->createParameter('invalidatedUuid')) |
|
203 | + ->where($update->expr()->eq('owncloud_name', $update->createParameter('nextcloudId'))); |
|
204 | 204 | |
205 | - while ($nextcloudId = array_shift($idList)) { |
|
206 | - $update->setParameter('nextcloudId', $nextcloudId); |
|
207 | - $update->setParameter('invalidatedUuid', 'invalidated_' . \bin2hex(\random_bytes(6))); |
|
208 | - try { |
|
209 | - $update->executeStatement(); |
|
210 | - $this->logger->warning( |
|
211 | - 'LDAP user or group with ID {nid} has a duplicated UUID value which therefore was invalidated. You may double-check your LDAP configuration and trigger an update of the UUID.', |
|
212 | - [ |
|
213 | - 'app' => 'user_ldap', |
|
214 | - 'nid' => $nextcloudId, |
|
215 | - ] |
|
216 | - ); |
|
217 | - } catch (Exception $e) { |
|
218 | - // Catch possible, but unlikely duplications if new invalidated errors. |
|
219 | - // There is the theoretical chance of an infinity loop is, when |
|
220 | - // the constraint violation has a different background. I cannot |
|
221 | - // think of one at the moment. |
|
222 | - if ($e->getReason() !== Exception::REASON_CONSTRAINT_VIOLATION) { |
|
223 | - throw $e; |
|
224 | - } |
|
225 | - $idList[] = $nextcloudId; |
|
226 | - } |
|
227 | - } |
|
228 | - } |
|
205 | + while ($nextcloudId = array_shift($idList)) { |
|
206 | + $update->setParameter('nextcloudId', $nextcloudId); |
|
207 | + $update->setParameter('invalidatedUuid', 'invalidated_' . \bin2hex(\random_bytes(6))); |
|
208 | + try { |
|
209 | + $update->executeStatement(); |
|
210 | + $this->logger->warning( |
|
211 | + 'LDAP user or group with ID {nid} has a duplicated UUID value which therefore was invalidated. You may double-check your LDAP configuration and trigger an update of the UUID.', |
|
212 | + [ |
|
213 | + 'app' => 'user_ldap', |
|
214 | + 'nid' => $nextcloudId, |
|
215 | + ] |
|
216 | + ); |
|
217 | + } catch (Exception $e) { |
|
218 | + // Catch possible, but unlikely duplications if new invalidated errors. |
|
219 | + // There is the theoretical chance of an infinity loop is, when |
|
220 | + // the constraint violation has a different background. I cannot |
|
221 | + // think of one at the moment. |
|
222 | + if ($e->getReason() !== Exception::REASON_CONSTRAINT_VIOLATION) { |
|
223 | + throw $e; |
|
224 | + } |
|
225 | + $idList[] = $nextcloudId; |
|
226 | + } |
|
227 | + } |
|
228 | + } |
|
229 | 229 | |
230 | - /** |
|
231 | - * @throws \OCP\DB\Exception |
|
232 | - * @return array<string> |
|
233 | - */ |
|
234 | - protected function getNextcloudIdsByUuid(string $table, string $uuid): array { |
|
235 | - $select = $this->dbc->getQueryBuilder(); |
|
236 | - $select->select('owncloud_name') |
|
237 | - ->from($table) |
|
238 | - ->where($select->expr()->eq('directory_uuid', $select->createNamedParameter($uuid))); |
|
230 | + /** |
|
231 | + * @throws \OCP\DB\Exception |
|
232 | + * @return array<string> |
|
233 | + */ |
|
234 | + protected function getNextcloudIdsByUuid(string $table, string $uuid): array { |
|
235 | + $select = $this->dbc->getQueryBuilder(); |
|
236 | + $select->select('owncloud_name') |
|
237 | + ->from($table) |
|
238 | + ->where($select->expr()->eq('directory_uuid', $select->createNamedParameter($uuid))); |
|
239 | 239 | |
240 | - $result = $select->executeQuery(); |
|
241 | - $idList = []; |
|
242 | - while (($id = $result->fetchOne()) !== false) { |
|
243 | - $idList[] = $id; |
|
244 | - } |
|
245 | - $result->closeCursor(); |
|
246 | - return $idList; |
|
247 | - } |
|
240 | + $result = $select->executeQuery(); |
|
241 | + $idList = []; |
|
242 | + while (($id = $result->fetchOne()) !== false) { |
|
243 | + $idList[] = $id; |
|
244 | + } |
|
245 | + $result->closeCursor(); |
|
246 | + return $idList; |
|
247 | + } |
|
248 | 248 | |
249 | - /** |
|
250 | - * @return Generator<string> |
|
251 | - * @throws \OCP\DB\Exception |
|
252 | - */ |
|
253 | - protected function getDuplicatedUuids(string $table): Generator { |
|
254 | - $select = $this->dbc->getQueryBuilder(); |
|
255 | - $select->select('directory_uuid') |
|
256 | - ->from($table) |
|
257 | - ->groupBy('directory_uuid') |
|
258 | - ->having($select->expr()->gt($select->func()->count('owncloud_name'), $select->createNamedParameter(1))); |
|
249 | + /** |
|
250 | + * @return Generator<string> |
|
251 | + * @throws \OCP\DB\Exception |
|
252 | + */ |
|
253 | + protected function getDuplicatedUuids(string $table): Generator { |
|
254 | + $select = $this->dbc->getQueryBuilder(); |
|
255 | + $select->select('directory_uuid') |
|
256 | + ->from($table) |
|
257 | + ->groupBy('directory_uuid') |
|
258 | + ->having($select->expr()->gt($select->func()->count('owncloud_name'), $select->createNamedParameter(1))); |
|
259 | 259 | |
260 | - $result = $select->executeQuery(); |
|
261 | - while (($uuid = $result->fetchOne()) !== false) { |
|
262 | - yield $uuid; |
|
263 | - } |
|
264 | - $result->closeCursor(); |
|
265 | - } |
|
260 | + $result = $select->executeQuery(); |
|
261 | + while (($uuid = $result->fetchOne()) !== false) { |
|
262 | + yield $uuid; |
|
263 | + } |
|
264 | + $result->closeCursor(); |
|
265 | + } |
|
266 | 266 | } |
@@ -128,12 +128,12 @@ discard block |
||
128 | 128 | |
129 | 129 | sort($serverConnections); |
130 | 130 | $lastKey = array_pop($serverConnections); |
131 | - $lastNumber = (int)str_replace('s', '', $lastKey); |
|
132 | - return 's' . str_pad((string)($lastNumber + 1), 2, '0', STR_PAD_LEFT); |
|
131 | + $lastNumber = (int) str_replace('s', '', $lastKey); |
|
132 | + return 's'.str_pad((string) ($lastNumber + 1), 2, '0', STR_PAD_LEFT); |
|
133 | 133 | } |
134 | 134 | |
135 | 135 | private function getServersConfig(string $value): array { |
136 | - $regex = '/' . $value . '$/S'; |
|
136 | + $regex = '/'.$value.'$/S'; |
|
137 | 137 | |
138 | 138 | $keys = $this->config->getAppKeys('user_ldap'); |
139 | 139 | $result = []; |
@@ -160,7 +160,7 @@ discard block |
||
160 | 160 | $query = $this->connection->getQueryBuilder(); |
161 | 161 | $query->delete('appconfig') |
162 | 162 | ->where($query->expr()->eq('appid', $query->createNamedParameter('user_ldap'))) |
163 | - ->andWhere($query->expr()->like('configkey', $query->createNamedParameter((string)$prefix . '%'))) |
|
163 | + ->andWhere($query->expr()->like('configkey', $query->createNamedParameter((string) $prefix.'%'))) |
|
164 | 164 | ->andWhere($query->expr()->notIn('configkey', $query->createNamedParameter([ |
165 | 165 | 'enabled', |
166 | 166 | 'installed_version', |
@@ -225,7 +225,7 @@ discard block |
||
225 | 225 | } |
226 | 226 | |
227 | 227 | if (!is_string($dn)) { |
228 | - throw new \LogicException('String expected ' . \gettype($dn) . ' given'); |
|
228 | + throw new \LogicException('String expected '.\gettype($dn).' given'); |
|
229 | 229 | } |
230 | 230 | |
231 | 231 | if (($sanitizedDn = $this->sanitizeDnCache->get($dn)) !== null) { |
@@ -14,266 +14,266 @@ |
||
14 | 14 | use OCP\Server; |
15 | 15 | |
16 | 16 | class Helper { |
17 | - /** @var CappedMemoryCache<string> */ |
|
18 | - protected CappedMemoryCache $sanitizeDnCache; |
|
19 | - |
|
20 | - public function __construct( |
|
21 | - private IConfig $config, |
|
22 | - private IDBConnection $connection, |
|
23 | - ) { |
|
24 | - $this->sanitizeDnCache = new CappedMemoryCache(10000); |
|
25 | - } |
|
26 | - |
|
27 | - /** |
|
28 | - * returns prefixes for each saved LDAP/AD server configuration. |
|
29 | - * |
|
30 | - * @param bool $activeConfigurations optional, whether only active configuration shall be |
|
31 | - * retrieved, defaults to false |
|
32 | - * @return array with a list of the available prefixes |
|
33 | - * |
|
34 | - * Configuration prefixes are used to set up configurations for n LDAP or |
|
35 | - * AD servers. Since configuration is stored in the database, table |
|
36 | - * appconfig under appid user_ldap, the common identifiers in column |
|
37 | - * 'configkey' have a prefix. The prefix for the very first server |
|
38 | - * configuration is empty. |
|
39 | - * Configkey Examples: |
|
40 | - * Server 1: ldap_login_filter |
|
41 | - * Server 2: s1_ldap_login_filter |
|
42 | - * Server 3: s2_ldap_login_filter |
|
43 | - * |
|
44 | - * The prefix needs to be passed to the constructor of Connection class, |
|
45 | - * except the default (first) server shall be connected to. |
|
46 | - * |
|
47 | - */ |
|
48 | - public function getServerConfigurationPrefixes($activeConfigurations = false): array { |
|
49 | - $referenceConfigkey = 'ldap_configuration_active'; |
|
50 | - |
|
51 | - $keys = $this->getServersConfig($referenceConfigkey); |
|
52 | - |
|
53 | - $prefixes = []; |
|
54 | - foreach ($keys as $key) { |
|
55 | - if ($activeConfigurations && $this->config->getAppValue('user_ldap', $key, '0') !== '1') { |
|
56 | - continue; |
|
57 | - } |
|
58 | - |
|
59 | - $len = strlen($key) - strlen($referenceConfigkey); |
|
60 | - $prefixes[] = substr($key, 0, $len); |
|
61 | - } |
|
62 | - asort($prefixes); |
|
63 | - |
|
64 | - return $prefixes; |
|
65 | - } |
|
66 | - |
|
67 | - /** |
|
68 | - * |
|
69 | - * determines the host for every configured connection |
|
70 | - * |
|
71 | - * @return array an array with configprefix as keys |
|
72 | - * |
|
73 | - */ |
|
74 | - public function getServerConfigurationHosts() { |
|
75 | - $referenceConfigkey = 'ldap_host'; |
|
76 | - |
|
77 | - $keys = $this->getServersConfig($referenceConfigkey); |
|
78 | - |
|
79 | - $result = []; |
|
80 | - foreach ($keys as $key) { |
|
81 | - $len = strlen($key) - strlen($referenceConfigkey); |
|
82 | - $prefix = substr($key, 0, $len); |
|
83 | - $result[$prefix] = $this->config->getAppValue('user_ldap', $key); |
|
84 | - } |
|
85 | - |
|
86 | - return $result; |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * return the next available configuration prefix |
|
91 | - * |
|
92 | - * @return string |
|
93 | - */ |
|
94 | - public function getNextServerConfigurationPrefix() { |
|
95 | - $serverConnections = $this->getServerConfigurationPrefixes(); |
|
96 | - |
|
97 | - if (count($serverConnections) === 0) { |
|
98 | - return 's01'; |
|
99 | - } |
|
100 | - |
|
101 | - sort($serverConnections); |
|
102 | - $lastKey = array_pop($serverConnections); |
|
103 | - $lastNumber = (int)str_replace('s', '', $lastKey); |
|
104 | - return 's' . str_pad((string)($lastNumber + 1), 2, '0', STR_PAD_LEFT); |
|
105 | - } |
|
106 | - |
|
107 | - private function getServersConfig(string $value): array { |
|
108 | - $regex = '/' . $value . '$/S'; |
|
109 | - |
|
110 | - $keys = $this->config->getAppKeys('user_ldap'); |
|
111 | - $result = []; |
|
112 | - foreach ($keys as $key) { |
|
113 | - if (preg_match($regex, $key) === 1) { |
|
114 | - $result[] = $key; |
|
115 | - } |
|
116 | - } |
|
117 | - |
|
118 | - return $result; |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * deletes a given saved LDAP/AD server configuration. |
|
123 | - * |
|
124 | - * @param string $prefix the configuration prefix of the config to delete |
|
125 | - * @return bool true on success, false otherwise |
|
126 | - */ |
|
127 | - public function deleteServerConfiguration($prefix) { |
|
128 | - if (!in_array($prefix, self::getServerConfigurationPrefixes())) { |
|
129 | - return false; |
|
130 | - } |
|
131 | - |
|
132 | - $query = $this->connection->getQueryBuilder(); |
|
133 | - $query->delete('appconfig') |
|
134 | - ->where($query->expr()->eq('appid', $query->createNamedParameter('user_ldap'))) |
|
135 | - ->andWhere($query->expr()->like('configkey', $query->createNamedParameter((string)$prefix . '%'))) |
|
136 | - ->andWhere($query->expr()->notIn('configkey', $query->createNamedParameter([ |
|
137 | - 'enabled', |
|
138 | - 'installed_version', |
|
139 | - 'types', |
|
140 | - 'bgjUpdateGroupsLastRun', |
|
141 | - ], IQueryBuilder::PARAM_STR_ARRAY))); |
|
142 | - |
|
143 | - if (empty($prefix)) { |
|
144 | - $query->andWhere($query->expr()->notLike('configkey', $query->createNamedParameter('s%'))); |
|
145 | - } |
|
146 | - |
|
147 | - $deletedRows = $query->execute(); |
|
148 | - return $deletedRows !== 0; |
|
149 | - } |
|
150 | - |
|
151 | - /** |
|
152 | - * checks whether there is one or more disabled LDAP configurations |
|
153 | - */ |
|
154 | - public function haveDisabledConfigurations(): bool { |
|
155 | - $all = $this->getServerConfigurationPrefixes(false); |
|
156 | - $active = $this->getServerConfigurationPrefixes(true); |
|
157 | - |
|
158 | - return count($all) !== count($active) || count($all) === 0; |
|
159 | - } |
|
160 | - |
|
161 | - /** |
|
162 | - * extracts the domain from a given URL |
|
163 | - * |
|
164 | - * @param string $url the URL |
|
165 | - * @return string|false domain as string on success, false otherwise |
|
166 | - */ |
|
167 | - public function getDomainFromURL($url) { |
|
168 | - $uinfo = parse_url($url); |
|
169 | - if (!is_array($uinfo)) { |
|
170 | - return false; |
|
171 | - } |
|
172 | - |
|
173 | - $domain = false; |
|
174 | - if (isset($uinfo['host'])) { |
|
175 | - $domain = $uinfo['host']; |
|
176 | - } elseif (isset($uinfo['path'])) { |
|
177 | - $domain = $uinfo['path']; |
|
178 | - } |
|
179 | - |
|
180 | - return $domain; |
|
181 | - } |
|
182 | - |
|
183 | - /** |
|
184 | - * sanitizes a DN received from the LDAP server |
|
185 | - * |
|
186 | - * This is used and done to have a stable format of DNs that can be compared |
|
187 | - * and identified again. The input DN value is modified as following: |
|
188 | - * |
|
189 | - * 1) whitespaces after commas are removed |
|
190 | - * 2) the DN is turned to lower-case |
|
191 | - * 3) the DN is escaped according to RFC 2253 |
|
192 | - * |
|
193 | - * When a future DN is supposed to be used as a base parameter, it has to be |
|
194 | - * run through DNasBaseParameter() first, to recode \5c into a backslash |
|
195 | - * again, otherwise the search or read operation will fail with LDAP error |
|
196 | - * 32, NO_SUCH_OBJECT. Regular usage in LDAP filters requires the backslash |
|
197 | - * being escaped, however. |
|
198 | - * |
|
199 | - * Internally, DNs are stored in their sanitized form. |
|
200 | - * |
|
201 | - * @param array|string $dn the DN in question |
|
202 | - * @return array|string the sanitized DN |
|
203 | - */ |
|
204 | - public function sanitizeDN($dn) { |
|
205 | - //treating multiple base DNs |
|
206 | - if (is_array($dn)) { |
|
207 | - $result = []; |
|
208 | - foreach ($dn as $singleDN) { |
|
209 | - $result[] = $this->sanitizeDN($singleDN); |
|
210 | - } |
|
211 | - return $result; |
|
212 | - } |
|
213 | - |
|
214 | - if (!is_string($dn)) { |
|
215 | - throw new \LogicException('String expected ' . \gettype($dn) . ' given'); |
|
216 | - } |
|
217 | - |
|
218 | - if (($sanitizedDn = $this->sanitizeDnCache->get($dn)) !== null) { |
|
219 | - return $sanitizedDn; |
|
220 | - } |
|
221 | - |
|
222 | - //OID sometimes gives back DNs with whitespace after the comma |
|
223 | - // a la "uid=foo, cn=bar, dn=..." We need to tackle this! |
|
224 | - $sanitizedDn = preg_replace('/([^\\\]),(\s+)/u', '\1,', $dn); |
|
225 | - |
|
226 | - //make comparisons and everything work |
|
227 | - $sanitizedDn = mb_strtolower($sanitizedDn, 'UTF-8'); |
|
228 | - |
|
229 | - //escape DN values according to RFC 2253 – this is already done by ldap_explode_dn |
|
230 | - //to use the DN in search filters, \ needs to be escaped to \5c additionally |
|
231 | - //to use them in bases, we convert them back to simple backslashes in readAttribute() |
|
232 | - $replacements = [ |
|
233 | - '\,' => '\5c2C', |
|
234 | - '\=' => '\5c3D', |
|
235 | - '\+' => '\5c2B', |
|
236 | - '\<' => '\5c3C', |
|
237 | - '\>' => '\5c3E', |
|
238 | - '\;' => '\5c3B', |
|
239 | - '\"' => '\5c22', |
|
240 | - '\#' => '\5c23', |
|
241 | - '(' => '\28', |
|
242 | - ')' => '\29', |
|
243 | - '*' => '\2A', |
|
244 | - ]; |
|
245 | - $sanitizedDn = str_replace(array_keys($replacements), array_values($replacements), $sanitizedDn); |
|
246 | - $this->sanitizeDnCache->set($dn, $sanitizedDn); |
|
247 | - |
|
248 | - return $sanitizedDn; |
|
249 | - } |
|
250 | - |
|
251 | - /** |
|
252 | - * converts a stored DN so it can be used as base parameter for LDAP queries, internally we store them for usage in LDAP filters |
|
253 | - * |
|
254 | - * @param string $dn the DN |
|
255 | - * @return string |
|
256 | - */ |
|
257 | - public function DNasBaseParameter($dn) { |
|
258 | - return str_ireplace('\\5c', '\\', $dn); |
|
259 | - } |
|
260 | - |
|
261 | - /** |
|
262 | - * listens to a hook thrown by server2server sharing and replaces the given |
|
263 | - * login name by a username, if it matches an LDAP user. |
|
264 | - * |
|
265 | - * @param array $param contains a reference to a $uid var under 'uid' key |
|
266 | - * @throws \Exception |
|
267 | - */ |
|
268 | - public static function loginName2UserName($param): void { |
|
269 | - if (!isset($param['uid'])) { |
|
270 | - throw new \Exception('key uid is expected to be set in $param'); |
|
271 | - } |
|
272 | - |
|
273 | - $userBackend = Server::get(User_Proxy::class); |
|
274 | - $uid = $userBackend->loginName2UserName($param['uid']); |
|
275 | - if ($uid !== false) { |
|
276 | - $param['uid'] = $uid; |
|
277 | - } |
|
278 | - } |
|
17 | + /** @var CappedMemoryCache<string> */ |
|
18 | + protected CappedMemoryCache $sanitizeDnCache; |
|
19 | + |
|
20 | + public function __construct( |
|
21 | + private IConfig $config, |
|
22 | + private IDBConnection $connection, |
|
23 | + ) { |
|
24 | + $this->sanitizeDnCache = new CappedMemoryCache(10000); |
|
25 | + } |
|
26 | + |
|
27 | + /** |
|
28 | + * returns prefixes for each saved LDAP/AD server configuration. |
|
29 | + * |
|
30 | + * @param bool $activeConfigurations optional, whether only active configuration shall be |
|
31 | + * retrieved, defaults to false |
|
32 | + * @return array with a list of the available prefixes |
|
33 | + * |
|
34 | + * Configuration prefixes are used to set up configurations for n LDAP or |
|
35 | + * AD servers. Since configuration is stored in the database, table |
|
36 | + * appconfig under appid user_ldap, the common identifiers in column |
|
37 | + * 'configkey' have a prefix. The prefix for the very first server |
|
38 | + * configuration is empty. |
|
39 | + * Configkey Examples: |
|
40 | + * Server 1: ldap_login_filter |
|
41 | + * Server 2: s1_ldap_login_filter |
|
42 | + * Server 3: s2_ldap_login_filter |
|
43 | + * |
|
44 | + * The prefix needs to be passed to the constructor of Connection class, |
|
45 | + * except the default (first) server shall be connected to. |
|
46 | + * |
|
47 | + */ |
|
48 | + public function getServerConfigurationPrefixes($activeConfigurations = false): array { |
|
49 | + $referenceConfigkey = 'ldap_configuration_active'; |
|
50 | + |
|
51 | + $keys = $this->getServersConfig($referenceConfigkey); |
|
52 | + |
|
53 | + $prefixes = []; |
|
54 | + foreach ($keys as $key) { |
|
55 | + if ($activeConfigurations && $this->config->getAppValue('user_ldap', $key, '0') !== '1') { |
|
56 | + continue; |
|
57 | + } |
|
58 | + |
|
59 | + $len = strlen($key) - strlen($referenceConfigkey); |
|
60 | + $prefixes[] = substr($key, 0, $len); |
|
61 | + } |
|
62 | + asort($prefixes); |
|
63 | + |
|
64 | + return $prefixes; |
|
65 | + } |
|
66 | + |
|
67 | + /** |
|
68 | + * |
|
69 | + * determines the host for every configured connection |
|
70 | + * |
|
71 | + * @return array an array with configprefix as keys |
|
72 | + * |
|
73 | + */ |
|
74 | + public function getServerConfigurationHosts() { |
|
75 | + $referenceConfigkey = 'ldap_host'; |
|
76 | + |
|
77 | + $keys = $this->getServersConfig($referenceConfigkey); |
|
78 | + |
|
79 | + $result = []; |
|
80 | + foreach ($keys as $key) { |
|
81 | + $len = strlen($key) - strlen($referenceConfigkey); |
|
82 | + $prefix = substr($key, 0, $len); |
|
83 | + $result[$prefix] = $this->config->getAppValue('user_ldap', $key); |
|
84 | + } |
|
85 | + |
|
86 | + return $result; |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * return the next available configuration prefix |
|
91 | + * |
|
92 | + * @return string |
|
93 | + */ |
|
94 | + public function getNextServerConfigurationPrefix() { |
|
95 | + $serverConnections = $this->getServerConfigurationPrefixes(); |
|
96 | + |
|
97 | + if (count($serverConnections) === 0) { |
|
98 | + return 's01'; |
|
99 | + } |
|
100 | + |
|
101 | + sort($serverConnections); |
|
102 | + $lastKey = array_pop($serverConnections); |
|
103 | + $lastNumber = (int)str_replace('s', '', $lastKey); |
|
104 | + return 's' . str_pad((string)($lastNumber + 1), 2, '0', STR_PAD_LEFT); |
|
105 | + } |
|
106 | + |
|
107 | + private function getServersConfig(string $value): array { |
|
108 | + $regex = '/' . $value . '$/S'; |
|
109 | + |
|
110 | + $keys = $this->config->getAppKeys('user_ldap'); |
|
111 | + $result = []; |
|
112 | + foreach ($keys as $key) { |
|
113 | + if (preg_match($regex, $key) === 1) { |
|
114 | + $result[] = $key; |
|
115 | + } |
|
116 | + } |
|
117 | + |
|
118 | + return $result; |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * deletes a given saved LDAP/AD server configuration. |
|
123 | + * |
|
124 | + * @param string $prefix the configuration prefix of the config to delete |
|
125 | + * @return bool true on success, false otherwise |
|
126 | + */ |
|
127 | + public function deleteServerConfiguration($prefix) { |
|
128 | + if (!in_array($prefix, self::getServerConfigurationPrefixes())) { |
|
129 | + return false; |
|
130 | + } |
|
131 | + |
|
132 | + $query = $this->connection->getQueryBuilder(); |
|
133 | + $query->delete('appconfig') |
|
134 | + ->where($query->expr()->eq('appid', $query->createNamedParameter('user_ldap'))) |
|
135 | + ->andWhere($query->expr()->like('configkey', $query->createNamedParameter((string)$prefix . '%'))) |
|
136 | + ->andWhere($query->expr()->notIn('configkey', $query->createNamedParameter([ |
|
137 | + 'enabled', |
|
138 | + 'installed_version', |
|
139 | + 'types', |
|
140 | + 'bgjUpdateGroupsLastRun', |
|
141 | + ], IQueryBuilder::PARAM_STR_ARRAY))); |
|
142 | + |
|
143 | + if (empty($prefix)) { |
|
144 | + $query->andWhere($query->expr()->notLike('configkey', $query->createNamedParameter('s%'))); |
|
145 | + } |
|
146 | + |
|
147 | + $deletedRows = $query->execute(); |
|
148 | + return $deletedRows !== 0; |
|
149 | + } |
|
150 | + |
|
151 | + /** |
|
152 | + * checks whether there is one or more disabled LDAP configurations |
|
153 | + */ |
|
154 | + public function haveDisabledConfigurations(): bool { |
|
155 | + $all = $this->getServerConfigurationPrefixes(false); |
|
156 | + $active = $this->getServerConfigurationPrefixes(true); |
|
157 | + |
|
158 | + return count($all) !== count($active) || count($all) === 0; |
|
159 | + } |
|
160 | + |
|
161 | + /** |
|
162 | + * extracts the domain from a given URL |
|
163 | + * |
|
164 | + * @param string $url the URL |
|
165 | + * @return string|false domain as string on success, false otherwise |
|
166 | + */ |
|
167 | + public function getDomainFromURL($url) { |
|
168 | + $uinfo = parse_url($url); |
|
169 | + if (!is_array($uinfo)) { |
|
170 | + return false; |
|
171 | + } |
|
172 | + |
|
173 | + $domain = false; |
|
174 | + if (isset($uinfo['host'])) { |
|
175 | + $domain = $uinfo['host']; |
|
176 | + } elseif (isset($uinfo['path'])) { |
|
177 | + $domain = $uinfo['path']; |
|
178 | + } |
|
179 | + |
|
180 | + return $domain; |
|
181 | + } |
|
182 | + |
|
183 | + /** |
|
184 | + * sanitizes a DN received from the LDAP server |
|
185 | + * |
|
186 | + * This is used and done to have a stable format of DNs that can be compared |
|
187 | + * and identified again. The input DN value is modified as following: |
|
188 | + * |
|
189 | + * 1) whitespaces after commas are removed |
|
190 | + * 2) the DN is turned to lower-case |
|
191 | + * 3) the DN is escaped according to RFC 2253 |
|
192 | + * |
|
193 | + * When a future DN is supposed to be used as a base parameter, it has to be |
|
194 | + * run through DNasBaseParameter() first, to recode \5c into a backslash |
|
195 | + * again, otherwise the search or read operation will fail with LDAP error |
|
196 | + * 32, NO_SUCH_OBJECT. Regular usage in LDAP filters requires the backslash |
|
197 | + * being escaped, however. |
|
198 | + * |
|
199 | + * Internally, DNs are stored in their sanitized form. |
|
200 | + * |
|
201 | + * @param array|string $dn the DN in question |
|
202 | + * @return array|string the sanitized DN |
|
203 | + */ |
|
204 | + public function sanitizeDN($dn) { |
|
205 | + //treating multiple base DNs |
|
206 | + if (is_array($dn)) { |
|
207 | + $result = []; |
|
208 | + foreach ($dn as $singleDN) { |
|
209 | + $result[] = $this->sanitizeDN($singleDN); |
|
210 | + } |
|
211 | + return $result; |
|
212 | + } |
|
213 | + |
|
214 | + if (!is_string($dn)) { |
|
215 | + throw new \LogicException('String expected ' . \gettype($dn) . ' given'); |
|
216 | + } |
|
217 | + |
|
218 | + if (($sanitizedDn = $this->sanitizeDnCache->get($dn)) !== null) { |
|
219 | + return $sanitizedDn; |
|
220 | + } |
|
221 | + |
|
222 | + //OID sometimes gives back DNs with whitespace after the comma |
|
223 | + // a la "uid=foo, cn=bar, dn=..." We need to tackle this! |
|
224 | + $sanitizedDn = preg_replace('/([^\\\]),(\s+)/u', '\1,', $dn); |
|
225 | + |
|
226 | + //make comparisons and everything work |
|
227 | + $sanitizedDn = mb_strtolower($sanitizedDn, 'UTF-8'); |
|
228 | + |
|
229 | + //escape DN values according to RFC 2253 – this is already done by ldap_explode_dn |
|
230 | + //to use the DN in search filters, \ needs to be escaped to \5c additionally |
|
231 | + //to use them in bases, we convert them back to simple backslashes in readAttribute() |
|
232 | + $replacements = [ |
|
233 | + '\,' => '\5c2C', |
|
234 | + '\=' => '\5c3D', |
|
235 | + '\+' => '\5c2B', |
|
236 | + '\<' => '\5c3C', |
|
237 | + '\>' => '\5c3E', |
|
238 | + '\;' => '\5c3B', |
|
239 | + '\"' => '\5c22', |
|
240 | + '\#' => '\5c23', |
|
241 | + '(' => '\28', |
|
242 | + ')' => '\29', |
|
243 | + '*' => '\2A', |
|
244 | + ]; |
|
245 | + $sanitizedDn = str_replace(array_keys($replacements), array_values($replacements), $sanitizedDn); |
|
246 | + $this->sanitizeDnCache->set($dn, $sanitizedDn); |
|
247 | + |
|
248 | + return $sanitizedDn; |
|
249 | + } |
|
250 | + |
|
251 | + /** |
|
252 | + * converts a stored DN so it can be used as base parameter for LDAP queries, internally we store them for usage in LDAP filters |
|
253 | + * |
|
254 | + * @param string $dn the DN |
|
255 | + * @return string |
|
256 | + */ |
|
257 | + public function DNasBaseParameter($dn) { |
|
258 | + return str_ireplace('\\5c', '\\', $dn); |
|
259 | + } |
|
260 | + |
|
261 | + /** |
|
262 | + * listens to a hook thrown by server2server sharing and replaces the given |
|
263 | + * login name by a username, if it matches an LDAP user. |
|
264 | + * |
|
265 | + * @param array $param contains a reference to a $uid var under 'uid' key |
|
266 | + * @throws \Exception |
|
267 | + */ |
|
268 | + public static function loginName2UserName($param): void { |
|
269 | + if (!isset($param['uid'])) { |
|
270 | + throw new \Exception('key uid is expected to be set in $param'); |
|
271 | + } |
|
272 | + |
|
273 | + $userBackend = Server::get(User_Proxy::class); |
|
274 | + $uid = $userBackend->loginName2UserName($param['uid']); |
|
275 | + if ($uid !== false) { |
|
276 | + $param['uid'] = $uid; |
|
277 | + } |
|
278 | + } |
|
279 | 279 | } |
@@ -29,12 +29,12 @@ |
||
29 | 29 | */ |
30 | 30 | class BadGateway extends \Sabre\DAV\Exception { |
31 | 31 | |
32 | - /** |
|
33 | - * Returns the HTTP status code for this exception |
|
34 | - * |
|
35 | - * @return int |
|
36 | - */ |
|
37 | - public function getHTTPCode() { |
|
38 | - return 502; |
|
39 | - } |
|
32 | + /** |
|
33 | + * Returns the HTTP status code for this exception |
|
34 | + * |
|
35 | + * @return int |
|
36 | + */ |
|
37 | + public function getHTTPCode() { |
|
38 | + return 502; |
|
39 | + } |
|
40 | 40 | } |
@@ -53,6 +53,6 @@ |
||
53 | 53 | } |
54 | 54 | |
55 | 55 | public function getPath(): string { |
56 | - return $this->getMountPoint() . $this->getInternalPath(); |
|
56 | + return $this->getMountPoint().$this->getInternalPath(); |
|
57 | 57 | } |
58 | 58 | } |
@@ -9,31 +9,31 @@ |
||
9 | 9 | use OCP\IUser; |
10 | 10 | |
11 | 11 | class CachedMountFileInfo extends CachedMountInfo implements ICachedMountFileInfo { |
12 | - private string $internalPath; |
|
12 | + private string $internalPath; |
|
13 | 13 | |
14 | - public function __construct( |
|
15 | - IUser $user, |
|
16 | - int $storageId, |
|
17 | - int $rootId, |
|
18 | - string $mountPoint, |
|
19 | - ?int $mountId, |
|
20 | - string $mountProvider, |
|
21 | - string $rootInternalPath, |
|
22 | - string $internalPath, |
|
23 | - ) { |
|
24 | - parent::__construct($user, $storageId, $rootId, $mountPoint, $mountProvider, $mountId, $rootInternalPath); |
|
25 | - $this->internalPath = $internalPath; |
|
26 | - } |
|
14 | + public function __construct( |
|
15 | + IUser $user, |
|
16 | + int $storageId, |
|
17 | + int $rootId, |
|
18 | + string $mountPoint, |
|
19 | + ?int $mountId, |
|
20 | + string $mountProvider, |
|
21 | + string $rootInternalPath, |
|
22 | + string $internalPath, |
|
23 | + ) { |
|
24 | + parent::__construct($user, $storageId, $rootId, $mountPoint, $mountProvider, $mountId, $rootInternalPath); |
|
25 | + $this->internalPath = $internalPath; |
|
26 | + } |
|
27 | 27 | |
28 | - public function getInternalPath(): string { |
|
29 | - if ($this->getRootInternalPath()) { |
|
30 | - return substr($this->internalPath, strlen($this->getRootInternalPath()) + 1); |
|
31 | - } else { |
|
32 | - return $this->internalPath; |
|
33 | - } |
|
34 | - } |
|
28 | + public function getInternalPath(): string { |
|
29 | + if ($this->getRootInternalPath()) { |
|
30 | + return substr($this->internalPath, strlen($this->getRootInternalPath()) + 1); |
|
31 | + } else { |
|
32 | + return $this->internalPath; |
|
33 | + } |
|
34 | + } |
|
35 | 35 | |
36 | - public function getPath(): string { |
|
37 | - return $this->getMountPoint() . $this->getInternalPath(); |
|
38 | - } |
|
36 | + public function getPath(): string { |
|
37 | + return $this->getMountPoint() . $this->getInternalPath(); |
|
38 | + } |
|
39 | 39 | } |
@@ -67,7 +67,7 @@ discard block |
||
67 | 67 | foreach ($directoryRange as $child) { |
68 | 68 | $mountPoints[] = new MountPoint( |
69 | 69 | AppdataPreviewObjectStoreStorage::class, |
70 | - '/appdata_' . $instanceId . '/preview/' . $parent . '/' . $child, |
|
70 | + '/appdata_'.$instanceId.'/preview/'.$parent.'/'.$child, |
|
71 | 71 | $this->getMultiBucketObjectStore($i), |
72 | 72 | $loader, |
73 | 73 | null, |
@@ -82,13 +82,13 @@ discard block |
||
82 | 82 | $fakeRootStorage = new ObjectStoreStorage($rootStorageArguments); |
83 | 83 | $fakeRootStorageJail = new Jail([ |
84 | 84 | 'storage' => $fakeRootStorage, |
85 | - 'root' => '/appdata_' . $instanceId . '/preview', |
|
85 | + 'root' => '/appdata_'.$instanceId.'/preview', |
|
86 | 86 | ]); |
87 | 87 | |
88 | 88 | // add a fallback location to be able to fetch existing previews from the old bucket |
89 | 89 | $mountPoints[] = new MountPoint( |
90 | 90 | $fakeRootStorageJail, |
91 | - '/appdata_' . $instanceId . '/preview/old-multibucket', |
|
91 | + '/appdata_'.$instanceId.'/preview/old-multibucket', |
|
92 | 92 | null, |
93 | 93 | $loader, |
94 | 94 | null, |
@@ -37,119 +37,119 @@ |
||
37 | 37 | * Mount provider for object store app data folder for previews |
38 | 38 | */ |
39 | 39 | class ObjectStorePreviewCacheMountProvider implements IRootMountProvider { |
40 | - private LoggerInterface $logger; |
|
41 | - /** @var IConfig */ |
|
42 | - private $config; |
|
43 | - |
|
44 | - public function __construct(LoggerInterface $logger, IConfig $config) { |
|
45 | - $this->logger = $logger; |
|
46 | - $this->config = $config; |
|
47 | - } |
|
48 | - |
|
49 | - /** |
|
50 | - * @return MountPoint[] |
|
51 | - * @throws \Exception |
|
52 | - */ |
|
53 | - public function getRootMounts(IStorageFactory $loader): array { |
|
54 | - if (!is_array($this->config->getSystemValue('objectstore_multibucket'))) { |
|
55 | - return []; |
|
56 | - } |
|
57 | - if ($this->config->getSystemValue('objectstore.multibucket.preview-distribution', false) !== true) { |
|
58 | - return []; |
|
59 | - } |
|
60 | - |
|
61 | - $instanceId = $this->config->getSystemValueString('instanceid', ''); |
|
62 | - $mountPoints = []; |
|
63 | - $directoryRange = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']; |
|
64 | - $i = 0; |
|
65 | - foreach ($directoryRange as $parent) { |
|
66 | - foreach ($directoryRange as $child) { |
|
67 | - $mountPoints[] = new MountPoint( |
|
68 | - AppdataPreviewObjectStoreStorage::class, |
|
69 | - '/appdata_' . $instanceId . '/preview/' . $parent . '/' . $child, |
|
70 | - $this->getMultiBucketObjectStore($i), |
|
71 | - $loader, |
|
72 | - null, |
|
73 | - null, |
|
74 | - self::class |
|
75 | - ); |
|
76 | - $i++; |
|
77 | - } |
|
78 | - } |
|
79 | - |
|
80 | - $rootStorageArguments = $this->getMultiBucketObjectStoreForRoot(); |
|
81 | - $fakeRootStorage = new ObjectStoreStorage($rootStorageArguments); |
|
82 | - $fakeRootStorageJail = new Jail([ |
|
83 | - 'storage' => $fakeRootStorage, |
|
84 | - 'root' => '/appdata_' . $instanceId . '/preview', |
|
85 | - ]); |
|
86 | - |
|
87 | - // add a fallback location to be able to fetch existing previews from the old bucket |
|
88 | - $mountPoints[] = new MountPoint( |
|
89 | - $fakeRootStorageJail, |
|
90 | - '/appdata_' . $instanceId . '/preview/old-multibucket', |
|
91 | - null, |
|
92 | - $loader, |
|
93 | - null, |
|
94 | - null, |
|
95 | - self::class |
|
96 | - ); |
|
97 | - |
|
98 | - return $mountPoints; |
|
99 | - } |
|
100 | - |
|
101 | - protected function getMultiBucketObjectStore(int $number): array { |
|
102 | - $config = $this->config->getSystemValue('objectstore_multibucket'); |
|
103 | - |
|
104 | - // sanity checks |
|
105 | - if (empty($config['class'])) { |
|
106 | - $this->logger->error('No class given for objectstore', ['app' => 'files']); |
|
107 | - } |
|
108 | - if (!isset($config['arguments'])) { |
|
109 | - $config['arguments'] = []; |
|
110 | - } |
|
111 | - |
|
112 | - /* |
|
40 | + private LoggerInterface $logger; |
|
41 | + /** @var IConfig */ |
|
42 | + private $config; |
|
43 | + |
|
44 | + public function __construct(LoggerInterface $logger, IConfig $config) { |
|
45 | + $this->logger = $logger; |
|
46 | + $this->config = $config; |
|
47 | + } |
|
48 | + |
|
49 | + /** |
|
50 | + * @return MountPoint[] |
|
51 | + * @throws \Exception |
|
52 | + */ |
|
53 | + public function getRootMounts(IStorageFactory $loader): array { |
|
54 | + if (!is_array($this->config->getSystemValue('objectstore_multibucket'))) { |
|
55 | + return []; |
|
56 | + } |
|
57 | + if ($this->config->getSystemValue('objectstore.multibucket.preview-distribution', false) !== true) { |
|
58 | + return []; |
|
59 | + } |
|
60 | + |
|
61 | + $instanceId = $this->config->getSystemValueString('instanceid', ''); |
|
62 | + $mountPoints = []; |
|
63 | + $directoryRange = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']; |
|
64 | + $i = 0; |
|
65 | + foreach ($directoryRange as $parent) { |
|
66 | + foreach ($directoryRange as $child) { |
|
67 | + $mountPoints[] = new MountPoint( |
|
68 | + AppdataPreviewObjectStoreStorage::class, |
|
69 | + '/appdata_' . $instanceId . '/preview/' . $parent . '/' . $child, |
|
70 | + $this->getMultiBucketObjectStore($i), |
|
71 | + $loader, |
|
72 | + null, |
|
73 | + null, |
|
74 | + self::class |
|
75 | + ); |
|
76 | + $i++; |
|
77 | + } |
|
78 | + } |
|
79 | + |
|
80 | + $rootStorageArguments = $this->getMultiBucketObjectStoreForRoot(); |
|
81 | + $fakeRootStorage = new ObjectStoreStorage($rootStorageArguments); |
|
82 | + $fakeRootStorageJail = new Jail([ |
|
83 | + 'storage' => $fakeRootStorage, |
|
84 | + 'root' => '/appdata_' . $instanceId . '/preview', |
|
85 | + ]); |
|
86 | + |
|
87 | + // add a fallback location to be able to fetch existing previews from the old bucket |
|
88 | + $mountPoints[] = new MountPoint( |
|
89 | + $fakeRootStorageJail, |
|
90 | + '/appdata_' . $instanceId . '/preview/old-multibucket', |
|
91 | + null, |
|
92 | + $loader, |
|
93 | + null, |
|
94 | + null, |
|
95 | + self::class |
|
96 | + ); |
|
97 | + |
|
98 | + return $mountPoints; |
|
99 | + } |
|
100 | + |
|
101 | + protected function getMultiBucketObjectStore(int $number): array { |
|
102 | + $config = $this->config->getSystemValue('objectstore_multibucket'); |
|
103 | + |
|
104 | + // sanity checks |
|
105 | + if (empty($config['class'])) { |
|
106 | + $this->logger->error('No class given for objectstore', ['app' => 'files']); |
|
107 | + } |
|
108 | + if (!isset($config['arguments'])) { |
|
109 | + $config['arguments'] = []; |
|
110 | + } |
|
111 | + |
|
112 | + /* |
|
113 | 113 | * Use any provided bucket argument as prefix |
114 | 114 | * and add the mapping from parent/child => bucket |
115 | 115 | */ |
116 | - if (!isset($config['arguments']['bucket'])) { |
|
117 | - $config['arguments']['bucket'] = ''; |
|
118 | - } |
|
116 | + if (!isset($config['arguments']['bucket'])) { |
|
117 | + $config['arguments']['bucket'] = ''; |
|
118 | + } |
|
119 | 119 | |
120 | - $config['arguments']['bucket'] .= "-preview-$number"; |
|
120 | + $config['arguments']['bucket'] .= "-preview-$number"; |
|
121 | 121 | |
122 | - // instantiate object store implementation |
|
123 | - $config['arguments']['objectstore'] = new $config['class']($config['arguments']); |
|
122 | + // instantiate object store implementation |
|
123 | + $config['arguments']['objectstore'] = new $config['class']($config['arguments']); |
|
124 | 124 | |
125 | - $config['arguments']['internal-id'] = $number; |
|
125 | + $config['arguments']['internal-id'] = $number; |
|
126 | 126 | |
127 | - return $config['arguments']; |
|
128 | - } |
|
127 | + return $config['arguments']; |
|
128 | + } |
|
129 | 129 | |
130 | - protected function getMultiBucketObjectStoreForRoot(): array { |
|
131 | - $config = $this->config->getSystemValue('objectstore_multibucket'); |
|
130 | + protected function getMultiBucketObjectStoreForRoot(): array { |
|
131 | + $config = $this->config->getSystemValue('objectstore_multibucket'); |
|
132 | 132 | |
133 | - // sanity checks |
|
134 | - if (empty($config['class'])) { |
|
135 | - $this->logger->error('No class given for objectstore', ['app' => 'files']); |
|
136 | - } |
|
137 | - if (!isset($config['arguments'])) { |
|
138 | - $config['arguments'] = []; |
|
139 | - } |
|
133 | + // sanity checks |
|
134 | + if (empty($config['class'])) { |
|
135 | + $this->logger->error('No class given for objectstore', ['app' => 'files']); |
|
136 | + } |
|
137 | + if (!isset($config['arguments'])) { |
|
138 | + $config['arguments'] = []; |
|
139 | + } |
|
140 | 140 | |
141 | - /* |
|
141 | + /* |
|
142 | 142 | * Use any provided bucket argument as prefix |
143 | 143 | * and add the mapping from parent/child => bucket |
144 | 144 | */ |
145 | - if (!isset($config['arguments']['bucket'])) { |
|
146 | - $config['arguments']['bucket'] = ''; |
|
147 | - } |
|
148 | - $config['arguments']['bucket'] .= '0'; |
|
145 | + if (!isset($config['arguments']['bucket'])) { |
|
146 | + $config['arguments']['bucket'] = ''; |
|
147 | + } |
|
148 | + $config['arguments']['bucket'] .= '0'; |
|
149 | 149 | |
150 | - // instantiate object store implementation |
|
151 | - $config['arguments']['objectstore'] = new $config['class']($config['arguments']); |
|
150 | + // instantiate object store implementation |
|
151 | + $config['arguments']['objectstore'] = new $config['class']($config['arguments']); |
|
152 | 152 | |
153 | - return $config['arguments']; |
|
154 | - } |
|
153 | + return $config['arguments']; |
|
154 | + } |
|
155 | 155 | } |
@@ -29,22 +29,22 @@ |
||
29 | 29 | use OCP\IImage; |
30 | 30 | |
31 | 31 | class OpenDocument extends Bundled { |
32 | - /** |
|
33 | - * {@inheritDoc} |
|
34 | - */ |
|
35 | - public function getMimeType(): string { |
|
36 | - return '/application\/vnd.oasis.opendocument.*/'; |
|
37 | - } |
|
32 | + /** |
|
33 | + * {@inheritDoc} |
|
34 | + */ |
|
35 | + public function getMimeType(): string { |
|
36 | + return '/application\/vnd.oasis.opendocument.*/'; |
|
37 | + } |
|
38 | 38 | |
39 | 39 | |
40 | - /** |
|
41 | - * @inheritDoc |
|
42 | - */ |
|
43 | - public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage { |
|
44 | - $image = $this->extractThumbnail($file, 'Thumbnails/thumbnail.png'); |
|
45 | - if (($image !== null) && $image->valid()) { |
|
46 | - return $image; |
|
47 | - } |
|
48 | - return null; |
|
49 | - } |
|
40 | + /** |
|
41 | + * @inheritDoc |
|
42 | + */ |
|
43 | + public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage { |
|
44 | + $image = $this->extractThumbnail($file, 'Thumbnails/thumbnail.png'); |
|
45 | + if (($image !== null) && $image->valid()) { |
|
46 | + return $image; |
|
47 | + } |
|
48 | + return null; |
|
49 | + } |
|
50 | 50 | } |
@@ -26,26 +26,26 @@ |
||
26 | 26 | use OCP\IImage; |
27 | 27 | |
28 | 28 | class Krita extends Bundled { |
29 | - /** |
|
30 | - * {@inheritDoc} |
|
31 | - */ |
|
32 | - public function getMimeType(): string { |
|
33 | - return '/application\/x-krita/'; |
|
34 | - } |
|
29 | + /** |
|
30 | + * {@inheritDoc} |
|
31 | + */ |
|
32 | + public function getMimeType(): string { |
|
33 | + return '/application\/x-krita/'; |
|
34 | + } |
|
35 | 35 | |
36 | 36 | |
37 | - /** |
|
38 | - * @inheritDoc |
|
39 | - */ |
|
40 | - public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage { |
|
41 | - $image = $this->extractThumbnail($file, 'mergedimage.png'); |
|
42 | - if (($image !== null) && $image->valid()) { |
|
43 | - return $image; |
|
44 | - } |
|
45 | - $image = $this->extractThumbnail($file, 'preview.png'); |
|
46 | - if (($image !== null) && $image->valid()) { |
|
47 | - return $image; |
|
48 | - } |
|
49 | - return null; |
|
50 | - } |
|
37 | + /** |
|
38 | + * @inheritDoc |
|
39 | + */ |
|
40 | + public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage { |
|
41 | + $image = $this->extractThumbnail($file, 'mergedimage.png'); |
|
42 | + if (($image !== null) && $image->valid()) { |
|
43 | + return $image; |
|
44 | + } |
|
45 | + $image = $this->extractThumbnail($file, 'preview.png'); |
|
46 | + if (($image !== null) && $image->valid()) { |
|
47 | + return $image; |
|
48 | + } |
|
49 | + return null; |
|
50 | + } |
|
51 | 51 | } |
@@ -28,17 +28,17 @@ |
||
28 | 28 | * @since 13.0.0 |
29 | 29 | */ |
30 | 30 | interface ICachedMountFileInfo extends ICachedMountInfo { |
31 | - /** |
|
32 | - * Return the path for the file within the cached mount |
|
33 | - * |
|
34 | - * @return string |
|
35 | - * @since 13.0.0 |
|
36 | - */ |
|
37 | - public function getInternalPath(): string; |
|
31 | + /** |
|
32 | + * Return the path for the file within the cached mount |
|
33 | + * |
|
34 | + * @return string |
|
35 | + * @since 13.0.0 |
|
36 | + */ |
|
37 | + public function getInternalPath(): string; |
|
38 | 38 | |
39 | - /** |
|
40 | - * @return string |
|
41 | - * @since 13.0.0 |
|
42 | - */ |
|
43 | - public function getPath(): string; |
|
39 | + /** |
|
40 | + * @return string |
|
41 | + * @since 13.0.0 |
|
42 | + */ |
|
43 | + public function getPath(): string; |
|
44 | 44 | } |
@@ -78,7 +78,7 @@ |
||
78 | 78 | * @param string $userSearch the pattern users are search for |
79 | 79 | */ |
80 | 80 | public function get(string $groupSearch = '', string $userSearch = ''): array { |
81 | - $key = $groupSearch . '::' . $userSearch; |
|
81 | + $key = $groupSearch.'::'.$userSearch; |
|
82 | 82 | if (isset($this->metaData[$key])) { |
83 | 83 | return $this->metaData[$key]; |
84 | 84 | } |
@@ -13,155 +13,155 @@ |
||
13 | 13 | use OCP\IUserSession; |
14 | 14 | |
15 | 15 | class MetaData { |
16 | - public const SORT_NONE = 0; |
|
17 | - public const SORT_USERCOUNT = 1; // May have performance issues on LDAP backends |
|
18 | - public const SORT_GROUPNAME = 2; |
|
19 | - |
|
20 | - /** @var array */ |
|
21 | - protected $metaData = []; |
|
22 | - /** @var int */ |
|
23 | - protected $sorting = self::SORT_NONE; |
|
24 | - |
|
25 | - /** |
|
26 | - * @param string $user the uid of the current user |
|
27 | - * @param bool $isAdmin whether the current users is an admin |
|
28 | - */ |
|
29 | - public function __construct( |
|
30 | - private string $user, |
|
31 | - private bool $isAdmin, |
|
32 | - private bool $isDelegatedAdmin, |
|
33 | - private IGroupManager $groupManager, |
|
34 | - private IUserSession $userSession, |
|
35 | - ) { |
|
36 | - } |
|
37 | - |
|
38 | - /** |
|
39 | - * returns an array with meta data about all available groups |
|
40 | - * the array is structured as follows: |
|
41 | - * [0] array containing meta data about admin groups |
|
42 | - * [1] array containing meta data about unprivileged groups |
|
43 | - * @param string $groupSearch only effective when instance was created with |
|
44 | - * isAdmin being true |
|
45 | - * @param string $userSearch the pattern users are search for |
|
46 | - */ |
|
47 | - public function get(string $groupSearch = '', string $userSearch = ''): array { |
|
48 | - $key = $groupSearch . '::' . $userSearch; |
|
49 | - if (isset($this->metaData[$key])) { |
|
50 | - return $this->metaData[$key]; |
|
51 | - } |
|
52 | - |
|
53 | - $adminGroups = []; |
|
54 | - $groups = []; |
|
55 | - $sortGroupsIndex = 0; |
|
56 | - $sortGroupsKeys = []; |
|
57 | - $sortAdminGroupsIndex = 0; |
|
58 | - $sortAdminGroupsKeys = []; |
|
59 | - |
|
60 | - foreach ($this->getGroups($groupSearch) as $group) { |
|
61 | - $groupMetaData = $this->generateGroupMetaData($group, $userSearch); |
|
62 | - if (strtolower($group->getGID()) !== 'admin') { |
|
63 | - $this->addEntry( |
|
64 | - $groups, |
|
65 | - $sortGroupsKeys, |
|
66 | - $sortGroupsIndex, |
|
67 | - $groupMetaData); |
|
68 | - } else { |
|
69 | - //admin group is hard coded to 'admin' for now. In future, |
|
70 | - //backends may define admin groups too. Then the if statement |
|
71 | - //has to be adjusted accordingly. |
|
72 | - $this->addEntry( |
|
73 | - $adminGroups, |
|
74 | - $sortAdminGroupsKeys, |
|
75 | - $sortAdminGroupsIndex, |
|
76 | - $groupMetaData); |
|
77 | - } |
|
78 | - } |
|
79 | - |
|
80 | - //whether sorting is necessary is will be checked in sort() |
|
81 | - $this->sort($groups, $sortGroupsKeys); |
|
82 | - $this->sort($adminGroups, $sortAdminGroupsKeys); |
|
83 | - |
|
84 | - $this->metaData[$key] = [$adminGroups, $groups]; |
|
85 | - return $this->metaData[$key]; |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * sets the sort mode, see SORT_* constants for supported modes |
|
90 | - */ |
|
91 | - public function setSorting(int $sortMode): void { |
|
92 | - switch ($sortMode) { |
|
93 | - case self::SORT_USERCOUNT: |
|
94 | - case self::SORT_GROUPNAME: |
|
95 | - $this->sorting = $sortMode; |
|
96 | - break; |
|
97 | - |
|
98 | - default: |
|
99 | - $this->sorting = self::SORT_NONE; |
|
100 | - } |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * adds an group entry to the resulting array |
|
105 | - * @param array $entries the resulting array, by reference |
|
106 | - * @param array $sortKeys the sort key array, by reference |
|
107 | - * @param int $sortIndex the sort key index, by reference |
|
108 | - * @param array $data the group's meta data as returned by generateGroupMetaData() |
|
109 | - */ |
|
110 | - private function addEntry(array &$entries, array &$sortKeys, int &$sortIndex, array $data): void { |
|
111 | - $entries[] = $data; |
|
112 | - if ($this->sorting === self::SORT_USERCOUNT) { |
|
113 | - $sortKeys[$sortIndex] = $data['usercount']; |
|
114 | - $sortIndex++; |
|
115 | - } elseif ($this->sorting === self::SORT_GROUPNAME) { |
|
116 | - $sortKeys[$sortIndex] = $data['name']; |
|
117 | - $sortIndex++; |
|
118 | - } |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * creates an array containing the group meta data |
|
123 | - * @return array with the keys 'id', 'name', 'usercount' and 'disabled' |
|
124 | - */ |
|
125 | - private function generateGroupMetaData(IGroup $group, string $userSearch): array { |
|
126 | - return [ |
|
127 | - 'id' => $group->getGID(), |
|
128 | - 'name' => $group->getDisplayName(), |
|
129 | - 'usercount' => $this->sorting === self::SORT_USERCOUNT ? $group->count($userSearch) : 0, |
|
130 | - 'disabled' => $group->countDisabled(), |
|
131 | - 'canAdd' => $group->canAddUser(), |
|
132 | - 'canRemove' => $group->canRemoveUser(), |
|
133 | - ]; |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * sorts the result array, if applicable |
|
138 | - * @param array $entries the result array, by reference |
|
139 | - * @param array $sortKeys the array containing the sort keys |
|
140 | - */ |
|
141 | - private function sort(array &$entries, array $sortKeys): void { |
|
142 | - if ($this->sorting === self::SORT_USERCOUNT) { |
|
143 | - array_multisort($sortKeys, SORT_DESC, $entries); |
|
144 | - } elseif ($this->sorting === self::SORT_GROUPNAME) { |
|
145 | - array_multisort($sortKeys, SORT_ASC, $entries); |
|
146 | - } |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * returns the available groups |
|
151 | - * @return IGroup[] |
|
152 | - */ |
|
153 | - public function getGroups(string $search = ''): array { |
|
154 | - if ($this->isAdmin || $this->isDelegatedAdmin) { |
|
155 | - return $this->groupManager->search($search); |
|
156 | - } else { |
|
157 | - $userObject = $this->userSession->getUser(); |
|
158 | - if ($userObject !== null && $this->groupManager instanceof GroupManager) { |
|
159 | - $groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($userObject); |
|
160 | - } else { |
|
161 | - $groups = []; |
|
162 | - } |
|
163 | - |
|
164 | - return $groups; |
|
165 | - } |
|
166 | - } |
|
16 | + public const SORT_NONE = 0; |
|
17 | + public const SORT_USERCOUNT = 1; // May have performance issues on LDAP backends |
|
18 | + public const SORT_GROUPNAME = 2; |
|
19 | + |
|
20 | + /** @var array */ |
|
21 | + protected $metaData = []; |
|
22 | + /** @var int */ |
|
23 | + protected $sorting = self::SORT_NONE; |
|
24 | + |
|
25 | + /** |
|
26 | + * @param string $user the uid of the current user |
|
27 | + * @param bool $isAdmin whether the current users is an admin |
|
28 | + */ |
|
29 | + public function __construct( |
|
30 | + private string $user, |
|
31 | + private bool $isAdmin, |
|
32 | + private bool $isDelegatedAdmin, |
|
33 | + private IGroupManager $groupManager, |
|
34 | + private IUserSession $userSession, |
|
35 | + ) { |
|
36 | + } |
|
37 | + |
|
38 | + /** |
|
39 | + * returns an array with meta data about all available groups |
|
40 | + * the array is structured as follows: |
|
41 | + * [0] array containing meta data about admin groups |
|
42 | + * [1] array containing meta data about unprivileged groups |
|
43 | + * @param string $groupSearch only effective when instance was created with |
|
44 | + * isAdmin being true |
|
45 | + * @param string $userSearch the pattern users are search for |
|
46 | + */ |
|
47 | + public function get(string $groupSearch = '', string $userSearch = ''): array { |
|
48 | + $key = $groupSearch . '::' . $userSearch; |
|
49 | + if (isset($this->metaData[$key])) { |
|
50 | + return $this->metaData[$key]; |
|
51 | + } |
|
52 | + |
|
53 | + $adminGroups = []; |
|
54 | + $groups = []; |
|
55 | + $sortGroupsIndex = 0; |
|
56 | + $sortGroupsKeys = []; |
|
57 | + $sortAdminGroupsIndex = 0; |
|
58 | + $sortAdminGroupsKeys = []; |
|
59 | + |
|
60 | + foreach ($this->getGroups($groupSearch) as $group) { |
|
61 | + $groupMetaData = $this->generateGroupMetaData($group, $userSearch); |
|
62 | + if (strtolower($group->getGID()) !== 'admin') { |
|
63 | + $this->addEntry( |
|
64 | + $groups, |
|
65 | + $sortGroupsKeys, |
|
66 | + $sortGroupsIndex, |
|
67 | + $groupMetaData); |
|
68 | + } else { |
|
69 | + //admin group is hard coded to 'admin' for now. In future, |
|
70 | + //backends may define admin groups too. Then the if statement |
|
71 | + //has to be adjusted accordingly. |
|
72 | + $this->addEntry( |
|
73 | + $adminGroups, |
|
74 | + $sortAdminGroupsKeys, |
|
75 | + $sortAdminGroupsIndex, |
|
76 | + $groupMetaData); |
|
77 | + } |
|
78 | + } |
|
79 | + |
|
80 | + //whether sorting is necessary is will be checked in sort() |
|
81 | + $this->sort($groups, $sortGroupsKeys); |
|
82 | + $this->sort($adminGroups, $sortAdminGroupsKeys); |
|
83 | + |
|
84 | + $this->metaData[$key] = [$adminGroups, $groups]; |
|
85 | + return $this->metaData[$key]; |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * sets the sort mode, see SORT_* constants for supported modes |
|
90 | + */ |
|
91 | + public function setSorting(int $sortMode): void { |
|
92 | + switch ($sortMode) { |
|
93 | + case self::SORT_USERCOUNT: |
|
94 | + case self::SORT_GROUPNAME: |
|
95 | + $this->sorting = $sortMode; |
|
96 | + break; |
|
97 | + |
|
98 | + default: |
|
99 | + $this->sorting = self::SORT_NONE; |
|
100 | + } |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * adds an group entry to the resulting array |
|
105 | + * @param array $entries the resulting array, by reference |
|
106 | + * @param array $sortKeys the sort key array, by reference |
|
107 | + * @param int $sortIndex the sort key index, by reference |
|
108 | + * @param array $data the group's meta data as returned by generateGroupMetaData() |
|
109 | + */ |
|
110 | + private function addEntry(array &$entries, array &$sortKeys, int &$sortIndex, array $data): void { |
|
111 | + $entries[] = $data; |
|
112 | + if ($this->sorting === self::SORT_USERCOUNT) { |
|
113 | + $sortKeys[$sortIndex] = $data['usercount']; |
|
114 | + $sortIndex++; |
|
115 | + } elseif ($this->sorting === self::SORT_GROUPNAME) { |
|
116 | + $sortKeys[$sortIndex] = $data['name']; |
|
117 | + $sortIndex++; |
|
118 | + } |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * creates an array containing the group meta data |
|
123 | + * @return array with the keys 'id', 'name', 'usercount' and 'disabled' |
|
124 | + */ |
|
125 | + private function generateGroupMetaData(IGroup $group, string $userSearch): array { |
|
126 | + return [ |
|
127 | + 'id' => $group->getGID(), |
|
128 | + 'name' => $group->getDisplayName(), |
|
129 | + 'usercount' => $this->sorting === self::SORT_USERCOUNT ? $group->count($userSearch) : 0, |
|
130 | + 'disabled' => $group->countDisabled(), |
|
131 | + 'canAdd' => $group->canAddUser(), |
|
132 | + 'canRemove' => $group->canRemoveUser(), |
|
133 | + ]; |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * sorts the result array, if applicable |
|
138 | + * @param array $entries the result array, by reference |
|
139 | + * @param array $sortKeys the array containing the sort keys |
|
140 | + */ |
|
141 | + private function sort(array &$entries, array $sortKeys): void { |
|
142 | + if ($this->sorting === self::SORT_USERCOUNT) { |
|
143 | + array_multisort($sortKeys, SORT_DESC, $entries); |
|
144 | + } elseif ($this->sorting === self::SORT_GROUPNAME) { |
|
145 | + array_multisort($sortKeys, SORT_ASC, $entries); |
|
146 | + } |
|
147 | + } |
|
148 | + |
|
149 | + /** |
|
150 | + * returns the available groups |
|
151 | + * @return IGroup[] |
|
152 | + */ |
|
153 | + public function getGroups(string $search = ''): array { |
|
154 | + if ($this->isAdmin || $this->isDelegatedAdmin) { |
|
155 | + return $this->groupManager->search($search); |
|
156 | + } else { |
|
157 | + $userObject = $this->userSession->getUser(); |
|
158 | + if ($userObject !== null && $this->groupManager instanceof GroupManager) { |
|
159 | + $groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($userObject); |
|
160 | + } else { |
|
161 | + $groups = []; |
|
162 | + } |
|
163 | + |
|
164 | + return $groups; |
|
165 | + } |
|
166 | + } |
|
167 | 167 | } |