📄 user.rb
字号:
# Copyright (c) 2006 The Pragmatic Programmers, LLC.# Reproduced from the book "Agile Web Development with Rails, 2nd Ed.",# published by The Pragmatic Bookshelf.# Available from www.pragmaticprogrammer.com/titles/rails2# # Permission is hereby granted, free of charge, to any person obtaining a copy# of this source code (the "Software"), to deal in the Software without# restriction, including without limitation the rights to use, copy, modify,# merge, publish, distribute, sublicense, and/or sell copies of the Software,# and to permit persons to whom the Software is furnished to do so, subject to# the following conditions:# # 1) This Software cannot be used in any training course or seminar, whether# presented live, via video, audio, screencast, or any other media, without# explicit prior permission from the publisher.# # 2) The above copyright notice and this permission notice shall be included in# all copies or substantial portions of the Software.# # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN# THE SOFTWARE. # Schema as of June 12, 2006 15:45 (schema version 7)## Table name: users## id :integer(11) not null, primary key# name :string(255) # hashed_password :string(255) # salt :string(255) #require 'digest/sha1'#START:validateclass User < ActiveRecord::Base validates_presence_of :name validates_uniqueness_of :name attr_accessor :password_confirmation validates_confirmation_of :password def validate errors.add_to_base("Missing password") if hashed_password.blank? end#END:validate #START:login def self.authenticate(name, password) user = self.find_by_name(name) if user expected_password = encrypted_password(password, user.salt) if user.hashed_password != expected_password user = nil end end user end #END:login # 'password' is a virtual attribute #START:accessors def password @password end def password=(pwd) @password = pwd create_new_salt self.hashed_password = User.encrypted_password(self.password, self.salt) end #END:accessors #START:after_destroy def after_destroy if User.count.zero? raise "Can't delete last user" end end #END:after_destroy private #START:create_new_salt def create_new_salt self.salt = self.object_id.to_s + rand.to_s end #END:create_new_salt #START:encrypted_password def self.encrypted_password(password, salt) string_to_hash = password + "wibble" + salt Digest::SHA1.hexdigest(string_to_hash) end #END:encrypted_password#START:validate end#END:validate
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -