Skip to content

Instantly share code, notes, and snippets.

Created January 28, 2010 23:26
Show Gist options
  • Save anonymous/289267 to your computer and use it in GitHub Desktop.
Save anonymous/289267 to your computer and use it in GitHub Desktop.
def addLdapUser(username, lastname, firstname, email, ldapuser, ldappassword)
config = loadConfig('../configuration.yaml')
l = LdapConnection.new
l.login(ldapuser, ldappassword)
uidNumber = l.findNextUIDNumber.to_s
gidNumber = config['UserSetup']['DefaultGroupID'].to_s
options = { 'cn' => [ lastname ] ,
'sn' => [ lastname ] ,
'mail' => [ email ] ,
'uid' => [ username ] ,
'uidNumber' => [ uidNumber ] ,
'gidNumber' => [ gidNumber ] ,
'gecos' => [ "#{firstname} #{lastname}" ] ,
'homedirectory' => [ config['UserSetup']['HomePrefix'] + '/' + username ] ,
'loginshell' => [ config['UserSetup']['LoginShell'] ] ,
'givenName' => [ firstname ] ,
'objectclass' => config['UserSetup']['ObjectClasses']
}
dn = "uid=#{username},#{config['UserSetup']['AccountOU']},#{config['LDAPInfo']['BaseDN']}"
begin
l.add(dn, options)
# This might be rescuing other types of errors than simply duplicates, but I haven't been able to create any yet
rescue LDAP::ResultError
puts "Userid '#{username}' already found in LDAP."
exit 2
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment