身份验证分为多个部分,与更改密码有关
我正在构建一个身份验证,从头开始没有 Devise 或 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
这是针对 :password_change
部分:users
contoller
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(更新不重要的事情) -- 编辑用户主页上的提交按钮,它只会更改不影响 < code>:user 登录,正常工作,并且所有需要更改的内容都会更新。
2(更新登录详细信息) -- 提交按钮名为 “change_login_update”
,:flash
工作正常,并且 的字段验证:email
和 :loginname
也可以。单击提交后,系统会要求用户输入正确的密码,并且在保存数据之前会进行密码匹配,但不会检查 :current_password
的输入数据是否正确格式。这似乎是添加到 :user
模型中的 :on
字段无法正常运行的问题。
3(更新密码) -- 提交按钮名为 “change_password_update”
,:flash
工作正常,但 :password< /code> 和
:password_confirmation
验证不会触发。密码匹配 :old_password
也适用于此。如果所有字段均输入正确,:flash[:success]
会触发,但密码不会更新。如果 :on =>;使用更新,
密码将正确保存,但其他所有内容都会损坏,因为 :password
无法在任何其他页面上进行编辑。
这似乎是 :on
语句未针对正确部分正确触发的问题。这是我第一次使用 :on
路径,因此我们将不胜感激。先感谢您。
I am building an authentication, from scratch no Devise or Authlogic, which will save three different parts of my :user
table independently. I am trying to get it to save the non-essentials, like the users real name and age verification, without a password. While if the the :user
wants to change there login details, like a :loginname
or :email
, then they need to enter their :password
in order to effect the change. Same goes for if the :user
wants to change their :password
.
Here is the model for the :user
table.
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
This is update section on the controller for :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
end
This is for the :user
login section
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
And this is for the :password_change
section on the :users
contoller
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 (Updating the non important things) -- The submit button on the edit user main page, where it will only change things that don't effect the :user
login, works without incident and everything that needs to change is updated.
2 (Updating the login details) -- The submit button is named "change_login_update"
,:flash
works correctly and field validation for :email
and :loginname
works as well. Upon clicking submit the user is asked for the proper password and it does do password matching before it will save the data, but it will not check to see if the input data for the :current_password
is in the correct format. This seems to be an issue with the :on
fields that were added to the :user
model not functioning.
3 (Updating the password) -- The submit button is named "change_password_update"
,:flash
works correctly but the :password
and :password_confirmation
validations don't fire. The password match, :old_password
, on this works as well. If the fields are all entered correctly the :flash[:success]
fires but the password is not updated. If the :on => update,
is used the password will save properly but everything else will break because the :password
is not available to edit on any other page.
It seems to be a problem with the :on
statements not firing correctly for the right sections. This is my first time working with :on
paths so any help would be greatly appreciated. Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不认为你可以这样使用 :on ,你只能将 :on 与 validates_each, validates_with 一起使用,唯一的选项是 - >
:save、:create、:update
因此,您不能只将随机方法传递给 :on 并期望事件在发生这种情况时触发,它只能在保存、创建和更新时触发(默认为保存)。
您可以在过滤器之后或之前执行您正在查看的操作。
使用:
before_create :run_before_create_validations
这将在任何创建之前触发 run_before_creation_validations 。
例如,您可以检查它是否是change_password_update,并运行一些验证。
I don't think you can use :on like that, you can only use :on with validates_each, validates_with and the only options are- >
:save, :create, :update
So you cant just pass a random method to :on an expect the event to fire when this happens, it can only fire on save, create and update(The default is save).
You can do what your are looking with after or before filter.
Use:
before_create :run_before_create_validations
this will fire run_before_creation_validations, before any create.
I here you can check if its a change_password_update for example, and run some validations.
我的解决方案是在所有验证正常工作之后向验证添加
:if
语句,但密码未正确保存,解决方案是我编写了
before_save
不正确的是,这是更正后的版本现在一切正常。谢谢大家的帮助
My solution was to add
:if
statements to the validationafter this all the validations worked correctly but the password was not saving correctly and the solution to that was that I had written my
before_save
incorrectly this is the corrected versionNow everything works perfectly. Thank you all for your help