【问题标题】:Call out sign_out method as a background task using devise使用 devise 将 sign_out 方法作为后台任务调用
【发布时间】:2015-12-28 07:28:40
【问题描述】:

我正在构建一个 Rails 应用程序,如果当前时间超过下午 6 点,我必须自动注销少数用户,如果当前时间超过晚上 7 点,我必须自动注销其他用户。我正在使用设计进行身份验证。我没有找到任何使用设计的解决方案。设计只有一个用于不活动的 timeout_in 参数。

我也尝试了延迟作业。但是在异步任务执行过程中,会话哈希是无法识别的。

我是否缺少某种明显的解决方案?

【问题讨论】:

  • Devise 使用签名的 cookie。您可以只为 cookie 添加过期时间。你试过吗?

标签: ruby-on-rails ruby devise


【解决方案1】:

从 1.5.2 版开始,您可以在 Devise 上动态设置有效期。 https://github.com/plataformatec/devise/wiki/How-To%3a-Add-timeout_in-value-dynamically

也许你可以试试这样的:

class User < ActiveRecord::Base
  devise (...), :timeoutable

  def timeout_in
    if should_logout_at_19
      timeout_period_for 19
    elsif should_logout_at_18
      timeout_period_for 18
    else
    ...
    end
  end

  # period in seconds until next session expiration hour.
  def timeout_period_for(given_hour)
    next_time = if Time.now.hour > given_hour
      Date.tomorrow.to_time.change(hour: given_hour)
    else
      Date.today.to_time.change(hour: given_hour)
    end
    next_time - Time.now
  end

  def should_logout_at_19
    # user criteria to logout at 7 PM
  end

end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-04
    相关资源
    最近更新 更多