Skip to content

Commit ed8495d

Browse files
authored
Merge pull request #748 from owncloud/expose_group_displayname
[full-ci] Expose group's displayname
2 parents 9b270c7 + 77f72ac commit ed8495d

File tree

11 files changed

+123
-47
lines changed

11 files changed

+123
-47
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
namespace OCA\User_LDAP\Migrations;
3+
4+
use OCP\Migration\ISimpleMigration;
5+
use OCP\Migration\IOutput;
6+
use OCP\IConfig;
7+
use OCA\User_LDAP\Helper;
8+
9+
class Version20220725070804 implements ISimpleMigration {
10+
/** @var IConfig */
11+
private $config;
12+
/** @var $helper */
13+
private $helper;
14+
15+
/**
16+
* @param IConfig $config
17+
*/
18+
public function __construct(IConfig $config, Helper $helper) {
19+
$this->config = $config;
20+
$this->helper = $helper;
21+
}
22+
/**
23+
* @param IOutput $out
24+
*/
25+
public function run(IOutput $out) {
26+
$prefixes = $this->helper->getServerConfigurationPrefixes();
27+
foreach ($prefixes as $prefix) {
28+
$groupnameValue = $this->config->getAppValue('user_ldap', "{$prefix}ldap_expert_groupname_attr", null);
29+
if ($groupnameValue === null) {
30+
$groupDisplaynameValue = $this->config->getAppValue('user_ldap', "{$prefix}ldap_group_display_name", null);
31+
if ($groupDisplaynameValue !== null) {
32+
$this->config->setAppValue('user_ldap', "{$prefix}ldap_expert_groupname_attr", $groupDisplaynameValue);
33+
}
34+
}
35+
}
36+
}
37+
}

appinfo/info.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ More information is available in the [LDAP User and Group Backend documentation]
1414
</description>
1515
<licence>AGPL</licence>
1616
<author>Jörn Friedrich Dreyer, Tom Needham, Juan Pablo Villafañez Ramos, Dominik Schmidt and Arthur Schiwon</author>
17-
<version>0.16.1</version>
17+
<version>0.17.0</version>
1818
<types>
1919
<authentication/>
2020
</types>
@@ -25,7 +25,7 @@ More information is available in the [LDAP User and Group Backend documentation]
2525
<screenshot>https://raw.githubusercontent.com/owncloud/screenshots/master/user_ldap/ownCloud-app-ldap-user-management.jpg</screenshot>
2626
<dependencies>
2727
<lib>ldap</lib>
28-
<owncloud min-version="10.4" max-version="10" />
28+
<owncloud min-version="10.11" max-version="10" />
2929
</dependencies>
3030

3131
<namespace>User_LDAP</namespace>

js/wizard/wizardTabExpert.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ OCA = OCA || {};
2828
$element: $('#ldap_expert_username_attr'),
2929
setMethod: 'setUsernameAttribute'
3030
},
31+
ldap_expert_groupname_attr: {
32+
$element: $('#ldap_expert_groupname_attr'),
33+
setMethod: 'setGroupnameAttribute'
34+
},
3135
ldap_expert_uuid_user_attr: {
3236
$element: $('#ldap_expert_uuid_user_attr'),
3337
setMethod: 'setUserUUIDAttribute'
@@ -73,6 +77,15 @@ OCA = OCA || {};
7377
this.setElementValue(this.managedItems.ldap_expert_username_attr.$element, attribute);
7478
},
7579

