Share.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OC\Share20;
  24. use OCP\Files\Cache\ICacheEntry;
  25. use OCP\Files\File;
  26. use OCP\Files\IRootFolder;
  27. use OCP\Files\Node;
  28. use OCP\Files\NotFoundException;
  29. use OCP\IUserManager;
  30. use OCP\Share\Exceptions\IllegalIDChangeException;
  31. class Share implements \OCP\Share\IShare {
  32. /** @var string */
  33. private $id;
  34. /** @var string */
  35. private $providerId;
  36. /** @var Node */
  37. private $node;
  38. /** @var int */
  39. private $fileId;
  40. /** @var string */
  41. private $nodeType;
  42. /** @var int */
  43. private $shareType;
  44. /** @var string */
  45. private $sharedWith;
  46. /** @var string */
  47. private $sharedBy;
  48. /** @var string */
  49. private $shareOwner;
  50. /** @var int */
  51. private $permissions;
  52. /** @var \DateTime */
  53. private $expireDate;
  54. /** @var string */
  55. private $password;
  56. /** @var string */
  57. private $token;
  58. /** @var int */
  59. private $parent;
  60. /** @var string */
  61. private $target;
  62. /** @var \DateTime */
  63. private $shareTime;
  64. /** @var bool */
  65. private $mailSend;
  66. /** @var IRootFolder */
  67. private $rootFolder;
  68. /** @var IUserManager */
  69. private $userManager;
  70. /** @var ICacheEntry|null */
  71. private $nodeCacheEntry;
  72. public function __construct(IRootFolder $rootFolder, IUserManager $userManager) {
  73. $this->rootFolder = $rootFolder;
  74. $this->userManager = $userManager;
  75. }
  76. /**
  77. * @inheritdoc
  78. */
  79. public function setId($id) {
  80. if (is_int($id)) {
  81. $id = (string)$id;
  82. }
  83. if(!is_string($id)) {
  84. throw new \InvalidArgumentException('String expected.');
  85. }
  86. if ($this->id !== null) {
  87. throw new IllegalIDChangeException('Not allowed to assign a new internal id to a share');
  88. }
  89. $this->id = trim($id);
  90. return $this;
  91. }
  92. /**
  93. * @inheritdoc
  94. */
  95. public function getId() {
  96. return $this->id;
  97. }
  98. /**
  99. * @inheritdoc
  100. */
  101. public function getFullId() {
  102. if ($this->providerId === null || $this->id === null) {
  103. throw new \UnexpectedValueException;
  104. }
  105. return $this->providerId . ':' . $this->id;
  106. }
  107. /**
  108. * @inheritdoc
  109. */
  110. public function setProviderId($id) {
  111. if(!is_string($id)) {
  112. throw new \InvalidArgumentException('String expected.');
  113. }
  114. if ($this->providerId !== null) {
  115. throw new IllegalIDChangeException('Not allowed to assign a new provider id to a share');
  116. }
  117. $this->providerId = trim($id);
  118. return $this;
  119. }
  120. /**
  121. * @inheritdoc
  122. */
  123. public function setNode(Node $node) {
  124. $this->fileId = null;
  125. $this->nodeType = null;
  126. $this->node = $node;
  127. return $this;
  128. }
  129. /**
  130. * @inheritdoc
  131. */
  132. public function getNode() {
  133. if ($this->node === null) {
  134. if ($this->shareOwner === null || $this->fileId === null) {
  135. throw new NotFoundException();
  136. }
  137. // for federated shares the owner can be a remote user, in this
  138. // case we use the initiator
  139. if($this->userManager->userExists($this->shareOwner)) {
  140. $userFolder = $this->rootFolder->getUserFolder($this->shareOwner);
  141. } else {
  142. $userFolder = $this->rootFolder->getUserFolder($this->sharedBy);
  143. }
  144. $nodes = $userFolder->getById($this->fileId);
  145. if (empty($nodes)) {
  146. throw new NotFoundException('Node for share not found, fileid: ' . $this->fileId);
  147. }
  148. $this->node = $nodes[0];
  149. }
  150. return $this->node;
  151. }
  152. /**
  153. * @inheritdoc
  154. */
  155. public function setNodeId($fileId) {
  156. $this->node = null;
  157. $this->fileId = $fileId;
  158. return $this;
  159. }
  160. /**
  161. * @inheritdoc
  162. */
  163. public function getNodeId() {
  164. if ($this->fileId === null) {
  165. $this->fileId = $this->getNode()->getId();
  166. }
  167. return $this->fileId;
  168. }
  169. /**
  170. * @inheritdoc
  171. */
  172. public function setNodeType($type) {
  173. if ($type !== 'file' && $type !== 'folder') {
  174. throw new \InvalidArgumentException();
  175. }
  176. $this->nodeType = $type;
  177. return $this;
  178. }
  179. /**
  180. * @inheritdoc
  181. */
  182. public function getNodeType() {
  183. if ($this->nodeType === null) {
  184. $node = $this->getNode();
  185. $this->nodeType = $node instanceof File ? 'file' : 'folder';
  186. }
  187. return $this->nodeType;
  188. }
  189. /**
  190. * @inheritdoc
  191. */
  192. public function setShareType($shareType) {
  193. $this->shareType = $shareType;
  194. return $this;
  195. }
  196. /**
  197. * @inheritdoc
  198. */
  199. public function getShareType() {
  200. return $this->shareType;
  201. }
  202. /**
  203. * @inheritdoc
  204. */
  205. public function setSharedWith($sharedWith) {
  206. if (!is_string($sharedWith)) {
  207. throw new \InvalidArgumentException();
  208. }
  209. $this->sharedWith = $sharedWith;
  210. return $this;
  211. }
  212. /**
  213. * @inheritdoc
  214. */
  215. public function getSharedWith() {
  216. return $this->sharedWith;
  217. }
  218. /**
  219. * @inheritdoc
  220. */
  221. public function setPermissions($permissions) {
  222. //TODO checkes
  223. $this->permissions = $permissions;
  224. return $this;
  225. }
  226. /**
  227. * @inheritdoc
  228. */
  229. public function getPermissions() {
  230. return $this->permissions;
  231. }
  232. /**
  233. * @inheritdoc
  234. */
  235. public function setExpirationDate($expireDate) {
  236. //TODO checks
  237. $this->expireDate = $expireDate;
  238. return $this;
  239. }
  240. /**
  241. * @inheritdoc
  242. */
  243. public function getExpirationDate() {
  244. return $this->expireDate;
  245. }
  246. /**
  247. * @inheritdoc
  248. */
  249. public function setSharedBy($sharedBy) {
  250. if (!is_string($sharedBy)) {
  251. throw new \InvalidArgumentException();
  252. }
  253. //TODO checks
  254. $this->sharedBy = $sharedBy;
  255. return $this;
  256. }
  257. /**
  258. * @inheritdoc
  259. */
  260. public function getSharedBy() {
  261. //TODO check if set
  262. return $this->sharedBy;
  263. }
  264. /**
  265. * @inheritdoc
  266. */
  267. public function setShareOwner($shareOwner) {
  268. if (!is_string($shareOwner)) {
  269. throw new \InvalidArgumentException();
  270. }
  271. //TODO checks
  272. $this->shareOwner = $shareOwner;
  273. return $this;
  274. }
  275. /**
  276. * @inheritdoc
  277. */
  278. public function getShareOwner() {
  279. //TODO check if set
  280. return $this->shareOwner;
  281. }
  282. /**
  283. * @inheritdoc
  284. */
  285. public function setPassword($password) {
  286. $this->password = $password;
  287. return $this;
  288. }
  289. /**
  290. * @inheritdoc
  291. */
  292. public function getPassword() {
  293. return $this->password;
  294. }
  295. /**
  296. * @inheritdoc
  297. */
  298. public function setToken($token) {
  299. $this->token = $token;
  300. return $this;
  301. }
  302. /**
  303. * @inheritdoc
  304. */
  305. public function getToken() {
  306. return $this->token;
  307. }
  308. /**
  309. * Set the parent of this share
  310. *
  311. * @param int parent
  312. * @return \OCP\Share\IShare
  313. * @deprecated The new shares do not have parents. This is just here for legacy reasons.
  314. */
  315. public function setParent($parent) {
  316. $this->parent = $parent;
  317. return $this;
  318. }
  319. /**
  320. * Get the parent of this share.
  321. *
  322. * @return int
  323. * @deprecated The new shares do not have parents. This is just here for legacy reasons.
  324. */
  325. public function getParent() {
  326. return $this->parent;
  327. }
  328. /**
  329. * @inheritdoc
  330. */
  331. public function setTarget($target) {
  332. $this->target = $target;
  333. return $this;
  334. }
  335. /**
  336. * @inheritdoc
  337. */
  338. public function getTarget() {
  339. return $this->target;
  340. }
  341. /**
  342. * @inheritdoc
  343. */
  344. public function setShareTime(\DateTime $shareTime) {
  345. $this->shareTime = $shareTime;
  346. return $this;
  347. }
  348. /**
  349. * @inheritdoc
  350. */
  351. public function getShareTime() {
  352. return $this->shareTime;
  353. }
  354. /**
  355. * @inheritdoc
  356. */
  357. public function setMailSend($mailSend) {
  358. $this->mailSend = $mailSend;
  359. return $this;
  360. }
  361. /**
  362. * @inheritdoc
  363. */
  364. public function getMailSend() {
  365. return $this->mailSend;
  366. }
  367. /**
  368. * @inheritdoc
  369. */
  370. public function setNodeCacheEntry(ICacheEntry $entry) {
  371. $this->nodeCacheEntry = $entry;
  372. }
  373. /**
  374. * @inheritdoc
  375. */
  376. public function getNodeCacheEntry() {
  377. return $this->nodeCacheEntry;
  378. }
  379. }