我正在使用最新的Devise和ldap插件运行rails 3.1。我已经交换了使用电子邮件地址的默认行为,现在使用Novell eDir中的uid属性作为我的用户字段。
我可以登录,我会根据根路线等重定向。
我现在想要的是从我的ldap获取电子邮件和名为category的自定义属性并将其插入到数据库中。我试图让before_save工作。
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
我可能很蠢但是我不能让它起作用。我在登录时从我的视图中获得以下输出:
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
我做错了什么?很想得到一些帮助!
祝你好运 //罗杰
答案 0 :(得分:2)
我不确定它对你有帮助,但你可以这样使用它:
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
当您的搜索取得少量结果而不是一个结果时会发生这种情况,您将获得零值 这是一个总是将数组模具返回到ldap性质的hack: 在文件config / initializer / devise.rb中添加以下补丁:
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
仔细阅读https://github.com/cschiewek/devise_ldap_authenticatable/issues/69并将补丁与原始代码进行比较,以便了解。