Completed
Push — master ( a927d2...6b440c )
by Joas
16:42
created
lib/private/Contacts/ContactsMenu/ContactsStore.php 1 patch
Indentation   +222 added lines, -222 removed lines patch added patch discarded remove patch
@@ -37,227 +37,227 @@
 block discarded – undo
37 37
 
38 38
 class ContactsStore implements IContactsStore {
39 39
 
40
-	/** @var IManager */
41
-	private $contactsManager;
42
-
43
-	/** @var IConfig */
44
-	private $config;
45
-
46
-	/** @var IUserManager */
47
-	private $userManager;
48
-
49
-	/** @var IGroupManager */
50
-	private $groupManager;
51
-
52
-	/**
53
-	 * @param IManager $contactsManager
54
-	 * @param IConfig $config
55
-	 * @param IUserManager $userManager
56
-	 * @param IGroupManager $groupManager
57
-	 */
58
-	public function __construct(IManager $contactsManager,
59
-								IConfig $config,
60
-								IUserManager $userManager,
61
-								IGroupManager $groupManager) {
62
-		$this->contactsManager = $contactsManager;
63
-		$this->config = $config;
64
-		$this->userManager = $userManager;
65
-		$this->groupManager = $groupManager;
66
-	}
67
-
68
-	/**
69
-	 * @param IUser $user
70
-	 * @param string|null $filter
71
-	 * @return IEntry[]
72
-	 */
73
-	public function getContacts(IUser $user, $filter) {
74
-		$allContacts = $this->contactsManager->search($filter ?: '', [
75
-			'FN',
76
-			'EMAIL'
77
-		]);
78
-
79
-		$entries = array_map(function(array $contact) {
80
-			return $this->contactArrayToEntry($contact);
81
-		}, $allContacts);
82
-		return $this->filterContacts(
83
-			$user,
84
-			$entries,
85
-			$filter
86
-		);
87
-	}
88
-
89
-	/**
90
-	 * Filters the contacts. Applies 3 filters:
91
-	 *  1. filter the current user
92
-	 *  2. if the `shareapi_allow_share_dialog_user_enumeration` config option is
93
-	 * enabled it will filter all local users
94
-	 *  3. if the `shareapi_exclude_groups` config option is enabled and the
95
-	 * current user is in an excluded group it will filter all local users.
96
-	 *  4. if the `shareapi_only_share_with_group_members` config option is
97
-	 * enabled it will filter all users which doens't have a common group
98
-	 * with the current user.
99
-	 *
100
-	 * @param IUser $self
101
-	 * @param Entry[] $entries
102
-	 * @param string $filter
103
-	 * @return Entry[] the filtered contacts
104
-	 */
105
-	private function filterContacts(IUser $self,
106
-									array $entries,
107
-									$filter) {
108
-		$disallowEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') !== 'yes';
109
-		$excludedGroups = $this->config->getAppValue('core', 'shareapi_exclude_groups', 'no') === 'yes';
110
-
111
-		// whether to filter out local users
112
-		$skipLocal = false;
113
-		// whether to filter out all users which doesn't have the same group as the current user
114
-		$ownGroupsOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes';
115
-
116
-		$selfGroups = $this->groupManager->getUserGroupIds($self);
117
-
118
-		if ($excludedGroups) {
119
-			$excludedGroups = $this->config->getAppValue('core', 'shareapi_exclude_groups_list', '');
120
-			$decodedExcludeGroups = json_decode($excludedGroups, true);
121
-			$excludeGroupsList = ($decodedExcludeGroups !== null) ? $decodedExcludeGroups :  [];
122
-
123
-			if (count(array_intersect($excludeGroupsList, $selfGroups)) !== 0) {
124
-				// a group of the current user is excluded -> filter all local users
125
-				$skipLocal = true;
126
-			}
127
-		}
128
-
129
-		$selfUID = $self->getUID();
130
-
131
-		return array_values(array_filter($entries, function(IEntry $entry) use ($self, $skipLocal, $ownGroupsOnly, $selfGroups, $selfUID, $disallowEnumeration, $filter) {
132
-			if ($skipLocal && $entry->getProperty('isLocalSystemBook') === true) {
133
-				return false;
134
-			}
135
-
136
-			// Prevent enumerating local users
137
-			if($disallowEnumeration && $entry->getProperty('isLocalSystemBook')) {
138
-				$filterUser = true;
139
-
140
-				$mailAddresses = $entry->getEMailAddresses();
141
-				foreach($mailAddresses as $mailAddress) {
142
-					if($mailAddress === $filter) {
143
-						$filterUser = false;
144
-						break;
145
-					}
146
-				}
147
-
148
-				if($entry->getProperty('UID') && $entry->getProperty('UID') === $filter) {
149
-					$filterUser = false;
150
-				}
151
-
152
-				if($filterUser) {
153
-					return false;
154
-				}
155
-			}
156
-
157
-			if ($ownGroupsOnly && $entry->getProperty('isLocalSystemBook') === true) {
158
-				$uid = $this->userManager->get($entry->getProperty('UID'));
159
-
160
-				if ($uid === NULL) {
161
-					return false;
162
-				}
163
-
164
-				$contactGroups = $this->groupManager->getUserGroupIds($uid);
165
-				if (count(array_intersect($contactGroups, $selfGroups)) === 0) {
166
-					// no groups in common, so shouldn't see the contact
167
-					return false;
168
-				}
169
-			}
170
-
171
-			return $entry->getProperty('UID') !== $selfUID;
172
-		}));
173
-	}
174
-
175
-	/**
176
-	 * @param IUser $user
177
-	 * @param integer $shareType
178
-	 * @param string $shareWith
179
-	 * @return IEntry|null
180
-	 */
181
-	public function findOne(IUser $user, $shareType, $shareWith) {
182
-		switch($shareType) {
183
-			case 0:
184
-			case 6:
185
-				$filter = ['UID'];
186
-				break;
187
-			case 4:
188
-				$filter = ['EMAIL'];
189
-				break;
190
-			default:
191
-				return null;
192
-		}
193
-
194
-		$userId = $user->getUID();
195
-		$allContacts = $this->contactsManager->search($shareWith, $filter);
196
-		$contacts = array_filter($allContacts, function($contact) use ($userId) {
197
-			return $contact['UID'] !== $userId;
198
-		});
199
-		$match = null;
200
-
201
-		foreach ($contacts as $contact) {
202
-			if ($shareType === 4 && isset($contact['EMAIL'])) {
203
-				if (in_array($shareWith, $contact['EMAIL'])) {
204
-					$match = $contact;
205
-					break;
206
-				}
207
-			}
208
-			if ($shareType === 0 || $shareType === 6) {
209
-				$isLocal = $contact['isLocalSystemBook'] ?? false;
210
-				if ($contact['UID'] === $shareWith && $isLocal === true) {
211
-					$match = $contact;
212
-					break;
213
-				}
214
-			}
215
-		}
216
-
217
-		if ($match) {
218
-			$match = $this->filterContacts($user, [$this->contactArrayToEntry($match)], $shareWith);
219
-			if (count($match) === 1) {
220
-				$match = $match[0];
221
-			} else {
222
-				$match = null;
223
-			}
224
-
225
-		}
226
-
227
-		return $match;
228
-	}
229
-
230
-	/**
231
-	 * @param array $contact
232
-	 * @return Entry
233
-	 */
234
-	private function contactArrayToEntry(array $contact) {
235
-		$entry = new Entry();
236
-
237
-		if (isset($contact['id'])) {
238
-			$entry->setId($contact['id']);
239
-		}
240
-
241
-		if (isset($contact['FN'])) {
242
-			$entry->setFullName($contact['FN']);
243
-		}
244
-
245
-		$avatarPrefix = "VALUE=uri:";
246
-		if (isset($contact['PHOTO']) && strpos($contact['PHOTO'], $avatarPrefix) === 0) {
247
-			$entry->setAvatar(substr($contact['PHOTO'], strlen($avatarPrefix)));
248
-		}
249
-
250
-		if (isset($contact['EMAIL'])) {
251
-			foreach ($contact['EMAIL'] as $email) {
252
-				$entry->addEMailAddress($email);
253
-			}
254
-		}
255
-
256
-		// Attach all other properties to the entry too because some
257
-		// providers might make use of it.
258
-		$entry->setProperties($contact);
259
-
260
-		return $entry;
261
-	}
40
+    /** @var IManager */
41
+    private $contactsManager;
42
+
43
+    /** @var IConfig */
44
+    private $config;
45
+
46
+    /** @var IUserManager */
47
+    private $userManager;
48
+
49
+    /** @var IGroupManager */
50
+    private $groupManager;
51
+
52
+    /**
53
+     * @param IManager $contactsManager
54
+     * @param IConfig $config
55
+     * @param IUserManager $userManager
56
+     * @param IGroupManager $groupManager
57
+     */
58
+    public function __construct(IManager $contactsManager,
59
+                                IConfig $config,
60
+                                IUserManager $userManager,
61
+                                IGroupManager $groupManager) {
62
+        $this->contactsManager = $contactsManager;
63
+        $this->config = $config;
64
+        $this->userManager = $userManager;
65
+        $this->groupManager = $groupManager;
66
+    }
67
+
68
+    /**
69
+     * @param IUser $user
70
+     * @param string|null $filter
71
+     * @return IEntry[]
72
+     */
73
+    public function getContacts(IUser $user, $filter) {
74
+        $allContacts = $this->contactsManager->search($filter ?: '', [
75
+            'FN',
76
+            'EMAIL'
77
+        ]);
78
+
79
+        $entries = array_map(function(array $contact) {
80
+            return $this->contactArrayToEntry($contact);
81
+        }, $allContacts);
82
+        return $this->filterContacts(
83
+            $user,
84
+            $entries,
85
+            $filter
86
+        );
87
+    }
88
+
89
+    /**
90
+     * Filters the contacts. Applies 3 filters:
91
+     *  1. filter the current user
92
+     *  2. if the `shareapi_allow_share_dialog_user_enumeration` config option is
93
+     * enabled it will filter all local users
94
+     *  3. if the `shareapi_exclude_groups` config option is enabled and the
95
+     * current user is in an excluded group it will filter all local users.
96
+     *  4. if the `shareapi_only_share_with_group_members` config option is
97
+     * enabled it will filter all users which doens't have a common group
98
+     * with the current user.
99
+     *
100
+     * @param IUser $self
101
+     * @param Entry[] $entries
102
+     * @param string $filter
103
+     * @return Entry[] the filtered contacts
104
+     */
105
+    private function filterContacts(IUser $self,
106
+                                    array $entries,
107
+                                    $filter) {
108
+        $disallowEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') !== 'yes';
109
+        $excludedGroups = $this->config->getAppValue('core', 'shareapi_exclude_groups', 'no') === 'yes';
110
+
111
+        // whether to filter out local users
112
+        $skipLocal = false;
113
+        // whether to filter out all users which doesn't have the same group as the current user
114
+        $ownGroupsOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes';
115
+
116
+        $selfGroups = $this->groupManager->getUserGroupIds($self);
117
+
118
+        if ($excludedGroups) {
119
+            $excludedGroups = $this->config->getAppValue('core', 'shareapi_exclude_groups_list', '');
120
+            $decodedExcludeGroups = json_decode($excludedGroups, true);
121
+            $excludeGroupsList = ($decodedExcludeGroups !== null) ? $decodedExcludeGroups :  [];
122
+
123
+            if (count(array_intersect($excludeGroupsList, $selfGroups)) !== 0) {
124
+                // a group of the current user is excluded -> filter all local users
125
+                $skipLocal = true;
126
+            }
127
+        }
128
+
129
+        $selfUID = $self->getUID();
130
+
131
+        return array_values(array_filter($entries, function(IEntry $entry) use ($self, $skipLocal, $ownGroupsOnly, $selfGroups, $selfUID, $disallowEnumeration, $filter) {
132
+            if ($skipLocal && $entry->getProperty('isLocalSystemBook') === true) {
133
+                return false;
134
+            }
135
+
136
+            // Prevent enumerating local users
137
+            if($disallowEnumeration && $entry->getProperty('isLocalSystemBook')) {
138
+                $filterUser = true;
139
+
140
+                $mailAddresses = $entry->getEMailAddresses();
141
+                foreach($mailAddresses as $mailAddress) {
142
+                    if($mailAddress === $filter) {
143
+                        $filterUser = false;
144
+                        break;
145
+                    }
146
+                }
147
+
148
+                if($entry->getProperty('UID') && $entry->getProperty('UID') === $filter) {
149
+                    $filterUser = false;
150
+                }
151
+
152
+                if($filterUser) {
153
+                    return false;
154
+                }
155
+            }
156
+
157
+            if ($ownGroupsOnly && $entry->getProperty('isLocalSystemBook') === true) {
158
+                $uid = $this->userManager->get($entry->getProperty('UID'));
159
+
160
+                if ($uid === NULL) {
161
+                    return false;
162
+                }
163
+
164
+                $contactGroups = $this->groupManager->getUserGroupIds($uid);
165
+                if (count(array_intersect($contactGroups, $selfGroups)) === 0) {
166
+                    // no groups in common, so shouldn't see the contact
167
+                    return false;
168
+                }
169
+            }
170
+
171
+            return $entry->getProperty('UID') !== $selfUID;
172
+        }));
173
+    }
174
+
175
+    /**
176
+     * @param IUser $user
177
+     * @param integer $shareType
178
+     * @param string $shareWith
179
+     * @return IEntry|null
180
+     */
181
+    public function findOne(IUser $user, $shareType, $shareWith) {
182
+        switch($shareType) {
183
+            case 0:
184
+            case 6:
185
+                $filter = ['UID'];
186
+                break;
187
+            case 4:
188
+                $filter = ['EMAIL'];
189
+                break;
190
+            default:
191
+                return null;
192
+        }
193
+
194
+        $userId = $user->getUID();
195
+        $allContacts = $this->contactsManager->search($shareWith, $filter);
196
+        $contacts = array_filter($allContacts, function($contact) use ($userId) {
197
+            return $contact['UID'] !== $userId;
198
+        });
199
+        $match = null;
200
+
201
+        foreach ($contacts as $contact) {
202
+            if ($shareType === 4 && isset($contact['EMAIL'])) {
203
+                if (in_array($shareWith, $contact['EMAIL'])) {
204
+                    $match = $contact;
205
+                    break;
206
+                }
207
+            }
208
+            if ($shareType === 0 || $shareType === 6) {
209
+                $isLocal = $contact['isLocalSystemBook'] ?? false;
210
+                if ($contact['UID'] === $shareWith && $isLocal === true) {
211
+                    $match = $contact;
212
+                    break;
213
+                }
214
+            }
215
+        }
216
+
217
+        if ($match) {
218
+            $match = $this->filterContacts($user, [$this->contactArrayToEntry($match)], $shareWith);
219
+            if (count($match) === 1) {
220
+                $match = $match[0];
221
+            } else {
222
+                $match = null;
223
+            }
224
+
225
+        }
226
+
227
+        return $match;
228
+    }
229
+
230
+    /**
231
+     * @param array $contact
232
+     * @return Entry
233
+     */
234
+    private function contactArrayToEntry(array $contact) {
235
+        $entry = new Entry();
236
+
237
+        if (isset($contact['id'])) {
238
+            $entry->setId($contact['id']);
239
+        }
240
+
241
+        if (isset($contact['FN'])) {
242
+            $entry->setFullName($contact['FN']);
243
+        }
244
+
245
+        $avatarPrefix = "VALUE=uri:";
246
+        if (isset($contact['PHOTO']) && strpos($contact['PHOTO'], $avatarPrefix) === 0) {
247
+            $entry->setAvatar(substr($contact['PHOTO'], strlen($avatarPrefix)));
248
+        }
249
+
250
+        if (isset($contact['EMAIL'])) {
251
+            foreach ($contact['EMAIL'] as $email) {
252
+                $entry->addEMailAddress($email);
253
+            }
254
+        }
255
+
256
+        // Attach all other properties to the entry too because some
257
+        // providers might make use of it.
258
+        $entry->setProperties($contact);
259
+
260
+        return $entry;
261
+    }
262 262
 
263 263
 }
Please login to merge, or discard this patch.