【问题标题】:Authentication broken up into sections issue with changing password身份验证分解为更改密码的部分问题
【发布时间】:2011-05-22 11:01:40
【问题描述】:

我正在构建一个身份验证,从头开始没有设计或 Authlogic,它将独立保存我的:user 表的三个不同部分。我试图让它保存非必需品,例如用户的真实姓名和年龄验证,而无需密码。如果:user 想要更改那里的登录详细信息,例如:loginname:email,那么他们需要输入:password 才能使更改生效。如果:user 想要更改他们的:password,也是如此。 这是:user 表的模型。

class User < ActiveRecord::Base
attr_accessor   :password, :old_password, :current_password
attr_accessible :loginname, :email, :password, :password_confirmation,
                 :name, :title_forName, :age_verify, :old_password, 
                 :current_password

email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
loginname_regex = /\A[\w\d]+\z/i
password_regex = /\A(?=.*[\d])(?=.*[A-Z])([1-zA-Z0-1@*#$.]{6,20})\z/

validates :loginname, :presence     => true,
                      :format       => { :with => loginname_regex },
                      :length       => { :maximum => 30},
                      :uniqueness   => { :case_sensitive => false }
validates :email,     :presence     => true,
                      :format       => { :with => email_regex },
                      :uniqueness   => { :case_sensitive => false }
validates :password,  :presence     => true,
                      :on           => :create, 
                      :on           => :change_password_update,
                      :on           => :change_password,
                      # :on           => :update, #This fixes the problem with the password not being validated, but breaks everything else
                      :confirmation => true,
                      :length       => { :within => 6..20 },
                      :format       => { :with => password_regex }
validates :name,      :length       => { :maximum => 75 }
validates :old_password, :presence  => true,
                         :on        => :change_password_update,
                         :on        => :change_password,
                         :format    => { :with => password_regex }
validates :current_password, :presence  => true,
                         :on        => :change_login_update,
                         :on        => :change_login,
                         :format    => { :with => password_regex }

before_save do
     if ( !self.password.blank? || !self.password_confirmation.blank?) #This will keep from a blank password being saved to an encryption.
        :encrypt_password
    end
end

这是:user控制器上的更新部分

def update
@user = User.find(params[:id])
if params["change_login_update"]
    change_login_update
else
    if params["change_password_update"]
        change_password_update
    else
        if @user.update_attributes(params[:user])
        flash[:success] = "Profile updated."
        redirect_to @user
        else
        @title = "Edit user"
        render 'edit'
        end
    end
end

结束

这是:user登录部分

def change_login_update
    if @user.has_password?(params[:user][:current_password])
        if @user.update_attributes(params[:user])
        flash[:success] = "Login details updated."
        redirect_to @user
        else
        @title = "Change Login Details"
        render 'change_login'
        end
    else
        flash[:notice] = "Password Didn't Match Our Records"
        @title = "Change Login Details"
        render 'change_login'
    end
  end

这是针对:users 控制器上的:password_change 部分

def change_password_update
    if @user.has_password?(params[:user][:old_password])
        if @user.update_attributes(params[:user])
        flash[:success] = "Password updated."
        redirect_to @user
        else
        @title = "Change Password"
        render 'change_password'
        end
    else
        flash[:notice] = "Original Password Didn't Match Our Records"
        @title = "Change Password"
        render 'change_password'
    end   
  end

1(更新不重要的东西) -- 编辑用户主页上的提交按钮,它只会更改不影响:user登录的东西,正常工作并且所有需要更改的内容都会更新。

2(更新登录详细信息) -- 名为 "change_login_update":flash 的提交按钮可以正常工作,:email:loginname 的字段验证也可以正常工作。单击提交后,系统会要求用户输入正确的密码,并且在保存数据之前会进行密码匹配,但不会检查:current_password 的输入数据是否格式正确。这似乎是添加到 :user 模型中的 :on 字段不起作用的问题。

3(更新密码) -- 提交按钮名为 "change_password_update",:flash 可以正常工作,但 :password:password_confirmation 验证不会触发。密码匹配 :old_password 也适用。如果所有字段都正确输入,:flash[:success] 会触发,但密码不会更新。如果使用:on =&gt; update,,密码将正确保存,但其他所有内容都会中断,因为:password 无法在任何其他页面上进行编辑。

:on 语句在正确的部分没有正确触发似乎是一个问题。这是我第一次使用:on 路径,因此我们将不胜感激。提前谢谢你。

【问题讨论】:

    标签: ruby-on-rails authentication


    【解决方案1】:

    我不认为你可以这样使用 :on,你只能使用 :on 和 validates_each、validates_with 并且唯一的选项是 - > :save, :create, :update

    所以你不能只传递一个随机方法给 :on 期望事件在发生这种情况时触发,它只能在保存、创建和更新时触发(默认为保存)。

    您可以在过滤器之后或之前执行您想要的操作。 使用:

    before_create :run_before_create_validations

    这将在任何创建之前触发 run_before_creation_validations。 我在这里你可以检查它是否是一个 change_password_update 例如,并运行一些验证。

    【讨论】:

      【解决方案2】:

      我的解决方案是在验证中添加:if 语句

      validates :password,  :presence     => true,
                            :on           => :create,
                            :confirmation => true,
                            :length       => { :within => 6..20 },
                            :format       => { :with => password_regex }
      validates :name,      :length       => { :maximum => 75 }
      validates :old_password, :presence  => true,
                                :format    => { :with => password_regex },
                                :if        => :old_password_check?
      validates :current_password, :presence  => true,
                               :if        => :login_check?,
                               :format    => { :with => password_regex }
      validates :password,  :presence     => true,
                             :confirmation => true,
                             :length       => { :within => 6..20 },
                             :format       => { :with => password_regex },
                             :if           => :old_password_check?
      

      在此之后,所有验证都正常工作,但密码没有正确保存,解决方法是我写错了我的before_save 这是更正的版本

      before_save do
           if( !self.password.blank? )
              encrypt_password
          end
      end
      

      现在一切正常。谢谢大家的帮助

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-03-26
        • 1970-01-01
        • 2022-11-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多