]> source.dussan.org Git - nextcloud-server.git/commitdiff
Add settings to ignore second display name in search
authorLouis Chemineau <louis@chmn.me>
Tue, 10 May 2022 11:05:08 +0000 (13:05 +0200)
committerLouis (Rebase PR Action) <artonge@users.noreply.github.com>
Tue, 7 Jun 2022 11:37:25 +0000 (11:37 +0000)
Signed-off-by: Louis Chemineau <louis@chmn.me>
apps/settings/lib/Settings/Admin/Sharing.php
apps/settings/tests/Settings/Admin/SharingTest.php
build/integration/features/bootstrap/CollaborationContext.php
lib/private/Collaboration/Collaborators/UserPlugin.php
tests/lib/Collaboration/Collaborators/UserPluginTest.php

index c44fec94b6e2f1297ef4257709cb2b2afe40bb93..ac001212aa4fd6811bb88df7e9b56433d5342c4b 100644 (file)
@@ -85,6 +85,7 @@ class Sharing implements IDelegatedSettings {
                        'restrictUserEnumerationToPhone' => $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_to_phone', 'no'),
                        'restrictUserEnumerationFullMatch' => $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_full_match', 'yes'),
                        'restrictUserEnumerationFullMatchUserId' => $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_full_match_userid', 'yes'),
+                       'restrictUserEnumerationFullMatchIgnoreSecondDisplayName' => $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_full_match_ignore_second_display_name', 'no'),
                        'enforceLinkPassword' => Util::isPublicLinkPasswordRequired(),
                        'onlyShareWithGroupMembers' => $this->shareManager->shareWithGroupMembersOnly(),
                        'shareAPIEnabled' => $this->config->getAppValue('core', 'shareapi_enabled', 'yes'),
index a353a1a653dff94ab8c21bc181a5711747e5c372..31c6ef9ed94fa736d802232e6710fe6672227f6d 100644 (file)
@@ -83,6 +83,7 @@ class SharingTest extends TestCase {
                                ['core', 'shareapi_restrict_user_enumeration_to_phone', 'no', 'no'],
                                ['core', 'shareapi_restrict_user_enumeration_full_match', 'yes', 'yes'],
                                ['core', 'shareapi_restrict_user_enumeration_full_match_userid', 'yes', 'yes'],
+                               ['core', 'shareapi_restrict_user_enumeration_full_match_ignore_second_display_name', 'no', 'no'],
                                ['core', 'shareapi_enabled', 'yes', 'yes'],
                                ['core', 'shareapi_default_expire_date', 'no', 'no'],
                                ['core', 'shareapi_expire_after_n_days', '7', '7'],
@@ -117,6 +118,7 @@ class SharingTest extends TestCase {
                                'restrictUserEnumerationToPhone' => 'no',
                                'restrictUserEnumerationFullMatch' => 'yes',
                                'restrictUserEnumerationFullMatchUserId' => 'yes',
+                               'restrictUserEnumerationFullMatchIgnoreSecondDisplayName' => 'no',
                                'enforceLinkPassword' => false,
                                'onlyShareWithGroupMembers' => false,
                                'shareAPIEnabled' => 'yes',
@@ -157,6 +159,7 @@ class SharingTest extends TestCase {
                                ['core', 'shareapi_restrict_user_enumeration_to_phone', 'no', 'no'],
                                ['core', 'shareapi_restrict_user_enumeration_full_match', 'yes', 'yes'],
                                ['core', 'shareapi_restrict_user_enumeration_full_match_userid', 'yes', 'yes'],
+                               ['core', 'shareapi_restrict_user_enumeration_full_match_ignore_second_display_name', 'no', 'no'],
                                ['core', 'shareapi_enabled', 'yes', 'yes'],
                                ['core', 'shareapi_default_expire_date', 'no', 'no'],
                                ['core', 'shareapi_expire_after_n_days', '7', '7'],
@@ -191,6 +194,7 @@ class SharingTest extends TestCase {
                                'restrictUserEnumerationToPhone' => 'no',
                                'restrictUserEnumerationFullMatch' => 'yes',
                                'restrictUserEnumerationFullMatchUserId' => 'yes',
+                               'restrictUserEnumerationFullMatchIgnoreSecondDisplayName' => 'no',
                                'enforceLinkPassword' => false,
                                'onlyShareWithGroupMembers' => false,
                                'shareAPIEnabled' => 'yes',
index a61105f090cc7f672de651dd10ea2334b0a2da86..77fea0c3960e9b2f41107d61674c5dbb157661ba 100644 (file)
@@ -70,6 +70,7 @@ class CollaborationContext implements Context {
                $this->deleteServerConfig('core', 'shareapi_restrict_user_enumeration_to_phone');
                $this->deleteServerConfig('core', 'shareapi_restrict_user_enumeration_full_match');
                $this->deleteServerConfig('core', 'shareapi_restrict_user_enumeration_full_match_userid');
+               $this->deleteServerConfig('core', 'shareapi_restrict_user_enumeration_full_match_ignore_second_display_name');
                $this->deleteServerConfig('core', 'shareapi_only_share_with_group_members');
        }
 
index 510f383249a18883d4f8fa51653626df32e1b9c6..819ecfa50d8d12dbd71bd178fe6686595cd98365 100644 (file)
@@ -56,6 +56,8 @@ class UserPlugin implements ISearchPlugin {
        protected $shareeEnumerationFullMatch;
        /* @var bool */
        protected $shareeEnumerationFullMatchUserId;
+       /* @var bool */
+       protected $shareeEnumerationFullMatchIgnoreSecondDisplayName;
 
        /** @var IConfig */
        private $config;
@@ -90,6 +92,7 @@ class UserPlugin implements ISearchPlugin {
                $this->shareeEnumerationPhone = $this->shareeEnumeration && $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_to_phone', 'no') === 'yes';
                $this->shareeEnumerationFullMatch = $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_full_match', 'yes') === 'yes';
                $this->shareeEnumerationFullMatchUserId = $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_full_match_userid', 'yes') === 'yes';
+               $this->shareeEnumerationFullMatchIgnoreSecondDisplayName = $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_full_match_ignore_second_display_name', 'no') === 'yes';
        }
 
        public function search($search, $limit, $offset, ISearchResult $searchResult) {
@@ -181,6 +184,7 @@ class UserPlugin implements ISearchPlugin {
                                $this->shareeEnumerationFullMatch &&
                                $lowerSearch !== '' && (strtolower($uid) === $lowerSearch ||
                                strtolower($userDisplayName) === $lowerSearch ||
+                               ($this->shareeEnumerationFullMatchIgnoreSecondDisplayName && trim(strtolower(preg_replace('/ \(.*\)$/', '', $userDisplayName))) === $lowerSearch) ||
                                strtolower($userEmail) === $lowerSearch)
                        ) {
                                if (strtolower($uid) === $lowerSearch) {
index acbcd42f04f75a5853c72d4939b8756c59843f3b..2b7a08fe4e1ac2b7b385a14661f31fc3b49b5966 100644 (file)
@@ -104,21 +104,12 @@ class UserPluginTest extends TestCase {
                );
        }
 
-       public function mockConfig($shareWithGroupOnly, $shareeEnumeration, $shareeEnumerationLimitToGroup, $shareeEnumerationPhone = false) {
+       public function mockConfig($mockedSettings) {
                $this->config->expects($this->any())
                        ->method('getAppValue')
                        ->willReturnCallback(
-                               function ($appName, $key, $default) use ($shareWithGroupOnly, $shareeEnumeration, $shareeEnumerationLimitToGroup, $shareeEnumerationPhone) {
-                                       if ($appName === 'core' && $key === 'shareapi_only_share_with_group_members') {
-                                               return $shareWithGroupOnly ? 'yes' : 'no';
-                                       } elseif ($appName === 'core' && $key === 'shareapi_allow_share_dialog_user_enumeration') {
-                                               return $shareeEnumeration ? 'yes' : 'no';
-                                       } elseif ($appName === 'core' && $key === 'shareapi_restrict_user_enumeration_to_group') {
-                                               return $shareeEnumerationLimitToGroup ? 'yes' : 'no';
-                                       } elseif ($appName === 'core' && $key === 'shareapi_restrict_user_enumeration_to_phone') {
-                                               return $shareeEnumerationPhone ? 'yes' : 'no';
-                                       }
-                                       return $default;
+                               function ($appName, $key, $default) use ($mockedSettings) {
+                                       return $mockedSettings[$appName][$key] ?? $default;
                                }
                        );
        }
@@ -470,7 +461,13 @@ class UserPluginTest extends TestCase {
                array $users = [],
                $shareeEnumerationPhone = false
        ) {
-               $this->mockConfig($shareWithGroupOnly, $shareeEnumeration, false, $shareeEnumerationPhone);
+               $this->mockConfig(["core" => [
+                       'shareapi_only_share_with_group_members' => $shareWithGroupOnly ? 'yes' : 'no',
+                       'shareapi_allow_share_dialog_user_enumeration' => $shareeEnumeration? 'yes' : 'no',
+                       'shareapi_restrict_user_enumeration_to_group' => false ? 'yes' : 'no',
+                       'shareapi_restrict_user_enumeration_to_phone' => $shareeEnumerationPhone ? 'yes' : 'no',
+               ]]);
+
                $this->instantiatePlugin();
 
                $this->session->expects($this->any())
@@ -586,6 +583,83 @@ class UserPluginTest extends TestCase {
                                        ['uid' => 'test2', 'groups' => ['groupB']],
                                ],
                                ['exact' => [], 'wide' => ['test1']],
+                               ['core' => ['shareapi_restrict_user_enumeration_to_group' => 'yes']],
+                       ],
+                       [
+                               'test',
+                               ['groupA'],
+                               [
+                                       ['uid' => 'test1', 'displayName' => 'Test user 1', 'groups' => ['groupA']],
+                                       ['uid' => 'test2', 'displayName' => 'Test user 2', 'groups' => ['groupA']],
+                               ],
+                               ['exact' => [], 'wide' => []],
+                               ['core' => ['shareapi_allow_share_dialog_user_enumeration' => 'no']],
+                       ],
+                       [
+                               'test1',
+                               ['groupA'],
+                               [
+                                       ['uid' => 'test1', 'displayName' => 'Test user 1', 'groups' => ['groupA']],
+                                       ['uid' => 'test2', 'displayName' => 'Test user 2', 'groups' => ['groupA']],
+                               ],
+                               ['exact' => ['test1'], 'wide' => []],
+                               ['core' => ['shareapi_allow_share_dialog_user_enumeration' => 'no']],
+                       ],
+                       [
+                               'test1',
+                               ['groupA'],
+                               [
+                                       ['uid' => 'test1', 'displayName' => 'Test user 1', 'groups' => ['groupA']],
+                                       ['uid' => 'test2', 'displayName' => 'Test user 2', 'groups' => ['groupA']],
+                               ],
+                               ['exact' => [], 'wide' => []],
+                               [
+                                       'core' => [
+                                               'shareapi_allow_share_dialog_user_enumeration' => 'no',
+                                               'shareapi_restrict_user_enumeration_full_match_userid' => 'no',
+                                       ],
+                               ]
+                       ],
+                       [
+                               'Test user 1',
+                               ['groupA'],
+                               [
+                                       ['uid' => 'test1', 'displayName' => 'Test user 1', 'groups' => ['groupA']],
+                                       ['uid' => 'test2', 'displayName' => 'Test user 2', 'groups' => ['groupA']],
+                               ],
+                               ['exact' => ['test1'], 'wide' => []],
+                               [
+                                       'core' => [
+                                               'shareapi_allow_share_dialog_user_enumeration' => 'no',
+                                               'shareapi_restrict_user_enumeration_full_match_userid' => 'no',
+                                       ],
+                               ]
+                       ],
+                       [
+                               'Test user 1',
+                               ['groupA'],
+                               [
+                                       ['uid' => 'test1', 'displayName' => 'Test user 1 (Second displayName for user 1)', 'groups' => ['groupA']],
+                                       ['uid' => 'test2', 'displayName' => 'Test user 2 (Second displayName for user 2)', 'groups' => ['groupA']],
+                               ],
+                               ['exact' => [], 'wide' => []],
+                               ['core' => ['shareapi_allow_share_dialog_user_enumeration' => 'no'],
+                               ]
+                       ],
+                       [
+                               'Test user 1',
+                               ['groupA'],
+                               [
+                                       ['uid' => 'test1', 'displayName' => 'Test user 1 (Second displayName for user 1)', 'groups' => ['groupA']],
+                                       ['uid' => 'test2', 'displayName' => 'Test user 2 (Second displayName for user 2)', 'groups' => ['groupA']],
+                               ],
+                               ['exact' => ['test1'], 'wide' => []],
+                               [
+                                       'core' => [
+                                               'shareapi_allow_share_dialog_user_enumeration' => 'no',
+                                               'shareapi_restrict_user_enumeration_full_match_ignore_second_display_name' => 'yes',
+                                       ],
+                               ]
                        ],
                        [
                                'test1',
@@ -595,6 +669,7 @@ class UserPluginTest extends TestCase {
                                        ['uid' => 'test2', 'groups' => ['groupB']],
                                ],
                                ['exact' => ['test1'], 'wide' => []],
+                               ['core' => ['shareapi_restrict_user_enumeration_to_group' => 'yes']],
                        ],
                        [
                                'test',
@@ -604,6 +679,7 @@ class UserPluginTest extends TestCase {
                                        ['uid' => 'test2', 'groups' => ['groupB', 'groupA']],
                                ],
                                ['exact' => [], 'wide' => ['test1', 'test2']],
+                               ['core' => ['shareapi_restrict_user_enumeration_to_group' => 'yes']],
                        ],
                        [
                                'test',
@@ -613,6 +689,7 @@ class UserPluginTest extends TestCase {
                                        ['uid' => 'test2', 'groups' => ['groupB', 'groupA']],
                                ],
                                ['exact' => [], 'wide' => ['test1', 'test2']],
+                               ['core' => ['shareapi_restrict_user_enumeration_to_group' => 'yes']],
                        ],
                        [
                                'test',
@@ -622,6 +699,7 @@ class UserPluginTest extends TestCase {
                                        ['uid' => 'test2', 'groups' => ['groupB', 'groupA']],
                                ],
                                ['exact' => [], 'wide' => ['test1', 'test2']],
+                               ['core' => ['shareapi_restrict_user_enumeration_to_group' => 'yes']],
                        ],
                        [
                                'test',
@@ -631,6 +709,7 @@ class UserPluginTest extends TestCase {
                                        ['uid' => 'test2', 'groups' => ['groupB', 'groupA']],
                                ],
                                ['exact' => [], 'wide' => []],
+                               ['core' => ['shareapi_restrict_user_enumeration_to_group' => 'yes']],
                        ],
                        [
                                'test',
@@ -640,6 +719,7 @@ class UserPluginTest extends TestCase {
                                        ['uid' => 'test2', 'groups' => []],
                                ],
                                ['exact' => [], 'wide' => []],
+                               ['core' => ['shareapi_restrict_user_enumeration_to_group' => 'yes']],
                        ],
                        [
                                'test',
@@ -649,6 +729,7 @@ class UserPluginTest extends TestCase {
                                        ['uid' => 'test2', 'groups' => []],
                                ],
                                ['exact' => [], 'wide' => []],
+                               ['core' => ['shareapi_restrict_user_enumeration_to_group' => 'yes']],
                        ],
                ];
        }
@@ -656,19 +737,38 @@ class UserPluginTest extends TestCase {
        /**
         * @dataProvider dataSearchEnumeration
         */
-       public function testSearchEnumerationLimit($search, $userGroups, $matchingUsers, $result) {
-               $this->mockConfig(false, true, true);
+       public function testSearchEnumerationLimit($search, $userGroups, $matchingUsers, $result, $mockedSettings) {
+               $this->mockConfig($mockedSettings);
 
                $userResults = [];
                foreach ($matchingUsers as $user) {
                        $userResults[$user['uid']] = $user['uid'];
                }
 
-               $mappedResultExact = array_map(function ($user) {
-                       return ['label' => $user, 'value' => ['shareType' => 0, 'shareWith' => $user], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => $user];
+               $usersById = [];
+               foreach ($matchingUsers as $user) {
+                       $usersById[$user['uid']] = $user;
+               }
+
+               $mappedResultExact = array_map(function ($user) use ($usersById, $search) {
+                       return [
+                               'label' => $search === $user ? $user : $usersById[$user]['displayName'],
+                               'value' => ['shareType' => 0, 'shareWith' => $user],
+                               'icon' => 'icon-user',
+                               'subline' => null,
+                               'status' => [],
+                               'shareWithDisplayNameUnique' => $user,
+                       ];
                }, $result['exact']);
                $mappedResultWide = array_map(function ($user) {
-                       return ['label' => $user, 'value' => ['shareType' => 0, 'shareWith' => $user], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => $user];
+                       return [
+                               'label' => $user,
+                               'value' => ['shareType' => 0, 'shareWith' => $user],
+                               'icon' => 'icon-user',
+                               'subline' => null,
+                               'status' => [],
+                               'shareWithDisplayNameUnique' => $user,
+                       ];
                }, $result['wide']);
 
                $this->userManager
@@ -679,6 +779,21 @@ class UserPluginTest extends TestCase {
                                }
                                return null;
                        });
+               $this->userManager
+               ->method('searchDisplayName')
+               ->willReturnCallback(function ($search) use ($matchingUsers) {
+                       $users = array_filter(
+                               $matchingUsers,
+                               function ($user) use ($search) {
+                                       return str_contains(strtolower($user['displayName']), strtolower($search));
+                               });
+                       return array_map(
+                               function ($user) {
+                                       return $this->getUserMock($user['uid'], $user['displayName']);
+                               },
+                               $users
+                       );
+               });
 
                $this->groupManager->method('displayNamesInGroup')
                        ->willReturn($userResults);