2015-03-13 54 views
0

我正在使用Twitter Omniauth并希望在我的控制器中设置before_actions来验证用户操作并限制用户窗体编辑,更新和删除其他用户帖子,就像devise authenticate_user!方法允许。如何在omniauth中定义它,因为它不是内置的?使用Omniauth验证用户操作

+0

您可以在Application Controller中实现自己的方法,并像使用authenticate_user!一样使用它。 – 2015-03-13 15:23:19

+0

我知道,但目前我有什么设置:def authenticate_user! redirect_to root_path除非user_signed_in? 结束 def user_signed_in? !! session [:user_id] end – 2015-03-13 15:23:59

回答

1

这是一个例子,你可以适应你的需求。

在你的ApplicationController:

def require_signin! 
if current_user.nil? 
    flash[:error] = "Please sign in..." 
    redirect_to signin_url 
end 
end 
helper_method :require_signin! 

def current_user 
    @current_user ||= User.find(session[:user_id]) if session[:user_id] 
end 
helper_method :current_user 

并使用require_signin!像您使用authenticate_user!

+0

请问require_signin!他们如何定义它阻止其他用户能够编辑,删除和更新与他们无关的信息? – 2015-03-13 17:57:52

+0

require_signin!像authenticate_user一样行事!它可以防止没有登录的用户执行操作:before_action:require_signin!,仅限于:[:edit,:update,:destroy]。为了防止编辑来自其他用户的内容,您可以使用current_user方法检查您的帖子是否属于current_user。 – 2015-03-13 19:37:27