diff options
author | Morris Jobke <hey@morrisjobke.de> | 2017-06-14 18:03:24 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-06-14 18:03:24 -0500 |
commit | 2398d40d98b7bac12b54952f081112bcaae94450 (patch) | |
tree | bb0d4221de594cbd867e667552815216ade5521e /apps/files_sharing | |
parent | fc47d0bbaa0e80fa073d091cb5b0db4b8612a4e3 (diff) | |
parent | f28511cac65a688a07965760bf39a7863f2e3e98 (diff) | |
download | nextcloud-server-2398d40d98b7bac12b54952f081112bcaae94450.tar.gz nextcloud-server-2398d40d98b7bac12b54952f081112bcaae94450.zip |
Merge pull request #5384 from nextcloud/allow-to-share-to-local-users-via-email
Allow to find local users by their email address
Diffstat (limited to 'apps/files_sharing')
-rw-r--r-- | apps/files_sharing/lib/Controller/ShareesAPIController.php | 64 | ||||
-rw-r--r-- | apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php | 15 |
2 files changed, 72 insertions, 7 deletions
diff --git a/apps/files_sharing/lib/Controller/ShareesAPIController.php b/apps/files_sharing/lib/Controller/ShareesAPIController.php index 7d345efb3eb..57d51ebac6a 100644 --- a/apps/files_sharing/lib/Controller/ShareesAPIController.php +++ b/apps/files_sharing/lib/Controller/ShareesAPIController.php @@ -593,24 +593,52 @@ class ShareesAPIController extends OCSController { * @return array */ protected function getEmail($search) { - $result = ['results' => [], 'exact' => []]; + $result = ['results' => [], 'exact' => [], 'exactIdMatch' => false]; // Search in contacts //@todo Pagination missing $addressBookContacts = $this->contactsManager->search($search, ['EMAIL', 'FN']); - $result['exactIdMatch'] = false; + $lowerSearch = strtolower($search); foreach ($addressBookContacts as $contact) { - if (isset($contact['isLocalSystemBook'])) { - continue; - } if (isset($contact['EMAIL'])) { $emailAddresses = $contact['EMAIL']; if (!is_array($emailAddresses)) { $emailAddresses = [$emailAddresses]; } foreach ($emailAddresses as $emailAddress) { - if (strtolower($contact['FN']) === strtolower($search) || strtolower($emailAddress) === strtolower($search)) { - if (strtolower($emailAddress) === strtolower($search)) { + $exactEmailMatch = strtolower($emailAddress) === $lowerSearch; + + if (isset($contact['isLocalSystemBook'])) { + if ($exactEmailMatch) { + $cloud = $this->cloudIdManager->resolveCloudId($contact['CLOUD'][0]); + if (!$this->hasUserInResult($cloud->getUser())) { + $this->result['exact']['users'][] = [ + 'label' => $contact['FN'] . " ($emailAddress)", + 'value' => [ + 'shareType' => Share::SHARE_TYPE_USER, + 'shareWith' => $cloud->getUser(), + ], + ]; + } + return ['results' => [], 'exact' => [], 'exactIdMatch' => true]; + } + if ($this->shareeEnumeration) { + $cloud = $this->cloudIdManager->resolveCloudId($contact['CLOUD'][0]); + if (!$this->hasUserInResult($cloud->getUser())) { + $this->result['users'][] = [ + 'label' => $contact['FN'] . " ($emailAddress)", + 'value' => [ + 'shareType' => Share::SHARE_TYPE_USER, + 'shareWith' => $cloud->getUser(), + ], + ]; + } + } + continue; + } + + if ($exactEmailMatch || strtolower($contact['FN']) === $lowerSearch) { + if ($exactEmailMatch) { $result['exactIdMatch'] = true; } $result['exact'][] = [ @@ -689,6 +717,28 @@ class ShareesAPIController extends OCSController { } /** + * Check if a given user is already part of the result + * + * @param string $userId + * @return bool + */ + protected function hasUserInResult($userId) { + foreach ($this->result['exact']['users'] as $result) { + if ($result['value']['shareWith'] === $userId) { + return true; + } + } + + foreach ($this->result['users'] as $result) { + if ($result['value']['shareWith'] === $userId) { + return true; + } + } + + return false; + } + + /** * Generates a bunch of pagination links for the current page * * @param int $page Current page diff --git a/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php b/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php index e3d869db3d6..e027d0751cb 100644 --- a/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php +++ b/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php @@ -1272,6 +1272,21 @@ class ShareesAPIControllerTest extends TestCase { ['results' => [], 'exact' => [], 'exactIdMatch' => false], true, ], + // Local user found by email + [ + 'test@example.com', + [ + [ + 'FN' => 'User', + 'EMAIL' => ['test@example.com'], + 'CLOUD' => ['test@localhost'], + 'isLocalSystemBook' => true, + ] + ], + false, + ['results' => [], 'exact' => [], 'exactIdMatch' => true], + false, + ] ]; } |