2011-08-04 20:32:01 +00:00
|
|
|
// Copyright 2011 Mark Cavage, Inc. All rights reserved.
|
|
|
|
|
|
|
|
var assert = require('assert');
|
|
|
|
var util = require('util');
|
|
|
|
|
|
|
|
var LDAPMessage = require('./message');
|
|
|
|
var dn = require('../dn');
|
2012-02-18 08:15:52 +00:00
|
|
|
var Attribute = require('../attribute');
|
2011-08-04 20:32:01 +00:00
|
|
|
var Change = require('../change');
|
|
|
|
var Protocol = require('../protocol');
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-09-30 23:39:19 +00:00
|
|
|
///--- API
|
|
|
|
|
|
|
|
var isDN = dn.DN.isDN;
|
|
|
|
|
|
|
|
|
2011-08-04 20:32:01 +00:00
|
|
|
///--- API
|
|
|
|
|
|
|
|
function ModifyRequest(options) {
|
|
|
|
if (options) {
|
2012-02-18 08:15:52 +00:00
|
|
|
if (typeof (options) !== 'object')
|
2011-08-04 20:32:01 +00:00
|
|
|
throw new TypeError('options must be an object');
|
2014-09-30 23:39:19 +00:00
|
|
|
if (options.object &&
|
|
|
|
!(isDN(options.object) || typeof (options.object) === 'string')) {
|
|
|
|
throw new TypeError('options.object must be a DN or string');
|
|
|
|
}
|
2011-08-04 20:32:01 +00:00
|
|
|
if (options.attributes) {
|
|
|
|
if (!Array.isArray(options.attributes))
|
|
|
|
throw new TypeError('options.attributes must be [Attribute]');
|
2012-02-18 08:15:52 +00:00
|
|
|
options.attributes.forEach(function (a) {
|
2011-08-04 20:32:01 +00:00
|
|
|
if (!(a instanceof Attribute))
|
|
|
|
throw new TypeError('options.attributes must be [Attribute]');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
options.protocolOp = Protocol.LDAP_REQ_MODIFY;
|
|
|
|
LDAPMessage.call(this, options);
|
|
|
|
|
|
|
|
this.object = options.object || null;
|
|
|
|
this.changes = options.changes ? options.changes.slice(0) : [];
|
|
|
|
|
|
|
|
var self = this;
|
2012-02-18 08:15:52 +00:00
|
|
|
this.__defineGetter__('type', function () { return 'ModifyRequest'; });
|
|
|
|
this.__defineGetter__('_dn', function () { return self.object; });
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
|
|
|
util.inherits(ModifyRequest, LDAPMessage);
|
|
|
|
module.exports = ModifyRequest;
|
|
|
|
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
ModifyRequest.prototype._parse = function (ber) {
|
2011-08-04 20:32:01 +00:00
|
|
|
assert.ok(ber);
|
|
|
|
|
2014-09-30 23:39:19 +00:00
|
|
|
this.object = ber.readString();
|
2011-08-04 20:32:01 +00:00
|
|
|
|
|
|
|
ber.readSequence();
|
|
|
|
var end = ber.offset + ber.length;
|
|
|
|
while (ber.offset < end) {
|
|
|
|
var c = new Change();
|
|
|
|
c.parse(ber);
|
2011-10-04 17:27:43 +00:00
|
|
|
c.modification.type = c.modification.type.toLowerCase();
|
2011-08-04 20:32:01 +00:00
|
|
|
this.changes.push(c);
|
|
|
|
}
|
|
|
|
|
2011-08-08 15:03:00 +00:00
|
|
|
this.changes.sort(Change.compare);
|
2011-08-04 20:32:01 +00:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
ModifyRequest.prototype._toBer = function (ber) {
|
2011-08-04 20:32:01 +00:00
|
|
|
assert.ok(ber);
|
|
|
|
|
|
|
|
ber.writeString(this.object.toString());
|
|
|
|
ber.startSequence();
|
2012-02-18 08:15:52 +00:00
|
|
|
this.changes.forEach(function (c) {
|
2011-08-04 20:32:01 +00:00
|
|
|
c.toBer(ber);
|
|
|
|
});
|
|
|
|
ber.endSequence();
|
|
|
|
|
|
|
|
return ber;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
ModifyRequest.prototype._json = function (j) {
|
2011-08-04 20:32:01 +00:00
|
|
|
assert.ok(j);
|
|
|
|
|
|
|
|
j.object = this.object;
|
|
|
|
j.changes = [];
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
this.changes.forEach(function (c) {
|
2011-08-04 20:32:01 +00:00
|
|
|
j.changes.push(c.json);
|
|
|
|
});
|
|
|
|
|
|
|
|
return j;
|
|
|
|
};
|