Passed
Push — master ( 645e3e...741e5e )
by Roeland
47:21 queued 28:58
created
apps/user_ldap/lib/Mapping/AbstractMapping.php 1 patch
Indentation   +331 added lines, -331 removed lines patch added patch discarded remove patch
@@ -35,363 +35,363 @@
 block discarded – undo
35 35
  * @package OCA\User_LDAP\Mapping
36 36
  */
37 37
 abstract class AbstractMapping {
38
-	/**
39
-	 * @var \OCP\IDBConnection $dbc
40
-	 */
41
-	protected $dbc;
42
-
43
-	/**
44
-	 * returns the DB table name which holds the mappings
45
-	 *
46
-	 * @return string
47
-	 */
48
-	abstract protected function getTableName(bool $includePrefix = true);
49
-
50
-	/**
51
-	 * @param \OCP\IDBConnection $dbc
52
-	 */
53
-	public function __construct(\OCP\IDBConnection $dbc) {
54
-		$this->dbc = $dbc;
55
-	}
56
-
57
-	/** @var array caches Names (value) by DN (key) */
58
-	protected $cache = [];
59
-
60
-	/**
61
-	 * checks whether a provided string represents an existing table col
62
-	 *
63
-	 * @param string $col
64
-	 * @return bool
65
-	 */
66
-	public function isColNameValid($col) {
67
-		switch ($col) {
68
-			case 'ldap_dn':
69
-			case 'owncloud_name':
70
-			case 'directory_uuid':
71
-				return true;
72
-			default:
73
-				return false;
74
-		}
75
-	}
76
-
77
-	/**
78
-	 * Gets the value of one column based on a provided value of another column
79
-	 *
80
-	 * @param string $fetchCol
81
-	 * @param string $compareCol
82
-	 * @param string $search
83
-	 * @return string|false
84
-	 * @throws \Exception
85
-	 */
86
-	protected function getXbyY($fetchCol, $compareCol, $search) {
87
-		if (!$this->isColNameValid($fetchCol)) {
88
-			//this is used internally only, but we don't want to risk
89
-			//having SQL injection at all.
90
-			throw new \Exception('Invalid Column Name');
91
-		}
92
-		$query = $this->dbc->prepare('
38
+    /**
39
+     * @var \OCP\IDBConnection $dbc
40
+     */
41
+    protected $dbc;
42
+
43
+    /**
44
+     * returns the DB table name which holds the mappings
45
+     *
46
+     * @return string
47
+     */
48
+    abstract protected function getTableName(bool $includePrefix = true);
49
+
50
+    /**
51
+     * @param \OCP\IDBConnection $dbc
52
+     */
53
+    public function __construct(\OCP\IDBConnection $dbc) {
54
+        $this->dbc = $dbc;
55
+    }
56
+
57
+    /** @var array caches Names (value) by DN (key) */
58
+    protected $cache = [];
59
+
60
+    /**
61
+     * checks whether a provided string represents an existing table col
62
+     *
63
+     * @param string $col
64
+     * @return bool
65
+     */
66
+    public function isColNameValid($col) {
67
+        switch ($col) {
68
+            case 'ldap_dn':
69
+            case 'owncloud_name':
70
+            case 'directory_uuid':
71
+                return true;
72
+            default:
73
+                return false;
74
+        }
75
+    }
76
+
77
+    /**
78
+     * Gets the value of one column based on a provided value of another column
79
+     *
80
+     * @param string $fetchCol
81
+     * @param string $compareCol
82
+     * @param string $search
83
+     * @return string|false
84
+     * @throws \Exception
85
+     */
86
+    protected function getXbyY($fetchCol, $compareCol, $search) {
87
+        if (!$this->isColNameValid($fetchCol)) {
88
+            //this is used internally only, but we don't want to risk
89
+            //having SQL injection at all.
90
+            throw new \Exception('Invalid Column Name');
91
+        }
92
+        $query = $this->dbc->prepare('
93 93
 			SELECT `' . $fetchCol . '`
94 94
 			FROM `' . $this->getTableName() . '`
95 95
 			WHERE `' . $compareCol . '` = ?
96 96
 		');
97 97
 
98
-		$res = $query->execute([$search]);
99
-		if ($res !== false) {
100
-			return $query->fetchColumn();
101
-		}
102
-
103
-		return false;
104
-	}
105
-
106
-	/**
107
-	 * Performs a DELETE or UPDATE query to the database.
108
-	 *
109
-	 * @param \Doctrine\DBAL\Driver\Statement $query
110
-	 * @param array $parameters
111
-	 * @return bool true if at least one row was modified, false otherwise
112
-	 */
113
-	protected function modify($query, $parameters) {
114
-		$result = $query->execute($parameters);
115
-		return ($result === true && $query->rowCount() > 0);
116
-	}
117
-
118
-	/**
119
-	 * Gets the LDAP DN based on the provided name.
120
-	 * Replaces Access::ocname2dn
121
-	 *
122
-	 * @param string $name
123
-	 * @return string|false
124
-	 */
125
-	public function getDNByName($name) {
126
-		$dn = array_search($name, $this->cache);
127
-		if ($dn === false && ($dn = $this->getXbyY('ldap_dn', 'owncloud_name', $name)) !== false) {
128
-			$this->cache[$dn] = $name;
129
-		}
130
-		return $dn;
131
-	}
132
-
133
-	/**
134
-	 * Updates the DN based on the given UUID
135
-	 *
136
-	 * @param string $fdn
137
-	 * @param string $uuid
138
-	 * @return bool
139
-	 */
140
-	public function setDNbyUUID($fdn, $uuid) {
141
-		$oldDn = $this->getDnByUUID($uuid);
142
-		$query = $this->dbc->prepare('
98
+        $res = $query->execute([$search]);
99
+        if ($res !== false) {
100
+            return $query->fetchColumn();
101
+        }
102
+
103
+        return false;
104
+    }
105
+
106
+    /**
107
+     * Performs a DELETE or UPDATE query to the database.
108
+     *
109
+     * @param \Doctrine\DBAL\Driver\Statement $query
110
+     * @param array $parameters
111
+     * @return bool true if at least one row was modified, false otherwise
112
+     */
113
+    protected function modify($query, $parameters) {
114
+        $result = $query->execute($parameters);
115
+        return ($result === true && $query->rowCount() > 0);
116
+    }
117
+
118
+    /**
119
+     * Gets the LDAP DN based on the provided name.
120
+     * Replaces Access::ocname2dn
121
+     *
122
+     * @param string $name
123
+     * @return string|false
124
+     */
125
+    public function getDNByName($name) {
126
+        $dn = array_search($name, $this->cache);
127
+        if ($dn === false && ($dn = $this->getXbyY('ldap_dn', 'owncloud_name', $name)) !== false) {
128
+            $this->cache[$dn] = $name;
129
+        }
130
+        return $dn;
131
+    }
132
+
133
+    /**
134
+     * Updates the DN based on the given UUID
135
+     *
136
+     * @param string $fdn
137
+     * @param string $uuid
138
+     * @return bool
139
+     */
140
+    public function setDNbyUUID($fdn, $uuid) {
141
+        $oldDn = $this->getDnByUUID($uuid);
142
+        $query = $this->dbc->prepare('
143 143
 			UPDATE `' . $this->getTableName() . '`
144 144
 			SET `ldap_dn` = ?
145 145
 			WHERE `directory_uuid` = ?
146 146
 		');
147 147
 
148
-		$r = $this->modify($query, [$fdn, $uuid]);
149
-
150
-		if ($r && is_string($oldDn) && isset($this->cache[$oldDn])) {
151
-			$this->cache[$fdn] = $this->cache[$oldDn];
152
-			unset($this->cache[$oldDn]);
153
-		}
154
-
155
-		return $r;
156
-	}
157
-
158
-	/**
159
-	 * Updates the UUID based on the given DN
160
-	 *
161
-	 * required by Migration/UUIDFix
162
-	 *
163
-	 * @param $uuid
164
-	 * @param $fdn
165
-	 * @return bool
166
-	 */
167
-	public function setUUIDbyDN($uuid, $fdn) {
168
-		$query = $this->dbc->prepare('
148
+        $r = $this->modify($query, [$fdn, $uuid]);
149
+
150
+        if ($r && is_string($oldDn) && isset($this->cache[$oldDn])) {
151
+            $this->cache[$fdn] = $this->cache[$oldDn];
152
+            unset($this->cache[$oldDn]);
153
+        }
154
+
155
+        return $r;
156
+    }
157
+
158
+    /**
159
+     * Updates the UUID based on the given DN
160
+     *
161
+     * required by Migration/UUIDFix
162
+     *
163
+     * @param $uuid
164
+     * @param $fdn
165
+     * @return bool
166
+     */
167
+    public function setUUIDbyDN($uuid, $fdn) {
168
+        $query = $this->dbc->prepare('
169 169
 			UPDATE `' . $this->getTableName() . '`
170 170
 			SET `directory_uuid` = ?
171 171
 			WHERE `ldap_dn` = ?
172 172
 		');
173 173
 
174
-		unset($this->cache[$fdn]);
175
-
176
-		return $this->modify($query, [$uuid, $fdn]);
177
-	}
178
-
179
-	/**
180
-	 * Gets the name based on the provided LDAP DN.
181
-	 *
182
-	 * @param string $fdn
183
-	 * @return string|false
184
-	 */
185
-	public function getNameByDN($fdn) {
186
-		if (!isset($this->cache[$fdn])) {
187
-			$this->cache[$fdn] = $this->getXbyY('owncloud_name', 'ldap_dn', $fdn);
188
-		}
189
-		return $this->cache[$fdn];
190
-	}
191
-
192
-	public function getListOfIdsByDn(array $fdns): array {
193
-		$fdnsSlice = count($fdns) > 1000 ? array_slice($fdns, 0, 1000) : $fdns;
194
-		$qb = $this->dbc->getQueryBuilder();
195
-		$qb->select('owncloud_name', 'ldap_dn')
196
-			->from($this->getTableName(false))
197
-			->where($qb->expr()->in('ldap_dn', $qb->createNamedParameter($fdnsSlice, QueryBuilder::PARAM_STR_ARRAY)));
198
-
199
-		$slice = 1;
200
-		while (isset($fdnsSlice[999])) {
201
-			// Oracle does not allow more than 1000 values in the IN list,
202
-			// but allows slicing
203
-			$fdnsSlice = array_slice($fdns, 1000 * $slice, 1000);
204
-			if (!empty($fdnsSlice)) {
205
-				$qb->orWhere($qb->expr()->in('ldap_dn', $qb->createNamedParameter($fdnsSlice, QueryBuilder::PARAM_STR_ARRAY)));
206
-			}
207
-			$slice++;
208
-		}
209
-
210
-		$stmt = $qb->execute();
211
-		$results = [];
212
-		while ($entry = $stmt->fetch(\Doctrine\DBAL\FetchMode::ASSOCIATIVE)) {
213
-			$results[$entry['ldap_dn']] = $entry['owncloud_name'];
214
-			$this->cache[$entry['ldap_dn']] = $entry['owncloud_name'];
215
-		}
216
-		$stmt->closeCursor();
217
-
218
-		return $results;
219
-	}
220
-
221
-	/**
222
-	 * Searches mapped names by the giving string in the name column
223
-	 *
224
-	 * @param string $search
225
-	 * @param string $prefixMatch
226
-	 * @param string $postfixMatch
227
-	 * @return string[]
228
-	 */
229
-	public function getNamesBySearch($search, $prefixMatch = "", $postfixMatch = "") {
230
-		$query = $this->dbc->prepare('
174
+        unset($this->cache[$fdn]);
175
+
176
+        return $this->modify($query, [$uuid, $fdn]);
177
+    }
178
+
179
+    /**
180
+     * Gets the name based on the provided LDAP DN.
181
+     *
182
+     * @param string $fdn
183
+     * @return string|false
184
+     */
185
+    public function getNameByDN($fdn) {
186
+        if (!isset($this->cache[$fdn])) {
187
+            $this->cache[$fdn] = $this->getXbyY('owncloud_name', 'ldap_dn', $fdn);
188
+        }
189
+        return $this->cache[$fdn];
190
+    }
191
+
192
+    public function getListOfIdsByDn(array $fdns): array {
193
+        $fdnsSlice = count($fdns) > 1000 ? array_slice($fdns, 0, 1000) : $fdns;
194
+        $qb = $this->dbc->getQueryBuilder();
195
+        $qb->select('owncloud_name', 'ldap_dn')
196
+            ->from($this->getTableName(false))
197
+            ->where($qb->expr()->in('ldap_dn', $qb->createNamedParameter($fdnsSlice, QueryBuilder::PARAM_STR_ARRAY)));
198
+
199
+        $slice = 1;
200
+        while (isset($fdnsSlice[999])) {
201
+            // Oracle does not allow more than 1000 values in the IN list,
202
+            // but allows slicing
203
+            $fdnsSlice = array_slice($fdns, 1000 * $slice, 1000);
204
+            if (!empty($fdnsSlice)) {
205
+                $qb->orWhere($qb->expr()->in('ldap_dn', $qb->createNamedParameter($fdnsSlice, QueryBuilder::PARAM_STR_ARRAY)));
206
+            }
207
+            $slice++;
208
+        }
209
+
210
+        $stmt = $qb->execute();
211
+        $results = [];
212
+        while ($entry = $stmt->fetch(\Doctrine\DBAL\FetchMode::ASSOCIATIVE)) {
213
+            $results[$entry['ldap_dn']] = $entry['owncloud_name'];
214
+            $this->cache[$entry['ldap_dn']] = $entry['owncloud_name'];
215
+        }
216
+        $stmt->closeCursor();
217
+
218
+        return $results;
219
+    }
220
+
221
+    /**
222
+     * Searches mapped names by the giving string in the name column
223
+     *
224
+     * @param string $search
225
+     * @param string $prefixMatch
226
+     * @param string $postfixMatch
227
+     * @return string[]
228
+     */
229
+    public function getNamesBySearch($search, $prefixMatch = "", $postfixMatch = "") {
230
+        $query = $this->dbc->prepare('
231 231
 			SELECT `owncloud_name`
232 232
 			FROM `' . $this->getTableName() . '`
233 233
 			WHERE `owncloud_name` LIKE ?
234 234
 		');
235 235
 
236
-		$res = $query->execute([$prefixMatch . $this->dbc->escapeLikeParameter($search) . $postfixMatch]);
237
-		$names = [];
238
-		if ($res !== false) {
239
-			while ($row = $query->fetch()) {
240
-				$names[] = $row['owncloud_name'];
241
-			}
242
-		}
243
-		return $names;
244
-	}
245
-
246
-	/**
247
-	 * Gets the name based on the provided LDAP UUID.
248
-	 *
249
-	 * @param string $uuid
250
-	 * @return string|false
251
-	 */
252
-	public function getNameByUUID($uuid) {
253
-		return $this->getXbyY('owncloud_name', 'directory_uuid', $uuid);
254
-	}
255
-
256
-	public function getDnByUUID($uuid) {
257
-		return $this->getXbyY('ldap_dn', 'directory_uuid', $uuid);
258
-	}
259
-
260
-	/**
261
-	 * Gets the UUID based on the provided LDAP DN
262
-	 *
263
-	 * @param string $dn
264
-	 * @return false|string
265
-	 * @throws \Exception
266
-	 */
267
-	public function getUUIDByDN($dn) {
268
-		return $this->getXbyY('directory_uuid', 'ldap_dn', $dn);
269
-	}
270
-
271
-	/**
272
-	 * gets a piece of the mapping list
273
-	 *
274
-	 * @param int $offset
275
-	 * @param int $limit
276
-	 * @return array
277
-	 */
278
-	public function getList($offset = null, $limit = null) {
279
-		$query = $this->dbc->prepare('
236
+        $res = $query->execute([$prefixMatch . $this->dbc->escapeLikeParameter($search) . $postfixMatch]);
237
+        $names = [];
238
+        if ($res !== false) {
239
+            while ($row = $query->fetch()) {
240
+                $names[] = $row['owncloud_name'];
241
+            }
242
+        }
243
+        return $names;
244
+    }
245
+
246
+    /**
247
+     * Gets the name based on the provided LDAP UUID.
248
+     *
249
+     * @param string $uuid
250
+     * @return string|false
251
+     */
252
+    public function getNameByUUID($uuid) {
253
+        return $this->getXbyY('owncloud_name', 'directory_uuid', $uuid);
254
+    }
255
+
256
+    public function getDnByUUID($uuid) {
257
+        return $this->getXbyY('ldap_dn', 'directory_uuid', $uuid);
258
+    }
259
+
260
+    /**
261
+     * Gets the UUID based on the provided LDAP DN
262
+     *
263
+     * @param string $dn
264
+     * @return false|string
265
+     * @throws \Exception
266
+     */
267
+    public function getUUIDByDN($dn) {
268
+        return $this->getXbyY('directory_uuid', 'ldap_dn', $dn);
269
+    }
270
+
271
+    /**
272
+     * gets a piece of the mapping list
273
+     *
274
+     * @param int $offset
275
+     * @param int $limit
276
+     * @return array
277
+     */
278
+    public function getList($offset = null, $limit = null) {
279
+        $query = $this->dbc->prepare('
280 280
 			SELECT
281 281
 				`ldap_dn` AS `dn`,
282 282
 				`owncloud_name` AS `name`,
283 283
 				`directory_uuid` AS `uuid`
284 284
 			FROM `' . $this->getTableName() . '`',
285
-			$limit,
286
-			$offset
287
-		);
288
-
289
-		$query->execute();
290
-		return $query->fetchAll();
291
-	}
292
-
293
-	/**
294
-	 * attempts to map the given entry
295
-	 *
296
-	 * @param string $fdn fully distinguished name (from LDAP)
297
-	 * @param string $name
298
-	 * @param string $uuid a unique identifier as used in LDAP
299
-	 * @return bool
300
-	 */
301
-	public function map($fdn, $name, $uuid) {
302
-		if (mb_strlen($fdn) > 255) {
303
-			\OC::$server->getLogger()->error(
304
-				'Cannot map, because the DN exceeds 255 characters: {dn}',
305
-				[
306
-					'app' => 'user_ldap',
307
-					'dn' => $fdn,
308
-				]
309
-			);
310
-			return false;
311
-		}
312
-
313
-		$row = [
314
-			'ldap_dn' => $fdn,
315
-			'owncloud_name' => $name,
316
-			'directory_uuid' => $uuid
317
-		];
318
-
319
-		try {
320
-			$result = $this->dbc->insertIfNotExist($this->getTableName(), $row);
321
-			if ((bool)$result === true) {
322
-				$this->cache[$fdn] = $name;
323
-			}
324
-			// insertIfNotExist returns values as int
325
-			return (bool)$result;
326
-		} catch (\Exception $e) {
327
-			return false;
328
-		}
329
-	}
330
-
331
-	/**
332
-	 * removes a mapping based on the owncloud_name of the entry
333
-	 *
334
-	 * @param string $name
335
-	 * @return bool
336
-	 */
337
-	public function unmap($name) {
338
-		$query = $this->dbc->prepare('
285
+            $limit,
286
+            $offset
287
+        );
288
+
289
+        $query->execute();
290
+        return $query->fetchAll();
291
+    }
292
+
293
+    /**
294
+     * attempts to map the given entry
295
+     *
296
+     * @param string $fdn fully distinguished name (from LDAP)
297
+     * @param string $name
298
+     * @param string $uuid a unique identifier as used in LDAP
299
+     * @return bool
300
+     */
301
+    public function map($fdn, $name, $uuid) {
302
+        if (mb_strlen($fdn) > 255) {
303
+            \OC::$server->getLogger()->error(
304
+                'Cannot map, because the DN exceeds 255 characters: {dn}',
305
+                [
306
+                    'app' => 'user_ldap',
307
+                    'dn' => $fdn,
308
+                ]
309
+            );
310
+            return false;
311
+        }
312
+
313
+        $row = [
314
+            'ldap_dn' => $fdn,
315
+            'owncloud_name' => $name,
316
+            'directory_uuid' => $uuid
317
+        ];
318
+
319
+        try {
320
+            $result = $this->dbc->insertIfNotExist($this->getTableName(), $row);
321
+            if ((bool)$result === true) {
322
+                $this->cache[$fdn] = $name;
323
+            }
324
+            // insertIfNotExist returns values as int
325
+            return (bool)$result;
326
+        } catch (\Exception $e) {
327
+            return false;
328
+        }
329
+    }
330
+
331
+    /**
332
+     * removes a mapping based on the owncloud_name of the entry
333
+     *
334
+     * @param string $name
335
+     * @return bool
336
+     */
337
+    public function unmap($name) {
338
+        $query = $this->dbc->prepare('
339 339
 			DELETE FROM `' . $this->getTableName() . '`
340 340
 			WHERE `owncloud_name` = ?');
341 341
 
342
-		return $this->modify($query, [$name]);
343
-	}
344
-
345
-	/**
346
-	 * Truncate's the mapping table
347
-	 *
348
-	 * @return bool
349
-	 */
350
-	public function clear() {
351
-		$sql = $this->dbc
352
-			->getDatabasePlatform()
353
-			->getTruncateTableSQL('`' . $this->getTableName() . '`');
354
-		return $this->dbc->prepare($sql)->execute();
355
-	}
356
-
357
-	/**
358
-	 * clears the mapping table one by one and executing a callback with
359
-	 * each row's id (=owncloud_name col)
360
-	 *
361
-	 * @param callable $preCallback
362
-	 * @param callable $postCallback
363
-	 * @return bool true on success, false when at least one row was not
364
-	 * deleted
365
-	 */
366
-	public function clearCb(callable $preCallback, callable $postCallback): bool {
367
-		$picker = $this->dbc->getQueryBuilder();
368
-		$picker->select('owncloud_name')
369
-			->from($this->getTableName());
370
-		$cursor = $picker->execute();
371
-		$result = true;
372
-		while ($id = $cursor->fetchColumn(0)) {
373
-			$preCallback($id);
374
-			if ($isUnmapped = $this->unmap($id)) {
375
-				$postCallback($id);
376
-			}
377
-			$result &= $isUnmapped;
378
-		}
379
-		$cursor->closeCursor();
380
-		return $result;
381
-	}
382
-
383
-	/**
384
-	 * returns the number of entries in the mappings table
385
-	 *
386
-	 * @return int
387
-	 */
388
-	public function count() {
389
-		$qb = $this->dbc->getQueryBuilder();
390
-		$query = $qb->select($qb->func()->count('ldap_dn'))
391
-			->from($this->getTableName());
392
-		$res = $query->execute();
393
-		$count = $res->fetchColumn();
394
-		$res->closeCursor();
395
-		return (int)$count;
396
-	}
342
+        return $this->modify($query, [$name]);
343
+    }
344
+
345
+    /**
346
+     * Truncate's the mapping table
347
+     *
348
+     * @return bool
349
+     */
350
+    public function clear() {
351
+        $sql = $this->dbc
352
+            ->getDatabasePlatform()
353
+            ->getTruncateTableSQL('`' . $this->getTableName() . '`');
354
+        return $this->dbc->prepare($sql)->execute();
355
+    }
356
+
357
+    /**
358
+     * clears the mapping table one by one and executing a callback with
359
+     * each row's id (=owncloud_name col)
360
+     *
361
+     * @param callable $preCallback
362
+     * @param callable $postCallback
363
+     * @return bool true on success, false when at least one row was not
364
+     * deleted
365
+     */
366
+    public function clearCb(callable $preCallback, callable $postCallback): bool {
367
+        $picker = $this->dbc->getQueryBuilder();
368
+        $picker->select('owncloud_name')
369
+            ->from($this->getTableName());
370
+        $cursor = $picker->execute();
371
+        $result = true;
372
+        while ($id = $cursor->fetchColumn(0)) {
373
+            $preCallback($id);
374
+            if ($isUnmapped = $this->unmap($id)) {
375
+                $postCallback($id);
376
+            }
377
+            $result &= $isUnmapped;
378
+        }
379
+        $cursor->closeCursor();
380
+        return $result;
381
+    }
382
+
383
+    /**
384
+     * returns the number of entries in the mappings table
385
+     *
386
+     * @return int
387
+     */
388
+    public function count() {
389
+        $qb = $this->dbc->getQueryBuilder();
390
+        $query = $qb->select($qb->func()->count('ldap_dn'))
391
+            ->from($this->getTableName());
392
+        $res = $query->execute();
393
+        $count = $res->fetchColumn();
394
+        $res->closeCursor();
395
+        return (int)$count;
396
+    }
397 397
 }
Please login to merge, or discard this patch.