80+
/**
81+
* sets the attribute to be used to create an ownCloud ID (groupname)
82+
*
83+
* @param {string} attribute
84+
*/
85+
setGroupnameAttribute: function(attribute) {
86+
this.setElementValue(this.managedItems.ldap_expert_groupname_attr.$element, attribute);
87+
},
88+
7689
/**
7790
* sets the attribute that provides an unique identifier per LDAP user
7891
* entry

lib/Access.php

Lines changed: 15 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -474,19 +474,18 @@ public function username2dn($name) {
474474
* returns the internal ownCloud name for the given LDAP DN of the group, false on DN outside of search DN or failure
475475
*
476476
* @param string $fdn the dn of the group object
477-
* @param string $ldapName optional, the display name of the object
478477
* @return string|false with the name to use in ownCloud, false on DN outside of search DN
479478
* @throws \OC\ServerNotAvailableException
480479
*/
481-
public function dn2groupname($fdn, $ldapName = null) {
480+
public function dn2groupname($fdn) {
482481
//To avoid bypassing the base DN settings under certain circumstances
483482
//with the group support, check whether the provided DN matches one of
484483
//the given Bases
485484
if (!$this->isDNPartOfBase($fdn, $this->connection->ldapBaseGroups)) {
486485
return false;
487486
}
488487

489-
return $this->dn2ocname($fdn, $ldapName, false);
488+
return $this->dn2ocname($fdn, false);
490489
}
491490

492491
/**
@@ -532,38 +531,36 @@ public function groupsMatchFilter($groupDNs) {
532531
* returns the internal ownCloud name for the given LDAP DN of the user, false on DN outside of search DN or failure
533532
*
534533
* @param string $fdn the dn of the user object
535-
* @param string $ldapName optional, the display name of the object
536534
* @return string|false with with the name to use in ownCloud
537535
* @throws \OC\ServerNotAvailableException
538536
*/
539-
public function dn2username($fdn, $ldapName = null) {
537+
public function dn2username($fdn) {
540538
//To avoid bypassing the base DN settings under certain circumstances
541539
//with the group support, check whether the provided DN matches one of
542540
//the given Bases
543541
if (!$this->isDNPartOfBase($fdn, $this->connection->ldapBaseUsers)) {
544542
return false;
545543
}
546544

547-
return $this->dn2ocname($fdn, $ldapName, true);
545+
return $this->dn2ocname($fdn, true);
548546
}
549547

550548
/**
551549
* returns an internal ownCloud name for the given LDAP DN, false on DN outside of search DN
552550
*
553551
* @param string $fdn the dn of the user object
554-
* @param string $ldapDisplayName optional, the display name of the object
555552
* @param bool $isUser optional, whether it is a user object (otherwise group assumed)
556553
* @return string|false with with the name to use in ownCloud
557554
* @throws \BadMethodCallException
558555
* @throws \OC\ServerNotAvailableException
559556
*/
560-
public function dn2ocname($fdn, $ldapDisplayName = null, $isUser = true) {
557+
public function dn2ocname($fdn, $isUser = true) {
561558
if ($isUser) {
562559
$mapper = $this->getUserMapper();
563-
$displayNameAttribute = $this->connection->ldapUserDisplayName;
560+
$nameAttribute = (string)$this->connection->ldapExpertUsernameAttr;
564561
} else {
565562
$mapper = $this->getGroupMapper();
566-
$displayNameAttribute = $this->connection->ldapGroupDisplayName;
563+
$nameAttribute = (string)$this->connection->ldapExpertGroupnameAttr;
567564
}
568565

569566
//let's try to retrieve the ownCloud name from the mappings table
@@ -589,29 +586,16 @@ public function dn2ocname($fdn, $ldapDisplayName = null, $isUser = true) {
589586
return false;
590587
}
591588

592-
if ($ldapDisplayName === null) {
593-
$ldapDisplayName = $this->readAttribute($fdn, $displayNameAttribute);
594-
if (!isset($ldapDisplayName[0]) && empty($ldapDisplayName[0])) {
595-
\OC::$server->getLogger()->error(
596-
"No or empty name for $fdn.",
597-
['app' => 'user_ldap']
598-
);
599-
return false;
600-
}
601-
$ldapDisplayName = $ldapDisplayName[0];
589+
if ($nameAttribute !== '') {
590+
$name = $this->readAttribute($fdn, $nameAttribute);
591+
$name = $name[0];
592+
} else {
593+
$name = $uuid;
602594
}
603595

596+
$intName = $name;
604597
if ($isUser) {
605-
$usernameAttribute = (string)$this->connection->ldapExpertUsernameAttr;
606-
if ($usernameAttribute !== '') {
607-
$username = $this->readAttribute($fdn, $usernameAttribute);
608-
$username = $username[0];
609-
} else {
610-
$username = $uuid;
611-
}
612-
$intName = $this->sanitizeUsername($username);
613-
} else {
614-
$intName = $ldapDisplayName;
598+
$intName = $this->sanitizeUsername($name);
615599
}
616600

617601
//a new user/group! Add it only if it doesn't conflict with other backend's users or existing groups
@@ -709,22 +693,10 @@ public function ownCloudGroupNames($ldapGroups) {
709693
* @throws \OC\ServerNotAvailableException
710694
*/
711695
private function ldap2ownCloudNames($ldapObjects, $isUsers) {
712-
if ($isUsers) {
713-
$nameAttribute = $this->connection->ldapUserDisplayName;
714-
$sndAttribute = $this->connection->ldapUserDisplayName2;
715-
} else {
716-
$nameAttribute = $this->connection->ldapGroupDisplayName;
717-
}
718696
$ownCloudNames = [];
719697

720698
foreach ($ldapObjects as $ldapObject) {
721-
$nameByLDAP = null;
722-
if (isset($ldapObject[$nameAttribute][0])) {
723-
// might be set, but not necessarily. if so, we use it.
724-
$nameByLDAP = $ldapObject[$nameAttribute][0];
725-
}
726-
727-
$ocName = $this->dn2ocname($ldapObject['dn'][0], $nameByLDAP, $isUsers);
699+
$ocName = $this->dn2ocname($ldapObject['dn'][0], $isUsers);
728700
if ($ocName) {
729701
$ownCloudNames[$ldapObject['dn'][0]] = $ocName;
730702
}

lib/Configuration.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
* @property string $hasMemberOfFilterSupport,
8181
* @property string $useMemberOfToDetectMembership,
8282
* @property string $ldapExpertUsernameAttr,
83+
* @property string $ldapExpertGroupnameAttr,
8384
* @property string $ldapExpertUUIDUserAttr,
8485
* @property string $ldapExpertUUIDGroupAttr,
8586
* @property string $lastJpegPhotoLookup,
@@ -148,6 +149,7 @@ class Configuration {
148149
'hasMemberOfFilterSupport' => false,
149150
'useMemberOfToDetectMembership' => true,
150151
'ldapExpertUsernameAttr' => null,
152+
'ldapExpertGroupnameAttr' => null,
151153
'ldapExpertUUIDUserAttr' => null,
152154
'ldapExpertUUIDGroupAttr' => null,
153155
'lastJpegPhotoLookup' => null,
@@ -546,6 +548,7 @@ public function getDefaults() {
546548
'ldap_attributes_for_user_search' => '',
547549
'ldap_attributes_for_group_search' => '',
548550
'ldap_expert_username_attr' => '',
551+
'ldap_expert_groupname_attr' => '',
549552
'ldap_expert_uuid_user_attr' => '',
550553
'ldap_expert_uuid_group_attr' => '',
551554
'has_memberof_filter_support' => 0,
@@ -605,6 +608,7 @@ public function getConfigTranslationArray() {
605608
'ldap_attributes_for_user_search' => 'ldapAttributesForUserSearch',
606609
'ldap_attributes_for_group_search' => 'ldapAttributesForGroupSearch',
607610
'ldap_expert_username_attr' => 'ldapExpertUsernameAttr',
611+
'ldap_expert_groupname_attr' => 'ldapExpertGroupnameAttr',
608612
'ldap_expert_uuid_user_attr' => 'ldapExpertUUIDUserAttr',
609613
'ldap_expert_uuid_group_attr' => 'ldapExpertUUIDGroupAttr',
610614
'has_memberof_filter_support' => 'hasMemberOfFilterSupport',

lib/Connection.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
* @property string $ldapQuotaAttribute
6262
* @property string $ldapEmailAttribute
6363
* @property string $ldapExpertUsernameAttr
64+
* @property string $ldapExpertGroupnameAttr
6465
* @property string $homeFolderNamingRule
6566
* @property array $ldapAttributesForUserSearch
6667
* @property string $ldapUuidUserAttribute

lib/Group_LDAP.php

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1001,6 +1001,43 @@ public function groupExists($gid) {
10011001
return true;
10021002
}
10031003

1004+
/**
1005+
* Get the details of the target group. The details consist (as of now)
1006+
* of the gid and the displayname of the group. More data might be added
1007+
* accordingly to the interface.
1008+
* The method returns null on error (such as missing displayname, or
1009+
* missing group)
1010+
* @param string $gid the gid of the group we want to get the details of
1011+
* @return array|null an array containing the gid and the displayname such as
1012+
* ['gid' => 'abcdef', 'displayname' => 'my group']
1013+
*/
1014+
public function getGroupDetails($gid) {
1015+
$cacheKey = "groupDetails-$gid";
1016+
$details = $this->access->getConnection()->getFromCache($cacheKey);
1017+
if ($details !== null) {
1018+
return $details;
1019+
}
1020+
1021+
$dn = $this->access->groupname2dn($gid);
1022+
if ($dn === false) {
1023+
return null;
1024+
}
1025+
1026+
$attr = $this->access->getConnection()->ldapGroupDisplayName;
1027+
$displayname = $this->access->readAttribute($dn, $attr);
1028+
if (!\is_array($displayname)) {
1029+
// displayname attr not found
1030+
return null;
1031+
}
1032+
1033+
$details = [
1034+
'gid' => $gid,
1035+
'displayName' => $displayname[0],
1036+
];
1037+
$this->access->getConnection()->writeToCache($cacheKey, $details);
1038+
return $details;
1039+
}
1040+
10041041
/**
10051042
* Check if backend implements actions
10061043
* @param int $actions bitwise-or'ed actions
@@ -1010,7 +1047,7 @@ public function groupExists($gid) {
10101047
* compared with OC_USER_BACKEND_CREATE_USER etc.
10111048
*/
10121049
public function implementsActions($actions) {
1013-
return (bool)(\OC\Group\Backend::COUNT_USERS & $actions);
1050+
return (bool)((\OC\Group\Backend::COUNT_USERS | \OC\Group\Backend::GROUP_DETAILS) & $actions);
10141051
}
10151052

10161053
/**

lib/Group_Proxy.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@ public function groupExists($gid) {
207207
return $this->handleRequest($gid, 'groupExists', [$gid]);
208208
}
209209

210+
public function getGroupDetails($gid) {
211+
return $this->handleRequest($gid, 'getGroupDetails', [$gid]);
212+
}
210213
/**
211214
* Check if backend implements actions
212215
* @param int $actions bitwise-or'ed actions

lib/User/IUserTools.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function getConnection();
3333

3434
public function readAttribute($dn, $attr, $filter = 'objectClass=*');
3535

36-
public function dn2username($dn, $ldapname = null);
36+
public function dn2username($dn);
3737

3838
/**
3939
* returns the LDAP DN for the given internal ownCloud name of the user

templates/settings.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,14 @@
290290
<input type="text" id="ldap_expert_username_attr" name="ldap_expert_username_attr" data-default="<?php p($_['ldap_expert_username_attr_default']); ?>" />
291291
</div>
292292
</section>
293+
<section>
294+
<h3><?php p($l->t('Internal Groupname')); ?></h3>
295+
<p><?php p($l->t('The internal groupname is used to uniquely identify the group. It has the same restrictions as the internal username, in particular, the group name must be immutable and unique. By default, the UUID will be used. This internal groupname won\'t likely by visible because a displayname attribute is intended to be used to show the group.')); ?></p>
296+
<div class="tablerow">
297+
<label for="ldap_expert_groupname_attr"><?php p($l->t('Internal Groupname Attribute:')); ?></label>
298+
<input type="text" id="ldap_expert_groupname_attr" name="ldap_expert_groupname_attr" data-default="<?php p($_['ldap_expert_groupname_attr_default']); ?>" />
299+
</div>
300+
</section>
293301
<section>
294302
<h3><?php p($l->t('Override UUID detection')); ?></h3>
295303
<p><?php p($l->t('By default, the UUID attribute is automatically detected. The UUID attribute is used to doubtlessly identify LDAP users and groups. Also, the internal username will be created based on the UUID, if not specified otherwise above. You can override the setting and pass an attribute of your choice. You must make sure that the attribute of your choice can be fetched for both users and groups and it is unique. Leave it empty for default behavior. Changes will have effect only on newly mapped (added) LDAP users and groups.')); ?></p>

0 commit comments

Comments
 (0)