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

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

Open
ghost opened this issue Aug 10, 2015 · 1 comment

Comments

@ghost
Copy link

ghost commented Aug 10, 2015

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.

@ghost ghost changed the title Provide access to ldap connection in ldap_authenticatable callback, or a new callback Provide access to ldap connection in after_ldap_authentication callback, or a new callback Aug 10, 2015
@botboe
Copy link

botboe commented Sep 22, 2017

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...

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

1 participant