Completed
Pull Request — master (#5585)
by Lukas
14:35
created
lib/private/Contacts/ContactsMenu/ContactsStore.php 4 patches
Unused Use Statements   -2 removed lines patch added patch discarded remove patch
@@ -24,14 +24,12 @@
 block discarded – undo
24 24
 
25 25
 namespace OC\Contacts\ContactsMenu;
26 26
 
27
-use OC\Share\Share;
28 27
 use OCP\Contacts\ContactsMenu\IEntry;
29 28
 use OCP\Contacts\IManager;
30 29
 use OCP\IConfig;
31 30
 use OCP\IGroupManager;
32 31
 use OCP\IUser;
33 32
 use OCP\IUserManager;
34
-use OCP\IUserSession;
35 33
 
36 34
 class ContactsStore {
37 35
 
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -99,7 +99,7 @@  discard block
 block discarded – undo
99 99
 		if ($excludedGroups) {
100 100
 			$excludedGroups = $this->config->getAppValue('core', 'shareapi_exclude_groups_list', '');
101 101
 			$decodedExcludeGroups = json_decode($excludedGroups, true);
102
-			$excludeGroupsList = !is_null($decodedExcludeGroups) ? $decodedExcludeGroups :  [];
102
+			$excludeGroupsList = !is_null($decodedExcludeGroups) ? $decodedExcludeGroups : [];
103 103
 
104 104
 			if (count(array_intersect($excludeGroupsList, $selfGroups)) !== 0) {
105 105
 				// a group of the current user is excluded -> filter all local users
@@ -136,7 +136,7 @@  discard block
 block discarded – undo
136 136
 	 * @return IEntry|null
137 137
 	 */
138 138
 	public function findOne(IUser $user, $shareType, $shareWith) {
139
-		switch($shareType) {
139
+		switch ($shareType) {
140 140
 			case 0:
141 141
 			case 6:
142 142
 				$filter = ['UID'];
Please login to merge, or discard this patch.
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -63,7 +63,7 @@
 block discarded – undo
63 63
 	/**
64 64
 	 * @param IUser $user
65 65
 	 * @param string|null $filter
66
-	 * @return IEntry[]
66
+	 * @return Entry[]
67 67
 	 */
68 68
 	public function getContacts(IUser $user, $filter) {
69 69
 		$allContacts = $this->contactsManager->search($filter ?: '', [
Please login to merge, or discard this patch.
Indentation   +182 added lines, -182 removed lines patch added patch discarded remove patch
@@ -35,187 +35,187 @@
 block discarded – undo
35 35
 
36 36
 class ContactsStore {
37 37
 
38
-	/** @var IManager */
39
-	private $contactsManager;
40
-
41
-	/** @var IConfig */
42
-	private $config;
43
-
44
-	/** @var IUserManager */
45
-	private $userManager;
46
-
47
-	/** @var IGroupManager */
48
-	private $groupManager;
49
-
50
-	/**
51
-	 * @param IManager $contactsManager
52
-	 * @param IConfig $config
53
-	 * @param IUserManager $userManager
54
-	 * @param IGroupManager $groupManager
55
-	 */
56
-	public function __construct(IManager $contactsManager, IConfig $config, IUserManager $userManager, IGroupManager $groupManager) {
57
-		$this->contactsManager = $contactsManager;
58
-		$this->config = $config;
59
-		$this->userManager = $userManager;
60
-		$this->groupManager = $groupManager;
61
-	}
62
-
63
-	/**
64
-	 * @param IUser $user
65
-	 * @param string|null $filter
66
-	 * @return IEntry[]
67
-	 */
68
-	public function getContacts(IUser $user, $filter) {
69
-		$allContacts = $this->contactsManager->search($filter ?: '', [
70
-			'FN',
71
-		]);
72
-
73
-		$entries = array_map(function(array $contact) {
74
-			return $this->contactArrayToEntry($contact);
75
-		}, $allContacts);
76
-		return $this->filterContacts($user, $entries);
77
-	}
78
-
79
-	/**
80
-	 * @brief filters the contacts. Applies 3 filters:
81
-	 *  1. filter the current user
82
-	 *  2. if the `shareapi_exclude_groups` config option is enabled and the
83
-	 * current user is in an excluded group it will filter all local users.
84
-	 *  3. if the `shareapi_only_share_with_group_members` config option is
85
-	 * enabled it will filter all users which doens't have a common group
86
-	 * with the current user.
87
-	 * @param IUser $self
88
-	 * @param Entry[] $entries
89
-	 * @return Entry[] the filtered contacts
90
-	 */
91
-	private function filterContacts(IUser $self, array $entries) {
92
-		$excludedGroups = $this->config->getAppValue('core', 'shareapi_exclude_groups', 'no') === 'yes';
93
-
94
-		// whether to filter out local users
95
-		$skipLocal = false;
96
-		// whether to filter out all users which doesn't have the same group as the current user
97
-		$ownGroupsOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes';
98
-
99
-		$selfGroups = $this->groupManager->getUserGroupIds($self);
100
-
101
-		if ($excludedGroups) {
102
-			$excludedGroups = $this->config->getAppValue('core', 'shareapi_exclude_groups_list', '');
103
-			$decodedExcludeGroups = json_decode($excludedGroups, true);
104
-			$excludeGroupsList = !is_null($decodedExcludeGroups) ? $decodedExcludeGroups :  [];
105
-
106
-			if (count(array_intersect($excludeGroupsList, $selfGroups)) !== 0) {
107
-				// a group of the current user is excluded -> filter all local users
108
-				$skipLocal = true;
109
-			}
110
-		}
111
-
112
-		$selfUID = $self->getUID();
113
-
114
-		return array_filter($entries, function(IEntry $entry) use ($self, $skipLocal, $ownGroupsOnly, $selfGroups, $selfUID) {
115
-
116
-			if ($skipLocal && $entry->getProperty('isLocalSystemBook') === true) {
117
-				return false;
118
-			}
119
-
120
-			if ($ownGroupsOnly && $entry->getProperty('isLocalSystemBook') === true) {
121
-				$contactGroups = $this->groupManager->getUserGroupIds($this->userManager->get($entry->getProperty('UID')));
122
-				if (count(array_intersect($contactGroups, $selfGroups)) === 0) {
123
-					// no groups in common, so shouldn't see the contact
124
-					return false;
125
-				}
126
-			}
127
-
128
-			return $entry->getProperty('UID') !== $selfUID;
129
-		});
130
-
131
-
132
-	}
133
-
134
-	/**
135
-	 * @param IUser $user
136
-	 * @param integer $shareType
137
-	 * @param string $shareWith
138
-	 * @return IEntry|null
139
-	 */
140
-	public function findOne(IUser $user, $shareType, $shareWith) {
141
-		switch($shareType) {
142
-			case 0:
143
-			case 6:
144
-				$filter = ['UID'];
145
-				break;
146
-			case 4:
147
-				$filter = ['EMAIL'];
148
-				break;
149
-			default:
150
-				return null;
151
-		}
152
-
153
-		$userId = $user->getUID();
154
-		$allContacts = $this->contactsManager->search($shareWith, $filter);
155
-		$contacts = array_filter($allContacts, function($contact) use ($userId) {
156
-			return $contact['UID'] !== $userId;
157
-		});
158
-		$match = null;
159
-
160
-		foreach ($contacts as $contact) {
161
-			if ($shareType === 4 && isset($contact['EMAIL'])) {
162
-				if (in_array($shareWith, $contact['EMAIL'])) {
163
-					$match = $contact;
164
-					break;
165
-				}
166
-			}
167
-			if ($shareType === 0 || $shareType === 6) {
168
-				if ($contact['UID'] === $shareWith && $contact['isLocalSystemBook'] === true) {
169
-					$match = $contact;
170
-					break;
171
-				}
172
-			}
173
-		}
174
-
175
-		if ($match) {
176
-			$match = $this->filterContacts($user, [$this->contactArrayToEntry($match)]);
177
-			if (count($match) === 1) {
178
-				$match = $match[0];
179
-			} else {
180
-				$match = null;
181
-			}
182
-
183
-		}
184
-
185
-		return $match;
186
-	}
187
-
188
-	/**
189
-	 * @param array $contact
190
-	 * @return Entry
191
-	 */
192
-	private function contactArrayToEntry(array $contact) {
193
-		$entry = new Entry();
194
-
195
-		if (isset($contact['id'])) {
196
-			$entry->setId($contact['id']);
197
-		}
198
-
199
-		if (isset($contact['FN'])) {
200
-			$entry->setFullName($contact['FN']);
201
-		}
202
-
203
-		$avatarPrefix = "VALUE=uri:";
204
-		if (isset($contact['PHOTO']) && strpos($contact['PHOTO'], $avatarPrefix) === 0) {
205
-			$entry->setAvatar(substr($contact['PHOTO'], strlen($avatarPrefix)));
206
-		}
207
-
208
-		if (isset($contact['EMAIL'])) {
209
-			foreach ($contact['EMAIL'] as $email) {
210
-				$entry->addEMailAddress($email);
211
-			}
212
-		}
213
-
214
-		// Attach all other properties to the entry too because some
215
-		// providers might make use of it.
216
-		$entry->setProperties($contact);
217
-
218
-		return $entry;
219
-	}
38
+    /** @var IManager */
39
+    private $contactsManager;
40
+
41
+    /** @var IConfig */
42
+    private $config;
43
+
44
+    /** @var IUserManager */
45
+    private $userManager;
46
+
47
+    /** @var IGroupManager */
48
+    private $groupManager;
49
+
50
+    /**
51
+     * @param IManager $contactsManager
52
+     * @param IConfig $config
53
+     * @param IUserManager $userManager
54
+     * @param IGroupManager $groupManager
55
+     */
56
+    public function __construct(IManager $contactsManager, IConfig $config, IUserManager $userManager, IGroupManager $groupManager) {
57
+        $this->contactsManager = $contactsManager;
58
+        $this->config = $config;
59
+        $this->userManager = $userManager;
60
+        $this->groupManager = $groupManager;
61
+    }
62
+
63
+    /**
64
+     * @param IUser $user
65
+     * @param string|null $filter
66
+     * @return IEntry[]
67
+     */
68
+    public function getContacts(IUser $user, $filter) {
69
+        $allContacts = $this->contactsManager->search($filter ?: '', [
70
+            'FN',
71
+        ]);
72
+
73
+        $entries = array_map(function(array $contact) {
74
+            return $this->contactArrayToEntry($contact);
75
+        }, $allContacts);
76
+        return $this->filterContacts($user, $entries);
77
+    }
78
+
79
+    /**
80
+     * @brief filters the contacts. Applies 3 filters:
81
+     *  1. filter the current user
82
+     *  2. if the `shareapi_exclude_groups` config option is enabled and the
83
+     * current user is in an excluded group it will filter all local users.
84
+     *  3. if the `shareapi_only_share_with_group_members` config option is
85
+     * enabled it will filter all users which doens't have a common group
86
+     * with the current user.
87
+     * @param IUser $self
88
+     * @param Entry[] $entries
89
+     * @return Entry[] the filtered contacts
90
+     */
91
+    private function filterContacts(IUser $self, array $entries) {
92
+        $excludedGroups = $this->config->getAppValue('core', 'shareapi_exclude_groups', 'no') === 'yes';
93
+
94
+        // whether to filter out local users
95
+        $skipLocal = false;
96
+        // whether to filter out all users which doesn't have the same group as the current user
97
+        $ownGroupsOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes';
98
+
99
+        $selfGroups = $this->groupManager->getUserGroupIds($self);
100
+
101
+        if ($excludedGroups) {
102
+            $excludedGroups = $this->config->getAppValue('core', 'shareapi_exclude_groups_list', '');
103
+            $decodedExcludeGroups = json_decode($excludedGroups, true);
104
+            $excludeGroupsList = !is_null($decodedExcludeGroups) ? $decodedExcludeGroups :  [];
105
+
106
+            if (count(array_intersect($excludeGroupsList, $selfGroups)) !== 0) {
107
+                // a group of the current user is excluded -> filter all local users
108
+                $skipLocal = true;
109
+            }
110
+        }
111
+
112
+        $selfUID = $self->getUID();
113
+
114
+        return array_filter($entries, function(IEntry $entry) use ($self, $skipLocal, $ownGroupsOnly, $selfGroups, $selfUID) {
115
+
116
+            if ($skipLocal && $entry->getProperty('isLocalSystemBook') === true) {
117
+                return false;
118
+            }
119
+
120
+            if ($ownGroupsOnly && $entry->getProperty('isLocalSystemBook') === true) {
121
+                $contactGroups = $this->groupManager->getUserGroupIds($this->userManager->get($entry->getProperty('UID')));
122
+                if (count(array_intersect($contactGroups, $selfGroups)) === 0) {
123
+                    // no groups in common, so shouldn't see the contact
124
+                    return false;
125
+                }
126
+            }
127
+
128
+            return $entry->getProperty('UID') !== $selfUID;
129
+        });
130
+
131
+
132
+    }
133
+
134
+    /**
135
+     * @param IUser $user
136
+     * @param integer $shareType
137
+     * @param string $shareWith
138
+     * @return IEntry|null
139
+     */
140
+    public function findOne(IUser $user, $shareType, $shareWith) {
141
+        switch($shareType) {
142
+            case 0:
143
+            case 6:
144
+                $filter = ['UID'];
145
+                break;
146
+            case 4:
147
+                $filter = ['EMAIL'];
148
+                break;
149
+            default:
150
+                return null;
151
+        }
152
+
153
+        $userId = $user->getUID();
154
+        $allContacts = $this->contactsManager->search($shareWith, $filter);
155
+        $contacts = array_filter($allContacts, function($contact) use ($userId) {
156
+            return $contact['UID'] !== $userId;
157
+        });
158
+        $match = null;
159
+
160
+        foreach ($contacts as $contact) {
161
+            if ($shareType === 4 && isset($contact['EMAIL'])) {
162
+                if (in_array($shareWith, $contact['EMAIL'])) {
163
+                    $match = $contact;
164
+                    break;
165
+                }
166
+            }
167
+            if ($shareType === 0 || $shareType === 6) {
168
+                if ($contact['UID'] === $shareWith && $contact['isLocalSystemBook'] === true) {
169
+                    $match = $contact;
170
+                    break;
171
+                }
172
+            }
173
+        }
174
+
175
+        if ($match) {
176
+            $match = $this->filterContacts($user, [$this->contactArrayToEntry($match)]);
177
+            if (count($match) === 1) {
178
+                $match = $match[0];
179
+            } else {
180
+                $match = null;
181
+            }
182
+
183
+        }
184
+
185
+        return $match;
186
+    }
187
+
188
+    /**
189
+     * @param array $contact
190
+     * @return Entry
191
+     */
192
+    private function contactArrayToEntry(array $contact) {
193
+        $entry = new Entry();
194
+
195
+        if (isset($contact['id'])) {
196
+            $entry->setId($contact['id']);
197
+        }
198
+
199
+        if (isset($contact['FN'])) {
200
+            $entry->setFullName($contact['FN']);
201
+        }
202
+
203
+        $avatarPrefix = "VALUE=uri:";
204
+        if (isset($contact['PHOTO']) && strpos($contact['PHOTO'], $avatarPrefix) === 0) {
205
+            $entry->setAvatar(substr($contact['PHOTO'], strlen($avatarPrefix)));
206
+        }
207
+
208
+        if (isset($contact['EMAIL'])) {
209
+            foreach ($contact['EMAIL'] as $email) {
210
+                $entry->addEMailAddress($email);
211
+            }
212
+        }
213
+
214
+        // Attach all other properties to the entry too because some
215
+        // providers might make use of it.
216
+        $entry->setProperties($contact);
217
+
218
+        return $entry;
219
+    }
220 220
 
221 221
 }
Please login to merge, or discard this patch.