如何使用Devise锁定用户?

Ahm*_*mza 10 authorization ruby-on-rails devise ruby-on-rails-3.2

我想在我的应用程序中为帐户持有者用户添加订阅类型功能,以便在固定的时间间隔后他们将无法访问其帐户?注意:我不想从数据库中删除他们的帐户.我已经devise-2.1.2在我的应用程序中安装了.有没有人知道怎么办?我是新手,Ruby on rails所以如果你请解释一下这对我很有帮助.

fel*_*pes 20

Devise有一个内置解决方案,:lockable可以选择Devise Lockable Documentation中的选项

您必须将设置lock_strategy设置为:failed_attempts.

步骤1 设置config/initializers/devise.rb使用:

# Defines which strategy will be used to lock an account.
config.lock_strategy = :failed_attempts

# Defines which key will be used when locking and unlocking an account
config.unlock_keys = [ :time ]

# Defines which strategy will be used to unlock an account.
# :time  = Re-enables login after a certain amount of time (see :unlock_in below)
config.unlock_strategy = :time

# Number of authentication tries before locking an account if lock_strategy
# is failed attempts.
config.maximum_attempts = 3

# Time interval to unlock the account if :time is enabled as unlock_strategy.
config.unlock_in = 2.hours
Run Code Online (Sandbox Code Playgroud)

第2步 你应该将锁定添加到你的模型如下:

class Example < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, 
         :lockable
Run Code Online (Sandbox Code Playgroud)

步骤3 生成迁移以使设计工作

class AddLockableToExamples < ActiveRecord::Migration
  def change
    add_column :examples, :failed_attempts, :integer, default: 0
    add_column :examples, :unlock_token, :string
    add_column :examples, :locked_at, :datetime
  end
end
Run Code Online (Sandbox Code Playgroud)

问候!!