在 Authlogic 中强制验证空白密码

发布于 2024-08-19 23:42:29 字数 1525 浏览 6 评论 0原文

我正在向使用 Authlogic 的 Rails 应用程序添加密码重置功能。我遵循此处的指南: http://www .binarylogic.com/2008/11/16/tutorial-reset-passwords-with-authlogic/ 一切都按我想要的方式工作,除了一件事:密码重置表单接受空白密码,但根本不接受改变他们。

我一直在四处寻找,并了解到这是预期的默认行为,因为它允许您使用户编辑表单仅在输入新密码时更改用户的密码,否则忽略它。但在这种情况下,我特别想强制验证密码,就像用户最初注册时一样。我已经找到了解决此问题的两种可能的解决方案,但无法弄清楚如何实现其中任何一个。

1) 有人在 Google 网上论坛上问了同样的问题:

用户模型使用空白密码保存

Ben 的响应是使用 @user.validate_password = true 强制验证密码。我尝试了此操作,但收到未定义的方法错误:undefined method 'validate_password_field=' for #

2) 似乎有一个名为 ignore_blank_passwords 的 Authlogic 配置选项。它记录在此处:

模块:Authlogic::ActsAsAuthentic::Password::Config#ignore_blank_passwords

这看起来可行,但我的理解是这是一个全局的您在用户模型中的初始 acts_as_authentic 调用中使用的配置选项,我不想在应用程序范围内更改它,因为我确实有一个针对需要空白密码的用户的常规编辑表单默认情况下会被忽略。

有人找到解决这个问题的方法吗?我在 Authlogic 1.4.1 的更改日志中看到 validate_password= ,从那时起它就没有被删除。我只是错误地使用它吗?有没有办法在每个请求的基础上使用 ignore_blank_passwords

I'm adding a password reset feature to my Rails application that uses Authlogic. I was following the guide here: http://www.binarylogic.com/2008/11/16/tutorial-reset-passwords-with-authlogic/ and everything works as I'd like except for one thing: the password reset form accepts blank passwords and simply doesn't change them.

I've been searching around, and have learned that this is the intended default behavior because it allows you to make user edit forms that only change the user's password if they enter a new one, and ignore it otherwise. But in this case, I specifically want to enforce validation of the password like when a user initially registers. I've found two possible solutions for this problem but haven't been able to figure out how to implement either of them.

1) Someone asked this same question on Google Groups:

User model saves with blank password

Ben's response was to use @user.validate_password = true to force validation of the password. I tried this but I get an undefined method error: undefined method 'validate_password_field=' for #<User>.

2) There seems to be an Authlogic configuration option called ignore_blank_passwords. It is documented here:

Module: Authlogic::ActsAsAuthentic::Password::Config#ignore_blank_passwords

This looks like it would work, but my understanding is that this is a global configuration option that you use in your initial acts_as_authentic call in the User model, and I don't want to change it application-wide, as I do have a regular edit form for users where I want blank passwords to be ignored by default.

Anyone found a solution to this? I see validate_password= in the change log for Authlogic 1.4.1 and nothing about it having been removed since then. Am I simply using it incorrectly? Is there a way to use ignore_blank_passwords on a per-request basis?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

陌伤浅笑 2024-08-26 23:42:29

这是一个旧线程,但由于尚未得到答复,我将发布此内容。

我已经设法比其他解决方案做得更干净一点,用我自己的解决方案“帮助”authlogic 验证。

我将其添加到用户:

class User < ActiveRecord::Base

  ...

  attr_writer :password_required

  validates_presence_of :password, :if => :password_required?

  def password_required?
    @password_required
  end

  ...
end

您可以通过创建 attr_accessor 并使用 :if => 将其减少到两行。 :password_required (无询问),但我更喜欢带有询问符号的其他语法。

然后你的控制器动作可以像这样完成:

def update
  @user.password = params[:user][:password]
  @user.password_confirmation = params[:user][: password_confirmation]
  @user.password_required = true

  if @user.save
    flash[:notice] = "Password successfully updated"
    redirect_to account_url
  else
    render :action => :edit
  end
end

这将产生局部效果;应用程序的其余部分不会受到影响(除非 password_required 在其他地方设置为 true)。

我希望它有帮助。

This is kind of an old thread, but since it is unanswered I'll post this.

