Completed
Push — master ( c32a94...a966d5 )
by Joas
17:48
created

MailPlugin   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 161
Duplicated Lines 24.84 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
dl 40
loc 161
rs 10
c 0
b 0
f 0
wmc 27
lcom 1
cbo 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 10 10 1
F search() 30 119 24
A isCurrentUser() 0 4 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Contacts\IManager;
31
use OCP\Federation\ICloudId;
32
use OCP\Federation\ICloudIdManager;
33
use OCP\IConfig;
34
use OCP\IGroupManager;
35
use OCP\IUser;
36
use OCP\IUserSession;
37
use OCP\Share;
38
39
class MailPlugin implements ISearchPlugin {
40
	protected $shareeEnumeration;
41
	protected $shareWithGroupOnly;
42
43
	/** @var IManager */
44
	private $contactsManager;
45
	/** @var ICloudIdManager */
46
	private $cloudIdManager;
47
	/** @var IConfig */
48
	private $config;
49
50
	/** @var IGroupManager */
51
	private $groupManager;
52
53
	/** @var IUserSession */
54
	private $userSession;
55
56 View Code Duplication
	public function __construct(IManager $contactsManager, ICloudIdManager $cloudIdManager, IConfig $config, IGroupManager $groupManager, IUserSession $userSession) {
57
		$this->contactsManager = $contactsManager;
58
		$this->cloudIdManager = $cloudIdManager;
59
		$this->config = $config;
60
		$this->groupManager = $groupManager;
61
		$this->userSession = $userSession;
62
63
		$this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes';
64
		$this->shareWithGroupOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes';
65
	}
66
67
	/**
68
	 * @param $search
69
	 * @param $limit
70
	 * @param $offset
71
	 * @param ISearchResult $searchResult
72
	 * @return bool
73
	 * @since 13.0.0
74
	 */
75
	public function search($search, $limit, $offset, ISearchResult $searchResult) {
76
		$result = ['wide' => [], 'exact' => []];
77
		$userType = new SearchResultType('users');
78
		$emailType = new SearchResultType('emails');
79
80
		// Search in contacts
81
		//@todo Pagination missing
82
		$addressBookContacts = $this->contactsManager->search($search, ['EMAIL', 'FN']);
83
		$lowerSearch = strtolower($search);
84
		foreach ($addressBookContacts as $contact) {
85
			if (isset($contact['EMAIL'])) {
86
				$emailAddresses = $contact['EMAIL'];
87
				if (!is_array($emailAddresses)) {
88
					$emailAddresses = [$emailAddresses];
89
				}
90
				foreach ($emailAddresses as $emailAddress) {
91
					$exactEmailMatch = strtolower($emailAddress) === $lowerSearch;
92
93
					if (isset($contact['isLocalSystemBook'])) {
94
						if ($this->shareWithGroupOnly) {
95
							/*
96
							 * Check if the user may share with the user associated with the e-mail of the just found contact
97
							 */
98
							$userGroups = $this->groupManager->getUserGroupIds($this->userSession->getUser());
0 ignored issues
show
Bug introduced by
It seems like $this->userSession->getUser() can be null; however, getUserGroupIds() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
99
							$found = false;
100
							foreach ($userGroups as $userGroup) {
101
								if ($this->groupManager->isInGroup($contact['UID'], $userGroup)) {
102
									$found = true;
103
									break;
104
								}
105
							}
106
							if (!$found) {
107
								continue;
108
							}
109
						}
110
						if ($exactEmailMatch) {
111
							try {
112
								$cloud = $this->cloudIdManager->resolveCloudId($contact['CLOUD'][0]);
113
							} catch (\InvalidArgumentException $e) {
114
								continue;
115
							}
116
117 View Code Duplication
							if (!$this->isCurrentUser($cloud) && !$searchResult->hasResult($userType, $cloud->getUser())) {
118
								$singleResult = [[
119
									'label' => $contact['FN'] . " ($emailAddress)",
120
									'value' => [
121
										'shareType' => Share::SHARE_TYPE_USER,
122
										'shareWith' => $cloud->getUser(),
123
									],
124
								]];
125
								$searchResult->addResultSet($userType, [], $singleResult);
126
								$searchResult->markExactIdMatch($emailType);
127
							}
128
							return false;
129
						}
130
131
						if ($this->shareeEnumeration) {
132
							try {
133
								$cloud = $this->cloudIdManager->resolveCloudId($contact['CLOUD'][0]);
134
							} catch (\InvalidArgumentException $e) {
135
								continue;
136
							}
137
138 View Code Duplication
							if (!$this->isCurrentUser($cloud) && !$searchResult->hasResult($userType, $cloud->getUser())) {
139
								$singleResult = [[
140
									'label' => $contact['FN'] . " ($emailAddress)",
141
									'value' => [
142
										'shareType' => Share::SHARE_TYPE_USER,
143
										'shareWith' => $cloud->getUser(),
144
									]],
145
								];
146
								$searchResult->addResultSet($userType, $singleResult, []);
147
							}
148
						}
149
						continue;
150
					}
151
152
					if ($exactEmailMatch || strtolower($contact['FN']) === $lowerSearch) {
153
						if ($exactEmailMatch) {
154
							$searchResult->markExactIdMatch($emailType);
155
						}
156
						$result['exact'][] = [
157
							'label' => $contact['FN'] . " ($emailAddress)",
158
							'value' => [
159
								'shareType' => Share::SHARE_TYPE_EMAIL,
160
								'shareWith' => $emailAddress,
161
							],
162
						];
163 View Code Duplication
					} else {
164
						$result['wide'][] = [
165
							'label' => $contact['FN'] . " ($emailAddress)",
166
							'value' => [
167
								'shareType' => Share::SHARE_TYPE_EMAIL,
168
								'shareWith' => $emailAddress,
169
							],
170
						];
171
					}
172
				}
173
			}
174
		}
175
176
		if (!$this->shareeEnumeration) {
177
			$result['wide'] = [];
178
		}
179
180
		if (!$searchResult->hasExactIdMatch($emailType) && filter_var($search, FILTER_VALIDATE_EMAIL)) {
181
			$result['exact'][] = [
182
				'label' => $search,
183
				'value' => [
184
					'shareType' => Share::SHARE_TYPE_EMAIL,
185
					'shareWith' => $search,
186
				],
187
			];
188
		}
189
190
		$searchResult->addResultSet($emailType, $result['wide'], $result['exact']);
191
192
		return true;
193
	}
194
195
	public function isCurrentUser(ICloudId $cloud): bool {
196
		$currentUser = $this->userSession->getUser();
197
		return $currentUser instanceof IUser ? $currentUser->getUID() === $cloud->getUser() : false;
198
	}
199
}
200