ldap move operation

Keywords: github

Recently, the epidemic was serious, and no one left home after returning from Shenzhen on No. 29.01.22.
Today is 2020.02.09, originally tomorrow's ticket, the home road closed, the ticket was returned.
Every day we pay attention to the epidemic, we will also see all kinds of public revelations, criticize this, praise that, CTM for a while, it will be amazing.
In today's advanced science and technology, human beings will still live hard and even lose their lives due to various difficulties.
It can be said that human beings are very insignificant in front of nature. We must revere nature and live in harmony with it, or take dates pills.

Okay, get to the point.

There is a requirement in the group for the first two days, mainly an operation to move entries in ldap.
Use is github.com/go-ldap/ldap The v3 version of this package.

Beginning to notice that it has a func (l *Conn) ModifyDN(m *ModifyDNRequest) error method like this,
However, in the comment for func NewModifyDNRequest (dn string, RDN string, delOld bool, newSup string)*ModifyDNRequest, the example it gives is simply renaming:

// A call like
//   mdnReq := NewModifyDNRequest("uid=someone,dc=example,dc=org", "uid=newname", true, "")
// will setup the request to just rename uid=someone,dc=example,dc=org to
// uid=newname,dc=example,dc=org.

In fact, its last parameter, newSup, is used correctly to move to a different group.

If you take another step, it has a test file, moddn_test.go:

package ldap

import (
	"log"
)

// ExampleConn_ModifyDN_renameNoMove shows how to rename an entry without moving it
func ExampleConn_ModifyDN_renameNoMove() {
	conn, err := Dial("tcp", "ldap.example.org:389")
	if err != nil {
		log.Fatalf("Failed to connect: %s\n", err)
	}
	defer conn.Close()

	_, err = conn.SimpleBind(&SimpleBindRequest{
		Username: "uid=someone,ou=people,dc=example,dc=org",
		Password: "MySecretPass",
	})
	if err != nil {
		log.Fatalf("Failed to bind: %s\n", err)
	}
	// just rename to uid=new,ou=people,dc=example,dc=org:
	req := NewModifyDNRequest("uid=user,ou=people,dc=example,dc=org", "uid=new", true, "")
	if err = conn.ModifyDN(req); err != nil {
		log.Fatalf("Failed to call ModifyDN(): %s\n", err)
	}
}

// ExampleConn_ModifyDN_renameAndMove shows how to rename an entry and moving it to a new base
func ExampleConn_ModifyDN_renameAndMove() {
	conn, err := Dial("tcp", "ldap.example.org:389")
	if err != nil {
		log.Fatalf("Failed to connect: %s\n", err)
	}
	defer conn.Close()

	_, err = conn.SimpleBind(&SimpleBindRequest{
		Username: "uid=someone,ou=people,dc=example,dc=org",
		Password: "MySecretPass",
	})
	if err != nil {
		log.Fatalf("Failed to bind: %s\n", err)
	}
	// rename to uid=new,ou=people,dc=example,dc=org and move to ou=users,dc=example,dc=org ->
	// uid=new,ou=users,dc=example,dc=org
	req := NewModifyDNRequest("uid=user,ou=people,dc=example,dc=org", "uid=new", true, "ou=users,dc=example,dc=org")

	if err = conn.ModifyDN(req); err != nil {
		log.Fatalf("Failed to call ModifyDN(): %s\n", err)
	}
}

// ExampleConn_ModifyDN_moveOnly shows how to move an entry to a new base without renaming the RDN
func ExampleConn_ModifyDN_moveOnly() {
	conn, err := Dial("tcp", "ldap.example.org:389")
	if err != nil {
		log.Fatalf("Failed to connect: %s\n", err)
	}
	defer conn.Close()

	_, err = conn.SimpleBind(&SimpleBindRequest{
		Username: "uid=someone,ou=people,dc=example,dc=org",
		Password: "MySecretPass",
	})
	if err != nil {
		log.Fatalf("Failed to bind: %s\n", err)
	}
	// move to ou=users,dc=example,dc=org -> uid=user,ou=users,dc=example,dc=org
	req := NewModifyDNRequest("uid=user,ou=people,dc=example,dc=org", "uid=user", true, "ou=users,dc=example,dc=org")
	if err = conn.ModifyDN(req); err != nil {
		log.Fatalf("Failed to call ModifyDN(): %s\n", err)
	}
}

Its example is clear, as long as you press the following to move within different groups:
Note that the first parameter and the last parameter ou are different (that is, different groups)

	req := NewModifyDNRequest("uid=user,ou=people,dc=example,dc=org", "uid=user", true, "ou=users,dc=example,dc=org")
// ...

(finished)

231 original articles were published. 77% were praised. 520,000 visits+
His message board follow

Posted by jvrothjr on Sat, 08 Feb 2020 18:13:25 -0800