Ruby on rails 散列密码并要求它执行删除操作
我是RubyonRails的新手,所以我不知道如何实现它。我有一个充满条目的表,其中有一个“password\u hash”字段。当用户创建新条目时,他们会输入密码。我想(显然)在“password\u hash”字段中放入该密码的散列。执行此操作的hash命令将转到何处?在模型中Ruby on rails 散列密码并要求它执行删除操作,ruby-on-rails,ruby,Ruby On Rails,Ruby,我是RubyonRails的新手,所以我不知道如何实现它。我有一个充满条目的表,其中有一个“password\u hash”字段。当用户创建新条目时,他们会输入密码。我想(显然)在“password\u hash”字段中放入该密码的散列。执行此操作的hash命令将转到何处?在模型中 其次,当有人使用“destroy”方法删除一个条目时,我希望他们必须输入密码,并且只有当该条目的哈希值与为该条目存储的哈希值匹配时,才销毁该条目。我猜这是控制器中的destroy方法,但我不知道如何进行检查。对于密码
其次,当有人使用“destroy”方法删除一个条目时,我希望他们必须输入密码,并且只有当该条目的哈希值与为该条目存储的哈希值匹配时,才销毁该条目。我猜这是控制器中的destroy方法,但我不知道如何进行检查。对于密码散列部分,请查看此sreecast 在同一屏幕广播中,您可以看到如何对用户进行身份验证。在您的情况下,您只需要使用密码进行身份验证
class SomeModelWithPassword < ActiveRecord::Base
attr_accessible :password
attr_accessor :password
before_save :encrypt_password
validates_presence_of :password, :on => :create
def correct_password?(password_try)
password_hash == BCrypt::Engine.hash_secret(password_try, password_salt)
end
def encrypt_password
if password.present?
self.password_salt = BCrypt::Engine.generate_salt
self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
end
end
end
以下是做你想做的事情的要素。您可以在模型上创建attr_访问器,让它自动生成密码散列,然后使用控件检查删除。BCrypt()将处理散列 您的型号:
require 'bcrypt'
class Entry < ActiveRecord::Base
include BCrypt
def password
@password ||= Password.new(password_hash)
end
def password=(new_password)
@password = Password.create(new_password)
self.password_hash = @password
end
end
需要“bcrypt”
类条目
您的控制器:
class EntriesController < ApplicationController
...
def destroy
@entry = Entry.find(params[:id])
if @entry.password == params[:password]
@entry.destroy
else
redirect_to @entry, :notice => 'You must enter a valid password to destroy an entry'
end
end
end
end
end
class EntriesController“您必须输入有效密码才能销毁条目”
结束
结束
结束
结束
结束
class EntriesController < ApplicationController
...
def destroy
@entry = Entry.find(params[:id])
if @entry.password == params[:password]
@entry.destroy
else
redirect_to @entry, :notice => 'You must enter a valid password to destroy an entry'
end
end
end
end
end