1 |
|
2 |
|
3 | const assert = require('assert-plus')
|
4 | const util = require('util')
|
5 |
|
6 | const LDAPMessage = require('./message')
|
7 | const Attribute = require('../attribute')
|
8 | const Protocol = require('../protocol')
|
9 | const lassert = require('../assert')
|
10 |
|
11 |
|
12 |
|
13 | function AddRequest (options) {
|
14 | options = options || {}
|
15 | assert.object(options)
|
16 | lassert.optionalStringDN(options.entry)
|
17 | lassert.optionalArrayOfAttribute(options.attributes)
|
18 |
|
19 | options.protocolOp = Protocol.LDAP_REQ_ADD
|
20 | LDAPMessage.call(this, options)
|
21 |
|
22 | this.entry = options.entry || null
|
23 | this.attributes = options.attributes ? options.attributes.slice(0) : []
|
24 | }
|
25 | util.inherits(AddRequest, LDAPMessage)
|
26 | Object.defineProperties(AddRequest.prototype, {
|
27 | type: {
|
28 | get: function getType () { return 'AddRequest' },
|
29 | configurable: false
|
30 | },
|
31 | _dn: {
|
32 | get: function getDN () { return this.entry },
|
33 | configurable: false
|
34 | }
|
35 | })
|
36 |
|
37 | AddRequest.prototype._parse = function (ber) {
|
38 | assert.ok(ber)
|
39 |
|
40 | this.entry = ber.readString()
|
41 |
|
42 | ber.readSequence()
|
43 |
|
44 | const end = ber.offset + ber.length
|
45 | while (ber.offset < end) {
|
46 | const a = new Attribute()
|
47 | a.parse(ber)
|
48 | a.type = a.type.toLowerCase()
|
49 | if (a.type === 'objectclass') {
|
50 | for (let i = 0; i < a.vals.length; i++) { a.vals[i] = a.vals[i].toLowerCase() }
|
51 | }
|
52 | this.attributes.push(a)
|
53 | }
|
54 |
|
55 | this.attributes.sort(Attribute.compare)
|
56 | return true
|
57 | }
|
58 |
|
59 | AddRequest.prototype._toBer = function (ber) {
|
60 | assert.ok(ber)
|
61 |
|
62 | ber.writeString(this.entry.toString())
|
63 | ber.startSequence()
|
64 | this.attributes.forEach(function (a) {
|
65 | a.toBer(ber)
|
66 | })
|
67 | ber.endSequence()
|
68 |
|
69 | return ber
|
70 | }
|
71 |
|
72 | AddRequest.prototype._json = function (j) {
|
73 | assert.ok(j)
|
74 |
|
75 | j.entry = this.entry.toString()
|
76 | j.attributes = []
|
77 |
|
78 | this.attributes.forEach(function (a) {
|
79 | j.attributes.push(a.json)
|
80 | })
|
81 |
|
82 | return j
|
83 | }
|
84 |
|
85 | AddRequest.prototype.indexOf = function (attr) {
|
86 | if (!attr || typeof (attr) !== 'string') { throw new TypeError('attr (string) required') }
|
87 |
|
88 | for (let i = 0; i < this.attributes.length; i++) {
|
89 | if (this.attributes[i].type === attr) { return i }
|
90 | }
|
91 |
|
92 | return -1
|
93 | }
|
94 |
|
95 | AddRequest.prototype.attributeNames = function () {
|
96 | const attrs = []
|
97 |
|
98 | for (let i = 0; i < this.attributes.length; i++) { attrs.push(this.attributes[i].type.toLowerCase()) }
|
99 |
|
100 | return attrs
|
101 | }
|
102 |
|
103 | AddRequest.prototype.getAttribute = function (name) {
|
104 | if (!name || typeof (name) !== 'string') { throw new TypeError('attribute name (string) required') }
|
105 |
|
106 | name = name.toLowerCase()
|
107 |
|
108 | for (let i = 0; i < this.attributes.length; i++) {
|
109 | if (this.attributes[i].type === name) { return this.attributes[i] }
|
110 | }
|
111 |
|
112 | return null
|
113 | }
|
114 |
|
115 | AddRequest.prototype.addAttribute = function (attr) {
|
116 | if (!(attr instanceof Attribute)) { throw new TypeError('attribute (Attribute) required') }
|
117 |
|
118 | return this.attributes.push(attr)
|
119 | }
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 | AddRequest.prototype.toObject = function () {
|
137 | const self = this
|
138 |
|
139 | const obj = {
|
140 | dn: self.entry ? self.entry.toString() : '',
|
141 | attributes: {}
|
142 | }
|
143 |
|
144 | if (!this.attributes || !this.attributes.length) { return obj }
|
145 |
|
146 | this.attributes.forEach(function (a) {
|
147 | if (!obj.attributes[a.type]) { obj.attributes[a.type] = [] }
|
148 |
|
149 | a.vals.forEach(function (v) {
|
150 | if (obj.attributes[a.type].indexOf(v) === -1) { obj.attributes[a.type].push(v) }
|
151 | })
|
152 | })
|
153 |
|
154 | return obj
|
155 | }
|
156 |
|
157 |
|
158 |
|
159 | module.exports = AddRequest
|