Completed
Push — master ( 53057f...a0132a )
by Morris
54:48 queued 36:18
created

ContactsStore::contactArrayToEntry()   C

Complexity

Conditions 7
Paths 16

Size

Total Lines 28
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 14
nc 16
nop 1
dl 0
loc 28
rs 6.7272
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright 2017 Christoph Wurst <[email protected]>
4
 * @copyright 2017 Lukas Reschke <[email protected]>
5
 *
6
 * @author 2017 Christoph Wurst <[email protected]>
7
 * @author 2017 Lukas Reschke <[email protected]>
8
 *
9
 * @license GNU AGPL version 3 or any later version
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
 *
24
 */
25
26
namespace OC\Contacts\ContactsMenu;
27
28
use OC\Share\Share;
29
use OCP\Contacts\ContactsMenu\IEntry;
30
use OCP\Contacts\IManager;
31
use OCP\IConfig;
32
use OCP\IGroupManager;
33
use OCP\IUser;
34
use OCP\IUserManager;
35
use OCP\IUserSession;
36
37
class ContactsStore {
38
39
	/** @var IManager */
40
	private $contactsManager;
41
42
	/** @var IConfig */
43
	private $config;
44
45
	/** @var IUserManager */
46
	private $userManager;
47
48
	/** @var IGroupManager */
49
	private $groupManager;
50
51
	/**
52
	 * @param IManager $contactsManager
53
	 * @param IConfig $config
54
	 * @param IUserManager $userManager
55
	 * @param IGroupManager $groupManager
56
	 */
57
	public function __construct(IManager $contactsManager,
58
								IConfig $config,
59
								IUserManager $userManager,
60
								IGroupManager $groupManager) {
61
		$this->contactsManager = $contactsManager;
62
		$this->config = $config;
63
		$this->userManager = $userManager;
64
		$this->groupManager = $groupManager;
65
	}
66
67
	/**
68
	 * @param IUser $user
69
	 * @param string|null $filter
70
	 * @return IEntry[]
71
	 */
72
	public function getContacts(IUser $user, $filter) {
73
		$allContacts = $this->contactsManager->search($filter ?: '', [
74
			'FN',
75
			'EMAIL'
76
		]);
77
78
		$entries = array_map(function(array $contact) {
79
			return $this->contactArrayToEntry($contact);
80
		}, $allContacts);
81
		return $this->filterContacts(
82
			$user,
83
			$entries,
84
			$filter
85
		);
86
	}
87
88
	/**
89
	 * Filters the contacts. Applies 3 filters:
90
	 *  1. filter the current user
91
	 *  2. if the `shareapi_allow_share_dialog_user_enumeration` config option is
92
	 * enabled it will filter all local users
93
	 *  3. if the `shareapi_exclude_groups` config option is enabled and the
94
	 * current user is in an excluded group it will filter all local users.
95
	 *  4. if the `shareapi_only_share_with_group_members` config option is
96
	 * enabled it will filter all users which doens't have a common group
97
	 * with the current user.
98
	 *
99
	 * @param IUser $self
100
	 * @param Entry[] $entries
101
	 * @param string $filter
102
	 * @return Entry[] the filtered contacts
103
	 */
104
	private function filterContacts(IUser $self,
105
									array $entries,
106
									$filter) {
107
		$disallowEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') !== 'yes';
108
		$excludedGroups = $this->config->getAppValue('core', 'shareapi_exclude_groups', 'no') === 'yes';
109
110
		// whether to filter out local users
111
		$skipLocal = false;
112
		// whether to filter out all users which doesn't have the same group as the current user
113
		$ownGroupsOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes';
114
115
		$selfGroups = $this->groupManager->getUserGroupIds($self);
116
117
		if ($excludedGroups) {
118
			$excludedGroups = $this->config->getAppValue('core', 'shareapi_exclude_groups_list', '');
119
			$decodedExcludeGroups = json_decode($excludedGroups, true);
120
			$excludeGroupsList = ($decodedExcludeGroups !== null) ? $decodedExcludeGroups :  [];
121
122
			if (count(array_intersect($excludeGroupsList, $selfGroups)) !== 0) {
123
				// a group of the current user is excluded -> filter all local users
124
				$skipLocal = true;
125
			}
126
		}
127
128
		$selfUID = $self->getUID();
129
130
		return array_values(array_filter($entries, function(IEntry $entry) use ($self, $skipLocal, $ownGroupsOnly, $selfGroups, $selfUID, $disallowEnumeration, $filter) {
131
			if ($skipLocal && $entry->getProperty('isLocalSystemBook') === true) {
132
				return false;
133
			}
134
135
			// Prevent enumerating local users
136
			if($disallowEnumeration && $entry->getProperty('isLocalSystemBook')) {
137
				$filterUser = true;
138
139
				$mailAddresses = $entry->getEMailAddresses();
140
				foreach($mailAddresses as $mailAddress) {
141
					if($mailAddress === $filter) {
142
						$filterUser = false;
143
						break;
144
					}
145
				}
146
147
				if($entry->getProperty('UID') && $entry->getProperty('UID') === $filter) {
148
					$filterUser = false;
149
				}
150
151
				if($filterUser) {
152
					return false;
153
				}
154
			}
155
156
			if ($ownGroupsOnly && $entry->getProperty('isLocalSystemBook') === true) {
157
				$contactGroups = $this->groupManager->getUserGroupIds($this->userManager->get($entry->getProperty('UID')));
0 ignored issues
show
Documentation introduced by
$entry->getProperty('UID') is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Bug introduced by
It seems like $this->userManager->get(...ry->getProperty('UID')) 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...
158
				if (count(array_intersect($contactGroups, $selfGroups)) === 0) {
159
					// no groups in common, so shouldn't see the contact
160
					return false;
161
				}
162
			}
163
164
			return $entry->getProperty('UID') !== $selfUID;
165
		}));
166
	}
167
168
	/**
169
	 * @param IUser $user
170
	 * @param integer $shareType
171
	 * @param string $shareWith
172
	 * @return IEntry|null
173
	 */
174
	public function findOne(IUser $user, $shareType, $shareWith) {
175
		switch($shareType) {
176
			case 0:
177
			case 6:
178
				$filter = ['UID'];
179
				break;
180
			case 4:
181
				$filter = ['EMAIL'];
182
				break;
183
			default:
184
				return null;
185
		}
186
187
		$userId = $user->getUID();
188
		$allContacts = $this->contactsManager->search($shareWith, $filter);
189
		$contacts = array_filter($allContacts, function($contact) use ($userId) {
190
			return $contact['UID'] !== $userId;
191
		});
192
		$match = null;
193
194
		foreach ($contacts as $contact) {
195
			if ($shareType === 4 && isset($contact['EMAIL'])) {
196
				if (in_array($shareWith, $contact['EMAIL'])) {
197
					$match = $contact;
198
					break;
199
				}
200
			}
201
			if ($shareType === 0 || $shareType === 6) {
202
				if ($contact['UID'] === $shareWith && $contact['isLocalSystemBook'] === true) {
203
					$match = $contact;
204
					break;
205
				}
206
			}
207
		}
208
209
		if ($match) {
210
			$match = $this->filterContacts($user, [$this->contactArrayToEntry($match)], $shareWith);
211
			if (count($match) === 1) {
212
				$match = $match[0];
213
			} else {
214
				$match = null;
215
			}
216
217
		}
218
219
		return $match;
220
	}
221
222
	/**
223
	 * @param array $contact
224
	 * @return Entry
225
	 */
226
	private function contactArrayToEntry(array $contact) {
227
		$entry = new Entry();
228
229
		if (isset($contact['id'])) {
230
			$entry->setId($contact['id']);
231
		}
232
233
		if (isset($contact['FN'])) {
234
			$entry->setFullName($contact['FN']);
235
		}
236
237
		$avatarPrefix = "VALUE=uri:";
238
		if (isset($contact['PHOTO']) && strpos($contact['PHOTO'], $avatarPrefix) === 0) {
239
			$entry->setAvatar(substr($contact['PHOTO'], strlen($avatarPrefix)));
240
		}
241
242
		if (isset($contact['EMAIL'])) {
243
			foreach ($contact['EMAIL'] as $email) {
244
				$entry->addEMailAddress($email);
245
			}
246
		}
247
248
		// Attach all other properties to the entry too because some
249
		// providers might make use of it.
250
		$entry->setProperties($contact);
251
252
		return $entry;
253
	}
254
255
}
256