search enhancements
This commit is contained in:
parent
44bd29952e
commit
add3e1a0d6
|
@ -48,8 +48,17 @@ EqualityFilter.prototype.matches = function(target) {
|
|||
throw new TypeError('target (object) required');
|
||||
|
||||
var matches = false;
|
||||
if (target.hasOwnProperty(this.attribute))
|
||||
if (target.hasOwnProperty(this.attribute)) {
|
||||
if (Array.isArray(target[this.attribute])) {
|
||||
for (var i = 0; i < target[this.attribute].length; i++) {
|
||||
matches = (this.value === target[this.attribute][i]);
|
||||
if (matches)
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
matches = (this.value === target[this.attribute]);
|
||||
}
|
||||
}
|
||||
|
||||
return matches;
|
||||
};
|
||||
|
|
|
@ -5,10 +5,16 @@ var assert = require('assert');
|
|||
var asn1 = require('asn1');
|
||||
|
||||
|
||||
var Protocol = require('../protocol');
|
||||
|
||||
|
||||
|
||||
///--- Globals
|
||||
|
||||
var BerWriter = asn1.BerWriter;
|
||||
|
||||
|
||||
|
||||
///--- API
|
||||
|
||||
function Filter(options) {
|
||||
|
@ -21,7 +27,21 @@ function Filter(options) {
|
|||
|
||||
var self = this;
|
||||
this.__defineGetter__('type', function() {
|
||||
return '0x' + self._type.toString(16);
|
||||
switch (self._type) {
|
||||
case Protocol.FILTER_AND: return 'and';
|
||||
case Protocol.FILTER_OR: return 'or';
|
||||
case Protocol.FILTER_NOT: return 'not';
|
||||
case Protocol.FILTER_EQUALITY: return 'equal';
|
||||
case Protocol.FILTER_SUBSTRINGS: return 'substring';
|
||||
case Protocol.FILTER_GE: return 'ge';
|
||||
case Protocol.FILTER_LE: return 'le';
|
||||
case Protocol.FILTER_PRESENT: return 'present';
|
||||
case Protocol.FILTER_APPROX: return 'approx';
|
||||
case Protocol.FILTER_EXT: return 'ext';
|
||||
default:
|
||||
throw new Error('0x' + self._type.toString(16) +
|
||||
' is an invalid search filter');
|
||||
}
|
||||
});
|
||||
}
|
||||
module.exports = Filter;
|
||||
|
|
|
@ -49,8 +49,17 @@ GreaterThanEqualsFilter.prototype.matches = function(target) {
|
|||
throw new TypeError('target (object) required');
|
||||
|
||||
var matches = false;
|
||||
if (target.hasOwnProperty(this.attribute))
|
||||
matches = (target[this.attribute] >= this.value);
|
||||
if (target.hasOwnProperty(this.attribute)) {
|
||||
if (Array.isArray(target[this.attribute])) {
|
||||
for (var i = 0; i < target[this.attribute].length; i++) {
|
||||
matches = (this.value <= target[this.attribute][i]);
|
||||
if (matches)
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
matches = (this.value <= target[this.attribute]);
|
||||
}
|
||||
}
|
||||
|
||||
return matches;
|
||||
};
|
||||
|
|
|
@ -49,8 +49,18 @@ LessThanEqualsFilter.prototype.matches = function(target) {
|
|||
throw new TypeError('target (object) required');
|
||||
|
||||
var matches = false;
|
||||
if (target.hasOwnProperty(this.attribute))
|
||||
matches = (target[this.attribute] <= this.value);
|
||||
if (target.hasOwnProperty(this.attribute)) {
|
||||
if (Array.isArray(target[this.attribute])) {
|
||||
for (var i = 0; i < target[this.attribute].length; i++) {
|
||||
matches = (this.value >= target[this.attribute][i]);
|
||||
if (matches)
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
matches = (this.value >= target[this.attribute]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return matches;
|
||||
};
|
||||
|
|
|
@ -42,7 +42,6 @@ PresenceFilter.prototype.matches = function(target) {
|
|||
if (typeof(target) !== 'object')
|
||||
throw new TypeError('target (object) required');
|
||||
|
||||
console.log(this.attribute);
|
||||
return target.hasOwnProperty(this.attribute);
|
||||
};
|
||||
|
||||
|
@ -50,7 +49,14 @@ PresenceFilter.prototype.matches = function(target) {
|
|||
PresenceFilter.prototype.parse = function(ber) {
|
||||
assert.ok(ber);
|
||||
|
||||
this.attribute = ber.buffer.slice(0, ber.length).toString('utf8');
|
||||
this.attribute =
|
||||
ber
|
||||
.buffer
|
||||
.slice(0, ber.length)
|
||||
.toString('utf8')
|
||||
.toLowerCase();
|
||||
|
||||
ber._offset += ber.length;
|
||||
|
||||
return true;
|
||||
};
|
||||
|
|
|
@ -75,7 +75,7 @@ LDAPMessage.prototype.parse = function(data, length) {
|
|||
this._parse(ber, length);
|
||||
|
||||
// Look for controls
|
||||
if (ber.peek === Protocol.LDAP_CONTROLS && ber.offset < length) {
|
||||
if (ber.peek() === 0xa0) {
|
||||
ber.readSequence();
|
||||
var end = ber.offset + ber.length;
|
||||
while (ber.offset < end) {
|
||||
|
|
|
@ -98,7 +98,7 @@ SearchEntry.prototype._json = function(j) {
|
|||
j.objectName = this.objectName.toString();
|
||||
j.attributes = [];
|
||||
this.attributes.forEach(function(a) {
|
||||
j.attributes.push(a.json);
|
||||
j.attributes.push(a.json || a);
|
||||
});
|
||||
|
||||
return j;
|
||||
|
|
|
@ -103,7 +103,8 @@ SearchRequest.prototype._parse = function(ber) {
|
|||
this.filter = filters.parse(ber);
|
||||
|
||||
// look for attributes
|
||||
if (ber.readSequence() === (Ber.Sequence | Ber.Constructor)) {
|
||||
if (ber.peek() === 0x30) {
|
||||
ber.readSequence();
|
||||
var end = ber.offset + ber.length;
|
||||
while (ber.offset < end)
|
||||
this.attributes.push(ber.readString().toLowerCase());
|
||||
|
|
|
@ -274,7 +274,7 @@ function Server(options) {
|
|||
return function(err) {
|
||||
function sendError(err) {
|
||||
res.status = err.code || errors.LDAP_OPERATIONS_ERROR;
|
||||
res.matchedDN = req.dn.toString();
|
||||
res.matchedDN = req.suffix.toString();
|
||||
res.errorMessage = err.message || '';
|
||||
return res.end();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue