From 7e2f41404fc7ffb36bf578d0a295b791bb2c733f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Pablo=20Villaf=C3=A1=C3=B1ez?= Date: Fri, 22 Jul 2022 10:56:33 +0200 Subject: [PATCH 1/7] Expose group's displayname --- lib/Access.php | 41 +++++++++++++---------------------------- lib/Group_LDAP.php | 17 ++++++++++++++++- lib/Group_Proxy.php | 3 +++ lib/User/IUserTools.php | 2 +- 4 files changed, 33 insertions(+), 30 deletions(-) diff --git a/lib/Access.php b/lib/Access.php index fdb415ec5..cabc4d62f 100644 --- a/lib/Access.php +++ b/lib/Access.php @@ -476,7 +476,7 @@ public function username2dn($name) { * @return string|false with the name to use in ownCloud, false on DN outside of search DN * @throws \OC\ServerNotAvailableException */ - public function dn2groupname($fdn, $ldapName = null) { + public function dn2groupname($fdn) { //To avoid bypassing the base DN settings under certain circumstances //with the group support, check whether the provided DN matches one of //the given Bases @@ -484,7 +484,7 @@ public function dn2groupname($fdn, $ldapName = null) { return false; } - return $this->dn2ocname($fdn, $ldapName, false); + return $this->dn2ocname($fdn, false); } /** @@ -534,7 +534,7 @@ public function groupsMatchFilter($groupDNs) { * @return string|false with with the name to use in ownCloud * @throws \OC\ServerNotAvailableException */ - public function dn2username($fdn, $ldapName = null) { + public function dn2username($fdn) { //To avoid bypassing the base DN settings under certain circumstances //with the group support, check whether the provided DN matches one of //the given Bases @@ -542,7 +542,7 @@ public function dn2username($fdn, $ldapName = null) { return false; } - return $this->dn2ocname($fdn, $ldapName, true); + return $this->dn2ocname($fdn, true); } /** @@ -555,13 +555,15 @@ public function dn2username($fdn, $ldapName = null) { * @throws \BadMethodCallException * @throws \OC\ServerNotAvailableException */ - public function dn2ocname($fdn, $ldapDisplayName = null, $isUser = true) { + public function dn2ocname($fdn, $isUser = true) { if ($isUser) { $mapper = $this->getUserMapper(); $displayNameAttribute = $this->connection->ldapUserDisplayName; + $nameAttribute = (string)$this->connection->ldapExpertUsernameAttr; } else { $mapper = $this->getGroupMapper(); $displayNameAttribute = $this->connection->ldapGroupDisplayName; + $nameAttribute = (string)$this->connection->ldapExpertGroupnameAttr; } //let's try to retrieve the ownCloud name from the mappings table @@ -587,30 +589,13 @@ public function dn2ocname($fdn, $ldapDisplayName = null, $isUser = true) { return false; } - if ($ldapDisplayName === null) { - $ldapDisplayName = $this->readAttribute($fdn, $displayNameAttribute); - if (!isset($ldapDisplayName[0]) && empty($ldapDisplayName[0])) { - \OC::$server->getLogger()->error( - "No or empty name for $fdn.", - ['app' => 'user_ldap'] - ); - return false; - } - $ldapDisplayName = $ldapDisplayName[0]; - } - - if ($isUser) { - $usernameAttribute = (string)$this->connection->ldapExpertUsernameAttr; - if ($usernameAttribute !== '') { - $username = $this->readAttribute($fdn, $usernameAttribute); - $username = $username[0]; - } else { - $username = $uuid; - } - $intName = $this->sanitizeUsername($username); + if ($nameAttribute !== '') { + $name = $this->readAttribute($fdn, $nameAttribute); + $name = $name[0]; } else { - $intName = $ldapDisplayName; + $name = $uuid; } + $intName = $this->sanitizeUsername($name); //a new user/group! Add it only if it doesn't conflict with other backend's users or existing groups //disabling Cache is required to avoid that the new user is cached as not-existing in fooExists check @@ -723,7 +708,7 @@ private function ldap2ownCloudNames($ldapObjects, $isUsers) { $nameByLDAP = $ldapObject[$nameAttribute][0]; } - $ocName = $this->dn2ocname($ldapObject['dn'][0], $nameByLDAP, $isUsers); + $ocName = $this->dn2ocname($ldapObject['dn'][0], $isUsers); if ($ocName) { $ownCloudNames[$ldapObject['dn'][0]] = $ocName; } diff --git a/lib/Group_LDAP.php b/lib/Group_LDAP.php index 383022849..bd586db73 100644 --- a/lib/Group_LDAP.php +++ b/lib/Group_LDAP.php @@ -1001,6 +1001,21 @@ public function groupExists($gid) { return true; } + public function getGroupDetails($gid) { + $dn = $this->access->groupname2dn($gid); + if ($dn === false) { + // FIXME: It seems local groups also end up going through here... + return null; + } + + $attr = $this->access->getConnection()->ldapGroupDisplayName; + $displayname = $this->access->readAttribute($dn, $attr); + return [ + 'gid' => $gid, + 'displayName' => $displayname[0], + ]; + } + /** * Check if backend implements actions * @param int $actions bitwise-or'ed actions @@ -1010,7 +1025,7 @@ public function groupExists($gid) { * compared with OC_USER_BACKEND_CREATE_USER etc. */ public function implementsActions($actions) { - return (bool)(\OC\Group\Backend::COUNT_USERS & $actions); + return (bool)((\OC\Group\Backend::COUNT_USERS | \OC\Group\Backend::GROUP_DETAILS) & $actions); } /** diff --git a/lib/Group_Proxy.php b/lib/Group_Proxy.php index 58eb6b0cb..bb2c72f9b 100644 --- a/lib/Group_Proxy.php +++ b/lib/Group_Proxy.php @@ -207,6 +207,9 @@ public function groupExists($gid) { return $this->handleRequest($gid, 'groupExists', [$gid]); } + public function getGroupDetails($gid) { + return $this->handleRequest($gid, 'getGroupDetails', [$gid]); + } /** * Check if backend implements actions * @param int $actions bitwise-or'ed actions diff --git a/lib/User/IUserTools.php b/lib/User/IUserTools.php index d85c615e3..752d24128 100644 --- a/lib/User/IUserTools.php +++ b/lib/User/IUserTools.php @@ -33,7 +33,7 @@ public function getConnection(); public function readAttribute($dn, $attr, $filter = 'objectClass=*'); - public function dn2username($dn, $ldapname = null); + public function dn2username($dn); /** * returns the LDAP DN for the given internal ownCloud name of the user From 9f7a03ad7918e49312557fb1a7cf691026af7bba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Pablo=20Villaf=C3=A1=C3=B1ez?= Date: Fri, 22 Jul 2022 15:13:06 +0200 Subject: [PATCH 2/7] Allow configuration of internal groupname attr --- js/wizard/wizardTabExpert.js | 13 +++++++++++++ lib/Access.php | 3 --- lib/Configuration.php | 4 ++++ lib/Connection.php | 1 + templates/settings.php | 8 ++++++++ 5 files changed, 26 insertions(+), 3 deletions(-) diff --git a/js/wizard/wizardTabExpert.js b/js/wizard/wizardTabExpert.js index 7cfd49ba0..d241b9a28 100644 --- a/js/wizard/wizardTabExpert.js +++ b/js/wizard/wizardTabExpert.js @@ -28,6 +28,10 @@ OCA = OCA || {}; $element: $('#ldap_expert_username_attr'), setMethod: 'setUsernameAttribute' }, + ldap_expert_groupname_attr: { + $element: $('#ldap_expert_groupname_attr'), + setMethod: 'setGroupnameAttribute' + }, ldap_expert_uuid_user_attr: { $element: $('#ldap_expert_uuid_user_attr'), setMethod: 'setUserUUIDAttribute' @@ -73,6 +77,15 @@ OCA = OCA || {}; this.setElementValue(this.managedItems.ldap_expert_username_attr.$element, attribute); }, + /** + * sets the attribute to be used to create an ownCloud ID (username) + * + * @param {string} attribute + */ + setGroupnameAttribute: function(attribute) { + this.setElementValue(this.managedItems.ldap_expert_groupname_attr.$element, attribute); + }, + /** * sets the attribute that provides an unique identifier per LDAP user * entry diff --git a/lib/Access.php b/lib/Access.php index cabc4d62f..e21c9a5a2 100644 --- a/lib/Access.php +++ b/lib/Access.php @@ -472,7 +472,6 @@ public function username2dn($name) { * returns the internal ownCloud name for the given LDAP DN of the group, false on DN outside of search DN or failure * * @param string $fdn the dn of the group object - * @param string $ldapName optional, the display name of the object * @return string|false with the name to use in ownCloud, false on DN outside of search DN * @throws \OC\ServerNotAvailableException */ @@ -530,7 +529,6 @@ public function groupsMatchFilter($groupDNs) { * returns the internal ownCloud name for the given LDAP DN of the user, false on DN outside of search DN or failure * * @param string $fdn the dn of the user object - * @param string $ldapName optional, the display name of the object * @return string|false with with the name to use in ownCloud * @throws \OC\ServerNotAvailableException */ @@ -549,7 +547,6 @@ public function dn2username($fdn) { * returns an internal ownCloud name for the given LDAP DN, false on DN outside of search DN * * @param string $fdn the dn of the user object - * @param string $ldapDisplayName optional, the display name of the object * @param bool $isUser optional, whether it is a user object (otherwise group assumed) * @return string|false with with the name to use in ownCloud * @throws \BadMethodCallException diff --git a/lib/Configuration.php b/lib/Configuration.php index 306c95131..ed235612e 100644 --- a/lib/Configuration.php +++ b/lib/Configuration.php @@ -80,6 +80,7 @@ * @property string $hasMemberOfFilterSupport, * @property string $useMemberOfToDetectMembership, * @property string $ldapExpertUsernameAttr, + * @property string $ldapExpertGroupnameAttr, * @property string $ldapExpertUUIDUserAttr, * @property string $ldapExpertUUIDGroupAttr, * @property string $lastJpegPhotoLookup, @@ -148,6 +149,7 @@ class Configuration { 'hasMemberOfFilterSupport' => false, 'useMemberOfToDetectMembership' => true, 'ldapExpertUsernameAttr' => null, + 'ldapExpertGroupnameAttr' => null, 'ldapExpertUUIDUserAttr' => null, 'ldapExpertUUIDGroupAttr' => null, 'lastJpegPhotoLookup' => null, @@ -546,6 +548,7 @@ public function getDefaults() { 'ldap_attributes_for_user_search' => '', 'ldap_attributes_for_group_search' => '', 'ldap_expert_username_attr' => '', + 'ldap_expert_groupname_attr' => '', 'ldap_expert_uuid_user_attr' => '', 'ldap_expert_uuid_group_attr' => '', 'has_memberof_filter_support' => 0, @@ -605,6 +608,7 @@ public function getConfigTranslationArray() { 'ldap_attributes_for_user_search' => 'ldapAttributesForUserSearch', 'ldap_attributes_for_group_search' => 'ldapAttributesForGroupSearch', 'ldap_expert_username_attr' => 'ldapExpertUsernameAttr', + 'ldap_expert_groupname_attr' => 'ldapExpertGroupnameAttr', 'ldap_expert_uuid_user_attr' => 'ldapExpertUUIDUserAttr', 'ldap_expert_uuid_group_attr' => 'ldapExpertUUIDGroupAttr', 'has_memberof_filter_support' => 'hasMemberOfFilterSupport', diff --git a/lib/Connection.php b/lib/Connection.php index d20032bd0..ccb8a7e4a 100644 --- a/lib/Connection.php +++ b/lib/Connection.php @@ -61,6 +61,7 @@ * @property string $ldapQuotaAttribute * @property string $ldapEmailAttribute * @property string $ldapExpertUsernameAttr + * @property string $ldapExpertGroupnameAttr * @property string $homeFolderNamingRule * @property array $ldapAttributesForUserSearch * @property string $ldapUuidUserAttribute diff --git a/templates/settings.php b/templates/settings.php index 90e74807e..7563f9488 100644 --- a/templates/settings.php +++ b/templates/settings.php @@ -290,6 +290,14 @@ +
+

t('Internal Groupname')); ?>

