繁体   English   中英

如何从设计表单中分离更改密码

[英]How to separate change password from devise form

我想做两件事:

1)更改默认的“编辑用户表单” - 随设备提供 - 删除“密码”并允许更新其他字段而无需输入密码即删除密码的默认验证。

2)创建一个单独的表单来更改密码

我有一切工作,只有一个问题,在单独的表格更新密码,我已经包括一个当前密码的字段。 使用表单时,没有对当前密码进行验证,所以我改变了

@user.update_attributes(params[:user]) 

@user.update_with_password(params[:user])

这有效,但它提出了另一个问题。 返回主表单中除了密码之外的所有其他详细信息,表单现在要求“当前密码”。 如果没有验证主表单上调用的当前密码,我怎样才能实现这一目的?

这是我的注册控制器:

def update
  @user = User.find(current_user.id)
  if @user.update_attributes(params[:user])
    set_flash_message :notice, :updated
    # Sign in the user bypassing validation in case his password changed
    sign_in @user, :bypass => true
    redirect_to after_update_path_for(@user)
  else
    clean_up_passwords(resource)
    respond_with_navigational(resource) do
      if params[:change_password] # or flash[:change_password]
        render :change_password
      else
        render :edit
      end
    end
  end
end

谢谢!

解决方案1

我找到了解决问题的方法(虽然非常混乱):

def update
  @user = User.find(current_user.id)

  if params[:user][:password].blank?
    if @user.update_attributes(params[:user])
      set_flash_message :notice, :updated
      # Sign in the user bypassing validation in case his password changed
      sign_in @user, :bypass => true
      redirect_to after_update_path_for(@user)
    else
      respond_with_navigational(resource) do
        render :edit
      end
    end
  else
    if @user.update_with_password(params[:user])
      set_flash_message :notice, :updated
      # Sign in the user bypassing validation in case his password changed
      sign_in @user, :bypass => true
      redirect_to after_update_path_for(@user)
    else
      clean_up_passwords(resource)
      respond_with_navigational(resource) do
        render :change_password
      end
    end
  end

解决方案2

你能提出更好的解决方案吗?

你有兴趣查看Devise wiki吗? 这两种情况都有例子

  1. https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-edit-their-account-without-providing-a-password
  2. https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-edit-their-password

您应该查看@user.update_with_password(params[:user]) vs @user.update_attributes(params[:user])

接受的答案并没有完全解决这个问题。 其中,我认为是为用户配置文件属性(如电子邮件,名字等)和密码设置单独的表单。 以下是您需要做的事情:

首先,利用Devise :: RegistrationsController进行个人资料更新。

  • 自定义视图并删除passwordpassword_confirmation字段。 如果它们没有出现,那么Devise会忽略这些。
  • 如果您不想要求当前密码进行配置文件更改, 请阅读此内容 不建议; 不安全。

其次,创建自己的控制器来管理密码更新和您自己的帮助程序,以便在更新时要求current_passwordpasswordpassword_confirmation

class PasswordsController < ApplicationController
  before_filter :authenticate_user!

  def edit
    @user = current_user
  end

  def update
    @user = User.find(current_user.id)
    if @user.update_password_with_password(user_params)
      # Sign in the user by passing validation in case their password changed
      sign_in @user, :bypass => true
      redirect_to edit_password_path, flash: { success: "Successfully updated password" }
    else
      render "edit"
    end

  end

  private

    def user_params
      params.require(:user).permit(:current_password, :password, :password_confirmation)
    end

end

这是需要新密码字段的帮助程序update_password_with_password

class User < ActiveRecord::Base
  def update_password_with_password(params, *options)
    current_password = params.delete(:current_password)

    result = if valid_password?(current_password)
               update_attributes(params, *options)
             else
               self.assign_attributes(params, *options)
               self.valid?
               self.errors.add(:current_password, current_password.blank? ? :blank : :invalid)
               false
             end

    clean_up_passwords
    result
  end
end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM