Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby net-ldap add user

I am trying to create a new user using ldap by doing this:

require 'net/ldap'

ldap = Net::LDAP.new
ldap.host = 'ldap'
ldap.auth('uid=myuser,ou=users,dc=my,dc=domain,dc=com', 'mypass')

ldap.bind # this executes successfully, up to this point, all is well

dn = 'uid=newuser,ou=users,dc=my,dc=domain,dc=com'
attributes = { cn: 'newuser', sn: 'surname', objectclass: ['top', 'agent'] }

ldap.add(dn: dn, attributes: attributes)

ldap.get_operation_result
#=> #<OpenStruct code=21, message="unknown result (21)">

I am new to ldap, and I can't find a place online that provides a clear example of how to use net-ldap to create a new user.

like image 880
HRÓÐÓLFR Avatar asked Apr 24 '26 10:04

HRÓÐÓLFR


1 Answers

I had these issues aswell, last 2 days, but finally figured out a solution to make my user. Here is the working code for me:

ldap = Net::LDAP.new
        ldap.host = SERVER
        ldap.port = PORT
        ldap.authenticate login, pass        

dn = "cn=Pick Name, dc=example, dc=com"
                attr = {
                  :cn => "Pick Name",
                  :objectclass => "User",
                  :sn => 'Name',
                  :telephoneNumber => "12345678",
                  :mail => "[email protected]"
                }
    ldap.add(:dn => dn, :attributes => attr)

The thing I think that has given me issues is the ObjectClass .. In the object class you write in the Type thats set in your AD. So if you wanted an organizational Unit, it would look like this: :objectclass => "organizationalUnit" Also I think the CN in DN and attr needs to be identical.

Hope this helps

like image 72
Thirst Avatar answered Apr 26 '26 01:04

Thirst



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!