node-ldap2json
Advanced tools
Comparing version 0.0.5 to 0.0.7
var ldap = require('ldapjs') | ||
exports.searchInLdap=function(options,cb){ | ||
function lditem2obj(lditem){ | ||
var o= new Object(); | ||
o.path=lditem.distinguishedName; | ||
o.hide=(lditem.showInAdvancedViewOnly=="TRUE") ? true : false; | ||
if ((lditem.cn!=undefined)||(lditem.ou!=undefined)) { | ||
if (lditem.objectClass.indexOf('organizationalUnit')>-1){ | ||
o.type='OU'; | ||
o.name=lditem.ou; | ||
} else if (lditem.objectClass.indexOf('person')>-1){ | ||
o.type='USER'; | ||
o.name=lditem.cn; | ||
} else if(lditem.objectClass.indexOf('container')>-1){ | ||
o.type='CN'; | ||
o.name=lditem.cn; | ||
} else if(lditem.objectClass.indexOf('group')>-1){ | ||
o.type='GROUP'; | ||
o.name=lditem.cn; | ||
} | ||
return o; | ||
} else return null; | ||
} | ||
exports.search=function(options,keyword,objecttype,cb){ | ||
var client=ldap.createClient({ | ||
@@ -9,7 +32,9 @@ url:'ldap://'+options.host+':389' | ||
var opts = { | ||
scope: 'one' | ||
scope: 'sub', | ||
filter: '(&(objectClass='+objecttype+')(cn=*'+keyword+'*))' | ||
}; | ||
client.bind(options.adminuser,options.pass,function(e){ | ||
//console.log("searchInLdap:SearchBase:"+options.base); | ||
client.bind(options.adminuser,options.adminpass,function(e){ | ||
if (e) { | ||
console.log("search:ClientBind:"+e.message); | ||
cb(e.message,null); | ||
@@ -19,4 +44,6 @@ return; | ||
var items = []; | ||
client.search(options.base,opts,function(err,res){ | ||
if (err) { | ||
console.log("search:ClientSearchParamErr:"+err.message); | ||
cb(e.message,null); | ||
@@ -28,6 +55,59 @@ return; | ||
}); | ||
res.on('error', function(err) { | ||
cb(err.message,null); | ||
res.on('error', function(err2) { | ||
console.log(); | ||
console.log("\033[41m\033[33m search:ClientSearch:"+err2.message+"\033[0m"); | ||
//cb(err2.message,null); | ||
//return; | ||
}); | ||
res.on('end', function(result) { | ||
var count=items.length; | ||
if (count==0) { | ||
cb(null,[]); | ||
} else { | ||
retitems = []; | ||
items.forEach(function(itm){ | ||
retitems.push(lditem2obj(itm)); | ||
count--; | ||
if (count<=0){ | ||
cb(null,retitems); | ||
} | ||
}); | ||
} | ||
}); | ||
}); | ||
}); | ||
} | ||
exports.searchInLdap=function(options,cb){ | ||
var client=ldap.createClient({ | ||
url:'ldap://'+options.host+':389' | ||
}); | ||
var opts = { | ||
scope: 'one', | ||
filter: '(|(objectClass=container)(objectClass=group)(objectClass=organizationalUnit))' | ||
}; | ||
//console.log("searchInLdap:SearchBase:"+options.base); | ||
client.bind(options.adminuser,options.adminpass,function(e){ | ||
if (e) { | ||
console.log("searchInLdap:ClientBind:"+e.message); | ||
cb(e.message,null); | ||
return; | ||
} | ||
var items = []; | ||
client.search(options.base,opts,function(err,res){ | ||
if (err) { | ||
console.log("searchInLdap:ClientSearchParamErr:"+err.message); | ||
cb(e.message,null); | ||
return; | ||
} | ||
res.on('searchEntry', function(entry) { | ||
items.push(entry.object); | ||
}); | ||
res.on('error', function(err2) { | ||
console.log(); | ||
console.log("\033[41m\033[33m searchInLdap:ClientSearch:"+err2.message+"\033[0m"); | ||
//cb(err2.message,null); | ||
//return; | ||
}); | ||
res.on('end', function(result) { | ||
@@ -40,22 +120,3 @@ var count=items.length; | ||
items.forEach(function(itm){ | ||
var o= new Object(); | ||
o.path=itm.distinguishedName; | ||
o.hide=(itm.showInAdvancedViewOnly=="TRUE") ? true : false; | ||
if (itm.objectClass.indexOf('organizationalUnit')>-1){ | ||
o.type='OU'; | ||
o.name=itm.ou; | ||
retitems.push(o); | ||
} else if (itm.objectClass.indexOf('person')>-1){ | ||
o.type='USER'; | ||
o.name=itm.cn; | ||
retitems.push(o); | ||
} else if(itm.objectClass.indexOf('container')>-1){ | ||
o.type='CN'; | ||
o.name=itm.cn; | ||
retitems.push(o); | ||
} else if(itm.objectClass.indexOf('group')>-1){ | ||
o.type='GROUP'; | ||
o.name=itm.cn; | ||
retitems.push(o); | ||
} | ||
retitems.push(lditem2obj(itm)); | ||
count--; | ||
@@ -74,2 +135,6 @@ if (count<=0){ | ||
exports.getAllInLdap=function(options,cb){ | ||
if (options==null){ | ||
cb("null options",null); | ||
return; | ||
} | ||
var client=ldap.createClient({ | ||
@@ -124,4 +189,61 @@ url:'ldap://'+options.host+':389' | ||
exports.getSGroupsInLdap=function(options,cb){ | ||
if (options==null){ | ||
cb("null options",null); | ||
return; | ||
} | ||
var client=ldap.createClient({ | ||
url:'ldap://'+options.host+':389' | ||
}); | ||
var opts = { | ||
scope: 'sub', | ||
filter: '(&(objectClass=group))' | ||
}; | ||
client.bind(options.adminuser,options.adminpass,function(e){ | ||
if (e) { | ||
cb(e.message,null); | ||
console.log("getSGroupsInLdap:"+e.message); | ||
return; | ||
} | ||
var items = []; | ||
client.search(options.base,opts,function(err,res){ | ||
if (err) { | ||
cb(e.message,null); | ||
return; | ||
} | ||
res.on('searchEntry', function(entry) { | ||
items.push(entry.object); | ||
}); | ||
res.on('error', function(err) { | ||
cb(err.message,null); | ||
console.log("getSGroupsInLdap:"+err.message); | ||
return; | ||
}); | ||
res.on('end', function(result) { | ||
var count=items.length; | ||
if (count==0) { | ||
cb(null,[]); | ||
} else { | ||
retitems = []; | ||
items.forEach(function(itm){ | ||
var o= new Object(); | ||
o.path=itm.distinguishedName; | ||
o.name=itm.cn; | ||
retitems.push(o); | ||
count--; | ||
if (count<=0){ | ||
cb(null,retitems); | ||
} | ||
}); | ||
} | ||
}); | ||
}); | ||
}); | ||
} | ||
exports.getUser=function(options,username,password,cb){ | ||
var client=ldap.createClient({ | ||
@@ -138,3 +260,3 @@ url:'ldap://'+options.host+':389' | ||
if (e) { | ||
cb(e.message,null); | ||
cb("binding error:"+e.message,null); | ||
return; | ||
@@ -144,3 +266,2 @@ } else { | ||
if (err){ | ||
console.log("hata"+err); | ||
} | ||
@@ -147,0 +268,0 @@ res.on('searchEntry', function(entry) { |
@@ -26,4 +26,5 @@ var | ||
if (e){ | ||
console.log(e); | ||
console.log(newSearchOptions); | ||
console.log("fillChilds:"+e); | ||
console.log("fillChilds:"+JSON.stringify(newSearchOptions)); | ||
return; | ||
} | ||
@@ -49,8 +50,14 @@ if (t.items.length>0){ | ||
var me={base:options.base,items:[]}; | ||
if (options.adminuser==undefined){ | ||
options.adminuser=options.uname; | ||
options.adminpass=options.pwd; | ||
options.base="dc="+options.domain.replace(".",",dc="); | ||
} | ||
lib.searchInLdap(options,function(e,items){ | ||
if (e){ | ||
console.log("getJson:"+e); | ||
cb(e,null); | ||
return; | ||
} | ||
var count=items.length; | ||
@@ -76,9 +83,9 @@ if (count==0) { | ||
exports.getSecurityGroups=function(options,cb){ | ||
lib.getAllInLdap(options,function(e,items){ | ||
lib.getSGroupsInLdap(options,function(e,items){ | ||
if (e){ | ||
console.log("getSecurityGroups:"+e); | ||
cb(e,null); | ||
return; | ||
} else { | ||
cb(null,items); | ||
} | ||
cb(null,items); | ||
}); | ||
@@ -88,10 +95,21 @@ } | ||
exports.checkUser=function(options,username,password,cb){ | ||
lib.getUser(options,username,password,function(e,items){ | ||
lib.getUser(options,username,password,function(e,user){ | ||
if (e){ | ||
console.log("CheckUser err :"+e+'\n'+JSON.stringify(options)+'\n'+username+'\n'+password); | ||
cb(e,null); | ||
return; | ||
} else { | ||
cb(null,user); | ||
} | ||
cb(null,items); | ||
}); | ||
} | ||
exports.searchUser=function(options,keyword,cb){ | ||
lib.search(options,keyword,'user',function(e,items){ | ||
if (e){ | ||
cb(e,null); | ||
} else { | ||
cb(null,items); | ||
} | ||
}); | ||
} | ||
{ | ||
"name": "node-ldap2json", | ||
"version": "0.0.5", | ||
"description": "LDAP2JSON makes a json return from LDAP Tree", | ||
"version": "0.0.7", | ||
"description": "LDAP2JSON makes shortcuts LDAP.JS", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "vows ./test/*.js" | ||
"test": "mocha -R spec" | ||
}, | ||
@@ -32,4 +32,5 @@ "repository": { | ||
,"devDependencies": { | ||
"vows": "0.7.x" | ||
"should" : "*", | ||
"mocha" : "*" | ||
} | ||
} |
@@ -5,10 +5,17 @@ var | ||
,assert = require('assert') | ||
,should = require('should') | ||
; | ||
var getTreeOptions = { | ||
host : '10.1.60.5', | ||
adminuser : 'bh\\Administrator', | ||
adminpass : 'Genel1234', | ||
base : 'dc=bh,dc=pvt' | ||
var testVariables = { | ||
TreeOptions : { | ||
host : '10.1.60.5', | ||
adminuser : 'bh\\Administrator', | ||
adminpass : '1', | ||
base : 'dc=bh,dc=pvt' | ||
}, | ||
testuser:'serkan', | ||
testuserpass : '1', | ||
searchkeyword : 'gök' | ||
} | ||
@@ -18,27 +25,36 @@ | ||
vows.describe('ldap2json').addBatch({ | ||
'CheckUser': { | ||
topic : function(){ | ||
ldap2json.checkUser(getTreeOptions,'serkan','Genel123',this.callback); | ||
}, | ||
'callback':function(e,usr){ | ||
assert.ifError(e); | ||
} | ||
}, | ||
'Get Domain Tree' : { | ||
topic : function() { | ||
ldap2json.getJson(getTreeOptions,this.callback); | ||
}, | ||
'callback':function(e,tree){ | ||
assert.ifError(e); | ||
} | ||
}, | ||
'Get Security Groups' : { | ||
topic : function() { | ||
ldap2json.getSecurityGroups(getTreeOptions,this.callback); | ||
}, | ||
'callback':function(e,groups){ | ||
assert.ifError(e); | ||
} | ||
} | ||
}).export(module); | ||
describe('Ldap2Json',function(){ | ||
describe('CheckUser',function(){ | ||
it('returns a user',function(done){ | ||
ldap2json.checkUser(testVariables.TreeOptions,'serkan','1',function(e,usr){ | ||
usr.should.be.object; | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe('Get Domain Tree',function(){ | ||
it('returns an array',function(done){ | ||
ldap2json.getJson(testVariables.TreeOptions,function(e,tree){ | ||
tree.should.be.object; | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe('Get Security Groups',function(){ | ||
it('returns an array',function(done){ | ||
ldap2json.getSecurityGroups(testVariables.TreeOptions,function(e,grps){ | ||
grps.should.be.object; | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe('Search Users',function(){ | ||
it('returns an array',function(done){ | ||
ldap2json.searchUser(testVariables.TreeOptions,testVariables.searchkeyword,function(e,items){ | ||
items.should.be.object; | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12680
416
2