【问题标题】:Force users to sign out in Devise强制用户在 Devise 中退出
【发布时间】:2012-10-02 17:14:06
【问题描述】:

我有一个应用程序,其中用户的会员资格已过期。

我正在我的applications.rb 文件中设置before_filter,以在允许他们进入网站之前检查他们的会员资格是否有效。

在我的 application.rb 文件中:

before_filter :check_account

def check_account
  if user_signed_in?
     if current_user.account.expired
       flash[:error] = "Your account is expired. Please contact Navanti for renewal."
       redirect_to destroy_user_session_path
     end
  end
end

我不断收到重定向循环错误。我猜这是因为被调用的注销页面也在执行before_filter,但如果我输入except => [:users => :sign_out],它仍然会引发循环错误。

感谢您的帮助。


请求的设计方法:

# DELETE /resource/sign_out

def destroy
  redirect_path = after_sign_out_path_for(resource_name)
  signed_out = (Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name))
  set_flash_message :notice, :signed_out if signed_out && is_navigational_format?  

  # We actually need to hardcode this as Rails default responder doesn't
  # support returning empty response on GET request
  respond_to do |format|
    format.any(*navigational_formats) { redirect_to redirect_path }
    format.all do
      head :no_content
    end
  end
end

【问题讨论】:

  • 为destroy_user_session_path编写代码。
  • 是的,请把销毁代码放在这里
  • 该代码来自 Devise gem - 我会尝试找到它并将其添加到问题中。
  • 我添加了代码 - 但这是在 gem 中找到的,我真的不想编辑它。我认为它与过滤器和正确的调用方式有关。
  • 我认为您最好阻止他们完全登录,而不是在登录后立即将其踢出。有关基本模式,请参阅this HOWTO

标签: ruby-on-rails ruby-on-rails-3 devise


【解决方案1】:

尝试通过以下方式查询控制器/动作:

def check_account
  return if params[:controller] == "devise/sessions" && params[:action] == "destroy"
  if user_signed_in?
     if current_user.account.expired
       flash[:error] = "Your account is expired. Please contact Navanti for renewal."
       redirect_to destroy_user_session_path
     end
  end
end

这应该可以消除您的重定向循环。

【讨论】:

  • 感谢 Erez - 效果很好。我注意到了一个后续问题,但会在另一个问题中提出。
猜你喜欢
  • 2017-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多