MailPlugin.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Arthur Schiwon <blizzz@arthur-schiwon.de>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OC\Collaboration\Collaborators;
  24. use OCP\Collaboration\Collaborators\ISearchPlugin;
  25. use OCP\Collaboration\Collaborators\ISearchResult;
  26. use OCP\Collaboration\Collaborators\SearchResultType;
  27. use OCP\Contacts\IManager;
  28. use OCP\Federation\ICloudId;
  29. use OCP\Federation\ICloudIdManager;
  30. use OCP\IConfig;
  31. use OCP\IGroupManager;
  32. use OCP\IUser;
  33. use OCP\IUserSession;
  34. use OCP\Share;
  35. class MailPlugin implements ISearchPlugin {
  36. protected $shareeEnumeration;
  37. protected $shareWithGroupOnly;
  38. /** @var IManager */
  39. private $contactsManager;
  40. /** @var ICloudIdManager */
  41. private $cloudIdManager;
  42. /** @var IConfig */
  43. private $config;
  44. /** @var IGroupManager */
  45. private $groupManager;
  46. /** @var IUserSession */
  47. private $userSession;
  48. public function __construct(IManager $contactsManager, ICloudIdManager $cloudIdManager, IConfig $config, IGroupManager $groupManager, IUserSession $userSession) {
  49. $this->contactsManager = $contactsManager;
  50. $this->cloudIdManager = $cloudIdManager;
  51. $this->config = $config;
  52. $this->groupManager = $groupManager;
  53. $this->userSession = $userSession;
  54. $this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes';
  55. $this->shareWithGroupOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes';
  56. }
  57. /**
  58. * @param $search
  59. * @param $limit
  60. * @param $offset
  61. * @param ISearchResult $searchResult
  62. * @return bool
  63. * @since 13.0.0
  64. */
  65. public function search($search, $limit, $offset, ISearchResult $searchResult) {
  66. $result = $userResults = ['wide' => [], 'exact' => []];
  67. $userType = new SearchResultType('users');
  68. $emailType = new SearchResultType('emails');
  69. // Search in contacts
  70. //@todo Pagination missing
  71. $addressBookContacts = $this->contactsManager->search($search, ['EMAIL', 'FN']);
  72. $lowerSearch = strtolower($search);
  73. foreach ($addressBookContacts as $contact) {
  74. if (isset($contact['EMAIL'])) {
  75. $emailAddresses = $contact['EMAIL'];
  76. if (\is_string($emailAddresses)) {
  77. $emailAddresses = [$emailAddresses];
  78. }
  79. foreach ($emailAddresses as $type => $emailAddress) {
  80. $displayName = $emailAddress;
  81. $emailAddressType = null;
  82. if (\is_array($emailAddress)) {
  83. $emailAddressData = $emailAddress;
  84. $emailAddress = $emailAddressData['value'];
  85. $emailAddressType = $emailAddressData['type'];
  86. }
  87. if (isset($contact['FN'])) {
  88. $displayName = $contact['FN'] . ' (' . $emailAddress . ')';
  89. }
  90. $exactEmailMatch = strtolower($emailAddress) === $lowerSearch;
  91. if (isset($contact['isLocalSystemBook'])) {
  92. if ($this->shareWithGroupOnly) {
  93. /*
  94. * Check if the user may share with the user associated with the e-mail of the just found contact
  95. */
  96. $userGroups = $this->groupManager->getUserGroupIds($this->userSession->getUser());
  97. $found = false;
  98. foreach ($userGroups as $userGroup) {
  99. if ($this->groupManager->isInGroup($contact['UID'], $userGroup)) {
  100. $found = true;
  101. break;
  102. }
  103. }
  104. if (!$found) {
  105. continue;
  106. }
  107. }
  108. if ($exactEmailMatch) {
  109. try {
  110. $cloud = $this->cloudIdManager->resolveCloudId($contact['CLOUD'][0]);
  111. } catch (\InvalidArgumentException $e) {
  112. continue;
  113. }
  114. if (!$this->isCurrentUser($cloud) && !$searchResult->hasResult($userType, $cloud->getUser())) {
  115. $singleResult = [[
  116. 'label' => $displayName,
  117. 'uuid' => $contact['UID'],
  118. 'name' => $contact['FN'],
  119. 'value' => [
  120. 'shareType' => Share::SHARE_TYPE_USER,
  121. 'shareWith' => $cloud->getUser(),
  122. ],
  123. ]];
  124. $searchResult->addResultSet($userType, [], $singleResult);
  125. $searchResult->markExactIdMatch($emailType);
  126. }
  127. return false;
  128. }
  129. if ($this->shareeEnumeration) {
  130. try {
  131. $cloud = $this->cloudIdManager->resolveCloudId($contact['CLOUD'][0]);
  132. } catch (\InvalidArgumentException $e) {
  133. continue;
  134. }
  135. if (!$this->isCurrentUser($cloud) && !$searchResult->hasResult($userType, $cloud->getUser())) {
  136. $userResults['wide'][] = [
  137. 'label' => $displayName,
  138. 'uuid' => $contact['UID'],
  139. 'name' => $contact['FN'],
  140. 'value' => [
  141. 'shareType' => Share::SHARE_TYPE_USER,
  142. 'shareWith' => $cloud->getUser(),
  143. ],
  144. ];
  145. }
  146. }
  147. continue;
  148. }
  149. if ($exactEmailMatch
  150. || isset($contact['FN']) && strtolower($contact['FN']) === $lowerSearch)
  151. {
  152. if ($exactEmailMatch) {
  153. $searchResult->markExactIdMatch($emailType);
  154. }
  155. $result['exact'][] = [
  156. 'label' => $displayName,
  157. 'uuid' => $contact['UID'],
  158. 'name' => $contact['FN'],
  159. 'type' => $emailAddressType ?? '',
  160. 'value' => [
  161. 'shareType' => Share::SHARE_TYPE_EMAIL,
  162. 'shareWith' => $emailAddress,
  163. ],
  164. ];
  165. } else {
  166. $result['wide'][] = [
  167. 'label' => $displayName,
  168. 'uuid' => $contact['UID'],
  169. 'name' => $contact['FN'],
  170. 'type' => $emailAddressType ?? '',
  171. 'value' => [
  172. 'shareType' => Share::SHARE_TYPE_EMAIL,
  173. 'shareWith' => $emailAddress,
  174. ],
  175. ];
  176. }
  177. }
  178. }
  179. }
  180. $reachedEnd = true;
  181. if (!$this->shareeEnumeration) {
  182. $result['wide'] = [];
  183. $userResults['wide'] = [];
  184. } else {
  185. $reachedEnd = (count($result['wide']) < $offset + $limit) &&
  186. (count($userResults['wide']) < $offset + $limit);
  187. $result['wide'] = array_slice($result['wide'], $offset, $limit);
  188. $userResults['wide'] = array_slice($userResults['wide'], $offset, $limit);
  189. }
  190. if (!$searchResult->hasExactIdMatch($emailType) && filter_var($search, FILTER_VALIDATE_EMAIL)) {
  191. $result['exact'][] = [
  192. 'label' => $search,
  193. 'uuid' => $search,
  194. 'value' => [
  195. 'shareType' => Share::SHARE_TYPE_EMAIL,
  196. 'shareWith' => $search,
  197. ],
  198. ];
  199. }
  200. if (!empty($userResults['wide'])) {
  201. $searchResult->addResultSet($userType, $userResults['wide'], []);
  202. }
  203. $searchResult->addResultSet($emailType, $result['wide'], $result['exact']);
  204. return !$reachedEnd;
  205. }
  206. public function isCurrentUser(ICloudId $cloud): bool {
  207. $currentUser = $this->userSession->getUser();
  208. return $currentUser instanceof IUser ? $currentUser->getUID() === $cloud->getUser() : false;
  209. }
  210. }