我正在开发基于Rails应用程序的身份验证系统。身份验证使用net-ldap类从Active Directory验证帐户信息(此部分工作正常)。基本ruby on rails身份验证问题
但是,我的session_helper似乎有问题。即使ActiveDirectoryUser.authenticate成功,signed_in助手始终返回false。登录后,脚本重定向到root_path(default_controller的主目录),然后再次重定向到signin_path--由于signed_in助手返回false。
请参阅下面的代码。我错过了什么?
感谢
application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery
include SessionsHelper
end
default_controller.rb
class DefaultController < ApplicationController
before_filter :signed_in_user
def home
end
private
def signed_in_user
redirect_to signin_path, notice: "Please sign in." unless signed_in?
end
end
sessions_helper.rb
module SessionsHelper
def sign_in(user)
@current_user = user
end
def current_user
@current_user ||= nil
end
def signed_in?
[email protected]_user.nil?
end
def sign_out
@current_user = nil
end
end
sessions_controller.rb
class SessionsController < ApplicationController
def new
end
def create
user = ActiveDirectoryUser.authenticate(params[:session][:username],params[:session][:password])
if user.nil?
# authentication failed
flash.now[:error] = 'Invalid email/password combination'
render 'new'
else
# authentication succeeded
sign_in @user
flash[:error] = 'Great success'
redirect_to root_path
end
end
def destroy
sign_out
redirect_to root_path
end
end
你如何保存用户登录成功的事实?我没有看到任何会话存储此信息的调用。 – heavysixer 2012-04-23 22:01:01
由于我对rails的理解有限,是不是通过sessions_controller.rb中的'sign_in user'来传递?用户对象是从LDAP类输出创建的,然后传递给sign_in帮助器 - sign_in帮助器应该创建一个全局范围为 – samJL 2012-04-23 22:35:17
的current_user对象是的,你必须在会话内部保存一些信息,通过存储它在数据库或饼干。 – heavysixer 2012-04-23 23:17:01