Repository: owncloud/ocis
Pull request #5586 (2023-02-16 10:45:03)

Properly generate new DN.
This fixes issue #5581 by properly parsing the old DN and replacing the
first part with the new DN.

update-docs[bot]: Thanks for opening this pull request! The maintainers of this repository would appreciate it if you would create a [changelog](https://github.com/owncloud/ocis/blob/master/changelog/README.md) item based on your changes.

sonarcloud[bot]: Kudos, SonarCloud Quality Gate passed!&nbsp; &nbsp; [![Quality Gate passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/passed-16px.png 'Quality Gate passed')](https://sonarcloud.io/dashboard?id=owncloud_ocis&pullRequest=5586)

[![Bug](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/bug-16px.png 'Bug')](https://sonarcloud.io/project/issues?id=owncloud_ocis&pullRequest=5586&resolved=false&types=BUG) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=owncloud_ocis&pullRequest=5586&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=owncloud_ocis&pullRequest=5586&resolved=false&types=BUG)  
[![Vulnerability](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/vulnerability-16px.png 'Vulnerability')](https://sonarcloud.io/project/issues?id=owncloud_ocis&pullRequest=5586&resolved=false&types=VULNERABILITY) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=owncloud_ocis&pullRequest=5586&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=owncloud_ocis&pullRequest=5586&resolved=false&types=VULNERABILITY)  
[![Security Hotspot](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/security_hotspot-16px.png 'Security Hotspot')](https://sonarcloud.io/project/security_hotspots?id=owncloud_ocis&pullRequest=5586&resolved=false&types=SECURITY_HOTSPOT) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/security_hotspots?id=owncloud_ocis&pullRequest=5586&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=owncloud_ocis&pullRequest=5586&resolved=false&types=SECURITY_HOTSPOT)  
[![Code Smell](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/code_smell-16px.png 'Code Smell')](https://sonarcloud.io/project/issues?id=owncloud_ocis&pullRequest=5586&resolved=false&types=CODE_SMELL) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=owncloud_ocis&pullRequest=5586&resolved=false&types=CODE_SMELL) [2 Code Smells](https://sonarcloud.io/project/issues?id=owncloud_ocis&pullRequest=5586&resolved=false&types=CODE_SMELL)

[![71.4%](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/CoverageChart/60-16px.png '71.4%')](https://sonarcloud.io/component_measures?id=owncloud_ocis&pullRequest=5586&metric=new_coverage&view=list) [71.4% Coverage](https://sonarcloud.io/component_measures?id=owncloud_ocis&pullRequest=5586&metric=new_coverage&view=list)  
[![0.0%](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/Duplications/3-16px.png '0.0%')](https://sonarcloud.io/component_measures?id=owncloud_ocis&pullRequest=5586&metric=new_duplicated_lines_density&view=list) [0.0% Duplication](https://sonarcloud.io/component_measures?id=owncloud_ocis&pullRequest=5586&metric=new_duplicated_lines_density&view=list)



Patch:
diff --git a/services/graph/pkg/identity/ldap.go b/services/graph/pkg/identity/ldap.go
index 3673d3e7fcf..e54a6034af4 100644
--- a/services/graph/pkg/identity/ldap.go
+++ b/services/graph/pkg/identity/ldap.go
@@ -493,7 +493,17 @@ func (i *LDAP) changeUserName(ctx context.Context, dn, originalUserName, newUser
 		return nil, err
 	}
 
-	u, err := i.getUserByDN(fmt.Sprintf("%s,%s", newDN, i.userBaseDN))
+	parsed, err := ldap.ParseDN(dn)
+	if err != nil {
+		return nil, err
+	}
+
+	newFullDN, err := replaceDN(parsed, newDN)
+	if err != nil {
+		return nil, err
+	}
+
+	u, err := i.getUserByDN(newFullDN)
 	if err != nil {
 		return nil, err
 	}
@@ -730,3 +740,19 @@ func (i *LDAP) expandLDAPAttributeEntries(ctx context.Context, e *ldap.Entry, at
 
 	return result, nil
 }
+
+func replaceDN(fullDN *ldap.DN, newDN string) (string, error) {
+	if len(fullDN.RDNs) == 0 {
+		return "", fmt.Errorf("Can't operate on an empty dn")
+	}
+
+	if len(fullDN.RDNs) == 1 {
+		return newDN, nil
+	}
+
+	for _, part := range fullDN.RDNs[1:] {
+		newDN += "," + part.String()
+	}
+
+	return newDN, nil
+}


Test patch:
diff --git a/services/graph/pkg/identity/ldap_test.go b/services/graph/pkg/identity/ldap_test.go
index f55702cb9e9..93cfe52ad7d 100644
--- a/services/graph/pkg/identity/ldap_test.go
+++ b/services/graph/pkg/identity/ldap_test.go
@@ -634,20 +634,24 @@ func TestUpdateUser(t *testing.T) {
 						&ldap.SearchResult{
 							Entries: []*ldap.Entry{
 								{
-									DN: "uid=oldName",
+									DN: "uid=oldName,ou=people,dc=test,dc=net",
 									Attributes: []*ldap.EntryAttribute{
 										{
-											Name:   "displayname",
+											Name:   lconfig.UserDisplayNameAttribute,
 											Values: []string{"testUser"},
 										},
 										{
-											Name:   "entryUUID",
+											Name:   lconfig.UserIDAttribute,
 											Values: []string{"testUser"},
 										},
 										{
-											Name:   "mail",
+											Name:   lconfig.UserEmailAttribute,
 											Values: []string{"testuser@example.org"},
 										},
+										{
+											Name:   lconfig.UserNameAttribute,
+											Values: []string{"oldName"},
+										},
 									},
 								},
 							},
@@ -662,7 +666,7 @@ func TestUpdateUser(t *testing.T) {
 							BaseDN: "ou=groups,dc=test",
 							Scope:  2, DerefAliases: 0, SizeLimit: 0, TimeLimit: 0,
 							TypesOnly:  false,
-							Filter:     "(&(objectClass=groupOfNames)(member=uid=oldName))",
+							Filter:     "(&(objectClass=groupOfNames)(member=uid=oldName,ou=people,dc=test,dc=net))",
 							Attributes: []string{"cn", "entryUUID"},
 							Controls:   []ldap.Control(nil),
 						},
@@ -692,7 +696,7 @@ func TestUpdateUser(t *testing.T) {
 					funcName: "ModifyDN",
 					args: []interface{}{
 						&ldap.ModifyDNRequest{
-							DN:           "uid=oldName",
+							DN:           "uid=oldName,ou=people,dc=test,dc=net",
 							NewRDN:       "uid=newName",
 							DeleteOldRDN: true,
 							NewSuperior:  "",
@@ -707,7 +711,7 @@ func TestUpdateUser(t *testing.T) {
 					funcName: "Search",
 					args: []interface{}{
 						&ldap.SearchRequest{
-							BaseDN:       "uid=newName,ou=people,dc=test",
+							BaseDN:       "uid=newName,ou=people,dc=test,dc=net",
 							Scope:        0,
 							DerefAliases: 0,
 							SizeLimit:    1,
@@ -722,7 +726,7 @@ func TestUpdateUser(t *testing.T) {
 						&ldap.SearchResult{
 							Entries: []*ldap.Entry{
 								{
-									DN: "uid=newName,ou=people,dc=test",
+									DN: "uid=newName,ou=people,dc=test,dc=net",
 									Attributes: []*ldap.EntryAttribute{
 										{
 											Name:   lconfig.UserIDAttribute,
@@ -757,14 +761,14 @@ func TestUpdateUser(t *testing.T) {
 									Operation: 0x1,
 									Modification: ldap.PartialAttribute{
 										Type: "member",
-										Vals: []string{"uid=oldName"},
+										Vals: []string{"uid=oldName,ou=people,dc=test,dc=net"},
 									},
 								},
 								{
 									Operation: 0x0,
 									Modification: ldap.PartialAttribute{
 										Type: "member",
-										Vals: []string{"uid=newName,ou=people,dc=test"},
+										Vals: []string{"uid=newName,ou=people,dc=test,dc=net"},
 									},
 								},
 							},