【发布时间】: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 => update,,密码将正确保存,但其他所有内容都会中断,因为:password 无法在任何其他页面上进行编辑。
:on 语句在正确的部分没有正确触发似乎是一个问题。这是我第一次使用:on 路径,因此我们将不胜感激。提前谢谢你。
【问题讨论】:
标签: ruby-on-rails authentication