# ActiveLDAP maintain user connection

**URL:** <https://www.ruby-forum.com/t/activeldap-maintain-user-connection/76783>\
**Category:** Rails\
**Created:** [March 5, 2006, 1:12am UTC](https://www.ruby-forum.com/t/activeldap-maintain-user-connection/76783 "2006-03-05T01:12:02Z")\
**Posts on this page:** 3\
**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 5, 2006, 1:12am UTC](https://www.ruby-forum.com/t/activeldap-maintain-user-connection/76783/1 "2006-03-05T01:12:02Z")

</div>

Hi, I’ve just started to do some work using ActiveLDAP and Rails. I’ve  
managed to get some basic login code that works to authenticate users,  
but I would like to be able to maintain that connection for the User  
Model so that the ACL’s in LDAP for each user will be used correctly as  
the various controller queries are made. I haven’t quite sorted out how  
to do that. Does anyone have any examples of code to do this, or a  
documentation pointer?

```

class User < ActiveLDAP::Base

        ldap_mapping :dnattr => 'uid', :prefix => USER_PREFIX, :classes 
=> [
                'top', 'shadowAccount', 'posixAccount', 'person', 
'inetOrgPerson',
                'OXUserObject', 'organizationalPerson'
        ]
        belongs_to :groups, :class_name => 'Group', :foreign_key => 
'memberUid'

        def self.login(username, password)
                begin
                        ActiveLDAP::Base.connect(
                                :host => HOST,
                                :port => PORT,
                                :base => BASE,
                                :bind_format => 
"uid=#{username},#{USER_PREFIX},#{BASE}",
                                :password_block => Proc.new { password 
},
                                :allow_anonymous => false,
                                :try_sasl => false
                        )
                        #ldap = ActiveLDAP::Base.connection
                        #ActiveLDAP::Base.close
                        return true
                        rescue ActiveLDAP::AuthenticationError
                        return false
                end
        end
end

```

---

<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 5, 2006, 3:02am UTC](https://www.ruby-forum.com/t/activeldap-maintain-user-connection/76783/2 "2006-03-05T03:02:43Z")

</div>

On Saturday 04 March 2006 05:12 pm, Leah C. wrote:

> Hi, I’ve just started to do some work using ActiveLDAP and Rails. I’ve  
> managed to get some basic login code that works to authenticate users,  
> but I would like to be able to maintain that connection for the User  
> Model so that the ACL’s in LDAP for each user will be used correctly as  
> the various controller queries are made. I haven’t quite sorted out how  
> to do that. Does anyone have any examples of code to do this, or a  
> documentation pointer?

You won’t be able to have multiple ldap connections under one rails app  
using  
ActiveLDAP. It uses a global scope for a connection handler in its  
foundation. If your design doesn’t need multiple connections at a  
single  
time, then this is okay. I ran into the problem of needing them on a  
per  
model basis.

You can roll your own system for that or you can try my  
plugin(acts\_as\_ldapable), just a warning quirks haven’t been worked  
out…  
But it allows for a singleton based ldap connection.

## –

Sincerely,  
Adam B.  
Plutonic Networks  
Learn about DotGNU =)  
[http://www.dotgnu.org](http://www.dotgnu.org)

---

<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 5, 2006, 5:28am UTC](https://www.ruby-forum.com/t/activeldap-maintain-user-connection/76783/3 "2006-03-05T05:28:56Z")

</div>

Adam B. wrote:

> You can roll your own system for that or you can try my  
> plugin(acts\_as\_ldapable), just a warning quirks haven’t been worked  
> out…  
> But it allows for a singleton based ldap connection.

Thanks. I’d like to take a look at your plugin. It looks like the link  
on the wiki is broken, however. Would you posting that?
