Ruby on rails 使用RubyonRails设计-强制用户在首次登录时更改密码

Ruby on rails 使用RubyonRails设计-强制用户在首次登录时更改密码,ruby-on-rails,ruby,devise,passwords,Ruby On Rails,Ruby,Devise,Passwords,我有一个运行Desive的RoR应用程序(Rails 4.2、Ruby 2.2.0)。我已经对其进行了设置,以便管理员用户(识别我添加到用户模型中的“is_admin”布尔值)能够创建新的用户帐户,向他们提供生成的密码和确认电子邮件。这一切都正常工作。我还添加了datetime列pass_changed,当用户更改密码时应更新该列,然后在上检查created_,以确保自帐户创建以来密码已更改。如果两个日期相等,则将用户重定向到密码更改表单 我编写了一个程序,用于检查用户是否更改了密码,并将其放置

我有一个运行Desive的RoR应用程序(Rails 4.2、Ruby 2.2.0)。我已经对其进行了设置,以便管理员用户(识别我添加到用户模型中的“is_admin”布尔值)能够创建新的用户帐户,向他们提供生成的密码和确认电子邮件。这一切都正常工作。我还添加了datetime列pass_changed,当用户更改密码时应更新该列,然后在上检查created_,以确保自帐户创建以来密码已更改。如果两个日期相等,则将用户重定向到密码更改表单

我编写了一个程序,用于检查用户是否更改了密码,并将其放置在我的应用程序_controller.rb中:

def check_changed_pass
@user = current_user
    if @user.pass_changed == @user.created_at #make sure user has changed their password before accessing internal pages
    redirect_to edit_user_registration_path, alert: "You must change your password before logging in for the first time"
    end
end
然后在my internal_controller.rb中(用于所有需要用户登录的内部系统):

before_action :check_changed_pass
到目前为止还不错,但问题在于试图在正确的时间更新pass_changed。我尝试了各种配置,但我找不到放置此代码的位置,以便在更新密码时触发,但不是每次更新用户模型(包括每次登录和注销)时都会触发,而我只希望在password得到更新

如果我在保存:更新之前放置“仅:[:编辑,:更新]”在我的控制器中,它不会在密码更新时触发,如果我将它放在用户模型中,我必须将过程也放在模型中,然后它将不会拾取当前用户,因为它在模型中不可用。理想的情况是,如果Desive有一个类似于after_数据库身份验证钩子的after_密码编辑钩子。我必须覆盖设备的注册_controller.rb以删除线路

prepend_before_filter :require_no_authentication, only: [ :cancel]
这样管理员用户就可以添加新用户了。我试着把update\u pass\u change放在那里,但在保存密码编辑之前,它似乎不会触发

在应用程序中_controller.rb

def update_pass_change # records that the user has changed their password
    unless current_user.pass_changed != current_user.created_at
    current_user.pass_changed = Time.now
    end
end
before_action :check_pass_changed

private

def check_pass_changed
 unless current_user.pass_changed
   redirect_to custome_password_edit_path, alert: "You must change your password before logging in for the first time"
 end
end

类似的未回答问题:

您可以在模型上使用回调,并在保存之前检查更改是否包括您的密码属性。类似如下:

class User < ActiveRecord::Base
  before_save :check_password_changed

  private
  def check_password_changed
    self.pass_changed = Time.now if changed.include? 'encrypted_password'
  end
end
class用户
您也可以试试这个

  • pass\u的数据类型更改为
    boolean
    ,默认值为
    false

  • 然后在应用程序中_controller.rb

    def update_pass_change # records that the user has changed their password
        unless current_user.pass_changed != current_user.created_at
        current_user.pass_changed = Time.now
        end
    end
    
    before_action :check_pass_changed
    
    private
    
    def check_pass_changed
     unless current_user.pass_changed
       redirect_to custome_password_edit_path, alert: "You must change your password before logging in for the first time"
     end
    end
    
  • 然后在用户控制器中创建自定义操作,以显示密码更改表单并进行更新。 如。 要显示表单:
    custome\u password\u edit
    要更新:
    更新用户密码
    另外,别忘了在
    routes.rb中写上面的内容

  • 成功更改后,将
    pass\u的值更改为
    true


  • 我这样做了,但出于某种原因它不起作用。检查密码更改运行时没有错误,但不会更新密码更改,尽管密码已正确更新。@HenryD检查
    加密密码
    是否是用户密码的正确属性。