UNPKG

3.68 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 Attribute = require('../attribute')
8const Protocol = require('../protocol')
9const lassert = require('../assert')
10
11/// --- API
12
13function 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}
25util.inherits(AddRequest, LDAPMessage)
26Object.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
37AddRequest.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
59AddRequest.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
72AddRequest.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
85AddRequest.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
95AddRequest.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
103AddRequest.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
115AddRequest.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 * Returns a "pure" JS representation of this object.
123 *
124 * An example object would look like:
125 *
126 * {
127 * "dn": "cn=unit, dc=test",
128 * "attributes": {
129 * "cn": ["unit", "foo"],
130 * "objectclass": ["top", "person"]
131 * }
132 * }
133 *
134 * @return {Object} that looks like the above.
135 */
136AddRequest.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/// --- Exports
158
159module.exports = AddRequest