GH-31 allow changing multiple fields in a single ldap_modify with better object sytnax

This commit is contained in:
Mark Cavage 2011-11-08 13:31:00 -08:00
parent 35f6b8a6c7
commit 920b9e00f4
4 changed files with 73 additions and 11 deletions

View File

@ -55,7 +55,11 @@ function Change(options) {
self._modification = attr;
return;
}
Object.keys(attr).forEach(function(k) {
var keys = Object.keys(attr);
if (keys.length > 1)
throw new Error('Only one attribute per Change allowed');
keys.forEach(function(k) {
var _attr = new Attribute({type: k});
if (Array.isArray(attr[k])) {
attr[k].forEach(function(v) {

View File

@ -435,17 +435,41 @@ Client.prototype.exop = function(name, value, controls, callback) {
Client.prototype.modify = function(name, change, controls, callback) {
if (typeof(name) !== 'string')
throw new TypeError('name (string) required');
if (!Array.isArray(change) && !(change instanceof Change))
if (typeof(change) !== 'object')
throw new TypeError('change (Change) required');
if (!Array.isArray(change)) {
var save = change;
change = [];
change.push(save);
var changes = [];
function changeFromObject(change) {
if (!change.operation && !change.type)
throw new Error('change.operation required');
if (typeof(change.modification) !== 'object')
throw new Error('change.modification (object) required');
Object.keys(change.modification).forEach(function(k) {
var mod = {};
mod[k] = change.modification[k];
changes.push(new Change({
operation: change.operation || change.type,
modification: mod
}));
});
}
change.forEach(function(c) {
if (!(c instanceof Change))
throw new TypeError('change ([Change]) required');
});
if (change instanceof Change) {
changes.push(change);
} else if (Array.isArray(change)) {
change.forEach(function(c) {
if (c instanceof Change) {
changes.push(c);
} else {
changeFromObject(c);
}
});
} else {
changeFromObject(change);
}
if (typeof(controls) === 'function') {
callback = controls;
controls = [];
@ -457,7 +481,7 @@ Client.prototype.modify = function(name, change, controls, callback) {
var req = new ModifyRequest({
object: dn.parse(name),
changes: change,
changes: changes,
controls: controls
});

View File

@ -50,6 +50,23 @@ test('new with args', function(t) {
});
test('GH-31 (multiple attributes per Change)', function(t) {
try {
var change = new Change({
operation: 'replace',
modification: {
cn: 'foo',
sn: 'bar'
}
});
t.fail('should have thrown');
} catch (e) {
t.ok(e);
t.end();
}
});
test('toBer', function(t) {
var change = new Change({
operation: 'Add',

View File

@ -324,6 +324,23 @@ test('modify array success', function(t) {
});
test('modify change plain object success (GH-31)', function(t) {
var change = {
type: 'replace',
modification: {
cn: 'test',
sn: 'bar'
}
};
client.modify('cn=modify, ' + SUFFIX, change, function(err, res) {
t.ifError(err);
t.ok(res);
t.equal(res.status, 0);
t.end();
});
});
test('modify DN new RDN only', function(t) {
client.modifyDN('cn=old, ' + SUFFIX, 'cn=new', function(err, res) {
t.ifError(err);