Devise/LDAP - Save extra ldap attributes in db

2.3k Views Asked by At

I'm running rails 3.1 with latest Devise and ldap plugin. I have exchanged the default behaviour of using email address and now uses the uid attribute in Novell eDir as my userfield.

I can log in, I get redirected based on root route etc.

What I am now struggeling with is to get email and a custom attribute named category from my ldap and inserted into the database. Im trying to get the before_save working.

class User < ActiveRecord::Base

    before_save :get_ldap_email
    before_save :get_ldap_category

    # Include default devise modules. Others available are:
    # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and  :omniauthable
    devise :ldap_authenticatable, :rememberable, :trackable

    # Setup accessible (or protected) attributes for your model
    attr_accessible :uid, :category, :email, :password, :password_confirmation, :remember_me

    def get_ldap_email
        self.email = Devise::LdapAdapter.get_ldap_param(self.uid, "mail")
    end

    def get_ldap_category
        self.category = Devise::LdapAdapter.get_ldap_param(self.uid, "category")
    end

end

Im probably dumb but I cant get this to work. I get the following output from my view at login:

NoMethodError in Devise::SessionsController#create

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each
Rails.root: /home/ntr/Dropbox/source/devise

Application Trace | Framework Trace | Full Trace
Request

Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"jljpH9bUXrPeBVy4jKnJoAfeuO/EiF0YxRqxm07VF5w=",
 "user"=>{"uid"=>"ntr",
 "password"=>"[FILTERED]",
 "remember_me"=>"0"},
 "commit"=>"Logga in"}
Show session dump

Show env dump

Response

Headers:

None

What am I doing wrong? Would love to get some help with this!

Best regards //Roger

1

There are 1 best solutions below

0
On

I'm not sure is it help you, but you can use it like that:

self.firstname = Devise::LdapAdapter.get_ldap_param(self.username, 'givenName').first
self.lastname = Devise::LdapAdapter.get_ldap_param(self.username, 'sn').first    
self.email = Devise::LdapAdapter.get_ldap_param(self.username, 'mail').first  

This happens when your search fetches few results instead one and you'll get nil valuse Here is a hack which always return array die to ldap nature: in the file config/initializer/devise.rb add following patch:

module Devise
  module LdapAdapter
    class LdapConnect
      def ldap_param_value(param)
        filter = Net::LDAP::Filter.eq(@attribute.to_s, @login.to_s)
        ldap_entry = nil
        @ldap.search(:filter => filter) {|entry| ldap_entry = entry}

        if ldap_entry 
          if ldap_entry[param]
            DeviseLdapAuthenticatable::Logger.send("Requested param #{param} has value #{ldap_entry.send(param)}")
            value = ldap_entry.send(param)           
          else
            DeviseLdapAuthenticatable::Logger.send("Requested param #{param} does not exist")
            #changes here
            value = nil
          end
        else
          DeviseLdapAuthenticatable::Logger.send("Requested ldap entry does not exist")
          value = nil
        end
      end
    end
  end
end

Look more at https://github.com/cschiewek/devise_ldap_authenticatable/issues/69 and compare patch with original code to understand.