AbstractMapping.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Aaron Wood <aaronjwood@gmail.com>
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OCA\User_LDAP\Mapping;
  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. * returns the DB table name which holds the mappings
  36. * @return string
  37. */
  38. abstract protected function getTableName();
  39. /**
  40. * @param \OCP\IDBConnection $dbc
  41. */
  42. public function __construct(\OCP\IDBConnection $dbc) {
  43. $this->dbc = $dbc;
  44. }
  45. /**
  46. * checks whether a provided string represents an existing table col
  47. * @param string $col
  48. * @return bool
  49. */
  50. public function isColNameValid($col) {
  51. switch($col) {
  52. case 'ldap_dn':
  53. case 'owncloud_name':
  54. case 'directory_uuid':
  55. return true;
  56. default:
  57. return false;
  58. }
  59. }
  60. /**
  61. * Gets the value of one column based on a provided value of another column
  62. * @param string $fetchCol
  63. * @param string $compareCol
  64. * @param string $search
  65. * @throws \Exception
  66. * @return string|false
  67. */
  68. protected function getXbyY($fetchCol, $compareCol, $search) {
  69. if(!$this->isColNameValid($fetchCol)) {
  70. //this is used internally only, but we don't want to risk
  71. //having SQL injection at all.
  72. throw new \Exception('Invalid Column Name');
  73. }
  74. $query = $this->dbc->prepare('
  75. SELECT `' . $fetchCol . '`
  76. FROM `'. $this->getTableName() .'`
  77. WHERE `' . $compareCol . '` = ?
  78. ');
  79. $res = $query->execute(array($search));
  80. if($res !== false) {
  81. return $query->fetchColumn();
  82. }
  83. return false;
  84. }
  85. /**
  86. * Performs a DELETE or UPDATE query to the database.
  87. * @param \Doctrine\DBAL\Driver\Statement $query
  88. * @param array $parameters
  89. * @return bool true if at least one row was modified, false otherwise
  90. */
  91. protected function modify($query, $parameters) {
  92. $result = $query->execute($parameters);
  93. return ($result === true && $query->rowCount() > 0);
  94. }
  95. /**
  96. * Gets the LDAP DN based on the provided name.
  97. * Replaces Access::ocname2dn
  98. * @param string $name
  99. * @return string|false
  100. */
  101. public function getDNByName($name) {
  102. return $this->getXbyY('ldap_dn', 'owncloud_name', $name);
  103. }
  104. /**
  105. * Updates the DN based on the given UUID
  106. * @param string $fdn
  107. * @param string $uuid
  108. * @return bool
  109. */
  110. public function setDNbyUUID($fdn, $uuid) {
  111. $query = $this->dbc->prepare('
  112. UPDATE `' . $this->getTableName() . '`
  113. SET `ldap_dn` = ?
  114. WHERE `directory_uuid` = ?
  115. ');
  116. return $this->modify($query, array($fdn, $uuid));
  117. }
  118. /**
  119. * Updates the UUID based on the given DN
  120. *
  121. * required by Migration/UUIDFix
  122. *
  123. * @param $uuid
  124. * @param $fdn
  125. * @return bool
  126. */
  127. public function setUUIDbyDN($uuid, $fdn) {
  128. $query = $this->dbc->prepare('
  129. UPDATE `' . $this->getTableName() . '`
  130. SET `directory_uuid` = ?
  131. WHERE `ldap_dn` = ?
  132. ');
  133. return $this->modify($query, [$uuid, $fdn]);
  134. }
  135. /**
  136. * Gets the name based on the provided LDAP DN.
  137. * @param string $fdn
  138. * @return string|false
  139. */
  140. public function getNameByDN($fdn) {
  141. return $this->getXbyY('owncloud_name', 'ldap_dn', $fdn);
  142. }
  143. /**
  144. * Searches mapped names by the giving string in the name column
  145. * @param string $search
  146. * @param string $prefixMatch
  147. * @param string $postfixMatch
  148. * @return string[]
  149. */
  150. public function getNamesBySearch($search, $prefixMatch = "", $postfixMatch = "") {
  151. $query = $this->dbc->prepare('
  152. SELECT `owncloud_name`
  153. FROM `'. $this->getTableName() .'`
  154. WHERE `owncloud_name` LIKE ?
  155. ');
  156. $res = $query->execute(array($prefixMatch.$this->dbc->escapeLikeParameter($search).$postfixMatch));
  157. $names = array();
  158. if($res !== false) {
  159. while($row = $query->fetch()) {
  160. $names[] = $row['owncloud_name'];
  161. }
  162. }
  163. return $names;
  164. }
  165. /**
  166. * Gets the name based on the provided LDAP UUID.
  167. * @param string $uuid
  168. * @return string|false
  169. */
  170. public function getNameByUUID($uuid) {
  171. return $this->getXbyY('owncloud_name', 'directory_uuid', $uuid);
  172. }
  173. /**
  174. * Gets the UUID based on the provided LDAP DN
  175. * @param string $dn
  176. * @return false|string
  177. * @throws \Exception
  178. */
  179. public function getUUIDByDN($dn) {
  180. return $this->getXbyY('directory_uuid', 'ldap_dn', $dn);
  181. }
  182. /**
  183. * gets a piece of the mapping list
  184. * @param int $offset
  185. * @param int $limit
  186. * @return array
  187. */
  188. public function getList($offset = null, $limit = null) {
  189. $query = $this->dbc->prepare('
  190. SELECT
  191. `ldap_dn` AS `dn`,
  192. `owncloud_name` AS `name`,
  193. `directory_uuid` AS `uuid`
  194. FROM `' . $this->getTableName() . '`',
  195. $limit,
  196. $offset
  197. );
  198. $query->execute();
  199. return $query->fetchAll();
  200. }
  201. /**
  202. * attempts to map the given entry
  203. * @param string $fdn fully distinguished name (from LDAP)
  204. * @param string $name
  205. * @param string $uuid a unique identifier as used in LDAP
  206. * @return bool
  207. */
  208. public function map($fdn, $name, $uuid) {
  209. if(mb_strlen($fdn) > 255) {
  210. \OC::$server->getLogger()->error(
  211. 'Cannot map, because the DN exceeds 255 characters: {dn}',
  212. [
  213. 'app' => 'user_ldap',
  214. 'dn' => $fdn,
  215. ]
  216. );
  217. return false;
  218. }
  219. $row = array(
  220. 'ldap_dn' => $fdn,
  221. 'owncloud_name' => $name,
  222. 'directory_uuid' => $uuid
  223. );
  224. try {
  225. $result = $this->dbc->insertIfNotExist($this->getTableName(), $row);
  226. // insertIfNotExist returns values as int
  227. return (bool)$result;
  228. } catch (\Exception $e) {
  229. return false;
  230. }
  231. }
  232. /**
  233. * removes a mapping based on the owncloud_name of the entry
  234. * @param string $name
  235. * @return bool
  236. */
  237. public function unmap($name) {
  238. $query = $this->dbc->prepare('
  239. DELETE FROM `'. $this->getTableName() .'`
  240. WHERE `owncloud_name` = ?');
  241. return $this->modify($query, array($name));
  242. }
  243. /**
  244. * Truncate's the mapping table
  245. * @return bool
  246. */
  247. public function clear() {
  248. $sql = $this->dbc
  249. ->getDatabasePlatform()
  250. ->getTruncateTableSQL('`' . $this->getTableName() . '`');
  251. return $this->dbc->prepare($sql)->execute();
  252. }
  253. /**
  254. * clears the mapping table one by one and executing a callback with
  255. * each row's id (=owncloud_name col)
  256. *
  257. * @param callable $preCallback
  258. * @param callable $postCallback
  259. * @return bool true on success, false when at least one row was not
  260. * deleted
  261. */
  262. public function clearCb(Callable $preCallback, Callable $postCallback): bool {
  263. $picker = $this->dbc->getQueryBuilder();
  264. $picker->select('owncloud_name')
  265. ->from($this->getTableName());
  266. $cursor = $picker->execute();
  267. $result = true;
  268. while($id = $cursor->fetchColumn(0)) {
  269. $preCallback($id);
  270. if($isUnmapped = $this->unmap($id)) {
  271. $postCallback($id);
  272. }
  273. $result &= $isUnmapped;
  274. }
  275. $cursor->closeCursor();
  276. return $result;
  277. }
  278. /**
  279. * returns the number of entries in the mappings table
  280. *
  281. * @return int
  282. */
  283. public function count() {
  284. $qb = $this->dbc->getQueryBuilder();
  285. $query = $qb->select($qb->func()->count('ldap_dn'))
  286. ->from($this->getTableName());
  287. $res = $query->execute();
  288. $count = $res->fetchColumn();
  289. $res->closeCursor();
  290. return (int)$count;
  291. }
  292. }