0
我试图在我的网站上实现“更改密码”功能。更改加密密码(Ruby On Rails)
我在user.rb以下
before_save :encrypt_password
def encrypt_password
self.encrypted_password = encrypt(password)
end
def encrypt
string
end
def has_password?(submitted_password)
encrypted_password == encrypt(submitted_password)
end
def encrypt_password
self.salt = make_salt unless has_password?(password)
self.encrypted_password = encrypt(password)
end
def encrypt(string)
secure_hash("#{salt}--#{string}")
end
def make_salt
secure_hash("#{Time.now.utc}--#{password}")
end
def secure_hash(string)
Digest::SHA2.hexdigest(string)
end
def self.authenticate(email, submitted_password)
user = find_by_email(email)
return nil if user.nil?
return user if user.has_password?(submitted_password)
end
创建帐户时,用户在姓名,电子邮件,密码和password_confirmation打字,但如果我进入轨道控制台,仰望它有一个encrypted_password,而不是密码和password_confirmation。
当我从用户(通过形式传递)获得输入,我所做的:
@user.update_attributes(:password => params[:password][:password], :password_confirmation => params[:password][:password_confirmation])
@user.save
,但是这是不工作!我想知道是否需要解密旧密码并加密新密码才能更新密码......有什么见解?谢谢你的时间!
我知道旁边没有关于Ruby,但你是什么我通过解密旧密码?你的意思是,从哈希密码中检索密码? – CatShoes
是你的整个user.rb文件?你有没有attr_accessible:password,:password_confirmation和attr_accessor:password来定义模型上的属性?试图保存时会得到什么错误信息? – Doon