node-ldapjs/lib/messages/add_request.js

160 lines
3.6 KiB
JavaScript
Raw Normal View History

2011-08-04 20:32:01 +00:00
// Copyright 2011 Mark Cavage, Inc. All rights reserved.
2020-10-31 21:07:32 +00:00
const assert = require('assert-plus')
const util = require('util')
2011-08-04 20:32:01 +00:00
2020-10-31 21:07:32 +00:00
const LDAPMessage = require('./message')
const Attribute = require('../attribute')
const Protocol = require('../protocol')
const lassert = require('../assert')
2011-08-04 20:32:01 +00:00
/// --- API
2011-08-04 20:32:01 +00:00
function AddRequest (options) {
options = options || {}
assert.object(options)
lassert.optionalStringDN(options.entry)
lassert.optionalArrayOfAttribute(options.attributes)
2011-08-04 20:32:01 +00:00
options.protocolOp = Protocol.LDAP_REQ_ADD
LDAPMessage.call(this, options)
2011-08-04 20:32:01 +00:00
this.entry = options.entry || null
this.attributes = options.attributes ? options.attributes.slice(0) : []
2011-08-04 20:32:01 +00:00
}
util.inherits(AddRequest, LDAPMessage)
Object.defineProperties(AddRequest.prototype, {
type: {
get: function getType () { return 'AddRequest' },
configurable: false
},
_dn: {
get: function getDN () { return this.entry },
configurable: false
}
})
2011-08-04 20:32:01 +00:00
AddRequest.prototype._parse = function (ber) {
assert.ok(ber)
2011-08-04 20:32:01 +00:00
this.entry = ber.readString()
2011-08-04 20:32:01 +00:00
ber.readSequence()
2011-08-04 20:32:01 +00:00
2020-10-31 21:07:32 +00:00
const end = ber.offset + ber.length
2011-08-04 20:32:01 +00:00
while (ber.offset < end) {
2020-10-31 21:07:32 +00:00
const a = new Attribute()
a.parse(ber)
a.type = a.type.toLowerCase()
if (a.type === 'objectclass') {
2020-10-31 21:07:32 +00:00
for (let i = 0; i < a.vals.length; i++) { a.vals[i] = a.vals[i].toLowerCase() }
}
this.attributes.push(a)
2011-08-04 20:32:01 +00:00
}
this.attributes.sort(Attribute.compare)
return true
}
2011-08-04 20:32:01 +00:00
AddRequest.prototype._toBer = function (ber) {
assert.ok(ber)
2011-08-04 20:32:01 +00:00
ber.writeString(this.entry.toString())
ber.startSequence()
this.attributes.forEach(function (a) {
a.toBer(ber)
})
ber.endSequence()
2011-08-04 20:32:01 +00:00
return ber
}
2011-08-04 20:32:01 +00:00
AddRequest.prototype._json = function (j) {
assert.ok(j)
2011-08-04 20:32:01 +00:00
j.entry = this.entry.toString()
j.attributes = []
2011-08-04 20:32:01 +00:00
this.attributes.forEach(function (a) {
j.attributes.push(a.json)
})
2011-08-04 20:32:01 +00:00
return j
}
AddRequest.prototype.indexOf = function (attr) {
if (!attr || typeof (attr) !== 'string') { throw new TypeError('attr (string) required') }
2020-10-31 21:07:32 +00:00
for (let i = 0; i < this.attributes.length; i++) {
if (this.attributes[i].type === attr) { return i }
}
return -1
}
AddRequest.prototype.attributeNames = function () {
2020-10-31 21:07:32 +00:00
const attrs = []
2020-10-31 21:07:32 +00:00
for (let i = 0; i < this.attributes.length; i++) { attrs.push(this.attributes[i].type.toLowerCase()) }
return attrs
}
AddRequest.prototype.getAttribute = function (name) {
if (!name || typeof (name) !== 'string') { throw new TypeError('attribute name (string) required') }
name = name.toLowerCase()
2020-10-31 21:07:32 +00:00
for (let i = 0; i < this.attributes.length; i++) {
if (this.attributes[i].type === name) { return this.attributes[i] }
}
return null
}
AddRequest.prototype.addAttribute = function (attr) {
if (!(attr instanceof Attribute)) { throw new TypeError('attribute (Attribute) required') }
return this.attributes.push(attr)
}
/**
* Returns a "pure" JS representation of this object.
*
* An example object would look like:
*
* {
* "dn": "cn=unit, dc=test",
* "attributes": {
* "cn": ["unit", "foo"],
* "objectclass": ["top", "person"]
* }
* }
*
* @return {Object} that looks like the above.
*/
AddRequest.prototype.toObject = function () {
2020-10-31 21:07:32 +00:00
const self = this
2020-10-31 21:07:32 +00:00
const obj = {
dn: self.entry ? self.entry.toString() : '',
attributes: {}
}
if (!this.attributes || !this.attributes.length) { return obj }
this.attributes.forEach(function (a) {
if (!obj.attributes[a.type]) { obj.attributes[a.type] = [] }
a.vals.forEach(function (v) {
if (obj.attributes[a.type].indexOf(v) === -1) { obj.attributes[a.type].push(v) }
})
})
return obj
}
/// --- Exports
module.exports = AddRequest