before_filter与设计 [英] before_filter with devise

查看:192
本文介绍了before_filter与设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Devise的内置 before_filter:authenticate_user!。如果用户失败了之前的过滤器(尝试在注销时尝试执行一个操作),我想在我的应用程序助手中调用自己的自定义方法。如何和在哪里可以做到这一点?

解决方案

我将在使用 user_signed_in的过滤器之前编写一个自定义。这将返回一个布尔值,而不执行 authenticate_user!的任何重定向类型的操作。



所以,你可以这样写一个过滤器:

  before_filter:custom_user_auth 
...
def custom_user_auth
除非user_signed_in?
#做自定义的东西,最终限制访问
#...受保护的资源,如果它需要
end
end
请注意,此过滤器之前不会保护您的资源免遭未经授权的用户,除非该的内部区域除非语句重定向或呈现。


I'm using Devise's built in before_filter :authenticate_user!. I want to call my own custom method in my application helper if a user fails the before filter (tries to do an action when logged out). How and where can I do this?

解决方案

I would write a custom before filter that uses user_signed_in?. This will just return a boolean, and not do any of the redirect-type actions that authenticate_user! does.

So, you could write a before filter like this:

before_filter :custom_user_auth
...
def custom_user_auth
    unless user_signed_in?
        # Do custom stuff, ultimately restricting access to the 
        # ...protected resource if it needs to be
    end
end

Do note that this before filter won't protect your resource from unauthorized users unless the inside area of that unless statement redirects or renders.

这篇关于before_filter与设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