PDO.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. <?php
  2. /**
  3. * PDO principal backend
  4. *
  5. * This is a simple principal backend that maps exactly to the users table, as
  6. * used by Sabre_DAV_Auth_Backend_PDO.
  7. *
  8. * It assumes all principals are in a single collection. The default collection
  9. * is 'principals/', but this can be overriden.
  10. *
  11. * @package Sabre
  12. * @subpackage DAVACL
  13. * @copyright Copyright (C) 2007-2012 Rooftop Solutions. All rights reserved.
  14. * @author Evert Pot (http://www.rooftopsolutions.nl/)
  15. * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
  16. */
  17. class Sabre_DAVACL_PrincipalBackend_PDO implements Sabre_DAVACL_IPrincipalBackend {
  18. /**
  19. * pdo
  20. *
  21. * @var PDO
  22. */
  23. protected $pdo;
  24. /**
  25. * PDO table name for 'principals'
  26. *
  27. * @var string
  28. */
  29. protected $tableName;
  30. /**
  31. * PDO table name for 'group members'
  32. *
  33. * @var string
  34. */
  35. protected $groupMembersTableName;
  36. /**
  37. * A list of additional fields to support
  38. *
  39. * @var array
  40. */
  41. protected $fieldMap = array(
  42. /**
  43. * This property can be used to display the users' real name.
  44. */
  45. '{DAV:}displayname' => array(
  46. 'dbField' => 'displayname',
  47. ),
  48. /**
  49. * This property is actually used by the CardDAV plugin, where it gets
  50. * mapped to {http://calendarserver.orgi/ns/}me-card.
  51. *
  52. * The reason we don't straight-up use that property, is because
  53. * me-card is defined as a property on the users' addressbook
  54. * collection.
  55. */
  56. '{http://sabredav.org/ns}vcard-url' => array(
  57. 'dbField' => 'vcardurl',
  58. ),
  59. /**
  60. * This is the users' primary email-address.
  61. */
  62. '{http://sabredav.org/ns}email-address' => array(
  63. 'dbField' => 'email',
  64. ),
  65. );
  66. /**
  67. * Sets up the backend.
  68. *
  69. * @param PDO $pdo
  70. * @param string $tableName
  71. * @param string $groupMembersTableName
  72. */
  73. public function __construct(PDO $pdo, $tableName = 'principals', $groupMembersTableName = 'groupmembers') {
  74. $this->pdo = $pdo;
  75. $this->tableName = $tableName;
  76. $this->groupMembersTableName = $groupMembersTableName;
  77. }
  78. /**
  79. * Returns a list of principals based on a prefix.
  80. *
  81. * This prefix will often contain something like 'principals'. You are only
  82. * expected to return principals that are in this base path.
  83. *
  84. * You are expected to return at least a 'uri' for every user, you can
  85. * return any additional properties if you wish so. Common properties are:
  86. * {DAV:}displayname
  87. * {http://sabredav.org/ns}email-address - This is a custom SabreDAV
  88. * field that's actualy injected in a number of other properties. If
  89. * you have an email address, use this property.
  90. *
  91. * @param string $prefixPath
  92. * @return array
  93. */
  94. public function getPrincipalsByPrefix($prefixPath) {
  95. $fields = array(
  96. 'uri',
  97. );
  98. foreach($this->fieldMap as $key=>$value) {
  99. $fields[] = $value['dbField'];
  100. }
  101. $result = $this->pdo->query('SELECT '.implode(',', $fields).' FROM '. $this->tableName);
  102. $principals = array();
  103. while($row = $result->fetch(PDO::FETCH_ASSOC)) {
  104. // Checking if the principal is in the prefix
  105. list($rowPrefix) = Sabre_DAV_URLUtil::splitPath($row['uri']);
  106. if ($rowPrefix !== $prefixPath) continue;
  107. $principal = array(
  108. 'uri' => $row['uri'],
  109. );
  110. foreach($this->fieldMap as $key=>$value) {
  111. if ($row[$value['dbField']]) {
  112. $principal[$key] = $row[$value['dbField']];
  113. }
  114. }
  115. $principals[] = $principal;
  116. }
  117. return $principals;
  118. }
  119. /**
  120. * Returns a specific principal, specified by it's path.
  121. * The returned structure should be the exact same as from
  122. * getPrincipalsByPrefix.
  123. *
  124. * @param string $path
  125. * @return array
  126. */
  127. public function getPrincipalByPath($path) {
  128. $fields = array(
  129. 'id',
  130. 'uri',
  131. );
  132. foreach($this->fieldMap as $key=>$value) {
  133. $fields[] = $value['dbField'];
  134. }
  135. $stmt = $this->pdo->prepare('SELECT '.implode(',', $fields).' FROM '. $this->tableName . ' WHERE uri = ?');
  136. $stmt->execute(array($path));
  137. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  138. if (!$row) return;
  139. $principal = array(
  140. 'id' => $row['id'],
  141. 'uri' => $row['uri'],
  142. );
  143. foreach($this->fieldMap as $key=>$value) {
  144. if ($row[$value['dbField']]) {
  145. $principal[$key] = $row[$value['dbField']];
  146. }
  147. }
  148. return $principal;
  149. }
  150. /**
  151. * Updates one ore more webdav properties on a principal.
  152. *
  153. * The list of mutations is supplied as an array. Each key in the array is
  154. * a propertyname, such as {DAV:}displayname.
  155. *
  156. * Each value is the actual value to be updated. If a value is null, it
  157. * must be deleted.
  158. *
  159. * This method should be atomic. It must either completely succeed, or
  160. * completely fail. Success and failure can simply be returned as 'true' or
  161. * 'false'.
  162. *
  163. * It is also possible to return detailed failure information. In that case
  164. * an array such as this should be returned:
  165. *
  166. * array(
  167. * 200 => array(
  168. * '{DAV:}prop1' => null,
  169. * ),
  170. * 201 => array(
  171. * '{DAV:}prop2' => null,
  172. * ),
  173. * 403 => array(
  174. * '{DAV:}prop3' => null,
  175. * ),
  176. * 424 => array(
  177. * '{DAV:}prop4' => null,
  178. * ),
  179. * );
  180. *
  181. * In this previous example prop1 was successfully updated or deleted, and
  182. * prop2 was succesfully created.
  183. *
  184. * prop3 failed to update due to '403 Forbidden' and because of this prop4
  185. * also could not be updated with '424 Failed dependency'.
  186. *
  187. * This last example was actually incorrect. While 200 and 201 could appear
  188. * in 1 response, if there's any error (403) the other properties should
  189. * always fail with 423 (failed dependency).
  190. *
  191. * But anyway, if you don't want to scratch your head over this, just
  192. * return true or false.
  193. *
  194. * @param string $path
  195. * @param array $mutations
  196. * @return array|bool
  197. */
  198. public function updatePrincipal($path, $mutations) {
  199. $updateAble = array();
  200. foreach($mutations as $key=>$value) {
  201. // We are not aware of this field, we must fail.
  202. if (!isset($this->fieldMap[$key])) {
  203. $response = array(
  204. 403 => array(
  205. $key => null,
  206. ),
  207. 424 => array(),
  208. );
  209. // Adding the rest to the response as a 424
  210. foreach($mutations as $subKey=>$subValue) {
  211. if ($subKey !== $key) {
  212. $response[424][$subKey] = null;
  213. }
  214. }
  215. return $response;
  216. }
  217. $updateAble[$this->fieldMap[$key]['dbField']] = $value;
  218. }
  219. // No fields to update
  220. $query = "UPDATE " . $this->tableName . " SET ";
  221. $first = true;
  222. foreach($updateAble as $key => $value) {
  223. if (!$first) {
  224. $query.= ', ';
  225. }
  226. $first = false;
  227. $query.= "$key = :$key ";
  228. }
  229. $query.='WHERE uri = :uri';
  230. $stmt = $this->pdo->prepare($query);
  231. $updateAble['uri'] = $path;
  232. $stmt->execute($updateAble);
  233. return true;
  234. }
  235. /**
  236. * This method is used to search for principals matching a set of
  237. * properties.
  238. *
  239. * This search is specifically used by RFC3744's principal-property-search
  240. * REPORT. You should at least allow searching on
  241. * http://sabredav.org/ns}email-address.
  242. *
  243. * The actual search should be a unicode-non-case-sensitive search. The
  244. * keys in searchProperties are the WebDAV property names, while the values
  245. * are the property values to search on.
  246. *
  247. * If multiple properties are being searched on, the search should be
  248. * AND'ed.
  249. *
  250. * This method should simply return an array with full principal uri's.
  251. *
  252. * If somebody attempted to search on a property the backend does not
  253. * support, you should simply return 0 results.
  254. *
  255. * You can also just return 0 results if you choose to not support
  256. * searching at all, but keep in mind that this may stop certain features
  257. * from working.
  258. *
  259. * @param string $prefixPath
  260. * @param array $searchProperties
  261. * @return array
  262. */
  263. public function searchPrincipals($prefixPath, array $searchProperties) {
  264. $query = 'SELECT uri FROM ' . $this->tableName . ' WHERE 1=1 ';
  265. $values = array();
  266. foreach($searchProperties as $property => $value) {
  267. switch($property) {
  268. case '{DAV:}displayname' :
  269. $query.=' AND displayname LIKE ?';
  270. $values[] = '%' . $value . '%';
  271. break;
  272. case '{http://sabredav.org/ns}email-address' :
  273. $query.=' AND email LIKE ?';
  274. $values[] = '%' . $value . '%';
  275. break;
  276. default :
  277. // Unsupported property
  278. return array();
  279. }
  280. }
  281. $stmt = $this->pdo->prepare($query);
  282. $stmt->execute($values);
  283. $principals = array();
  284. while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  285. // Checking if the principal is in the prefix
  286. list($rowPrefix) = Sabre_DAV_URLUtil::splitPath($row['uri']);
  287. if ($rowPrefix !== $prefixPath) continue;
  288. $principals[] = $row['uri'];
  289. }
  290. return $principals;
  291. }
  292. /**
  293. * Returns the list of members for a group-principal
  294. *
  295. * @param string $principal
  296. * @return array
  297. */
  298. public function getGroupMemberSet($principal) {
  299. $principal = $this->getPrincipalByPath($principal);
  300. if (!$principal) throw new Sabre_DAV_Exception('Principal not found');
  301. $stmt = $this->pdo->prepare('SELECT principals.uri as uri FROM '.$this->groupMembersTableName.' AS groupmembers LEFT JOIN '.$this->tableName.' AS principals ON groupmembers.member_id = principals.id WHERE groupmembers.principal_id = ?');
  302. $stmt->execute(array($principal['id']));
  303. $result = array();
  304. while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  305. $result[] = $row['uri'];
  306. }
  307. return $result;
  308. }
  309. /**
  310. * Returns the list of groups a principal is a member of
  311. *
  312. * @param string $principal
  313. * @return array
  314. */
  315. public function getGroupMembership($principal) {
  316. $principal = $this->getPrincipalByPath($principal);
  317. if (!$principal) throw new Sabre_DAV_Exception('Principal not found');
  318. $stmt = $this->pdo->prepare('SELECT principals.uri as uri FROM '.$this->groupMembersTableName.' AS groupmembers LEFT JOIN '.$this->tableName.' AS principals ON groupmembers.principal_id = principals.id WHERE groupmembers.member_id = ?');
  319. $stmt->execute(array($principal['id']));
  320. $result = array();
  321. while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  322. $result[] = $row['uri'];
  323. }
  324. return $result;
  325. }
  326. /**
  327. * Updates the list of group members for a group principal.
  328. *
  329. * The principals should be passed as a list of uri's.
  330. *
  331. * @param string $principal
  332. * @param array $members
  333. * @return void
  334. */
  335. public function setGroupMemberSet($principal, array $members) {
  336. // Grabbing the list of principal id's.
  337. $stmt = $this->pdo->prepare('SELECT id, uri FROM '.$this->tableName.' WHERE uri IN (? ' . str_repeat(', ? ', count($members)) . ');');
  338. $stmt->execute(array_merge(array($principal), $members));
  339. $memberIds = array();
  340. $principalId = null;
  341. while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  342. if ($row['uri'] == $principal) {
  343. $principalId = $row['id'];
  344. } else {
  345. $memberIds[] = $row['id'];
  346. }
  347. }
  348. if (!$principalId) throw new Sabre_DAV_Exception('Principal not found');
  349. // Wiping out old members
  350. $stmt = $this->pdo->prepare('DELETE FROM '.$this->groupMembersTableName.' WHERE principal_id = ?;');
  351. $stmt->execute(array($principalId));
  352. foreach($memberIds as $memberId) {
  353. $stmt = $this->pdo->prepare('INSERT INTO '.$this->groupMembersTableName.' (principal_id, member_id) VALUES (?, ?);');
  354. $stmt->execute(array($principalId, $memberId));
  355. }
  356. }
  357. }