UNPKG

1.79 kBJavaScriptView Raw
1// Copyright 2011 Mark Cavage, Inc. All rights reserved.
2
3const assert = require('assert-plus')
4const util = require('util')
5
6const LDAPMessage = require('./message')
7const Change = require('../change')
8const Protocol = require('../protocol')
9const lassert = require('../assert')
10
11/// --- API
12
13function ModifyRequest (options) {
14 options = options || {}
15 assert.object(options)
16 lassert.optionalStringDN(options.object)
17 lassert.optionalArrayOfAttribute(options.attributes)
18
19 options.protocolOp = Protocol.LDAP_REQ_MODIFY
20 LDAPMessage.call(this, options)
21
22 this.object = options.object || null
23 this.changes = options.changes ? options.changes.slice(0) : []
24}
25util.inherits(ModifyRequest, LDAPMessage)
26Object.defineProperties(ModifyRequest.prototype, {
27 type: {
28 get: function getType () { return 'ModifyRequest' },
29 configurable: false
30 },
31 _dn: {
32 get: function getDN () { return this.object },
33 configurable: false
34 }
35})
36
37ModifyRequest.prototype._parse = function (ber) {
38 assert.ok(ber)
39
40 this.object = ber.readString()
41
42 ber.readSequence()
43 const end = ber.offset + ber.length
44 while (ber.offset < end) {
45 const c = new Change()
46 c.parse(ber)
47 c.modification.type = c.modification.type.toLowerCase()
48 this.changes.push(c)
49 }
50
51 this.changes.sort(Change.compare)
52 return true
53}
54
55ModifyRequest.prototype._toBer = function (ber) {
56 assert.ok(ber)
57
58 ber.writeString(this.object.toString())
59 ber.startSequence()
60 this.changes.forEach(function (c) {
61 c.toBer(ber)
62 })
63 ber.endSequence()
64
65 return ber
66}
67
68ModifyRequest.prototype._json = function (j) {
69 assert.ok(j)
70
71 j.object = this.object
72 j.changes = []
73
74 this.changes.forEach(function (c) {
75 j.changes.push(c.json)
76 })
77
78 return j
79}
80
81/// --- Exports
82
83module.exports = ModifyRequest