Completed
Push — master ( 8de354...4e0f9f )
by Maxence
01:38
created

LocalFilesService::getSharesFromParent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
declare(strict_types=1);
3
4
5
/**
6
 * Files_FullTextSearch - Index the content of your files
7
 *
8
 * This file is licensed under the Affero General Public License version 3 or
9
 * later. See the COPYING file.
10
 *
11
 * @author Maxence Lange <[email protected]>
12
 * @copyright 2018
13
 * @license GNU AGPL version 3 or any later version
14
 *
15
 * This program is free software: you can redistribute it and/or modify
16
 * it under the terms of the GNU Affero General Public License as
17
 * published by the Free Software Foundation, either version 3 of the
18
 * License, or (at your option) any later version.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 * GNU Affero General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU Affero General Public License
26
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27
 *
28
 */
29
30
31
namespace OCA\Files_FullTextSearch\Service;
32
33
34
use Exception;
35
use OC\Share\Constants;
36
use OCA\Files_FullTextSearch\Db\SharesRequest;
37
use OCA\Files_FullTextSearch\Exceptions\KnownFileSourceException;
38
use OCA\Files_FullTextSearch\Model\FilesDocument;
39
use OCA\Files_FullTextSearch\Model\FileShares;
40
use OCP\Files\IRootFolder;
41
use OCP\Files\Node;
42
use OCP\FullTextSearch\Model\DocumentAccess;
43
use OCP\IGroupManager;
44
use OCP\IUserManager;
45
use OCP\Share\IManager;
46
47
48
/**
49
 * Class LocalFilesService
50
 *
51
 * @package OCA\Files_FullTextSearch\Service
52
 */
53
class LocalFilesService {
54
55
56
	/** @var IRootFolder */
57
	private $rootFolder;
58
59
	/** @var IGroupManager */
60
	private $groupManager;
61
62
	/** @var IUserManager */
63
	private $userManager;
64
65
	/** @var IManager */
66
	private $shareManager;
67
68
	/** @var SharesRequest */
69
	private $sharesRequest;
70
71
	/** @var ConfigService */
72
	private $configService;
73
74
	/** @var MiscService */
75
	private $miscService;
76
77
78
	/**
79
	 * LocalFilesService constructor.
80
	 *
81
	 * @param IRootFolder $rootFolder
82
	 * @param IGroupManager $groupManager
83
	 * @param IUserManager $userManager
84
	 * @param IManager $shareManager
85
	 * @param SharesRequest $sharesRequest
86
	 * @param ConfigService $configService
87
	 * @param MiscService $miscService
88
	 */
89 View Code Duplication
	public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
90
		IRootFolder $rootFolder, IGroupManager $groupManager, IUserManager $userManager,
91
		IManager $shareManager, SharesRequest $sharesRequest, ConfigService $configService,
92
		MiscService $miscService
93
	) {
94
		$this->rootFolder = $rootFolder;
95
		$this->groupManager = $groupManager;
96
		$this->userManager = $userManager;
97
		$this->shareManager = $shareManager;
98
99
		$this->sharesRequest = $sharesRequest;
100
		$this->configService = $configService;
101
		$this->miscService = $miscService;
102
	}
103
104
105
	/**
106
	 * @param Node $file
107
	 * @param string $source
108
	 *
109
	 * @throws KnownFileSourceException
110
	 */
111
	public function getFileSource(Node $file, string &$source) {
112
		if ($file->getMountPoint()
113
				 ->getMountType() !== '') {
114
			return;
115
		}
116
117
		$source = ConfigService::FILES_LOCAL;
118
119
		throw new KnownFileSourceException();
120
	}
121
122
123
	/**
124
	 * @param FilesDocument $document
125
	 * @param Node $file
126
	 */
127
	public function updateDocumentAccess(FilesDocument $document, Node $file) {
128
129
		$ownerId = '';
130
		if ($file->getOwner() !== null) {
131
			$ownerId = $file->getOwner()
132
							->getUID();
133
		}
134
135
		$access = new DocumentAccess($ownerId);
136
137
		$fileShares = new FileShares();
138
		$this->getSharesFromFile($file, $fileShares);
139
		$access->setUsers($fileShares->getUsers());
140
		$access->setGroups($fileShares->getGroups());
141
		$access->setCircles($fileShares->getCircles());
142
		$access->setLinks($fileShares->getLinks());
143
144
		$document->setAccess($access);
145
	}
146
147
148
	/**
149
	 * @param Node $file
150
	 * @param array $users
151
	 */