I've managed to do it a bit more cleanly than the other solutions, "helping" authlogic validations with my own.

I added this to user:

class User < ActiveRecord::Base

  ...

  attr_writer :password_required

  validates_presence_of :password, :if => :password_required?

  def password_required?
    @password_required
  end

  ...
end

You can reduce it to two lines by making an attr_accessor and using :if => :password_required (no interrogation), but I prefer this other syntax with the interrogation sign.

Then your controller action can be done like this:

def update
  @user.password = params[:user][:password]
  @user.password_confirmation = params[:user][: password_confirmation]
  @user.password_required = true

  if @user.save
    flash[:notice] = "Password successfully updated"
    redirect_to account_url
  else
    render :action => :edit
  end
end

This will have a local effect; the rest of the application will not be affected (unless password_required is set to true in other places, that is).

I hope it helps.

鸠书 2024-08-26 23:42:29

这就是我所做的。

class User < ActiveRecord::Base
  attr_accessor :ignore_blank_passwords

  # object level attribute overrides the config level
  # attribute
  def ignore_blank_passwords?
    ignore_blank_passwords.nil? ? super : (ignore_blank_passwords == true)
  end
end

现在,在控制器中,将 ignore_blank_passwords 属性设置为 false。

user.ignore_blank_passwords = false

在这里,您是在 AuthLogic 的范围内工作。您不必更改验证逻辑。

This what I did.

class User < ActiveRecord::Base
  attr_accessor :ignore_blank_passwords

  # object level attribute overrides the config level
  # attribute
  def ignore_blank_passwords?
    ignore_blank_passwords.nil? ? super : (ignore_blank_passwords == true)
  end
end

Now in your controller, set the ignore_blank_passwords attribute to false.

user.ignore_blank_passwords = false

Here, you are working within the confines of AuthLogic. You don't have to change the validation logic.

后来的我们 2024-08-26 23:42:29
User.ignore_blank_passwords = false

使用模型而不是对象来设置此属性。

def update_passwords
  User.ignore_blank_passwords = false
  if @user.update_attributes(params[:user])
    ...
  end
  User.ignore_blank_passwords = true
end
User.ignore_blank_passwords = false

Use model, not object for setting this property.

def update_passwords
  User.ignore_blank_passwords = false
  if @user.update_attributes(params[:user])
    ...
  end
  User.ignore_blank_passwords = true
end
心安伴我暖 2024-08-26 23:42:29

也许测试控制器中的参数值? (航空代码):

def update
  @user.password = params[:user][:password]
  @user.password_confirmation = params[:user][: password_confirmation]
  if @user.password.blank?
    flash[:error] = "Password cannot be blank"
    render :action => :edit
    return
  end
  if @user.save
    flash[:notice] = "Password successfully updated"
    redirect_to account_url
  else
    render :action => :edit
  end
end

Maybe test the value of the parameter in the controller? (air code):

def update
  @user.password = params[:user][:password]
  @user.password_confirmation = params[:user][: password_confirmation]
  if @user.password.blank?
    flash[:error] = "Password cannot be blank"
    render :action => :edit
    return
  end
  if @user.save
    flash[:notice] = "Password successfully updated"
    redirect_to account_url
  else
    render :action => :edit
  end
end
寻找我们的幸福 2024-08-26 23:42:29

除了 zetetic 的解决方案之外,您还可以这样做:

def update
  @user.password = params[:user][:password]
  @user.password_confirmation = params[:user][: password_confirmation]

  if @user.changed? && @user.save
    flash[:notice] = "Password successfully updated"
    redirect_to account_url
  else
    render :action => :edit
  end
end

您基本上是在检查 authlogic 是否更改了用户记录(如果密码为空则不会更改)。在 else 块中,您可以检查密码是否为空,并向用户记录添加适当的错误消息或显示闪现消息。

Apart from zetetic's solution you could do it this way:

def update
  @user.password = params[:user][:password]
  @user.password_confirmation = params[:user][: password_confirmation]

  if @user.changed? && @user.save
    flash[:notice] = "Password successfully updated"
    redirect_to account_url
  else
    render :action => :edit
  end
end

You're basically checking if authlogic changed the user record (which it doesn't if the password is empty). In the else block you can check if the password was blank and add an appropriate error message to the user record or display a flash message.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文