设计可锁定第n次尝试时的自定义消息 [英] Custom message on devise lockable n-th attempt

查看:83
本文介绍了设计可锁定第n次尝试时的自定义消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来显示可锁定模块中各种失败尝试的自定义消息。

I'm looking for a method to display custom messages for a various numbers of failed attempts in devise lockable module.

我发现只有最后一次尝试可以会默认收到通知:
https ://github.com/plataformatec/devise/blob/master/lib/devise/models/lockable.rb#L121

I found out that only the last attempt could be notified by default: https://github.com/plataformatec/devise/blob/master/lib/devise/models/lockable.rb#L121

有什么优雅的方法吗?显示不同的警告消息,例如第7次和第8次失败尝试?您可以提出一个吗?

Is there any elegant way to display different warning message for say 7th, and 8th failed attempt? Could you propose one?

推荐答案

您可以使用

module Devise::Models::Lockable
  def unauthenticated_message
    # If set to paranoid mode, do not show the locked message because it
    # leaks the existence of an account.
    if Devise.paranoid
      super
    elsif access_locked? || (lock_strategy_enabled?(:failed_attempts) && attempts_exceeded?)
      :locked
    elsif lock_strategy_enabled?(:failed_attempts) && last_attempt? && self.class.last_attempt_warning
      :last_attempt
    else
      return "invalid_fail_attempt_#{failed_attempts}" if temporary
      #super normally this would return :invalid see super class
    end
  end
end

然后您需要添加翻译内容

then you would need to add your translations for

devise.failure.user.invalid_1: "one shot down! 4 to go"
devise.failure.user.invalid_2: "ooh 2 strikes!!"
devise.failure.user.invalid_3: "3 strikes!! but your not out yet! 2 left"

并且您不需要数字4,因为该数字将由运行在 last_attempt?上的块处理(假设您的尝试次数限制为5,调整为

and you don't need number 4 as that would be handled by the block that runs on last_attempt? (assuming you have a 5 attempt limit, adjust as needed.)

该代码也可能在超类中出现,无论在哪里。.

That code might also go in the super class wherever that is..

这篇关于设计可锁定第n次尝试时的自定义消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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