152
	public function getShareUsersFromFile(Node $file, array &$users) {
153
		if ($file->getOwner() === null) {
154
			return;
155
		}
156
157
		try {
158
			$shares = $this->shareManager->getAccessList($file, true, true);
159
		} catch (Exception $e) {
160
			return;
161
		}
162
163
		if (!array_key_exists('users', $shares)) {
164
			return;
165
		}
166
167
		foreach ($shares['users'] as $user => $node) {
168
			if (in_array($user, $users) || $this->userManager->get($user) === null) {
169
				continue;
170
			}
171
172
			array_push($users, $user);
173
		}
174
175
	}
176
177
178
	/**
179
	 * same a getShareUsers, but we do it 'manually'
180
	 *
181
	 * @param DocumentAccess $access
182
	 * @param array $users
183
	 */
184
	public function getSharedUsersFromAccess(DocumentAccess $access, array &$users) {
185
186
		$result = array_merge(
187
			$access->getUsers(),
188
			$this->getSharedUsersFromAccessGroups($access),
189
			$this->getSharedUsersFromAccessCircles($access)
190
		);
191
192
		foreach ($result as $user) {
193
			if (!in_array($user, $users)) {
194
				$users[] = $user;
195
			}
196
		}
197
	}
198
199
200
	/**
201
	 * @param DocumentAccess $access
202
	 *
203
	 * @return array
204
	 */
205
	private function getSharedUsersFromAccessGroups(DocumentAccess $access): array {
206
207
		$result = [];
208
		$users = [];
209
		foreach ($access->getGroups() as $groupName) {
210
			$group = $this->groupManager->get($groupName);
211
			if ($group === null) {
212
				// TODO: set a warning
213
				continue;
214
			}
215
			$users = array_merge($users, $group->getUsers());
216
		}
217
218
		foreach ($users as $user) {
219
			$result[] = $user->getUID();
220
		}
221
222
		return $result;
223
	}
224
225
226
	/**
227
	 * // TODO: get users from circles.
228
	 *
229
	 * @param DocumentAccess $access
230
	 *
231
	 * @return array
232
	 */
233
	private function getSharedUsersFromAccessCircles(DocumentAccess $access): array {
0 ignored issues
show
Unused Code introduced by
The parameter $access is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
234
		$result = [];
235
236
		return $result;
237
	}
238
239
240
	/**
241
	 * @param Node $file
242
	 * @param FileShares $fileShares
243
	 */
244
	private function getSharesFromFile(Node $file, FileShares $fileShares) {
245
246
		if (strlen($file->getPath()) <= 1) {
247
			return;
248
		}
249
250
		// we get shares from parent first
251
		$this->getSharesFromFile($file->getParent(), $fileShares);
252
253
		$shares = $this->sharesRequest->getFromFile($file);
0 ignored issues
show
Deprecated Code introduced by
The method OCA\Files_FullTextSearch...sRequest::getFromFile() has been deprecated.

This method has been deprecated.

Loading history...
254
		foreach ($shares as $share) {
255
			if ($share['parent'] !== null) {
256
				continue;
257
			}
258
259
			$this->parseUsersShares($share, $fileShares);
260
			$this->parseUsersGroups($share, $fileShares);
261
			$this->parseUsersCircles($share, $fileShares);
262
			$this->parseUsersLinks($share, $fileShares);
263
		}
264
	}
265
266
267
	/**
268
	 * @param array $share
269
	 * @param FileShares $fileShares
270
	 */
271
	private function parseUsersShares(array $share, FileShares $fileShares) {
272
		if ((int)$share['share_type'] !== Constants::SHARE_TYPE_USER) {
273
			return;
274
		}
275
276
		$fileShares->addUser($share['share_with']);
277
	}
278
279
280
	/**
281
	 * @param array $share
282
	 * @param FileShares $fileShares
283
	 */
284
	private function parseUsersGroups(array $share, FileShares $fileShares) {
285
		if ((int)$share['share_type'] !== Constants::SHARE_TYPE_GROUP) {
286
			return;
287
		}
288
289
		$fileShares->addGroup($share['share_with']);
290
	}
291
292
293
	/**
294
	 * @param array $share
295
	 * @param FileShares $fileShares
296
	 */
297
	private function parseUsersCircles(array $share, FileShares $fileShares) {
298
		if ((int)$share['share_type'] !== Constants::SHARE_TYPE_CIRCLE) {
299
			return;
300
		}
301
302
		$fileShares->addCircle($share['share_with']);
303
	}
304
305
306
	/**
307
	 * @param array $share
308
	 * @param FileShares $fileShares
309
	 */
310
	private function parseUsersLinks(array $share, FileShares $fileShares) {
311
		if ((int)$share['share_type'] !== Constants::SHARE_TYPE_LINK) {
312
			return;
313
		}
314
315
		$fileShares->addLink($share['share_with']);
316
	}
317
318
}
319
320