AbstractMapping.php 7.7 KB

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