1
0

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