cschiewek / devise_ldap_authenticatable

Devise Module for LDAP
MIT License
593 stars 359 forks source link

Provide access to ldap connection in after_ldap_authentication callback, or a new callback #214

Open ghost opened 9 years ago

ghost commented 9 years ago

It would be beneficial if the ldap connection Devise::LDAP::Connection were made available in the after_ldap_authentication callback. Or, if a new callback were created which made the connection available.

For example, immediately after an LDAP user is authenticated, I want to save their photo. Something like this:

def save_ldap_photo(ldap)
  filter = Net::LDAP::Filter.eq('sAMAccountName', "#{LDAP_CONFIG[:domain]}\\#{@user.login}")
  ldap.search(base: LDAP_CONFIG[:base], filter: filter, return_result: true) do |entry|
    [:thumbnailphoto, :jpegphoto, :photo].each do |photo_key|
      if entry.attribute_names.include?(photo_key)
        ldap_photo = entry[photo_key][0]
        File.open(avatar_path, 'wb') { |f| f.write(ldap_photo) }
        break
      end
    end
  end
  File.size?(avatar_path)
end

Currently, I cannot do this without pulling in Net::LDAP and re-authenticating/binding with the login and password. If there were a callback which provided access to the ldap connection, I could just use it.

I looked at the source superficially, and the call to the callback in strategy.rb#L20 would have to change (among other things) to pass in the connection.

botboe commented 6 years ago

I access devise's Net::LDAP-object:

ldap_connection = Devise::LDAP::Adapter.ldap_connect(nil).ldap # I'm using 'config.ldap_use_admin_to_bind = true' in devise.rb
      resultset = ldap_connection
                      .search(base: 'dc=foo, dc=lan'...

It will use devise's config to establish a connection to the ldap server automatically.

Hope that helps...