我按照这些说明在我的 Rails 应用程序中通过 Devise 设置基本 LDAP 身份验证:http: //blackfistsecurity.blogspot.dk/2011/12/rails-authentication-using-devise-and.html
使用 AD 帐户首次登录会在数据库中创建相应的用户,从 get_ldap_param() 找到的 AD 帐户的对象属性中填充姓氏、名字、显示名和电子邮件
该过程运行良好,除非 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
我是否需要修改 Devise gem 才能做到这一点,还是可以在通话后以某种方式完成?
请原谅我的新手
谢谢