+

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.')); ?>

+
+ + +
+

t('Override UUID detection')); ?>

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.')); ?>

From b86e4447c21973952d2e5e7e3aabd3d55517ffed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Pablo=20Villaf=C3=A1=C3=B1ez?= Date: Mon, 25 Jul 2022 10:17:00 +0200 Subject: [PATCH 3/7] Cache details and include migration --- appinfo/Migrations/Version20220725070804.php | 37 ++++++++++++++++++++ appinfo/info.xml | 2 +- lib/Group_LDAP.php | 11 +++++- 3 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 appinfo/Migrations/Version20220725070804.php diff --git a/appinfo/Migrations/Version20220725070804.php b/appinfo/Migrations/Version20220725070804.php new file mode 100644 index 000000000..1481572ea --- /dev/null +++ b/appinfo/Migrations/Version20220725070804.php @@ -0,0 +1,37 @@ +config = $config; + $this->helper = $helper; + } + /** + * @param IOutput $out + */ + public function run(IOutput $out) { + $prefixes = $this->helper->getServerConfigurationPrefixes(); + foreach ($prefixes as $prefix) { + $groupnameValue = $this->config->getAppValue('user_ldap', "{$prefix}ldap_expert_groupname_attr", null); + if ($groupnameValue === null) { + $groupDisplaynameValue = $this->config->getAppValue('user_ldap', "{$prefix}ldap_group_display_name", null); + if ($groupDisplaynameValue !== null) { + $this->config->setAppValue('user_ldap', "{$prefix}ldap_expert_groupname_attr", $groupDisplaynameValue); + } + } + } + } +} diff --git a/appinfo/info.xml b/appinfo/info.xml index c9b4374ac..8fb8a2fa5 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -14,7 +14,7 @@ More information is available in the [LDAP User and Group Backend documentation] AGPL Jörn Friedrich Dreyer, Tom Needham, Juan Pablo Villafañez Ramos, Dominik Schmidt and Arthur Schiwon - 0.16.0 + 0.17.0 diff --git a/lib/Group_LDAP.php b/lib/Group_LDAP.php index bd586db73..1ad6542ca 100644 --- a/lib/Group_LDAP.php +++ b/lib/Group_LDAP.php @@ -1002,6 +1002,12 @@ public function groupExists($gid) { } public function getGroupDetails($gid) { + $cacheKey = "groupDetails-$gid"; + $details = $this->access->getConnection()->getFromCache($cacheKey); + if ($details !== null) { + return $details; + } + $dn = $this->access->groupname2dn($gid); if ($dn === false) { // FIXME: It seems local groups also end up going through here... @@ -1010,10 +1016,13 @@ public function getGroupDetails($gid) { $attr = $this->access->getConnection()->ldapGroupDisplayName; $displayname = $this->access->readAttribute($dn, $attr); - return [ + + $details = [ 'gid' => $gid, 'displayName' => $displayname[0], ]; + $this->access->getConnection()->writeToCache($cacheKey, $details); + return $details; } /** From 4f0d1ddd11125122b07d3706e1de83de603ba852 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Pablo=20Villaf=C3=A1=C3=B1ez?= Date: Mon, 25 Jul 2022 10:52:17 +0200 Subject: [PATCH 4/7] Remove dead code --- lib/Access.php | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/lib/Access.php b/lib/Access.php index e21c9a5a2..f24c88468 100644 --- a/lib/Access.php +++ b/lib/Access.php @@ -555,11 +555,9 @@ public function dn2username($fdn) { public function dn2ocname($fdn, $isUser = true) { if ($isUser) { $mapper = $this->getUserMapper(); - $displayNameAttribute = $this->connection->ldapUserDisplayName; $nameAttribute = (string)$this->connection->ldapExpertUsernameAttr; } else { $mapper = $this->getGroupMapper(); - $displayNameAttribute = $this->connection->ldapGroupDisplayName; $nameAttribute = (string)$this->connection->ldapExpertGroupnameAttr; } @@ -690,21 +688,9 @@ public function ownCloudGroupNames($ldapGroups) { * @throws \OC\ServerNotAvailableException */ private function ldap2ownCloudNames($ldapObjects, $isUsers) { - if ($isUsers) { - $nameAttribute = $this->connection->ldapUserDisplayName; - $sndAttribute = $this->connection->ldapUserDisplayName2; - } else { - $nameAttribute = $this->connection->ldapGroupDisplayName; - } $ownCloudNames = []; foreach ($ldapObjects as $ldapObject) { - $nameByLDAP = null; - if (isset($ldapObject[$nameAttribute][0])) { - // might be set, but not necessarily. if so, we use it. - $nameByLDAP = $ldapObject[$nameAttribute][0]; - } - $ocName = $this->dn2ocname($ldapObject['dn'][0], $isUsers); if ($ocName) { $ownCloudNames[$ldapObject['dn'][0]] = $ocName; From 9ddbf086360289083da8525dfc59d0dc0e266e44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Pablo=20Villaf=C3=A1=C3=B1ez?= Date: Tue, 26 Jul 2022 16:59:09 +0200 Subject: [PATCH 5/7] Fix comment --- js/wizard/wizardTabExpert.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/wizard/wizardTabExpert.js b/js/wizard/wizardTabExpert.js index d241b9a28..ab7fe717c 100644 --- a/js/wizard/wizardTabExpert.js +++ b/js/wizard/wizardTabExpert.js @@ -78,7 +78,7 @@ OCA = OCA || {}; }, /** - * sets the attribute to be used to create an ownCloud ID (username) + * sets the attribute to be used to create an ownCloud ID (groupname) * * @param {string} attribute */ From 6144bfd4b87d566fb15bfbecd22dd33c10c33574 Mon Sep 17 00:00:00 2001 From: Phil Davis Date: Tue, 2 Aug 2022 17:31:53 +0545 Subject: [PATCH 6/7] Define ldapExpertGroupnameAttr in acceptance test CI --- tests/acceptance/setConfig.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/acceptance/setConfig.sh b/tests/acceptance/setConfig.sh index 562561de1..ce7bf2566 100755 --- a/tests/acceptance/setConfig.sh +++ b/tests/acceptance/setConfig.sh @@ -7,6 +7,7 @@ configID="LDAPTestId" ./occ ldap:set-config "$configID" ldapBaseGroups "dc=owncloud,dc=com" ./occ ldap:set-config "$configID" ldapBaseUsers "dc=owncloud,dc=com" ./occ ldap:set-config "$configID" ldapEmailAttribute "mail" +./occ ldap:set-config "$configID" ldapExpertGroupnameAttr "cn" ./occ ldap:set-config "$configID" ldapExpertUUIDUserAttr "uid" ./occ ldap:set-config "$configID" ldapGroupDisplayName "cn" ./occ ldap:set-config "$configID" ldapGroupFilter "(&(|(objectclass=posixGroup)))" From 8b21e28ce57a2caef1035a3b79fc6ce557197ccc Mon Sep 17 00:00:00 2001 From: Phil Davis Date: Tue, 2 Aug 2022 21:40:31 +0545 Subject: [PATCH 7/7] Avoid array offset error in getGroupDetails when group does not exist --- lib/Group_LDAP.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/Group_LDAP.php b/lib/Group_LDAP.php index 1ad6542ca..76811da8a 100644 --- a/lib/Group_LDAP.php +++ b/lib/Group_LDAP.php @@ -1011,15 +1011,26 @@ public function getGroupDetails($gid) { $dn = $this->access->groupname2dn($gid); if ($dn === false) { // FIXME: It seems local groups also end up going through here... + // Because OC\Group]Manager\getGroupObject loops through all backends + // and when the backend implementsActions(\OC\Group\Backend::GROUP_DETAILS) + // it calls getGroupDetails without checking if the group even exists in + // that backend. return null; } $attr = $this->access->getConnection()->ldapGroupDisplayName; $displayname = $this->access->readAttribute($dn, $attr); + if (\is_array($displayname)) { + $groupDisplayName = $displayname[0]; + } else { + // The attribute was not found, maybe because the group does not even exist + // Default to the group id + $groupDisplayName = $gid; + } $details = [ 'gid' => $gid, - 'displayName' => $displayname[0], + 'displayName' => $groupDisplayName, ]; $this->access->getConnection()->writeToCache($cacheKey, $details); return $details;