2012-04-23 109 views
2

我正在开发基于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 
+1

你如何保存用户登录成功的事实?我没有看到任何会话存储此信息的调用。 – heavysixer 2012-04-23 22:01:01

+0

由于我对rails的理解有限,是不是通过sessions_controller.rb中的'sign_in user'来传递?用户对象是从LDAP类输出创建的,然后传递给sign_in帮助器 - sign_in帮助器应该创建一个全局范围为 – samJL 2012-04-23 22:35:17

+1

的current_user对象是的,你必须在会话内部保存一些信息,通过存储它在数据库或饼干。 – heavysixer 2012-04-23 23:17:01

回答

0

您应该使用会话坚持那种数据(将课税为每个请求),它的用户数据。但我强烈建议你使用诸如设计宝石,这些宝石可以为你做所有的认证事情。为什么重塑这个伟大的权利?

我相信这会适合你。

module SessionsHelper 
    def sign_in(user) 
    session[:user_id] = user.id 
    end 

    def current_user 
    ActiveDirectoryUser.find(session[:user_id]) ||= nil 
    end 

    def signed_in? 
    !session[:user_id].nil? 
    end 

    def sign_out 
    session[:user_id] = nil 
    end 
end 
+0

这个家伙刚刚起步,所以我猜他是在学习。尽管如此,我并不是设计师的忠实粉丝 – andy 2013-01-21 09:06:46