Backend.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Thomas Citharel <tcit@tcit.fr>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\DAV\DAV\Sharing;
  26. use OCA\DAV\Connector\Sabre\Principal;
  27. use OCP\IDBConnection;
  28. use OCP\IGroupManager;
  29. use OCP\IUserManager;
  30. class Backend {
  31. /** @var IDBConnection */
  32. private $db;
  33. /** @var IUserManager */
  34. private $userManager;
  35. /** @var IGroupManager */
  36. private $groupManager;
  37. /** @var Principal */
  38. private $principalBackend;
  39. /** @var string */
  40. private $resourceType;
  41. const ACCESS_OWNER = 1;
  42. const ACCESS_READ_WRITE = 2;
  43. const ACCESS_READ = 3;
  44. /**
  45. * @param IDBConnection $db
  46. * @param IUserManager $userManager
  47. * @param IGroupManager $groupManager
  48. * @param Principal $principalBackend
  49. * @param string $resourceType
  50. */
  51. public function __construct(IDBConnection $db, IUserManager $userManager, IGroupManager $groupManager, Principal $principalBackend, $resourceType) {
  52. $this->db = $db;
  53. $this->userManager = $userManager;
  54. $this->groupManager = $groupManager;
  55. $this->principalBackend = $principalBackend;
  56. $this->resourceType = $resourceType;
  57. }
  58. /**
  59. * @param IShareable $shareable
  60. * @param string[] $add
  61. * @param string[] $remove
  62. */
  63. public function updateShares(IShareable $shareable, array $add, array $remove) {
  64. foreach($add as $element) {
  65. $principal = $this->principalBackend->findByUri($element['href'], '');
  66. if ($principal !== '') {
  67. $this->shareWith($shareable, $element);
  68. }
  69. }
  70. foreach($remove as $element) {
  71. $principal = $this->principalBackend->findByUri($element, '');
  72. if ($principal !== '') {
  73. $this->unshare($shareable, $element);
  74. }
  75. }
  76. }
  77. /**
  78. * @param IShareable $shareable
  79. * @param string $element
  80. */
  81. private function shareWith($shareable, $element) {
  82. $user = $element['href'];
  83. $parts = explode(':', $user, 2);
  84. if ($parts[0] !== 'principal') {
  85. return;
  86. }
  87. // don't share with owner
  88. if ($shareable->getOwner() === $parts[1]) {
  89. return;
  90. }
  91. $principal = explode('/', $parts[1], 3);
  92. if (count($principal) !== 3 || $principal[0] !== 'principals' || !in_array($principal[1], ['users', 'groups'], true)) {
  93. // Invalid principal
  94. return;
  95. }
  96. if (($principal[1] === 'users' && !$this->userManager->userExists($principal[2])) ||
  97. ($principal[1] === 'groups' && !$this->groupManager->groupExists($principal[2]))) {
  98. // User or group does not exist
  99. return;
  100. }
  101. // remove the share if it already exists
  102. $this->unshare($shareable, $element['href']);
  103. $access = self::ACCESS_READ;
  104. if (isset($element['readOnly'])) {
  105. $access = $element['readOnly'] ? self::ACCESS_READ : self::ACCESS_READ_WRITE;
  106. }
  107. $query = $this->db->getQueryBuilder();
  108. $query->insert('dav_shares')
  109. ->values([
  110. 'principaluri' => $query->createNamedParameter($parts[1]),
  111. 'type' => $query->createNamedParameter($this->resourceType),
  112. 'access' => $query->createNamedParameter($access),
  113. 'resourceid' => $query->createNamedParameter($shareable->getResourceId())
  114. ]);
  115. $query->execute();
  116. }
  117. /**
  118. * @param $resourceId
  119. */
  120. public function deleteAllShares($resourceId) {
  121. $query = $this->db->getQueryBuilder();
  122. $query->delete('dav_shares')
  123. ->where($query->expr()->eq('resourceid', $query->createNamedParameter($resourceId)))
  124. ->andWhere($query->expr()->eq('type', $query->createNamedParameter($this->resourceType)))
  125. ->execute();
  126. }
  127. public function deleteAllSharesByUser($principaluri) {
  128. $query = $this->db->getQueryBuilder();
  129. $query->delete('dav_shares')
  130. ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principaluri)))
  131. ->andWhere($query->expr()->eq('type', $query->createNamedParameter($this->resourceType)))
  132. ->execute();
  133. }
  134. /**
  135. * @param IShareable $shareable
  136. * @param string $element
  137. */
  138. private function unshare($shareable, $element) {
  139. $parts = explode(':', $element, 2);
  140. if ($parts[0] !== 'principal') {
  141. return;
  142. }
  143. // don't share with owner
  144. if ($shareable->getOwner() === $parts[1]) {
  145. return;
  146. }
  147. $query = $this->db->getQueryBuilder();
  148. $query->delete('dav_shares')
  149. ->where($query->expr()->eq('resourceid', $query->createNamedParameter($shareable->getResourceId())))
  150. ->andWhere($query->expr()->eq('type', $query->createNamedParameter($this->resourceType)))
  151. ->andWhere($query->expr()->eq('principaluri', $query->createNamedParameter($parts[1])))
  152. ;
  153. $query->execute();
  154. }
  155. /**
  156. * Returns the list of people whom this resource is shared with.
  157. *
  158. * Every element in this array should have the following properties:
  159. * * href - Often a mailto: address
  160. * * commonName - Optional, for example a first + last name
  161. * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants.
  162. * * readOnly - boolean
  163. * * summary - Optional, a description for the share
  164. *
  165. * @param int $resourceId
  166. * @return array
  167. */
  168. public function getShares($resourceId) {
  169. $query = $this->db->getQueryBuilder();
  170. $result = $query->select(['principaluri', 'access'])
  171. ->from('dav_shares')
  172. ->where($query->expr()->eq('resourceid', $query->createNamedParameter($resourceId)))
  173. ->andWhere($query->expr()->eq('type', $query->createNamedParameter($this->resourceType)))
  174. ->execute();
  175. $shares = [];
  176. while($row = $result->fetch()) {
  177. $p = $this->principalBackend->getPrincipalByPath($row['principaluri']);
  178. $shares[]= [
  179. 'href' => "principal:${row['principaluri']}",
  180. 'commonName' => isset($p['{DAV:}displayname']) ? $p['{DAV:}displayname'] : '',
  181. 'status' => 1,
  182. 'readOnly' => (int) $row['access'] === self::ACCESS_READ,
  183. '{http://owncloud.org/ns}principal' => $row['principaluri'],
  184. '{http://owncloud.org/ns}group-share' => is_null($p)
  185. ];
  186. }
  187. return $shares;
  188. }
  189. /**
  190. * For shared resources the sharee is set in the ACL of the resource
  191. *
  192. * @param int $resourceId
  193. * @param array $acl
  194. * @return array
  195. */
  196. public function applyShareAcl($resourceId, $acl) {
  197. $shares = $this->getShares($resourceId);
  198. foreach ($shares as $share) {
  199. $acl[] = [
  200. 'privilege' => '{DAV:}read',
  201. 'principal' => $share['{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}principal'],
  202. 'protected' => true,
  203. ];
  204. if (!$share['readOnly']) {
  205. $acl[] = [
  206. 'privilege' => '{DAV:}write',
  207. 'principal' => $share['{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}principal'],
  208. 'protected' => true,
  209. ];
  210. } else if ($this->resourceType === 'calendar') {
  211. // Allow changing the properties of read only calendars,
  212. // so users can change the visibility.
  213. $acl[] = [
  214. 'privilege' => '{DAV:}write-properties',
  215. 'principal' => $share['{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}principal'],
  216. 'protected' => true,
  217. ];
  218. }
  219. }
  220. return $acl;
  221. }
  222. }