...

Source file src/github.com/go-ldap/ldap/v3/moddn.go

Documentation: github.com/go-ldap/ldap/v3

     1  package ldap
     2  
     3  import (
     4  	"fmt"
     5  	ber "github.com/go-asn1-ber/asn1-ber"
     6  )
     7  
     8  // ModifyDNRequest holds the request to modify a DN
     9  type ModifyDNRequest struct {
    10  	DN           string
    11  	NewRDN       string
    12  	DeleteOldRDN bool
    13  	NewSuperior  string
    14  	// Controls hold optional controls to send with the request
    15  	Controls []Control
    16  }
    17  
    18  // NewModifyDNRequest creates a new request which can be passed to ModifyDN().
    19  //
    20  // To move an object in the tree, set the "newSup" to the new parent entry DN. Use an
    21  // empty string for just changing the object's RDN.
    22  //
    23  // For moving the object without renaming, the "rdn" must be the first
    24  // RDN of the given DN.
    25  //
    26  // A call like
    27  //
    28  //	mdnReq := NewModifyDNRequest("uid=someone,dc=example,dc=org", "uid=newname", true, "")
    29  //
    30  // will setup the request to just rename uid=someone,dc=example,dc=org to
    31  // uid=newname,dc=example,dc=org.
    32  func NewModifyDNRequest(dn string, rdn string, delOld bool, newSup string) *ModifyDNRequest {
    33  	return &ModifyDNRequest{
    34  		DN:           dn,
    35  		NewRDN:       rdn,
    36  		DeleteOldRDN: delOld,
    37  		NewSuperior:  newSup,
    38  	}
    39  }
    40  
    41  // NewModifyDNWithControlsRequest creates a new request which can be passed to ModifyDN()
    42  // and also allows setting LDAP request controls.
    43  //
    44  // Refer NewModifyDNRequest for other parameters
    45  func NewModifyDNWithControlsRequest(dn string, rdn string, delOld bool,
    46  	newSup string, controls []Control) *ModifyDNRequest {
    47  	return &ModifyDNRequest{
    48  		DN:           dn,
    49  		NewRDN:       rdn,
    50  		DeleteOldRDN: delOld,
    51  		NewSuperior:  newSup,
    52  		Controls:     controls,
    53  	}
    54  }
    55  
    56  func (req *ModifyDNRequest) appendTo(envelope *ber.Packet) error {
    57  	pkt := ber.Encode(ber.ClassApplication, ber.TypeConstructed, ApplicationModifyDNRequest, nil, "Modify DN Request")
    58  	pkt.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, req.DN, "DN"))
    59  	pkt.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, req.NewRDN, "New RDN"))
    60  	if req.DeleteOldRDN {
    61  		buf := []byte{0xff}
    62  		pkt.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagBoolean, string(buf), "Delete old RDN"))
    63  	} else {
    64  		pkt.AppendChild(ber.NewBoolean(ber.ClassUniversal, ber.TypePrimitive, ber.TagBoolean, req.DeleteOldRDN, "Delete old RDN"))
    65  	}
    66  	if req.NewSuperior != "" {
    67  		pkt.AppendChild(ber.NewString(ber.ClassContext, ber.TypePrimitive, 0, req.NewSuperior, "New Superior"))
    68  	}
    69  
    70  	envelope.AppendChild(pkt)
    71  	if len(req.Controls) > 0 {
    72  		envelope.AppendChild(encodeControls(req.Controls))
    73  	}
    74  
    75  	return nil
    76  }
    77  
    78  // ModifyDN renames the given DN and optionally move to another base (when the "newSup" argument
    79  // to NewModifyDNRequest() is not "").
    80  func (l *Conn) ModifyDN(m *ModifyDNRequest) error {
    81  	msgCtx, err := l.doRequest(m)
    82  	if err != nil {
    83  		return err
    84  	}
    85  	defer l.finishMessage(msgCtx)
    86  
    87  	packet, err := l.readPacket(msgCtx)
    88  	if err != nil {
    89  		return err
    90  	}
    91  
    92  	if packet.Children[1].Tag == ApplicationModifyDNResponse {
    93  		err := GetLDAPError(packet)
    94  		if err != nil {
    95  			return err
    96  		}
    97  	} else {
    98  		return fmt.Errorf("ldap: unexpected response: %d", packet.Children[1].Tag)
    99  	}
   100  
   101  	return nil
   102  }
   103  

View as plain text