From c6c8a41d2f34a05dbb8b24b280c767b022a3c338 Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Thu, 23 May 2019 16:58:58 +0200 Subject: [PATCH] group display name support (service level + ldap) Signed-off-by: Arthur Schiwon --- apps/files_external/ajax/applicable.php | 2 +- apps/user_ldap/lib/Access.php | 7 ++++ apps/user_ldap/lib/Connection.php | 1 + apps/user_ldap/lib/Group_LDAP.php | 28 ++++++++++++- apps/user_ldap/lib/Group_Proxy.php | 7 +++- apps/user_ldap/tests/Group_LDAPTest.php | 39 +++++++++++++++++++ lib/composer/composer/autoload_classmap.php | 1 + lib/composer/composer/autoload_static.php | 1 + lib/private/Group/Group.php | 10 +++++ .../Group/Backend/IGetDisplayNameBackend.php | 36 +++++++++++++++++ 10 files changed, 129 insertions(+), 3 deletions(-) create mode 100644 lib/public/Group/Backend/IGetDisplayNameBackend.php diff --git a/apps/files_external/ajax/applicable.php b/apps/files_external/ajax/applicable.php index a946962a29..016a4dc7f9 100644 --- a/apps/files_external/ajax/applicable.php +++ b/apps/files_external/ajax/applicable.php @@ -41,7 +41,7 @@ if (isset($_GET['offset'])) { $groups = []; foreach (\OC::$server->getGroupManager()->search($pattern, $limit, $offset) as $group) { - $groups[$group->getGID()] = $group->getGID(); + $groups[$group->getGID()] = $group->getDisplayName(); } $users = []; diff --git a/apps/user_ldap/lib/Access.php b/apps/user_ldap/lib/Access.php index 1044938446..6a074bbed2 100644 --- a/apps/user_ldap/lib/Access.php +++ b/apps/user_ldap/lib/Access.php @@ -712,6 +712,8 @@ class Access extends LDAPUtility { $sndName = isset($ldapObject[$sndAttribute][0]) ? $ldapObject[$sndAttribute][0] : ''; $this->cacheUserDisplayName($ncName, $nameByLDAP, $sndName); + } else if($nameByLDAP !== null) { + $this->cacheGroupDisplayName($ncName, $nameByLDAP); } } } @@ -765,6 +767,11 @@ class Access extends LDAPUtility { $this->connection->writeToCache($cacheKeyTrunk.$ocName, $displayName); } + public function cacheGroupDisplayName(string $ncName, string $displayName): void { + $cacheKey = 'group_getDisplayName' . $ncName; + $this->connection->writeToCache($cacheKey, $displayName); + } + /** * creates a unique name for internal Nextcloud use for users. Don't call it directly. * @param string $name the display name of the object diff --git a/apps/user_ldap/lib/Connection.php b/apps/user_ldap/lib/Connection.php index 4335f8e439..35770f082f 100644 --- a/apps/user_ldap/lib/Connection.php +++ b/apps/user_ldap/lib/Connection.php @@ -65,6 +65,7 @@ use OCP\ILogger; * @property bool|string ldapNestedGroups * @property string[] ldapBaseGroups * @property string ldapGroupFilter + * @property string ldapGroupDisplayName */ class Connection extends LDAPUtility { private $ldapConnectionRes = null; diff --git a/apps/user_ldap/lib/Group_LDAP.php b/apps/user_ldap/lib/Group_LDAP.php index cd4bd18cb4..ae2434056b 100644 --- a/apps/user_ldap/lib/Group_LDAP.php +++ b/apps/user_ldap/lib/Group_LDAP.php @@ -42,10 +42,11 @@ namespace OCA\User_LDAP; use OC\Cache\CappedMemoryCache; +use OCP\Group\Backend\IGetDisplayNameBackend; use OCP\GroupInterface; use OCP\ILogger; -class Group_LDAP extends BackendUtility implements \OCP\GroupInterface, IGroupLDAP { +class Group_LDAP extends BackendUtility implements \OCP\GroupInterface, IGroupLDAP, IGetDisplayNameBackend { protected $enabled = false; /** @@ -1221,4 +1222,29 @@ class Group_LDAP extends BackendUtility implements \OCP\GroupInterface, IGroupLD return $connection->getConnectionResource(); } + /** + * @throws \OC\ServerNotAvailableException + */ + public function getDisplayName(string $gid): string { + if ($this->groupPluginManager instanceof IGetDisplayNameBackend) { + return $this->groupPluginManager->getDisplayName($gid); + } + + $cacheKey = 'group_getDisplayName' . $gid; + if (!is_null($displayName = $this->access->connection->getFromCache($cacheKey))) { + return $displayName; + } + + $displayName = $this->access->readAttribute( + $this->access->groupname2dn($gid), + $this->access->connection->ldapGroupDisplayName); + + if ($displayName && (count($displayName) > 0)) { + $displayName = $displayName[0]; + $this->access->connection->writeToCache($cacheKey, $displayName); + return $displayName; + } + + return ''; + } } diff --git a/apps/user_ldap/lib/Group_Proxy.php b/apps/user_ldap/lib/Group_Proxy.php index 9fccb64cd4..c0599ad487 100644 --- a/apps/user_ldap/lib/Group_Proxy.php +++ b/apps/user_ldap/lib/Group_Proxy.php @@ -27,7 +27,9 @@ namespace OCA\User_LDAP; -class Group_Proxy extends Proxy implements \OCP\GroupInterface, IGroupLDAP { +use OCP\Group\Backend\IGetDisplayNameBackend; + +class Group_Proxy extends Proxy implements \OCP\GroupInterface, IGroupLDAP, IGetDisplayNameBackend { private $backends = array(); private $refBackend = null; @@ -272,4 +274,7 @@ class Group_Proxy extends Proxy implements \OCP\GroupInterface, IGroupLDAP { return $this->handleRequest($gid, 'getNewLDAPConnection', array($gid)); } + public function getDisplayName(string $gid): string { + return $this->handleRequest($gid, 'getDisplayName', [$gid]); + } } diff --git a/apps/user_ldap/tests/Group_LDAPTest.php b/apps/user_ldap/tests/Group_LDAPTest.php index cb5760e317..a505c403ac 100644 --- a/apps/user_ldap/tests/Group_LDAPTest.php +++ b/apps/user_ldap/tests/Group_LDAPTest.php @@ -1057,4 +1057,43 @@ class Group_LDAPTest extends TestCase { $this->assertEquals($expectedMembers, $resultingMembers, '', 0.0, 10, true); } + public function displayNameProvider() { + return [ + ['Graphic Novelists', ['Graphic Novelists']], + ['', false], + ]; + } + + /** + * @dataProvider displayNameProvider + */ + public function testGetDisplayName(string $expected, $ldapResult) { + $gid = 'graphic_novelists'; + + $access = $this->getAccessMock(); + $access->expects($this->atLeastOnce()) + ->method('readAttribute') + ->willReturn($ldapResult); + + $access->connection = $this->createMock(Connection::class); + $access->connection->expects($this->any()) + ->method('__get') + ->willReturnCallback(function($name) { + if($name === 'ldapGroupMemberAssocAttr') { + return 'member'; + } else if($name === 'ldapGroupFilter') { + return 'objectclass=nextcloudGroup'; + } else if($name === 'ldapGroupDisplayName') { + return 'cn'; + } + return null; + }); + + /** @var GroupPluginManager $pluginManager */ + $pluginManager = $this->createMock(GroupPluginManager::class); + + $ldap = new GroupLDAP($access, $pluginManager); + $this->assertSame($expected, $ldap->getDisplayName($gid)); + } + } diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index d9dd418ce0..b15cdddb3e 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -276,6 +276,7 @@ return array( 'OCP\\Group\\Backend\\ICountUsersBackend' => $baseDir . '/lib/public/Group/Backend/ICountUsersBackend.php', 'OCP\\Group\\Backend\\ICreateGroupBackend' => $baseDir . '/lib/public/Group/Backend/ICreateGroupBackend.php', 'OCP\\Group\\Backend\\IDeleteGroupBackend' => $baseDir . '/lib/public/Group/Backend/IDeleteGroupBackend.php', + 'OCP\\Group\\Backend\\IGetDisplayNameBackend' => $baseDir . '/lib/public/Group/Backend/IGetDisplayNameBackend.php', 'OCP\\Group\\Backend\\IGroupDetailsBackend' => $baseDir . '/lib/public/Group/Backend/IGroupDetailsBackend.php', 'OCP\\Group\\Backend\\IHideFromCollaborationBackend' => $baseDir . '/lib/public/Group/Backend/IHideFromCollaborationBackend.php', 'OCP\\Group\\Backend\\IIsAdminBackend' => $baseDir . '/lib/public/Group/Backend/IIsAdminBackend.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index d9693ab032..69c940353f 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -306,6 +306,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c 'OCP\\Group\\Backend\\ICountUsersBackend' => __DIR__ . '/../../..' . '/lib/public/Group/Backend/ICountUsersBackend.php', 'OCP\\Group\\Backend\\ICreateGroupBackend' => __DIR__ . '/../../..' . '/lib/public/Group/Backend/ICreateGroupBackend.php', 'OCP\\Group\\Backend\\IDeleteGroupBackend' => __DIR__ . '/../../..' . '/lib/public/Group/Backend/IDeleteGroupBackend.php', + 'OCP\\Group\\Backend\\IGetDisplayNameBackend' => __DIR__ . '/../../..' . '/lib/public/Group/Backend/IGetDisplayNameBackend.php', 'OCP\\Group\\Backend\\IGroupDetailsBackend' => __DIR__ . '/../../..' . '/lib/public/Group/Backend/IGroupDetailsBackend.php', 'OCP\\Group\\Backend\\IHideFromCollaborationBackend' => __DIR__ . '/../../..' . '/lib/public/Group/Backend/IHideFromCollaborationBackend.php', 'OCP\\Group\\Backend\\IIsAdminBackend' => __DIR__ . '/../../..' . '/lib/public/Group/Backend/IIsAdminBackend.php', diff --git a/lib/private/Group/Group.php b/lib/private/Group/Group.php index 3876c57596..a50a5ffde7 100644 --- a/lib/private/Group/Group.php +++ b/lib/private/Group/Group.php @@ -30,6 +30,7 @@ namespace OC\Group; +use OCP\Group\Backend\IGetDisplayNameBackend; use OCP\Group\Backend\IHideFromCollaborationBackend; use OC\Hooks\PublicEmitter; use OCP\GroupInterface; @@ -86,6 +87,15 @@ class Group implements IGroup { public function getDisplayName() { if (is_null($this->displayName)) { + foreach ($this->backends as $backend) { + if ($backend instanceof IGetDisplayNameBackend) { + $displayName = $backend->getDisplayName($this->gid); + if (trim($displayName) !== '') { + $this->displayName = $displayName; + return $this->displayName; + } + } + } return $this->gid; } return $this->displayName; diff --git a/lib/public/Group/Backend/IGetDisplayNameBackend.php b/lib/public/Group/Backend/IGetDisplayNameBackend.php new file mode 100644 index 0000000000..69d1742a1e --- /dev/null +++ b/lib/public/Group/Backend/IGetDisplayNameBackend.php @@ -0,0 +1,36 @@ + + * + * @author Arthur Schiwon + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCP\Group\Backend; + +/** + * @since 17.0.0 + */ +interface IGetDisplayNameBackend { + /** + * @since 17.0.0 + */ + public function getDisplayName(string $gid): string; + +}