Rails上的经过身份验证的系统

时间:2011-08-11 16:38:39

标签: ruby-on-rails authentication oauth

我在Rails上有app。我的身份验证sysem工作正常,但是: 1)总是在授权后我去THIS_path 2)如果我不是经过身份验证的用户,但是在我扔到授权页面之后,我扔到了pm_root_path,而不是reasearch_root_path。

为什么呢?为什么store_location方法不起作用?

我的lib / authenticated_system.rb:

module AuthenticatedSystem
protected

def logged_in?
  !!current_user
end

def current_user
  @current_user ||= login_from_session unless @current_user == false
  @current_user
end

def current_user=(new_user)
  session[:user_id] = new_user ? new_user.id : nil
  @current_user = new_user || false
end

def authorized?
  logged_in?
end


def login_required
  authorized? || access_denied
end

def access_denied
  respond_to do |format|
    format.html do
      store_location
      redirect_to root_path
    end
  end
end

def store_location
  session[:return_to] = request.request_uri
end

def redirect_back_or_default(default)
  redirect_to(session[:return_to] || default)
  session[:return_to] = nil
end

def self.included(base)
  base.send :helper_method, :current_user, :logged_in?
end

def login_from_session
  if session[:user_id]
    self.current_user = User.find_by_id(session[:user_id])
    self.current_user.last_logged_in = Time.now
    self.current_user.save
    self.current_user
  end
end

我在session_controller中的方法:

def open_id_authentication(domain=nil)
  domain = "" if domain.nil?
  complete_identity_url = IDENTITY_URL + domain
  authenticate_with_open_id(complete_identity_url, OPENID_OPTS) do |openid_result, identity_url, registration|
    if openid_result.successful?
      matches = /\/a\/(.*)\/o8/.match(params["openid.op_endpoint"])
      google_domain = matches[1] if matches[1]
      if valid_account?(google_domain)
        account = Account.find_by_google_domain(google_domain)
        session[:account_id] = account.id
        self.current_user = User.openid_registration(registration, identity_url, account.id)
      else
        flash[:error] = t('flash.session.domain_not_registered')
        redirect_to accounts_path
        return false
    end

        redirect_back_or_default(THIS_path)
    else
      flash[:error] = t('flash.open_id.authentication_failed')
      redirect_to accounts_path
    end
  end
end

1 个答案:

答案 0 :(得分:0)

如果您更改redirect_back_to_default以提升会话[:return_to]

,会显示什么
def redirect_back_or_default(default)
  raise session[:return_to].inspect
  redirect_to(session[:return_to] || default)
  session[:return_to] = nil
end

代码对我来说并不奇怪。您是否确认调用了store_location?