2012-04-30 47 views
0

我按照這些說明設置通過制定基本LDAP身份驗證在我的Rails應用程序: http://blackfistsecurity.blogspot.dk/2011/12/rails-authentication-using-devise-and.htmlNoMethodError與設計LDAP和空白的用戶對象屬性

首次登錄與AD帳戶創建了相應的用戶數據庫,填充姓氏,名字,顯示名和電子郵件來自AD發現的對象屬性get_ldap_param()

該過程工作正常除AD用戶具有空白屬性時,如空白姓氏(AD屬性'sn') - 導致以下錯誤:

NoMethodError in Devise::SessionsController#create 
undefined method `sn' for #<Net::LDAP::Entry:0x00000004920758> 

用戶模型如下:

class User < ActiveRecord::Base 
    # Include default devise modules. Others available are: 
    # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable 
    devise :ldap_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable 

    attr_accessible :username, :email, :password, :password_confirmation, :remember_me, :firstname, :lastname, :displayname 

    before_save :get_ldap_lastname, :get_ldap_firstname, :get_ldap_displayname, :get_ldap_email 

    def get_ldap_lastname 
     Rails::logger.info("### Getting the users last name") 
     tempname = Devise::LdapAdapter.get_ldap_param(self.username,"sn") 
     puts "\tLDAP returned lastname of " + tempname 
     self.lastname = tempname 
    end 

    def get_ldap_firstname 
     Rails::logger.info("### Getting the users first name") 
     tempname = Devise::LdapAdapter.get_ldap_param(self.username,"givenname") 
     puts "\tLDAP returned firstname of " + tempname 
     self.firstname = tempname 
    end 

    def get_ldap_displayname 
     Rails::logger.info("### Getting the users display name") 
     tempname = Devise::LdapAdapter.get_ldap_param(self.username,"displayname") 
     self.displayname = tempname 
    end 

    def get_ldap_email 
     Rails::logger.info("### Getting the users email address") 
     tempmail = Devise::LdapAdapter.get_ldap_param(self.username,"mail") 
     self.email = tempmail 
    end 
end 

錯誤是從該調用來:

Devise::LdapAdapter.get_ldap_param(self.username,"sn") 

當get_ldap_param遇到空白屬性,我想它會返回空,而不是不確定的。

我試着用下面的代碼解決了這個問題,但它不起作用,因爲錯誤發生在調用get_ldap_param的那一刻。

if tempname.none? 
    tempname = "" 
end 

我是否需要修改設計的寶石要做到這一點,還是可以調用後莫名其妙地做了什麼?

請原諒我的newbness

感謝

回答

1

這看起來像devise_ldap_authenticatable色器件模塊中的錯誤。它使用method accessor for the LDAP entry attribute values instead of the hash operatior

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) 
     value = value.first if value.is_a?(Array) and value.count == 1 
     value 

按照文檔紅寶石淨LDAP,散列訪問應該被用來檢索類似下面的值:

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 value = ldap_entry[param] 
     DeviseLdapAuthenticatable::Logger.send("Requested param #{param} has value #{value}") 
     value = value.first if value.is_a?(Array) and value.count == 1 
     value 

這是一個錯誤,你可以用devise_ldap_authenticatable更新the issue you filed這個信息。

+0

我修改了你的修改ldap_adapter.rb - 我現在可以通過登錄過程,而不是空白的信息,但它插入'--- []',可能插入空白欄數組。就像你說的那樣,def似乎是devise_ldap中的一個bug。我將添加你對我發佈在他們的git頁面上的問題的回答。謝謝 – samJL