Passed
Push — master ( 1a9f55...7c7f0d )
by Roeland
12:55 queued 10s
created

RemoteGroupPlugin::splitGroupRemote()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 3
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Arthur Schiwon <[email protected]>
4
 *
5
 * @author Arthur Schiwon <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
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
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OC\Collaboration\Collaborators;
25
26
27
use OCP\Collaboration\Collaborators\ISearchPlugin;
28
use OCP\Collaboration\Collaborators\ISearchResult;
29
use OCP\Collaboration\Collaborators\SearchResultType;
30
use OCP\Federation\ICloudFederationProviderManager;
31
use OCP\Federation\ICloudIdManager;
32
use OCP\Share;
33
34
class RemoteGroupPlugin implements ISearchPlugin {
35
	protected $shareeEnumeration;
36
37
	/** @var ICloudIdManager */
38
	private $cloudIdManager;
39
	/** @var bool */
40
	private $enabled = false;
41
42
	public function __construct(ICloudFederationProviderManager $cloudFederationProviderManager, ICloudIdManager $cloudIdManager) {
43
		try {
44
			$fileSharingProvider = $cloudFederationProviderManager->getCloudFederationProvider('file');
45
			$supportedShareTypes = $fileSharingProvider->getSupportedShareTypes();
46
			if (in_array('group', $supportedShareTypes)) {
47
				$this->enabled = true;
48
			}
49
		} catch (\Exception $e) {
50
			// do nothing, just don't enable federated group shares
51
		}
52
		$this->cloudIdManager = $cloudIdManager;
53
	}
54
55
	public function search($search, $limit, $offset, ISearchResult $searchResult) {
56
		$result = ['wide' => [], 'exact' => []];
57
		$resultType = new SearchResultType('remote_groups');
58
59
		if ($this->enabled && $this->cloudIdManager->isValidCloudId($search) && $offset === 0) {
60
			list($remoteGroup, $serverUrl) = $this->splitGroupRemote($search);
61
			$result['exact'][] = [
62
				'label' => $remoteGroup . " ($serverUrl)",
63
				'guid' => $remoteGroup,
64
				'name' => $remoteGroup,
65
				'value' => [
66
					'shareType' => Share::SHARE_TYPE_REMOTE_GROUP,
0 ignored issues
show
Deprecated Code introduced by
The constant OC\Share\Constants::SHARE_TYPE_REMOTE_GROUP has been deprecated: 17.0.0 - use IShare::REMOTE_GROUP instead ( Ignorable by Annotation )

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

66
					'shareType' => /** @scrutinizer ignore-deprecated */ Share::SHARE_TYPE_REMOTE_GROUP,

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
67
					'shareWith' => $search,
68
					'server' => $serverUrl,
69
				],
70
			];
71
		}
72
73
		$searchResult->addResultSet($resultType, $result['wide'], $result['exact']);
74
75
		return true;
76
	}
77
78
	/**
79
	 * split group and remote from federated cloud id
80
	 *
81
	 * @param string $address federated share address
82
	 * @return array [user, remoteURL]
83
	 * @throws \InvalidArgumentException
84
	 */
85
	public function splitGroupRemote($address) {
86
		try {
87
			$cloudId = $this->cloudIdManager->resolveCloudId($address);
88
			return [$cloudId->getUser(), $cloudId->getRemote()];
89
		} catch (\InvalidArgumentException $e) {
90
			throw new \InvalidArgumentException('Invalid Federated Cloud ID', 0, $e);
91
		}
92
	}
93
94
}
95