group display name support (service level + ldap)

Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
This commit is contained in:
Arthur Schiwon 2019-05-23 16:58:58 +02:00
parent 96e892770d
commit c6c8a41d2f
No known key found for this signature in database
GPG Key ID: 7424F1874854DF23
10 changed files with 129 additions and 3 deletions

View File

@ -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 = [];

View File

@ -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

View File

@ -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;

View File

@ -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 '';
}
}

View File

@ -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]);
}
}

View File

@ -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));
}
}

View File

@ -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',

View File

@ -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',

View File

@ -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;

View File

@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2019 Arthur Schiwon <blizzz@arthur-schiwon.de>
*
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
*
* @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 <http://www.gnu.org/licenses/>.
*
*/
namespace OCP\Group\Backend;
/**
* @since 17.0.0
*/
interface IGetDisplayNameBackend {
/**
* @since 17.0.0
*/
public function getDisplayName(string $gid): string;
}