Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can you add examples of AddRequest and DelRequest? #145

Closed
OuSatoru opened this issue Dec 22, 2017 · 6 comments
Closed

Can you add examples of AddRequest and DelRequest? #145

OuSatoru opened this issue Dec 22, 2017 · 6 comments

Comments

@OuSatoru
Copy link

OuSatoru commented Dec 22, 2017

Is moving a user deleting and then adding to another directory?
When I add a user it returns error code 65 'Object Class Violation'

@mevric
Copy link

mevric commented Feb 16, 2018

Code to add entries using go-ldap

package main

import (
"fmt"
"github.com/go-ldap/ldap"
"log"
)

const (
host = "192.168.30.111"
port = "389"
hostPort = host + ":" + port
userID = "uid=jdoe,ou=people,dc=it,dc=example,dc=com"
password = "jdoe"
)

func main() {
addEntries()
}

func addEntries(){
fmt.Println("Adding started")
l, err := ldap.Dial("tcp", hostPort)
if err != nil {
log.Fatal(err)
}
defer l.Close()

bindusername := "uid=admin,ou=system"
bindpassword := "secret"

err = l.Bind(bindusername, bindpassword)
if err != nil {
	log.Fatal(err)
	return
}

a := ldap.NewAddRequest("dc=mview,dc=example,dc=com")
a.Attribute("objectClass" ,[]string{"top"})
a.Attribute("objectClass" ,[]string{"dcObject"})
a.Attribute("objectClass" ,[]string{"organization"})
a.Attribute("dc" ,[]string{"mview"})
a.Attribute("o" ,[]string{"Google"})
add(a , l)

a = ldap.NewAddRequest("ou=groups,dc=mview,dc=example,dc=com")
a.Attribute("objectClass" ,[]string{"organizationalUnit"})
a.Attribute("objectClass" ,[]string{"top"})
a.Attribute("ou" ,[]string{"groups"})
a.Attribute("description" ,[]string{"groups"})
add(a , l)

a = ldap.NewAddRequest("ou=people,dc=mview,dc=example,dc=com")
a.Attribute("objectClass" ,[]string{"organizationalUnit"})
a.Attribute("objectClass" ,[]string{"top"})
a.Attribute("ou" ,[]string{"people"})
add(a , l)

a = ldap.NewAddRequest("cn=superadmin,ou=groups,dc=mview,dc=example,dc=com")
a.Attribute("objectClass" ,[]string{"extensibleObject"})
a.Attribute("objectClass" ,[]string{"groupOfNames"})
a.Attribute("objectClass" ,[]string{"top"})
a.Attribute("cn" ,[]string{"superadmin"})
a.Attribute("member" ,[]string{"uid=adminUser1,ou=people,dc=mview,dc=example,dc=com"})
a.Attribute("description" ,[]string{"superadmin group"})
add(a , l)

a = ldap.NewAddRequest("cn=readonly,ou=groups,dc=mview,dc=example,dc=com")
a.Attribute("objectClass" ,[]string{"extensibleObject"})
a.Attribute("objectClass" ,[]string{"groupOfNames"})
a.Attribute("objectClass" ,[]string{"top"})
a.Attribute("cn" ,[]string{"readonly"})
a.Attribute("member" ,[]string{"uid=readUser1,ou=people,dc=mview,dc=example,dc=com"})
a.Attribute("description" ,[]string{"readUser1 group"})
add(a , l)

a = ldap.NewAddRequest("uid=adminUser1,ou=people,dc=mview,dc=example,dc=com")
a.Attribute("objectClass" ,[]string{"extensibleObject"})
a.Attribute("objectClass" ,[]string{"uidObject"})
a.Attribute("objectClass" ,[]string{"account"})
a.Attribute("objectClass" ,[]string{"userSecurityInformation"})
a.Attribute("objectClass" ,[]string{"top"})
a.Attribute("uid" ,[]string{"adminUser1"})
a.Attribute("email" ,[]string{"[email protected]"})
a.Attribute("member" ,[]string{"cn=superadmin,ou=groups,dc=mview,dc=example,dc=com"})
a.Attribute("name" ,[]string{"Superadmin"})
a.Attribute("sn" ,[]string{"YYY"})
a.Attribute("userPassword" ,[]string{"adminUser1"})
add(a , l)

a = ldap.NewAddRequest("uid=readUser1,ou=people,dc=mview,dc=example,dc=com")
a.Attribute("objectClass" ,[]string{"extensibleObject"})
a.Attribute("objectClass" ,[]string{"uidObject"})
a.Attribute("objectClass" ,[]string{"account"})
a.Attribute("objectClass" ,[]string{"userSecurityInformation"})
a.Attribute("objectClass" ,[]string{"top"})
a.Attribute("uid" ,[]string{"readUser1"})
a.Attribute("email" ,[]string{"[email protected]"})
a.Attribute("member" ,[]string{"cn=readonly,ou=groups,dc=mview,dc=example,dc=com"})
a.Attribute("name" ,[]string{"Guser"})
a.Attribute("sn" ,[]string{"XXX"})
a.Attribute("userPassword" ,[]string{"readUser1"})
add(a , l)

}
func add(addRequest *ldap.AddRequest , l *ldap.Conn) {
err := l.Add(addRequest)
if err != nil {
fmt.Println("Entry NOT done",err)
} else {
fmt.Println("Entry DONE",err)
}
}

@vetinari
Copy link
Contributor

Moving a User is done with the ModifyDN(). This is currently (finallly ... ;-)) in master, but not tagged for gopkg.in/ldap.v2 ...

@gallart
Copy link

gallart commented Oct 12, 2018

Hi @mevric ,

Your code triggers the following error :

Entry NOT done LDAP Result Code 20 "Attribute Or Value Exists": attribute 'objectClass' provided more than once

How did you manage to pass multiple objectClass to your new entries?

Thx,

Gaëtan

@johnweldon
Copy link
Member

#150 calls for more example code - closing this in favor of that issue

@aintbrokedontfix
Copy link

Hi @mevric ,

Your code triggers the following error :

Entry NOT done LDAP Result Code 20 "Attribute Or Value Exists": attribute 'objectClass' provided more than once

How did you manage to pass multiple objectClass to your new entries?

Thx,

Gaëtan

Just in case... here is how you add multiple objectClass.

https://stackoverflow.com/questions/59803733/how-do-i-add-multiple-objectclass-attributes-using-gopkg-in-ldap-v3-ldap-newaddr/59805141#59805141

This was referenced Jun 27, 2023
@ghost
Copy link

ghost commented Jun 27, 2023

note that new versions of NewAddRequest need two arguments:

var controls = []ldap.Control{}
ldap.NewAddRequest("cn=tom,ou=users,dc=example,dc=org", controls)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants