[英]Creating a custom Devise Strategy
Been fighting with this for a while now, not sure why it isn't working. 现在已经为此战斗了一段时间,不确定为什么它不起作用。
The gist is looking to use Devise with LDAP. 要点是寻求将Devise与LDAP一起使用。 I don't need to do anything except for authenticate so I have no need to use anything except for a custom strategy.
除了身份验证外,我不需要做任何事情,因此除了自定义策略外,我不需要使用其他任何东西。
I created one based on https://github.com/plataformatec/devise/wiki/How-To:-Authenticate-via-LDAP and as far as I can tell everything should work except whenever I try to run the server (or rake route) I get a NameError
我创建了一个基于https://github.com/plataformatec/devise/wiki/How-To:-Authenticate-via-LDAP的应用程序 ,据我所知,一切正常,除非我尝试运行服务器(或耙)路线)我收到一个
NameError
lib/devise/models.rb:88:in `const_get': uninitialized constant Devise::Models::LdapAuthenticatable (NameError)
I've traced the error down to my app/models/user.rb
我已将错误追溯到我的
app/models/user.rb
class User < ActiveRecord::Base
devise :ldap_authenticatable, :rememberable, :trackable, :timeoutable
end
If I remove :ldap_authenticatable
then the crash goes away but I have no routes to user#session
and a login prompt cannot be accessed. 如果删除
:ldap_authenticatable
则崩溃消失了,但是我没有通往user#session
路由,并且无法访问登录提示。
My supporting files: 我的支持文件:
lib/ldap_authenticatable.rb
require 'net/ldap'
require 'devise/strategies/authenticatable'
module Devise
module Strategies
class LdapAuthenticatable < Authenticatable
def authenticate!
if params[:user]
ldap = Net::LDAP.new
ldap.host = 'redacted'
ldap.port = 389
ldap.auth login, password
if ldap.bind
user = User.where(login: login).first_or_create do |user|
success!(user)
else
fail(:invalid_login)
end
end
end
def login
params[:user][:login]
end
def password
params[:user][:password]
end
end
end
end
Warden::Strategies.add(:ldap_authenticatable, Devise::Strategies::LdapAuthenticatable)
And finally, inside config/initializers/devise.rb
最后,在
config/initializers/devise.rb
Devise.setup do |config|
# ==> LDAP Configuration
require 'ldap_authenticatable'
config.warden do |manager|
manager.default_strategies(:scope => :user).unshift :ldap_authenticatable
end
end
I've exhausted my searches, maybe someone can see something I am missing. 我已经用尽了所有搜索功能,也许有人可以看到我所缺少的内容。
Cheers 干杯
Is your lib/ldap_authenticatable.rb
is in the autoload path or explicitly required? 您的
lib/ldap_authenticatable.rb
是在自动加载路径中还是显式要求? Since Rails 3 code in lib folder isn't autoloaded by default any more. 由于默认情况下,lib文件夹中的Rails 3代码不再自动加载。 Here is one way on how to solve it
这是解决问题的一种方法
IMHO Devise is a great gem. 恕我直言,devise是一个伟大的宝石。 However in order to write your own strategy you have to be familiar not only with Devise but with Warden source code as well, and a lot of boilerplate code needs to be written in various places, so I start to investigate how to make customizations easier for Devise and come up with this gem devise_custom_authenticatable .
但是,为了编写自己的策略,您不仅必须熟悉Devise,还必须熟悉Warden源代码,并且需要在不同的地方编写很多样板代码,因此我开始研究如何使自定义更容易实现。设计并提出这个gem devise_custom_authenticatable 。 You can check it and probably it'll solve your problem in a different way.
您可以检查它,并可能以其他方式解决您的问题。 This gem is used in production code base for rather busy application, so it battle proven :)
该gem在生产代码库中用于相当繁忙的应用程序,因此经过了实践证明:)
File paths should match namespaces. 文件路径应与名称空间匹配。 You need to add 2 levels of directories.
您需要添加2级目录。
mkdir lib/devise
mkdir lib/devise/strategies
mv lib/ldap_authenticatable.rb lib/devise/strategies/ldap_authenticatable.rb
Since you are namespaced to 由于您被命名为
module Devise
module Strategies
class LdapAuthenticatable < Authenticatable
...
few steps to take care while creating custom strategy: 创建自定义策略时要注意的几个步骤:
you will have to take care of the strategies
folder as @csi mentioned along with it create a models
folder and inside models create ldap_authenticatable.rb
. 你将不得不照顾的
strategies
文件夹@csi与它一起提到建立一个models
文件夹,里面的模型创建ldap_authenticatable.rb
。 so the structure will look like this. 所以结构看起来像这样
lib/devise/strategies/ldap_authenticatable.rb lib/devise/models/ldap_authenticatable.rb
Add these lines to lib/devise/models/ldap_authenticatable.rb
将这些行添加到
lib/devise/models/ldap_authenticatable.rb
require Rails.root.join('lib/devise/strategies/ldap_authenticatable') module Devise module Models module LdapAuthenticatable extend ActiveSupport::Concern end end end
In config/initializers/devise.rb
add these lines to the top. 在
config/initializers/devise.rb
将这些行添加到顶部。
Devise.add_module(:ldap_authenticatable, { strategy: true, controller: :sessions, model: 'devise/models/ldap_authenticatable', route: :session })
This should take care of the custom auth. 这应注意自定义身份验证。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.