如何使用 Devise 锁定用户? [英] How to lock users using Devise?

查看:26
本文介绍了如何使用 Devise 锁定用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用程序中为帐户持有人用户添加订阅类型的功能,这样即使尝试登录失败,他们也将无法访问他们的帐户.注意:我不想从数据库中删除他们的帐户.我已经在我的应用程序中安装了 devise-2.1.2.有没有人知道如何做到这一点?我是 Ruby on rails 的新手,所以如果你解释一下步骤,这对我会很有帮助.

I want to add a subscription type functionality in my application for the account holder users such that with few failed login attempts they will not be able to access their account. Note: I don't want to delete their account from the database. I've already installed devise-2.1.2 in my application. Do any body have any idea how can it be done? I am newbie to Ruby on rails so it will be very helpful to me if you please explain the steps.

推荐答案

Devise 有一个内置解决方案,在 设计可锁定文档

Devise have a buil-in solution with the :lockable option check in the Devise Lockable Documentation

您必须将 lock_strategy 设置为 :failed_attempts.

第一步将您的 config/initializers/devise.rb 设置为使用:

Step 1 Set your config/initializers/devise.rb to use:

# 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

第 2 步您应该将可锁定的模型添加到您的模型中:

Step 2 Your should add the lockable to you Model as this:

class Example < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, 
         :lockable

第 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

问候!!

这篇关于如何使用 Devise 锁定用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