Ruby   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ruby-on-rails – Rails:使用Authlogic进行基本身份验证大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用Authlogic,我想在我的控制器中实现基本http身份验证,以便我可以定义哪个操作需要身份验证.

我知道如何进行基本http身份验证authenticate_or_request_with_http_basic和before_filter,但我想在其他方面如何使用Authlogic插件实现它.

class ItemsController < ApplicationController  
  before_filter :authenticate,:only => [:index,:create]
  ...
end

解决方法

Here is a great screencast逐步解释了如何在rails项目中使用authlogic.

设置authlogic后,在Application Controller中定义以下有用的与身份验证相关的帮助程序方法.

def current_user_session
  return @current_user_session if Defined?(@current_user_session)
  @current_user_session = UserSession.find
end

def current_user
  return @current_user if Defined?(@current_user)
  @current_user = current_user_session && current_user_session.record
end

def require_user
  unless current_user
    store_LOCATIOn
    flash[:notice] = "You must be logged in to access this page"
    redirect_to new_user_session_url
    return false
  end
end

def require_no_user
  if current_user
    store_LOCATIOn
    flash[:notice] = "You must be logged out to access this page"
    redirect_to root_url
    return false
  end
end

定义这些方法后,您可以指定要求用户登录的操作:

before_filter :require_user,:only => [:new,:edit]

大佬总结

以上是大佬教程为你收集整理的ruby-on-rails – Rails:使用Authlogic进行基本身份验证全部内容,希望文章能够帮你解决ruby-on-rails – Rails:使用Authlogic进行基本身份验证所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。