2
0
Fork 0
tcrud/lib/backend/ldapjs.js

157 lines
3.9 KiB
JavaScript

var tcrudSetup = require('./../tcrud-setup');
var debug = require('debug')('ff:tcrud:ldapjs');
module.exports = {
registrate: function(key,client) {
tcrudSetup.pluginLoad(new LdapJSPlugin(key,client));
}
}
// ------- LdapJSPlugin Object
function LdapJSPlugin(key,client) {
this.key = key;
this.client = client;
}
LdapJSPlugin.prototype.configPlugin = function (ctx) {
ctx.key='ldapjs#'+this.key;
ctx.description='Adds ldap backend support.';
};
LdapJSPlugin.prototype.createBackend = function() {
return new LdapJSBackend(this);
}
// -------- LdapJSBackend Object
function LdapJSBackend(plugin) {
this.plugin = plugin;
}
LdapJSBackend.prototype.getKey = function() {
return this.plugin.key;
}
// see http://ldapjs.org/client.html#connection-management
LdapJSBackend.prototype.findAll = function(tview,crudType) {
var self = this;
return function(data, dataParam, cb) {
var opts = {
filter2: '(&(l=Seattle)(email=*@foo.com))',
scope: 'sub'
};
debug('findAll %s',opts);
var ss = 'ou=users,dc=nss,dc=forwardfire,dc=net';
console.log("ldap search "+ss);
self.plugin.client.search(ss, opts, function(err, res) {
if (err) {
debug(err);
cb(err);
return;
}
//var querySql = 'SELECT '+selectField(tview,crudType)+' FROM '+tview.tmeta.tmodel.tid+'';
var result = [];
res.on('searchEntry', function(entry) {
result.push(entry.object);
});
res.on('error', function(err) {
cb(err);
});
res.on('end', function(status) {
cb(err,result);
});
});
};
}
LdapJSBackend.prototype.createNew = function(tview,crudType) {
var self = this;
return function(data, dataParam, cb) {
var entry = {
cn: 'foo',
sn: 'bar',
email: ['foo@bar.com', 'foo1@bar.com'],
objectclass: 'fooPerson'
};
self.plugin.client.add('cn=foo, o=example', entry, function(err) {
if (err) {
debug(err);
cb(err);
} else {
cb(err,result.rows);
}
});
};
}
LdapJSBackend.prototype.findOne = function(tview,crudType) {
var self = this;
return function(data, dataParam, cb) {
var querySql = 'SELECT '+selectField(tview,crudType)+' FROM '+tview.tmeta.tmodel.tid+' WHERE '+whereKeys(tview)+'';
debug('findOne param %s',JSON.stringify(dataParam));
debug('findOne sql %s',querySql);
var query = self.db.query(querySql, dataParam, function (err, result) {
if (err) {
debug(err);
cb(err);
} else {
cb(err,result.rows[0]);
}
});
};
}
LdapJSBackend.prototype.updateOne = function(tview,crudType) {
var self = this;
return function(data, dataParam, cb) {
var querySql = 'UPDATE '+tview.tmeta.tmodel.tid+' '+updateValues(tview,data)+' WHERE '+whereKeys(tview);
debug('updateOne param %s',JSON.stringify(dataParam));
debug('updateOne data %s',JSON.stringify(data));
debug('updateOne sql %s',querySql);
var query = self.db.query(querySql, data, function (err, result) {
if (err) {
debug(err);
cb(err);
} else {
cb(err,result.rows[0]);
}
});
};
}
LdapJSBackend.prototype.deleteOne = function(tview,crudType) {
var self = this;
return function(data, dataParam, cb) {
var querySql = 'DELETE FROM '+tview.tmeta.tmodel.tid+' WHERE '+whereKeys(tview);
debug('deleteOne param %s',JSON.stringify(dataParam));
debug('deleteOne sql %s',querySql);
var query = self.db.query(querySql, dataParam, function (err, result) {
if (err) {
debug(err);
cb(err);
} else {
cb(err,result.rows[0]);
}
});
};
}
LdapJSBackend.prototype.findAllCount = function(tview,crudType) {
var self = this;
return function(data, dataParam, cb) {
var querySql = 'SELECT count(*) FROM '+tview.tmeta.tmodel.tid+' WHERE '+whereKeys(tview);
debug('findAllCount param %s',JSON.stringify(dataParam));
debug('findAllCount sql %s',querySql);
var query = self.db.query(querySql, dataParam, function (err, result) {
if (err) {
debug(err);
cb(err);
} else {
cb(err,result.rows[0]);
}
});
};
}