# ActiveLDAP and variable sub scope object writing

**URL:** <https://www.ruby-forum.com/t/activeldap-and-variable-sub-scope-object-writing/55223>\
**Category:** Rails\
**Created:** [March 18, 2006, 2:07am UTC](https://www.ruby-forum.com/t/activeldap-and-variable-sub-scope-object-writing/55223 "2006-03-18T02:07:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![heinous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e95f7d/32.png) [@heinous](https://www.ruby-forum.com/u/heinous)\
**Post date:** [March 18, 2006, 2:07am UTC](https://www.ruby-forum.com/t/activeldap-and-variable-sub-scope-object-writing/55223/1 "2006-03-18T02:07:02Z")

</div>

Anyone out there using ActiveLDAP have an idea how I can accomplish  
creating an object one level below a known base where we have a variable  
item in the middle?

That first sentence doesn’t even make sense to me. Here’s what I want  
to do: I have a user class that I use for managing users. Each user  
gets a ou called addressbook (which in turn will contain sub-entries,  
but we’re not worried about that) like so:

dn: uid=removed\_email\_address@domain.invalid,ou=Users,ou=OxObjects,dc=example,dc=com  
uid: removed\_email\_address@domain.invalid

dn: ou=addr,uid=removed\_email\_address@domain.invalid,ou=Users,ou=OxObjects,dc=example,dc=com  
objectClass: top  
objectClass: organizationalUnit  
ou: addr

dn:  
uid=135,ou=addr,uid=removed\_email\_address@domain.invalid,ou=Users,ou=OxObjects,dc=example,dc=com

I want to create the ou=addr,uid=removed\_email\_address@domain.invalid,PREFIX entry each time  
I create a user, but I can’t figure out how to get ActiveLDAP to let me  
do that. I had tried doing something like creating another model like  
this:

class Addr \< ActiveLDAP::Base  
ldap\_mapping :dnattr =\> ‘ou=addr,uid’, :prefix =\> USER\_PREFIX,  
:classes =\> [  
‘top’, ‘organizationalUnit’ ]  
end

And modifying the base.rb code for ActiveLDAP with a goofy hack to allow  
setting this sort of :dnattr value, but it picks up all the methods and  
attributes of a user object, so I can’t quite do that… Here’s the ex  
from base.rb:

```
  # Break val apart if it is a dn
  if val.match(/^ou=addr.*/i)
    val = $1
  elsif val.match(/^#{dnattr()}=([^,=]+),#{base()}$/i)
    val = $1
  elsif val.match(/[=,]/)
    @@logger.info "initialize: Changing val from '#{val}' to '' 

```

because it doesn’t match the DN."  
val = ‘’  
end

And before I start breaking this any furthor, I was wondering if someone  
has already done this sort of thing.

---

<div class="post-metadata">

**Author:** ![heinous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e95f7d/32.png) [@heinous](https://www.ruby-forum.com/u/heinous)\
**Post date:** [March 18, 2006, 4:09am UTC](https://www.ruby-forum.com/t/activeldap-and-variable-sub-scope-object-writing/55223/2 "2006-03-18T04:09:12Z")

</div>

On Sat, 2006-03-18 at 02:07 +0100, Leah C. wrote:

> uid: removed\_email\_address@domain.invalid  
> I create a user, but I can’t figure out how to get ActiveLDAP to let me  
> setting this sort of :dnattr value, but it picks up all the methods and  
> because it doesn’t match the DN."  
> val = ‘’  
> end
> 
> And before I start breaking this any furthor, I was wondering if someone  
> has already done this sort of thing.

* * *

Leah,

I don’t know OxObjects - open-Xchange but I do subscribe to their mail  
list as well as OGO and monitor a bit.

When I do personal address books, I don’t use a uid for the dn, I  
actually use cn instead but perhaps OxObjects stores them as you are  
trying.

Your notes at the top suggest a bit of confusion about the structure  
itself which would probably help if you had clarity about how they were  
to be stored…is it possible that you can get a response from your LDAP  
server from a personal Address Book entry that was created by  
open-Xchange? Something like this (not open-Xchange)

# ldapsearch -x \

-h [srv1.azapple.com](http://srv1.azapple.com)   
-D ‘cn=root,dc=azapple,dc=com’ -W   
-b ‘uid=craig,ou=People,dc=azapple,dc=com’   
‘(cn=test user)’  
Enter LDAP Password:

# extended LDIF

# 

# LDAPv3

# base \<uid=craig,ou=People,dc=azapple,dc=com\> with scope sub

# filter: (cn=test user)

# requesting: ALL

# 

# Test User, AddressBook, craig, People, [azapple.com](http://azapple.com)

dn: cn=Test User,ou=AddressBook,uid=craig,ou=People,dc=azapple,dc=com  
objectClass: top  
objectClass: person  
objectClass: organizationalPerson  
objectClass: inetOrgPerson  
cn: Test User  
sn: User  
title: Clients  
o: Test Company

# search result

search: 2  
result: 0 Success

# numResponses: 2

# numEntries: 1

I think that would help you figure out what the structure of entries are  
supposed to be exactly. It’s important to note that each ‘objectclass’  
has specific 'requirements of attributes such as above…objectclass  
person requires ‘cn’ and objectclass inetOrgPerson requires ‘sn’  
attributes - that is if memory serves me correctly but it would seem  
strange that an ‘address book entry’ would use uid at all as that is  
generally reserved for a system user.

As for fiddling…I haven’t started with LDAP / rails yet but that is in  
my next scheduled phase so it won’t be long.

Craig

---

<div class="post-metadata">

**Author:** ![heinous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e95f7d/32.png) [@heinous](https://www.ruby-forum.com/u/heinous)\
**Post date:** [March 20, 2006, 6:50pm UTC](https://www.ruby-forum.com/t/activeldap-and-variable-sub-scope-object-writing/55223/3 "2006-03-20T18:50:31Z")

</div>

I am just looking to put in the holding ou container for the address  
book, not the actual address entries. I use uid style DN’s with  
openxchange, and so these ou’s actually will look like:

ou=addr,uid=removed\_email\_address@domain.invalid,ou=Users,ou=OxObjects,dc=example,dc=com

I just need to understand how to add the ou=addr under the user’s DN of:

uid=removed\_email\_address@domain.invalid,ou=Users,ou=OxObjects,dc=example,dc=com

using the ActiveLDAP libraries. I did check all these values from a  
working OX user.

Thanks,  
Leah

Craig W. wrote:

> On Sat, 2006-03-18 at 02:07 +0100, Leah C. wrote:  
> SNIP

---

<div class="post-metadata">

**Author:** ![heinous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e95f7d/32.png) [@heinous](https://www.ruby-forum.com/u/heinous)\
**Post date:** [March 20, 2006, 8:18pm UTC](https://www.ruby-forum.com/t/activeldap-and-variable-sub-scope-object-writing/55223/4 "2006-03-20T20:18:45Z")

</div>

On Mon, 2006-03-20 at 19:46 +0100, Leah C. wrote:

> I should also mention that as soon as my LDAP objects have sub entries,  
> the delete functions don’t work either as it seems that they do not  
> recursively delete.

* * *

I think if you have enough logging turned on for you LDAP, you would see  
why. As I understand it, most LDAP systems don’t allow you to delete  
containers with objects in them. Client applications would probably want  
to obtain a list of all objects inside, delete them and then delete the  
container.

Craig

---

<div class="post-metadata">

**Author:** ![heinous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e95f7d/32.png) [@heinous](https://www.ruby-forum.com/u/heinous)\
**Post date:** [March 20, 2006, 7:46pm UTC](https://www.ruby-forum.com/t/activeldap-and-variable-sub-scope-object-writing/55223/5 "2006-03-20T19:46:59Z")

</div>

I should also mention that as soon as my LDAP objects have sub entries,  
the delete functions don’t work either as it seems that they do not  
recursively delete.

Leah C. wrote:

> I am just looking to put in the holding ou container for the address  
> book, not the actual address entries. I use uid style DN’s with  
> openxchange, and so these ou’s actually will look like:
> 
> ou=addr,uid=removed\_email\_address@domain.invalid,ou=Users,ou=OxObjects,dc=example,dc=com
> 
> I just need to understand how to add the ou=addr under the user’s DN of:
> 
> uid=removed\_email\_address@domain.invalid,ou=Users,ou=OxObjects,dc=example,dc=com
> 
> using the ActiveLDAP libraries. I did check all these values from a  
> working OX user.
> 
> Thanks,  
> Leah
> 
> Craig W. wrote:
> 
> > On Sat, 2006-03-18 at 02:07 +0100, Leah C. wrote:  
> > SNIP

---

<div class="post-metadata">

**Author:** ![heinous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e95f7d/32.png) [@heinous](https://www.ruby-forum.com/u/heinous)\
**Post date:** [March 20, 2006, 9:48pm UTC](https://www.ruby-forum.com/t/activeldap-and-variable-sub-scope-object-writing/55223/6 "2006-03-20T21:48:21Z")

</div>

Craig W. wrote:

> On Mon, 2006-03-20 at 19:46 +0100, Leah C. wrote:
> 
> > I should also mention that as soon as my LDAP objects have sub entries,  
> > the delete functions don’t work either as it seems that they do not  
> > recursively delete.
> 
> * * *
> 
> I think if you have enough logging turned on for you LDAP, you would see  
> why. As I understand it, most LDAP systems don’t allow you to delete  
> containers with objects in them. Client applications would probably want  
> to obtain a list of all objects inside, delete them and then delete the  
> container.

Yes, that is correct. So I need to understand how to correctly set the  
PREFIX to uid=removed\_email\_address@domain.invalid,ou=Users,ou=OxObjects,dc=example,dc=com  
in the Addr model for ActiveLDAP. Since the uid=removed\_email\_address@domain.invalid part  
is dynamic, I think I need to somehow tell the Addr class what that is,  
and I am not clear on how to do that…

class Addr \< ActiveLDAP::Base

ldap\_mapping :dnattr =\> ‘ou’, :prefix =\> PREFIX, :classes =\> [  
‘top’, ‘organizationalUnit’ ]

end

If I could create a model like this, then I could easily remove the  
ou=addr entries. I think.

Leah

---

<div class="post-metadata">

**Author:** ![heinous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e95f7d/32.png) [@heinous](https://www.ruby-forum.com/u/heinous)\
**Post date:** [March 20, 2006, 11:16pm UTC](https://www.ruby-forum.com/t/activeldap-and-variable-sub-scope-object-writing/55223/7 "2006-03-20T23:16:31Z")

</div>

Leah C. wrote:

> Yes, that is correct. So I need to understand how to correctly set the  
> PREFIX to uid=removed\_email\_address@domain.invalid,ou=Users,ou=OxObjects,dc=example,dc=com  
> in the Addr model for ActiveLDAP. Since the uid=removed\_email\_address@domain.invalid part  
> is dynamic, I think I need to somehow tell the Addr class what that is,  
> and I am not clear on how to do that…
> 
> class Addr \< ActiveLDAP::Base
> 
> ldap\_mapping :dnattr =\> ‘ou’, :prefix =\> PREFIX, :classes =\> [  
> ‘top’, ‘organizationalUnit’ ]
> 
> end
> 
> If I could create a model like this, then I could easily remove the  
> ou=addr entries. I think.

One thing I was thinking of trying was something like this, does it make  
sense? I’m a little new to working with Ruby objects:

class Addr \< ActiveLDAP::Base

def initialize(val,uid)  
super(val)  
@uid = uid  
@prefix = ‘uid=’ + @uid + ‘,’ + USER\_PREFIX  
end

ldap\_mapping :dnattr =\> ‘ou=addr,uid’, :prefix =\> @prefix, :classes =\>  
[  
‘top’, ‘organizationalUnit’ ]

end

And then calling Addr like:

def add\_addr(user)  
new\_addr = Addr.new(‘addr’,user.uid.to\_s)  
new\_addr.organizationalUnit = ‘addr’  
new\_addr.write  
end
