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

问题描述

我想在我的应用程序中为帐户持有者添加订阅类型功能,以便在一段固定的时间间隔后,他们将无法访问其帐户?注意:我不想从数据库中删除他们的帐户。我已经在我的应用程序中安装了 devise-2.1.2 。任何身体有什么想法怎么办?我是新手上的 Ruby on rails ,所以这对我来说非常有帮助,如果你能解释这些步骤。

解决方案

Devise具有一个填充解决方案,:lockable 选项检查



你必须设置 lock_strategy 设置为:failed_attempts。



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

 #定义哪个策略将用于锁定帐户。 
config.lock_strategy =:failed_attempts

#定义锁定和解锁帐户时将使用哪个密钥
config.unlock_keys = [:time]

#定义用于解锁帐户的策略。
#:time =在一段时间后重新启用登录(请参阅下面的unlock_in)
config.unlock_strategy =:time

#锁定前的验证次数帐号如果lock_strategy
#失败尝试。
config.maximum_attempts = 3

#解锁帐户的时间间隔:如果启用时间为unlock_strategy。
config.unlock_in = 2.hours

步骤2
您应该向您添加可锁定的模型:

 类示例< ActiveRecord :: Base 
devise:database_authenticatable,:可注册,
:可恢复,可记忆,可跟踪,可验证,
:可锁定
pre>

步骤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

Regards !!


I want to add a subscription type functionality in my application for the account holder users such that after a fixed interval of time they will not be able to access their account? Note: I dont 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 have a buil-in solution with the :lockable option check in the Devise Lockable Documentation

You have to set the lock_strategy set to :failed_attempts.

Step 1Set 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

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

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

Step 3Generate the migrations to make devise work

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

Regards!!

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

07-23 13:34
查看更多