]> source.dussan.org Git - nextcloud-server.git/commitdiff
Backport of #5384 to stable12 5428/head
authorJoas Schilling <coding@schilljs.com>
Tue, 13 Jun 2017 16:07:47 +0000 (18:07 +0200)
committerArthur Schiwon <blizzz@arthur-schiwon.de>
Thu, 15 Jun 2017 17:23:31 +0000 (19:23 +0200)
Allow to find local users by their email address

Signed-off-by: Joas Schilling <coding@schilljs.com>
Make sure to only add system users once

Signed-off-by: Joas Schilling <coding@schilljs.com>
Add unit test

Signed-off-by: Joas Schilling <coding@schilljs.com>
apps/files_sharing/lib/Controller/ShareesAPIController.php
apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php

index 7d345efb3eb388a7c62eff25ab9ad45ae6afde59..57d51ebac6a01cc902219ee16f2c760880050abf 100644 (file)
@@ -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'][] = [
@@ -688,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
         *
index e3d869db3d6dc41c73563c8b54b46a937c54a4fd..e027d0751cb75a14f27d23f640623e86cabc6712 100644 (file)
@@ -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,
+                       ]
                ];
        }