gitee.com/zhaochuninhefei/fabric-ca-gm@v0.0.2/ldap_v2/del.go (about)

     1  //
     2  // https://tools.ietf.org/html/rfc4511
     3  //
     4  // DelRequest ::= [APPLICATION 10] LDAPDN
     5  
     6  package ldap
     7  
     8  import (
     9  	"errors"
    10  
    11  	log "gitee.com/zhaochuninhefei/zcgolog/zclog"
    12  	ber "gopkg.in/asn1-ber.v1"
    13  )
    14  
    15  // DelRequest implements an LDAP deletion request
    16  type DelRequest struct {
    17  	// DN is the name of the directory entry to delete
    18  	DN string
    19  	// Controls hold optional controls to send with the request
    20  	Controls []Control
    21  }
    22  
    23  func (d DelRequest) encode() *ber.Packet {
    24  	request := ber.Encode(ber.ClassApplication, ber.TypePrimitive, ApplicationDelRequest, d.DN, "Del Request")
    25  	request.Data.Write([]byte(d.DN))
    26  	return request
    27  }
    28  
    29  // NewDelRequest creates a delete request for the given DN and controls
    30  func NewDelRequest(DN string,
    31  	Controls []Control) *DelRequest {
    32  	return &DelRequest{
    33  		DN:       DN,
    34  		Controls: Controls,
    35  	}
    36  }
    37  
    38  // Del executes the given delete request
    39  func (l *Conn) Del(delRequest *DelRequest) error {
    40  	packet := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "LDAP Request")
    41  	packet.AppendChild(ber.NewInteger(ber.ClassUniversal, ber.TypePrimitive, ber.TagInteger, l.nextMessageID(), "MessageID"))
    42  	packet.AppendChild(delRequest.encode())
    43  	if delRequest.Controls != nil {
    44  		packet.AppendChild(encodeControls(delRequest.Controls))
    45  	}
    46  
    47  	l.Debug.PrintPacket(packet)
    48  
    49  	msgCtx, err := l.sendMessage(packet)
    50  	if err != nil {
    51  		return err
    52  	}
    53  	defer l.finishMessage(msgCtx)
    54  
    55  	l.Debug.Printf("%d: waiting for response", msgCtx.id)
    56  	packetResponse, ok := <-msgCtx.responses
    57  	if !ok {
    58  		return NewError(ErrorNetwork, errors.New("ldap: response channel closed"))
    59  	}
    60  	packet, err = packetResponse.ReadPacket()
    61  	l.Debug.Printf("%d: got response %p", msgCtx.id, packet)
    62  	if err != nil {
    63  		return err
    64  	}
    65  
    66  	if l.Debug {
    67  		if err := addLDAPDescriptions(packet); err != nil {
    68  			return err
    69  		}
    70  		ber.PrintPacket(packet)
    71  	}
    72  
    73  	if packet.Children[1].Tag == ApplicationDelResponse {
    74  		resultCode, resultDescription := getLDAPResultCode(packet)
    75  		if resultCode != 0 {
    76  			return NewError(resultCode, errors.New(resultDescription))
    77  		}
    78  	} else {
    79  		log.Printf("Unexpected Response: %d", packet.Children[1].Tag)
    80  	}
    81  
    82  	l.Debug.Printf("%d: returning", msgCtx.id)
    83  	return nil
    84  }