Completed
Push — stable8.2 ( e9036a...6707df )
by
unknown
59:39
created

AbstractMapping::getNameByDN()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * @author Arthur Schiwon <[email protected]>
4
 * @author Morris Jobke <[email protected]>
5
 *
6
 * @copyright Copyright (c) 2015, ownCloud, Inc.
7
 * @license AGPL-3.0
8
 *
9
 * This code is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License, version 3,
11
 * as published by the Free Software Foundation.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License, version 3,
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
20
 *
21
 */
22
23
namespace OCA\User_LDAP\Mapping;
24
25
/**
26
* Class AbstractMapping
27
* @package OCA\User_LDAP\Mapping
28
*/
29
abstract class AbstractMapping {
30
	/**
31
	 * @var \OCP\IDBConnection $dbc
32
	 */
33
	protected $dbc;
34
35
	/**
36
	 * returns the DB table name which holds the mappings
37
	 * @return string
38
	 */
39
	abstract protected function getTableName();
40
41
	/**
42
	 * @param \OCP\IDBConnection $dbc
43
	 */
44 22
	public function __construct(\OCP\IDBConnection $dbc) {
45 22
		$this->dbc = $dbc;
46 22
	}
47
48
	/**
49
	 * checks whether a provided string represents an existing table col
50
	 * @param string $col
51
	 * @return bool
52
	 */
53 8
	public function isColNameValid($col) {
54
		switch($col) {
55 8
			case 'ldap_dn':
56 8
			case 'owncloud_name':
57 8
			case 'directory_uuid':
58 8
				return true;
59 2
			default:
60 2
				return false;
61 2
		}
62
	}
63
64
	/**
65
	 * Gets the value of one column based on a provided value of another column
66
	 * @param string $fetchCol
67
	 * @param string $compareCol
68
	 * @param string $search
69
	 * @throws \Exception
70
	 * @return string|false
71
	 */
72 6
	protected function getXbyY($fetchCol, $compareCol, $search) {
73 6
		if(!$this->isColNameValid($fetchCol)) {
74
			//this is used internally only, but we don't want to risk
75
			//having SQL injection at all.
76
			throw new \Exception('Invalid Column Name');
77
		}
78 6
		$query = $this->dbc->prepare('
79 6
			SELECT `' . $fetchCol . '`
80 6
			FROM `'. $this->getTableName() .'`
81 6
			WHERE `' . $compareCol . '` = ?
82 6
		');
83
84 6
		$res = $query->execute(array($search));
85 6
		if($res !== false) {
86 6
			return $query->fetchColumn();
87
		}
88
89
		return false;
90
	}
91
92
	/**
93
	 * Performs a DELETE or UPDATE query to the database.
94
	 * @param \Doctrine\DBAL\Driver\Statement $query
95
	 * @param array $parameters
96
	 * @return bool true if at least one row was modified, false otherwise
97
	 */
98 4
	protected function modify($query, $parameters) {
99 4
		$result = $query->execute($parameters);
100 4
		return ($result === true && $query->rowCount() > 0);
101
	}
102
103
	/**
104
	 * Gets the LDAP DN based on the provided name.
105
	 * Replaces Access::ocname2dn
106
	 * @param string $name
107
	 * @return string|false
108
	 */
109 4
	public function getDNByName($name) {
110 4
		return $this->getXbyY('ldap_dn', 'owncloud_name', $name);
111
	}
112
113
	/**
114
	 * Updates the DN based on the given UUID
115
	 * @param string $fdn
116
	 * @param string $uuid
117
	 * @return bool
118
	 */
119 2 View Code Duplication
	public function setDNbyUUID($fdn, $uuid) {
120 2
		$query = $this->dbc->prepare('
121 2
			UPDATE `' . $this->getTableName() . '`
122
			SET `ldap_dn` = ?
123
			WHERE `directory_uuid` = ?
124 2
		');
125
126 2
		return $this->modify($query, array($fdn, $uuid));
127
	}
128
129
	/**
130
	 * Gets the name based on the provided LDAP DN.
131
	 * @param string $fdn
132
	 * @return string|false
133
	 */
134 4
	public function getNameByDN($fdn) {
135 4
		return $this->getXbyY('owncloud_name', 'ldap_dn', $fdn);
136
	}
137
138
	/**
139
	 * Searches mapped names by the giving string in the name column
140
	 * @param string $search
141
	 * @return string[]
142
	 */
143 2
	public function getNamesBySearch($search) {
144 2
		$query = $this->dbc->prepare('
145
			SELECT `owncloud_name`
146 2
			FROM `'. $this->getTableName() .'`
147
			WHERE `owncloud_name` LIKE ?
148 2
		');
149
150 2
		$res = $query->execute(array($search));
151 2
		$names = array();
152 2
		if($res !== false) {
153 2
			while($row = $query->fetch()) {
154 2
				$names[] = $row['owncloud_name'];
155 2
			}
156 2
		}
157 2
		return $names;
158
	}
159
160
	/**
161
	 * Gets the name based on the provided LDAP UUID.
162
	 * @param string $uuid
163
	 * @return string|false
164
	 */
165 4
	public function getNameByUUID($uuid) {
166 4
		return $this->getXbyY('owncloud_name', 'directory_uuid', $uuid);
167
	}
168
169
	/**
170
	 * Gets the UUID based on the provided LDAP DN
171
	 * @param string $dn
172
	 * @return false|string
173
	 * @throws \Exception
174
	 */
175
	public function getUUIDByDN($dn) {
176
		return $this->getXbyY('directory_uuid', 'ldap_dn', $dn);
177
	}
178
179
	/**
180
	 * gets a piece of the mapping list
181
	 * @param int $offset
182
	 * @param int $limit
183
	 * @return array
184
	 */
185 2
	public function getList($offset = null, $limit = null) {
186 2
		$query = $this->dbc->prepare('
187
			SELECT
188
				`ldap_dn` AS `dn`,
189
				`owncloud_name` AS `name`,
190
				`directory_uuid` AS `uuid`
191 2
			FROM `' . $this->getTableName() . '`',
192 2
			$limit,
193
			$offset
194 2
		);
195
196 2
		$query->execute();
197 2
		return $query->fetchAll();
198
	}
199
200
	/**
201
	 * attempts to map the given entry
202
	 * @param string $fdn fully distinguished name (from LDAP)
203
	 * @param string $name
204
	 * @param string $uuid a unique identifier as used in LDAP
205
	 * @return bool
206
	 */
207 14
	public function map($fdn, $name, $uuid) {
208
		$row = array(
209 14
			'ldap_dn'        => $fdn,
210 14
			'owncloud_name'  => $name,
211
			'directory_uuid' => $uuid
212 14
		);
213
214
		try {
215 14
			$result = $this->dbc->insertIfNotExist($this->getTableName(), $row);
216
			// insertIfNotExist returns values as int
217 14
			return (bool)$result;
218 2
		} catch (\Exception $e) {
219 2
			return false;
220
		}
221
	}
222
223
	/**
224
	 * removes a mapping based on the owncloud_name of the entry
225
	 * @param string $name
226
	 * @return bool
227
	 */
228 2 View Code Duplication
	public function unmap($name) {
229 2
		$query = $this->dbc->prepare('
230 2
			DELETE FROM `'. $this->getTableName() .'`
231 2
			WHERE `owncloud_name` = ?');
232
233 2
		return $this->modify($query, array($name));
234
	}
235
236
	/**
237
	 * Truncate's the mapping table
238
	 * @return bool
239
	 */
240 14
	public function clear() {
241 14
		$sql = $this->dbc
242 14
			->getDatabasePlatform()
243 14
			->getTruncateTableSQL('`' . $this->getTableName() . '`');
244 14
		return $this->dbc->prepare($sql)->execute();
245
	}
246
}
247