node-ldapjs/lib/messages/search_entry.js

189 lines
4.3 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
// var asn1 = require('asn1')
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
/// --- Globals
2011-08-04 20:32:01 +00:00
// var BerWriter = asn1.BerWriter
2011-08-04 20:32:01 +00:00
/// --- API
2011-08-04 20:32:01 +00:00
function SearchEntry (options) {
options = options || {}
assert.object(options)
lassert.optionalStringDN(options.objectName)
2011-08-04 20:32:01 +00:00
options.protocolOp = Protocol.LDAP_REP_SEARCH_ENTRY
LDAPMessage.call(this, options)
2011-08-04 20:32:01 +00:00
this.objectName = options.objectName || null
this.setAttributes(options.attributes || [])
}
util.inherits(SearchEntry, LDAPMessage)
Object.defineProperties(SearchEntry.prototype, {
type: {
get: function getType () { return 'SearchEntry' },
configurable: false
},
_dn: {
get: function getDN () { return this.objectName },
configurable: false
},
object: {
get: function getObject () {
2020-10-31 21:07:32 +00:00
const obj = {
dn: this.dn.toString(),
controls: []
}
this.attributes.forEach(function (a) {
if (a.vals && a.vals.length) {
if (a.vals.length > 1) {
obj[a.type] = a.vals.slice()
} else {
obj[a.type] = a.vals[0]
}
} else {
obj[a.type] = []
}
})
2020-12-07 01:26:27 +00:00
this.controls.forEach(function (element) {
obj.controls.push(element.json)
})
return obj
},
configurable: false
},
raw: {
get: function getRaw () {
2020-10-31 21:07:32 +00:00
const obj = {
dn: this.dn.toString(),
controls: []
}
this.attributes.forEach(function (a) {
if (a.buffers && a.buffers.length) {
if (a.buffers.length > 1) {
obj[a.type] = a.buffers.slice()
} else {
obj[a.type] = a.buffers[0]
}
} else {
obj[a.type] = []
}
})
2020-12-07 01:26:27 +00:00
this.controls.forEach(function (element) {
obj.controls.push(element.json)
})
return obj
},
configurable: false
}
})
2011-08-04 20:32:01 +00:00
SearchEntry.prototype.addAttribute = function (attr) {
if (!attr || typeof (attr) !== 'object') { throw new TypeError('attr (attribute) required') }
2011-08-04 20:32:01 +00:00
this.attributes.push(attr)
}
2011-08-04 20:32:01 +00:00
SearchEntry.prototype.toObject = function () {
return this.object
}
SearchEntry.prototype.fromObject = function (obj) {
if (typeof (obj) !== 'object') { throw new TypeError('object required') }
2020-10-31 21:07:32 +00:00
const self = this
if (obj.controls) { this.controls = obj.controls }
if (obj.attributes) { obj = obj.attributes }
this.attributes = []
Object.keys(obj).forEach(function (k) {
self.attributes.push(new Attribute({ type: k, vals: obj[k] }))
})
return true
}
SearchEntry.prototype.setAttributes = function (obj) {
if (typeof (obj) !== 'object') { throw new TypeError('object required') }
if (Array.isArray(obj)) {
obj.forEach(function (a) {
if (!Attribute.isAttribute(a)) { throw new TypeError('entry must be an Array of Attributes') }
})
this.attributes = obj
} else {
2020-10-31 21:07:32 +00:00
const self = this
self.attributes = []
Object.keys(obj).forEach(function (k) {
2020-10-31 21:07:32 +00:00
const attr = new Attribute({ type: k })
if (Array.isArray(obj[k])) {
obj[k].forEach(function (v) {
attr.addValue(v.toString())
})
} else {
attr.addValue(obj[k].toString())
}
self.attributes.push(attr)
})
}
}
SearchEntry.prototype._json = function (j) {
assert.ok(j)
2011-08-04 20:32:01 +00:00
j.objectName = this.objectName.toString()
j.attributes = []
this.attributes.forEach(function (a) {
j.attributes.push(a.json || a)
})
2011-08-04 20:32:01 +00:00
return j
}
2011-08-04 20:32:01 +00:00
SearchEntry.prototype._parse = function (ber) {
assert.ok(ber)
2011-08-04 20:32:01 +00:00
this.objectName = ber.readString()
assert.ok(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)
this.attributes.push(a)
2011-08-04 20:32:01 +00:00
}
return true
}
2011-08-04 20:32:01 +00:00
SearchEntry.prototype._toBer = function (ber) {
assert.ok(ber)
2011-08-04 20:32:01 +00:00
2020-10-31 21:07:32 +00:00
const formattedObjectName = this.objectName.format({ skipSpace: true })
2020-07-04 09:05:59 +00:00
ber.writeString(formattedObjectName)
ber.startSequence()
this.attributes.forEach(function (a) {
2011-08-04 20:32:01 +00:00
// This may or may not be an attribute
ber = Attribute.toBer(a, ber)
})
ber.endSequence()
return ber
}
/// --- Exports
module.exports = SearchEntry