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