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