【问题标题】:Sign out a devise user from a model从模型中注销设计用户
【发布时间】:2016-04-27 18:08:10
【问题描述】:

在我的应用程序中,我需要不时退出特定用户。我需要从界面或 sidekiq 工作人员那里完成。所以我想在我的user 模型中创建一个sign_out 方法。

我在文档中看到 devise 提供了一个 sign_out 方法,但仅在控制器中。有没有办法从模型或类似的东西访问这个方法。

谢谢

【问题讨论】:

  • 给用户添加“force_logged_out”字段然后在下一个会话中注销控制器呢?

标签: ruby-on-rails devise


【解决方案1】:

您需要先阅读此答案https://stackoverflow.com/a/24388643/4269732

如果我要实现这种行为,那么我会在用户模型中添加一个列,例如 expire_at_next_request? 然后只需在 before_filter 中检查此值,如果为真则注销用户。

class ApplicationController < ActionController::Base
  before_filter :logout_if_requested

  def logout_if_requested
    if current_user && current_user.expire_at_next_request?
      current_user.update_attributes(:expire_at_next_request=>false)
      sign_out current_user
      redirect_to :new_session_path
    end
  end

【讨论】:

  • 这会为 Web 应用程序处理的每个请求增加 IO 开销。似乎有点矫枉过正。
猜你喜欢
  • 1970-01-01
  • 2015-09-21
  • 2015-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-11
  • 1970-01-01
  • 2021-04-22
相关资源
最近更新 更多