Passed
Push — master ( 3a99ef...34868f )
by Roeland
09:54 queued 11s
created

SharesPlugin::getShare()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 3
eloc 18
c 3
b 0
f 0
nc 3
nop 1
dl 0
loc 24
rs 9.6666
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Joas Schilling <[email protected]>
6
 * @author Maxence Lange <[email protected]>
7
 * @author Robin Appelman <[email protected]>
8
 * @author Roeland Jago Douma <[email protected]>
9
 * @author Vincent Petry <[email protected]>
10
 * @author Vinicius Cubas Brand <[email protected]>
11
 * @author Daniel Tygel <[email protected]>
12
 *
13
 * @license AGPL-3.0
14
 *
15
 * This code is free software: you can redistribute it and/or modify
16
 * it under the terms of the GNU Affero General Public License, version 3,
17
 * as published by the Free Software Foundation.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
 * GNU Affero General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU Affero General Public License, version 3,
25
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
26
 *
27
 */
28
namespace OCA\DAV\Connector\Sabre;
29
30
use \Sabre\DAV\PropFind;
31
use OCP\IUserSession;
32
use OCP\Share\IShare;
33
34
/**
35
 * Sabre Plugin to provide share-related properties
36
 */
37
class SharesPlugin extends \Sabre\DAV\ServerPlugin {
38
39
	const NS_OWNCLOUD = 'http://owncloud.org/ns';
40
	const NS_NEXTCLOUD = 'http://nextcloud.org/ns';
41
	const SHARETYPES_PROPERTYNAME = '{http://owncloud.org/ns}share-types';
42
	const SHAREES_PROPERTYNAME = '{http://nextcloud.org/ns}sharees';
43
44
	/**
45
	 * Reference to main server object
46
	 *
47
	 * @var \Sabre\DAV\Server
48
	 */
49
	private $server;
50
51
	/**
52
	 * @var \OCP\Share\IManager
53
	 */
54
	private $shareManager;
55
56
	/**
57
	 * @var \Sabre\DAV\Tree
58
	 */
59
	private $tree;
60
61
	/**
62
	 * @var string
63
	 */
64
	private $userId;
65
66
	/**
67
	 * @var \OCP\Files\Folder
68
	 */
69
	private $userFolder;
70
71
	/** @var IShare[] */
72
	private $cachedShares = [];
73
74
	private $cachedFolders = [];
75
76
	/**
77
	 * @param \Sabre\DAV\Tree $tree tree
78
	 * @param IUserSession $userSession user session
79
	 * @param \OCP\Files\Folder $userFolder user home folder
80
	 * @param \OCP\Share\IManager $shareManager share manager
81
	 */
82
	public function __construct(
83
		\Sabre\DAV\Tree $tree,
84
		IUserSession $userSession,
85
		\OCP\Files\Folder $userFolder,
86
		\OCP\Share\IManager $shareManager
87
	) {
88
		$this->tree = $tree;
89
		$this->shareManager = $shareManager;
90
		$this->userFolder = $userFolder;
91
		$this->userId = $userSession->getUser()->getUID();
92
	}
93
94
	/**
95
	 * This initializes the plugin.
96
	 *
97
	 * This function is called by \Sabre\DAV\Server, after
98
	 * addPlugin is called.
99
	 *
100
	 * This method should set up the required event subscriptions.
101
	 *
102
	 * @param \Sabre\DAV\Server $server
103
	 */
104
	public function initialize(\Sabre\DAV\Server $server) {
105
		$server->xml->namespacesMap[self::NS_OWNCLOUD] = 'oc';
0 ignored issues
show
Bug introduced by
The property namespacesMap does not exist on Sabre\DAV\Xml\Service. Did you mean namespaceMap?
Loading history...
106
		$server->xml->elementMap[self::SHARETYPES_PROPERTYNAME] = ShareTypeList::class;
107
		$server->protectedProperties[] = self::SHARETYPES_PROPERTYNAME;
108
		$server->protectedProperties[] = self::SHAREES_PROPERTYNAME;
109
110
		$this->server = $server;
111
		$this->server->on('propFind', array($this, 'handleGetProperties'));
112
	}
113
114
	private function getShare(\OCP\Files\Node $node): array {
115
		$result = [];
116
		$requestedShareTypes = [
117
			\OCP\Share::SHARE_TYPE_USER,
118
			\OCP\Share::SHARE_TYPE_GROUP,
119
			\OCP\Share::SHARE_TYPE_LINK,
120
			\OCP\Share::SHARE_TYPE_REMOTE,
121
			\OCP\Share::SHARE_TYPE_EMAIL,
122
			\OCP\Share::SHARE_TYPE_ROOM,
123
			\OCP\Share::SHARE_TYPE_CIRCLE,
124
		];
125
		foreach ($requestedShareTypes as $requestedShareType) {
126
			$shares = $this->shareManager->getSharesBy(
127
				$this->userId,
128
				$requestedShareType,
129
				$node,
130
				false,
131
				-1
132
			);
133
			foreach ($shares as $share) {
134
				$result[] = $share;
135
			}
136
		}
137
		return $result;
138
	}
139
140
	private function getSharesFolder(\OCP\Files\Folder $node): array {
141
		return $this->shareManager->getSharesInFolder(
142
			$this->userId,
143
			$node,
144
			true
145
		);
146
	}
147
148
	private function getShares(\Sabre\DAV\INode $sabreNode): array {
149
		if (isset($this->cachedShares[$sabreNode->getId()])) {
0 ignored issues
show
Bug introduced by
The method getId() does not exist on Sabre\DAV\INode. It seems like you code against a sub-type of Sabre\DAV\INode such as OCA\DAV\Connector\Sabre\Node or OCA\DAV\Comments\EntityCollection or OCA\DAV\Connector\Sabre\Directory or OCA\DAV\Comments\EntityCollection or OCA\DAV\Connector\Sabre\Directory or OCA\DAV\Connector\Sabre\Directory or OCA\DAV\Connector\Sabre\File. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

149
		if (isset($this->cachedShares[$sabreNode->/** @scrutinizer ignore-call */ getId()])) {
Loading history...
150
			$shares = $this->cachedShares[$sabreNode->getId()];
151
		} else {
152
			list($parentPath,) = \Sabre\Uri\split($sabreNode->getPath());
0 ignored issues
show
Bug introduced by
The method getPath() does not exist on Sabre\DAV\INode. It seems like you code against a sub-type of Sabre\DAV\INode such as OCA\DAV\Connector\Sabre\Node or OCA\DAV\Connector\Sabre\Directory or OCA\DAV\Connector\Sabre\Directory or OCA\DAV\Connector\Sabre\Directory or OCA\DAV\Connector\Sabre\File. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

152
			list($parentPath,) = \Sabre\Uri\split($sabreNode->/** @scrutinizer ignore-call */ getPath());
Loading history...
153
			if ($parentPath === '') {
154
				$parentPath = '/';
155
			}
156
			// if we already cached the folder this file is in we know there are no shares for this file
157
			if (array_search($parentPath, $this->cachedFolders) === false) {
158
				$node = $this->userFolder->get($sabreNode->getPath());
159
				$shares = $this->getShare($node);
160
				$this->cachedShares[$sabreNode->getId()] = $shares;
161
			} else {
162
				return [];
163
			}
164
		}
165
166
		return $shares;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $shares could return the type OCP\Share\IShare which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
167
	}
168
169
	/**
170
	 * Adds shares to propfind response
171
	 *
172
	 * @param PropFind $propFind propfind object
173
	 * @param \Sabre\DAV\INode $sabreNode sabre node
174
	 */
175
	public function handleGetProperties(
176
		PropFind $propFind,
177
		\Sabre\DAV\INode $sabreNode
178
	) {
179
		if (!($sabreNode instanceof \OCA\DAV\Connector\Sabre\Node)) {
180
			return;
181
		}
182
183
		// need prefetch ?
184
		if ($sabreNode instanceof \OCA\DAV\Connector\Sabre\Directory
185
			&& $propFind->getDepth() !== 0
186
			&& (
187
				!is_null($propFind->getStatus(self::SHARETYPES_PROPERTYNAME)) ||
188
				!is_null($propFind->getStatus(self::SHAREES_PROPERTYNAME))
189
			)
190
		) {
191
			$folderNode = $this->userFolder->get($sabreNode->getPath());
192
193
			$this->cachedFolders[] = $sabreNode->getPath();
194
			$childShares = $this->getSharesFolder($folderNode);
195
			foreach ($childShares as $id => $shares) {
196
				$this->cachedShares[$id] = $shares;
197
			}
198
		}
199
200
		$propFind->handle(self::SHARETYPES_PROPERTYNAME, function () use ($sabreNode) {
201
			$shares = $this->getShares($sabreNode);
202
203
			$shareTypes = array_unique(array_map(function(IShare $share) {
204
				return $share->getShareType();
205
			}, $shares));
206
207
			return new ShareTypeList($shareTypes);
208
		});
209
210
		$propFind->handle(self::SHAREES_PROPERTYNAME, function() use ($sabreNode) {
211
			$shares = $this->getShares($sabreNode);
212
213
			return new ShareeList($shares);
214
		});
215
	}
216
}
217