2
0
Fork 0
tcrud/lib/backend/ldapjs.js
2017-06-10 16:53:29 +02:00

151 lines
3.7 KiB
JavaScript

var config = require('./../tcrud-config');
var debug = require('debug')('ff:tcrud:ldapjs');
module.exports = {
registrate: function(key,client) {
config.registratePlugin(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;
}
LdapJSBackend.prototype.findAll = function(tview,crudType) {
var self = this;
return function(data, dataParam, cb) {
var opts = {
filter: '(&(l=Seattle)(email=*@foo.com))',
scope: 'sub'
};
debug('findAll %s',opts);
self.plugin.client.search('o=example', 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);
});
res.on('end', function(result) {
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]);
}
});
};
}