Спасибо, Сри, за вашу помощь. Теперь он работает правильно! Вот мое решение:
# config/initializers/ldpa_authenticatable.rb
module Devise
module Models
module LdapAuthenticatable
extend ActiveSupport::Concern
end
end
module Strategies
class LdapAuthenticatable < Authenticatable
# This method first checks if the user is present in the database.
# After that, we authenticate the user thanks to the LDAP server
def authenticate!
if params[:user]
user = User.find_by(username: username)
if user.nil?
return fail(:invalid_login)
else
ldap = Ldap.client
ldap_username = Ldap.get_login(username)
puts ldap_username.inspect
ldap.authenticate ldap_username, password
if ldap.bind
success!(user)
else
return fail(:invalid_login)
end
end
end
end
def username
params[:user][:username]
end
def password
params[:user][:password]
end
end
end
end
Warden::Strategies.add(:ldap_authenticatable, Devise::Strategies::LdapAuthenticatable)
Devise.add_module(:ldap_authenticatable,
route: :session, ## This will add the routes, rather than in the routes.rb
strategy: true,
controller: :sessions,
model: "ldap_authenticatable")