1 |
|
2 |
|
3 | const assert = require('assert-plus')
|
4 | const util = require('util')
|
5 |
|
6 | const LDAPMessage = require('./message')
|
7 | const Change = require('../change')
|
8 | const Protocol = require('../protocol')
|
9 | const lassert = require('../assert')
|
10 |
|
11 |
|
12 |
|
13 | function 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 | }
|
25 | util.inherits(ModifyRequest, LDAPMessage)
|
26 | Object.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 |
|
37 | ModifyRequest.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 |
|
55 | ModifyRequest.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 |
|
68 | ModifyRequest.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 |
|
82 |
|
83 | module.exports = ModifyRequest
|