From ae693129dbc69d5554c02711809ba292207f4bbc Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 13 Jun 2017 18:07:47 +0200 Subject: [PATCH 1/3] Allow to find local users by their email address Signed-off-by: Joas Schilling --- .../lib/Controller/ShareesAPIController.php | 37 +++++++++++++++---- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/apps/files_sharing/lib/Controller/ShareesAPIController.php b/apps/files_sharing/lib/Controller/ShareesAPIController.php index 7d345efb3e..7766d762d3 100644 --- a/apps/files_sharing/lib/Controller/ShareesAPIController.php +++ b/apps/files_sharing/lib/Controller/ShareesAPIController.php @@ -593,24 +593,47 @@ 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 ($exactEmailMatch || strtolower($contact['FN']) === $lowerSearch) { + if (isset($contact['isLocalSystemBook'])) { + if ($exactEmailMatch) { + $cloud = $this->cloudIdManager->resolveCloudId($contact['CLOUD'][0]); + $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]); + $this->result['users'][] = [ + 'label' => $contact['FN'] . " ($emailAddress)", + 'value' => [ + 'shareType' => Share::SHARE_TYPE_USER, + 'shareWith' => $cloud->getUser(), + ], + ]; + } + continue; + } + + if ($exactEmailMatch) { $result['exactIdMatch'] = true; } $result['exact'][] = [ From 4f98852f5254db0ed17baea813cd12ced0d2e65b Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 14 Jun 2017 15:07:21 +0200 Subject: [PATCH 2/3] Make sure to only add system users once Signed-off-by: Joas Schilling --- .../lib/Controller/ShareesAPIController.php | 43 +++++++++++++++---- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/apps/files_sharing/lib/Controller/ShareesAPIController.php b/apps/files_sharing/lib/Controller/ShareesAPIController.php index 7766d762d3..57d51ebac6 100644 --- a/apps/files_sharing/lib/Controller/ShareesAPIController.php +++ b/apps/files_sharing/lib/Controller/ShareesAPIController.php @@ -607,10 +607,11 @@ class ShareesAPIController extends OCSController { } foreach ($emailAddresses as $emailAddress) { $exactEmailMatch = strtolower($emailAddress) === $lowerSearch; - if ($exactEmailMatch || strtolower($contact['FN']) === $lowerSearch) { - if (isset($contact['isLocalSystemBook'])) { - if ($exactEmailMatch) { - $cloud = $this->cloudIdManager->resolveCloudId($contact['CLOUD'][0]); + + 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' => [ @@ -618,10 +619,12 @@ class ShareesAPIController extends OCSController { 'shareWith' => $cloud->getUser(), ], ]; - return ['results' => [], 'exact' => [], 'exactIdMatch' => true]; } - if ($this->shareeEnumeration) { - $cloud = $this->cloudIdManager->resolveCloudId($contact['CLOUD'][0]); + 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' => [ @@ -630,9 +633,11 @@ class ShareesAPIController extends OCSController { ], ]; } - continue; } + continue; + } + if ($exactEmailMatch || strtolower($contact['FN']) === $lowerSearch) { if ($exactEmailMatch) { $result['exactIdMatch'] = true; } @@ -711,6 +716,28 @@ class ShareesAPIController extends OCSController { $this->result['lookup'] = $result; } + /** + * 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 * From f28511cac65a688a07965760bf39a7863f2e3e98 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 14 Jun 2017 15:32:34 +0200 Subject: [PATCH 3/3] Add unit test Signed-off-by: Joas Schilling --- .../tests/Controller/ShareesAPIControllerTest.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php b/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php index e3d869db3d..e027d0751c 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, + ] ]; }