To use it from Rails, set up a configuration file, config/ldap.yml, as follows:
LDAP | 125
development:
host: (ldap server name)
port: 389
base: dc=mycompany,dc=com
password: my_password
production:
...
Then, at the bottom of config/environment.rb, set up the connection:
ldap_path = File.join(RAILS_ROOT,"config","ldap.yml")
ldap_config = YAML.load(File.read(ldap_path))[RAILS_ENV]
ActiveLDAP::Base.establish_connection(ldap_config)
To set up ActiveLDAP, just subclass ActiveLDAP::Base and set the LDAPmapping on
a class-by-class basis:
class Employee < ActiveLDAP::Base
ldap_mapping :prefix => "ou=Employees"
end
LDAP queries can then be executed using the class methods on ActiveLDAP::Base:
@dan = Employee.find :attribute => "cn", :value => "Dan"
Authenticating with LDAP
One of the most common reasons for using LDAPis to integrate into an existing
authentication structure. If an LDAPserver is provided for a Windows domain, this
will allow the web application to authenticate users against that domain rather than
maintaining its own user models separately.
Set up the ldap.yml file as described previously (without specifying a password), but
do not bind to the LDAPserver from environment.rb. We will perform the bind as
part of the authentication process. The following code is adapted from the Rails wiki:*
class LdapUser < ActiveLDAP::Base
ldap_mapping :prefix => (LDAP prefix for your users)
LDAP_PATH = File.
Pages:
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197