Completed
Pull Request — master (#5585)
by Tobia
22:09 queued 08:03
created
lib/private/Contacts/ContactsMenu/ContactsStore.php 4 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -132,7 +132,7 @@
 block discarded – undo
132 132
 	 * @param IUser $user
133 133
 	 * @param integer $shareType
134 134
 	 * @param string $shareWith
135
-	 * @return IEntry|null
135
+	 * @return null|Entry
136 136
 	 */
137 137
 	public function findOne(IUser $user, $shareType, $shareWith) {
138 138
 		switch($shareType) {
Please login to merge, or discard this patch.
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.
Indentation   +169 added lines, -169 removed lines patch added patch discarded remove patch
@@ -35,174 +35,174 @@
 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 $entries Entry[]
89
-	 * @return array the filtered contacts
90
-	 */
91
-	private function filterContacts(IUser $self, Array $entries) {
92
-		$excludedGroups = $this->config->getAppValue('core', 'shareapi_exclude_groups', 'no') === 'yes' ? true : false;
93
-
94
-		$skipLocal = false; // whether to filter out local users
95
-		$ownGroupsOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes' ? true : false; // whether to filter out all users which doesn't have the same group as the current user
96
-
97
-		$selfGroups = $this->groupManager->getUserGroupIds($self);
98
-
99
-		if ($excludedGroups) {
100
-			$excludedGroups = $this->config->getAppValue('core', 'shareapi_exclude_groups_list', '');
101
-			$excludeGroupsList = !is_null(json_decode($excludedGroups)) ? json_decode($excludedGroups, true) :  [];
102
-
103
-			if (count(array_intersect($excludeGroupsList, $selfGroups)) !== 0) {
104
-				// a group of the current user is excluded -> filter all local users
105
-				$skipLocal = true;
106
-			}
107
-		}
108
-
109
-		$selfUID = $self->getUID();
110
-
111
-		return array_filter($entries, function(IEntry $entry) use ($self, $skipLocal, $ownGroupsOnly, $selfGroups, $selfUID) {
112
-
113
-			if ($skipLocal && $entry->getProperty('isLocalSystemBook') === true) {
114
-				return false;
115
-			}
116
-
117
-			if ($ownGroupsOnly && $entry->getProperty('isLocalSystemBook') === true) {
118
-				$contactGroups = $this->groupManager->getUserGroupIds($this->userManager->get($entry->getProperty('UID')));
119
-				if (count(array_intersect($contactGroups, $selfGroups)) === 0) {
120
-					// no groups in common, so shouldn't see the contact
121
-					return false;
122
-				}
123
-			}
124
-
125
-			return $entry->getProperty('UID') !== $selfUID;
126
-		});
127
-
128
-
129
-	}
130
-
131
-	/**
132
-	 * @param IUser $user
133
-	 * @param integer $shareType
134
-	 * @param string $shareWith
135
-	 * @return IEntry|null
136
-	 */
137
-	public function findOne(IUser $user, $shareType, $shareWith) {
138
-		switch($shareType) {
139
-			case 0:
140
-			case 6:
141
-				$filter = ['UID'];
142
-				break;
143
-			case 4:
144
-				$filter = ['EMAIL'];
145
-				break;
146
-			default:
147
-				return null;
148
-		}
149
-
150
-		$userId = $user->getUID();
151
-		$allContacts = $this->contactsManager->search($shareWith, $filter);
152
-		$contacts = array_filter($allContacts, function($contact) use ($userId) {
153
-			return $contact['UID'] !== $userId;
154
-		});
155
-		$match = null;
156
-
157
-		foreach ($contacts as $contact) {
158
-			if ($shareType === 4 && isset($contact['EMAIL'])) {
159
-				if (in_array($shareWith, $contact['EMAIL'])) {
160
-					$match = $contact;
161
-					break;
162
-				}
163
-			}
164
-			if ($shareType === 0 || $shareType === 6) {
165
-				if ($contact['UID'] === $shareWith && $contact['isLocalSystemBook'] === true) {
166
-					$match = $contact;
167
-					break;
168
-				}
169
-			}
170
-		}
171
-
172
-		return $match ? $this->contactArrayToEntry($match) : null;
173
-	}
174
-
175
-	/**
176
-	 * @param array $contact
177
-	 * @return Entry
178
-	 */
179
-	private function contactArrayToEntry(array $contact) {
180
-		$entry = new Entry();
181
-
182
-		if (isset($contact['id'])) {
183
-			$entry->setId($contact['id']);
184
-		}
185
-
186
-		if (isset($contact['FN'])) {
187
-			$entry->setFullName($contact['FN']);
188
-		}
189
-
190
-		$avatarPrefix = "VALUE=uri:";
191
-		if (isset($contact['PHOTO']) && strpos($contact['PHOTO'], $avatarPrefix) === 0) {
192
-			$entry->setAvatar(substr($contact['PHOTO'], strlen($avatarPrefix)));
193
-		}
194
-
195
-		if (isset($contact['EMAIL'])) {
196
-			foreach ($contact['EMAIL'] as $email) {
197
-				$entry->addEMailAddress($email);
198
-			}
199
-		}
200
-
201
-		// Attach all other properties to the entry too because some
202
-		// providers might make use of it.
203
-		$entry->setProperties($contact);
204
-
205
-		return $entry;
206
-	}
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 $entries Entry[]
89
+     * @return array the filtered contacts
90
+     */
91
+    private function filterContacts(IUser $self, Array $entries) {
92
+        $excludedGroups = $this->config->getAppValue('core', 'shareapi_exclude_groups', 'no') === 'yes' ? true : false;
93
+
94
+        $skipLocal = false; // whether to filter out local users
95
+        $ownGroupsOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes' ? true : false; // whether to filter out all users which doesn't have the same group as the current user
96
+
97
+        $selfGroups = $this->groupManager->getUserGroupIds($self);
98
+
99
+        if ($excludedGroups) {
100
+            $excludedGroups = $this->config->getAppValue('core', 'shareapi_exclude_groups_list', '');
101
+            $excludeGroupsList = !is_null(json_decode($excludedGroups)) ? json_decode($excludedGroups, true) :  [];
102
+
103
+            if (count(array_intersect($excludeGroupsList, $selfGroups)) !== 0) {
104
+                // a group of the current user is excluded -> filter all local users
105
+                $skipLocal = true;
106
+            }
107
+        }
108
+
109
+        $selfUID = $self->getUID();
110
+
111
+        return array_filter($entries, function(IEntry $entry) use ($self, $skipLocal, $ownGroupsOnly, $selfGroups, $selfUID) {
112
+
113
+            if ($skipLocal && $entry->getProperty('isLocalSystemBook') === true) {
114
+                return false;
115
+            }
116
+
117
+            if ($ownGroupsOnly && $entry->getProperty('isLocalSystemBook') === true) {
118
+                $contactGroups = $this->groupManager->getUserGroupIds($this->userManager->get($entry->getProperty('UID')));
119
+                if (count(array_intersect($contactGroups, $selfGroups)) === 0) {
120
+                    // no groups in common, so shouldn't see the contact
121
+                    return false;
122
+                }
123
+            }
124
+
125
+            return $entry->getProperty('UID') !== $selfUID;
126
+        });
127
+
128
+
129
+    }
130
+
131
+    /**
132
+     * @param IUser $user
133
+     * @param integer $shareType
134
+     * @param string $shareWith
135
+     * @return IEntry|null
136
+     */
137
+    public function findOne(IUser $user, $shareType, $shareWith) {
138
+        switch($shareType) {
139
+            case 0:
140
+            case 6:
141
+                $filter = ['UID'];
142
+                break;
143
+            case 4:
144
+                $filter = ['EMAIL'];
145
+                break;
146
+            default:
147
+                return null;
148
+        }
149
+
150
+        $userId = $user->getUID();
151
+        $allContacts = $this->contactsManager->search($shareWith, $filter);
152
+        $contacts = array_filter($allContacts, function($contact) use ($userId) {
153
+            return $contact['UID'] !== $userId;
154
+        });
155
+        $match = null;
156
+
157
+        foreach ($contacts as $contact) {
158
+            if ($shareType === 4 && isset($contact['EMAIL'])) {
159
+                if (in_array($shareWith, $contact['EMAIL'])) {
160
+                    $match = $contact;
161
+                    break;
162
+                }
163
+            }
164
+            if ($shareType === 0 || $shareType === 6) {
165
+                if ($contact['UID'] === $shareWith && $contact['isLocalSystemBook'] === true) {
166
+                    $match = $contact;
167
+                    break;
168
+                }
169
+            }
170
+        }
171
+
172
+        return $match ? $this->contactArrayToEntry($match) : null;
173
+    }
174
+
175
+    /**
176
+     * @param array $contact
177
+     * @return Entry
178
+     */
179
+    private function contactArrayToEntry(array $contact) {
180
+        $entry = new Entry();
181
+
182
+        if (isset($contact['id'])) {
183
+            $entry->setId($contact['id']);
184
+        }
185
+
186
+        if (isset($contact['FN'])) {
187
+            $entry->setFullName($contact['FN']);
188
+        }
189
+
190
+        $avatarPrefix = "VALUE=uri:";
191
+        if (isset($contact['PHOTO']) && strpos($contact['PHOTO'], $avatarPrefix) === 0) {
192
+            $entry->setAvatar(substr($contact['PHOTO'], strlen($avatarPrefix)));
193
+        }
194
+
195
+        if (isset($contact['EMAIL'])) {
196
+            foreach ($contact['EMAIL'] as $email) {
197
+                $entry->addEMailAddress($email);
198
+            }
199
+        }
200
+
201
+        // Attach all other properties to the entry too because some
202
+        // providers might make use of it.
203
+        $entry->setProperties($contact);
204
+
205
+        return $entry;
206
+    }
207 207
 
208 208
 }
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -98,7 +98,7 @@  discard block
 block discarded – undo
98 98
 
99 99
 		if ($excludedGroups) {
100 100
 			$excludedGroups = $this->config->getAppValue('core', 'shareapi_exclude_groups_list', '');
101
-			$excludeGroupsList = !is_null(json_decode($excludedGroups)) ? json_decode($excludedGroups, true) :  [];
101
+			$excludeGroupsList = !is_null(json_decode($excludedGroups)) ? json_decode($excludedGroups, true) : [];
102 102
 
103 103
 			if (count(array_intersect($excludeGroupsList, $selfGroups)) !== 0) {
104 104
 				// a group of the current user is excluded -> filter all local users
@@ -135,7 +135,7 @@  discard block
 block discarded – undo
135 135
 	 * @return IEntry|null
136 136
 	 */
137 137
 	public function findOne(IUser $user, $shareType, $shareWith) {
138
-		switch($shareType) {
138
+		switch ($shareType) {
139 139
 			case 0:
140 140
 			case 6:
141 141
 				$filter = ['UID'];
Please login to merge, or discard this patch.