...

Source file src/github.com/go-ldap/ldap/v3/add.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  // Attribute represents an LDAP attribute
     9  type Attribute struct {
    10  	// Type is the name of the LDAP attribute
    11  	Type string
    12  	// Vals are the LDAP attribute values
    13  	Vals []string
    14  }
    15  
    16  func (a *Attribute) encode() *ber.Packet {
    17  	seq := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "Attribute")
    18  	seq.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, a.Type, "Type"))
    19  	set := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSet, nil, "AttributeValue")
    20  	for _, value := range a.Vals {
    21  		set.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, value, "Vals"))
    22  	}
    23  	seq.AppendChild(set)
    24  	return seq
    25  }
    26  
    27  // AddRequest represents an LDAP AddRequest operation
    28  type AddRequest struct {
    29  	// DN identifies the entry being added
    30  	DN string
    31  	// Attributes list the attributes of the new entry
    32  	Attributes []Attribute
    33  	// Controls hold optional controls to send with the request
    34  	Controls []Control
    35  }
    36  
    37  func (req *AddRequest) appendTo(envelope *ber.Packet) error {
    38  	pkt := ber.Encode(ber.ClassApplication, ber.TypeConstructed, ApplicationAddRequest, nil, "Add Request")
    39  	pkt.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, req.DN, "DN"))
    40  	attributes := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "Attributes")
    41  	for _, attribute := range req.Attributes {
    42  		attributes.AppendChild(attribute.encode())
    43  	}
    44  	pkt.AppendChild(attributes)
    45  
    46  	envelope.AppendChild(pkt)
    47  	if len(req.Controls) > 0 {
    48  		envelope.AppendChild(encodeControls(req.Controls))
    49  	}
    50  
    51  	return nil
    52  }
    53  
    54  // Attribute adds an attribute with the given type and values
    55  func (req *AddRequest) Attribute(attrType string, attrVals []string) {
    56  	req.Attributes = append(req.Attributes, Attribute{Type: attrType, Vals: attrVals})
    57  }
    58  
    59  // NewAddRequest returns an AddRequest for the given DN, with no attributes
    60  func NewAddRequest(dn string, controls []Control) *AddRequest {
    61  	return &AddRequest{
    62  		DN:       dn,
    63  		Controls: controls,
    64  	}
    65  }
    66  
    67  // Add performs the given AddRequest
    68  func (l *Conn) Add(addRequest *AddRequest) error {
    69  	msgCtx, err := l.doRequest(addRequest)
    70  	if err != nil {
    71  		return err
    72  	}
    73  	defer l.finishMessage(msgCtx)
    74  
    75  	packet, err := l.readPacket(msgCtx)
    76  	if err != nil {
    77  		return err
    78  	}
    79  
    80  	if packet.Children[1].Tag == ApplicationAddResponse {
    81  		err := GetLDAPError(packet)
    82  		if err != nil {
    83  			return err
    84  		}
    85  	} else {
    86  		return fmt.Errorf("ldap: unexpected response: %d", packet.Children[1].Tag)
    87  	}
    88  	return nil
    89  }
    90  

View as plain text