Ldap Agent
Connected to ldap server
Install
npm install --save ldap-agent
Examples
Config file
{
"cn": ["Manager"],
"dc": ["maxcrc", "com"],
"url": "ldap://127.0.0.1:389",
"password": "123456"
}
Add
const LdapAgent = require('ldap-agent');
const config = require('./config.json');
const Client = new LdapAgent(config);
Client.Init()
.then(() => {
console.log('Connected to ldap server');
const entry = {
cn: 'foo',
sn: 'bar',
mail: 'sergio@telefonica.com',
objectclass: [
'person',
'organizationalPerson',
'inetOrgPerson',
],
};
return Client.Add(['usuarios'], ['testFolder'], entry, 'mail');
})
.then((data) => {
console.log('Field added:', data);
})
.catch((errorGeneric) => {
console.error(`Error generic: ${errorGeneric.message}`);
});
Connection (callback)
const LdapAgent = require('ldap-agent');
const config = require('./config.json');
const Client = new LdapAgent(config);
Client.Init((connectError) => {
if (connectError) {
console.error(`Error connection: ${connectError.message}`);
return;
}
console.log('Connected to ldap server (callback)');
});
Connection (promise)
const LdapAgent = require('ldap-agent');
const config = require('./config.json');
const Client = new LdapAgent(config);
Client.Init()
.then(() => {
console.log('Connected to ldap server (promise)');
})
.catch((connectError) => {
console.error(`Error connection: ${connectError.message}`);
});
Delete
const LdapAgent = require('ldap-agent');
const config = require('./config.json');
const Client = new LdapAgent(config);
Client.Init()
.then(() => {
console.log('Connected to ldap server');
const condition = {
mail: 'test@telefonica.com',
};
return Client.Delete(condition, ['usuarios'], ['testFolder']);
})
.then((data) => {
console.log('Field deleted:', data);
})
.catch((errorGeneric) => {
console.error(`Error generic: ${errorGeneric.message}`);
});
Search all
const LdapAgent = require('ldap-agent');
const config = require('./config.json');
const Client = new LdapAgent(config);
Client.Init()
.then(() => {
console.log('Connected to ldap server');
return Client.Find(['usuarios'], ['testFolder'], { attributes: 'sn' });
})
.then((data) => {
console.log(`${data.length} fields found!`);
})
.catch((errorGeneric) => {
console.error(`Error generic: ${errorGeneric.message}`);
});
Search with one filter
const LdapAgent = require('ldap-agent');
const config = require('./config.json');
const Client = new LdapAgent(config);
Client.Init()
.then(() => {
console.log('Connected to ldap server');
const options = {
attributes: 'sn',
filter: {
mail: 'test@telefonica.com',
},
};
return Client.Find(['usuarios'], ['testFolder'], options);
})
.then((data) => {
console.log('Field found:', data);
})
.catch((errorGeneric) => {
console.error(`Error generic: ${errorGeneric.message}`);
});
Search two filters
const LdapAgent = require('ldap-agent');
const config = require('./config.json');
const Client = new LdapAgent(config);
Client.Init()
.then(() => {
console.log('Connected to ldap server');
const options = {
attributes: 'sn',
filter: {
mail: '*@telefonica.com',
sn: 'bar',
},
};
return Client.Find(['usuarios'], ['testFolder'], options);
})
.then((data) => {
console.log('Fields found:', data.length);
})
.catch((errorGeneric) => {
console.error(`Error generic: ${errorGeneric.message}`);
});
Update values
const LdapAgent = require('ldap-agent');
const config = require('./config.json');
const Client = new LdapAgent(config);
Client.Init()
.then(() => {
console.log('Connected to ldap server');
const update = {
sn: 'New value',
};
const where = {
mail: 'test@telefonica.com',
};
return Client.Update(update, where, ['usuarios'], ['testFolder']);
})
.then((data) => {
console.log('Field updated:', data);
})
.catch((errorGeneric) => {
console.error(`Error generic: ${errorGeneric.message}`);
});