From cfb927672c7d5f7b53b8765ff32f16789f449c88 Mon Sep 17 00:00:00 2001 From: Aaron Bean Date: Tue, 22 Jul 2014 14:02:23 -0500 Subject: [PATCH] initial commit --- .gitignore | 3 ++ lib/edir.js | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 25 +++++++++++++ 3 files changed, 129 insertions(+) create mode 100644 .gitignore create mode 100644 lib/edir.js create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..96de7bd --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea +node_modules +*.iml diff --git a/lib/edir.js b/lib/edir.js new file mode 100644 index 0000000..5f41538 --- /dev/null +++ b/lib/edir.js @@ -0,0 +1,101 @@ +'use strict'; + +var _ = require('lodash'); +var Ldap = require('lderp').Ldap; +var util = require('util'); + +function EdirLdap(host, options, zombieUsername, zombiePassword) { + options = options || {}; + Ldap.call(this, host, options); + this.name = 'edir'; +// this.defaultAttributes = ['ACL', 'cn', 'dn', 'givenName', 'groupMembership', 'loginDisabled', 'loginTime', 'mail', 'objectClass', 'sn', 'telephoneNumber', 'uid']; + this.usernameAttribute = options.usernameAttribute || 'cn'; + this.zombieUsername = options.zombieUsername || ''; + this.zombiePassword = options.zombiePassword || ''; +} + +util.inherits(EdirLdap, Ldap); + +EdirLdap.prototype.bindAsZombie = function (zombieUsername, zombiePassword) { + return Ldap.prototype.bindAsUser.call(this, buildDn(zombieUsername || this.zombieUsername), zombiePassword || this.zombiePassword); +}; + +var buildDn = function (cn) { + return 'cn=' + cn + ',' + config.baseDn; +}; + +var buildObjectClass = function () { + return [ + 'inetOrgPerson', + 'organizationalPerson', + 'Person', + 'ndsLoginProperties', + 'Top' + ]; +}; + +EdirLdap.prototype.buildUserEntry = function (options) { + var entry = _.clone(options); + entry.objectClass = buildObjectClass(); + entry.uid = entry.cn; + return entry; +}; + +EdirLdap.prototype.createUser = function (options) { + return Ldap.prototype.createUser.call(this, buildDn(options.cn), options); +}; + +EdirLdap.prototype.deleteUser = function (cn) { + return Ldap.prototype.deleteUser.call(this, buildDn(cn)); +}; + +EdirLdap.prototype.findAllEmailAddressless = function (startsWith) { + return Ldap.prototype._search.call(this, '(&(cn=' + startsWith + '*)(!(cn=*@*)))'); +}; + +EdirLdap.prototype.modifyUser = function (cn, options) { + var self = this; + var newCn = options.cn || options.username || null; + var newUserPassword = options.userPassword || options.password || null; + var newSn = options.sn || options.firstname || null; + var newGivenName = options.givenName || options.lastname || null; + var newMail = options.mail || options.email || null; + return findUser(cn) + .then(function (user) { + self.user = user; + if (!!newCn) { + var newDn = buildDn(newCn); + return self._client.modifyDNAsync(user.dn, newDn) + .then(function () { + return newDn; + }) + } + else { + return user.dn; + } + }) + .then(function (dn) { + var changes = []; + if (!!newCn) { + changes.push(self.buildLdapChangeObject('replace', {uid: newCn})); // keeping UID and CN in sync + } + if (!!newUserPassword) { + changes.push(self.buildLdapChangeObject('replace', {userPassword: newUserPassword})); + } + if (!!newSn) { + changes.push(self.buildLdapChangeObject('replace', {sn: newSn})); + } + if (!!newGivenName) { + changes.push(self.buildLdapChangeObject('replace', {givenName: newGivenName})); + } + if (!!newMail) { + changes.push(self.buildLdapChangeObject('replace', {mail: newMail})); + } + return self._client.modifyAsync(dn, changes); + }) + .then(function (r) { + return r; + }) +}; + +module.exports = EdirLdap; diff --git a/package.json b/package.json new file mode 100644 index 0000000..acf1fdb --- /dev/null +++ b/package.json @@ -0,0 +1,25 @@ +{ + "name": "lderp-edir", + "description": "eDirectory extension for lderp", + "version": "0.0.1", + "keywords": [ + "ldap", + "promise", + "edirectory" + ], + "homepage": "https://github.com/beardon/lderp-edir", + "bugs": { + "url": "https://github.com/beardon/lderp-edir/issues", + "email": "aaron.bean@beardon.com" + }, + "author": { + "name": "Aaron Bean", + "email": "aaron.bean@beardon.com", + "url": "https://github.com/aaronbean" + }, + "main": "./lib/edir", + "dependencies": { + "lderp": "0.0.3", + "lodash": "^2.4.1" + } +}