Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- !! post.rb
- class Post < ActiveRecord::Base
- has_many :comments
- has_one :author, :class_name => "Account"
- validates_presence_of :title, :entry, :author_id
- validates_uniqueness_of :title
- validates_length_of :title, :within => 3..100
- attr_protected :id
- end
- !! account.rb
- require 'digest/sha1'
- class Account < ActiveRecord::Base
- validates_presence_of :login, :password, :name, :pen_name
- validates_length_of :login, :within => 3..20
- validates_length_of :pen_name, :within => 3..30
- validates_length_of :password, :within => 3..20
- validates_confirmation_of :password, :confirm_password
- attr_protected :id, :salt
- def forgot_password
- salt = random_string(10)
- self.update_attributes[:password => Account.sha1(Account.random_string(8) salt), :salt => salt]
- self.save
- # send email to Account...
- end
- def self.authenticate(params)
- user = Account.find_by_login(params[:login])
- return nil if user.nil?
- return user if user.password == Account.sha1(params[:password] user.salt)
- nil
- end
- def password=(pass)
- self[:salt] = Account.random_string(10) if self[:salt].empty?
- self[:password] = Account.sha1(pass self[:salt])
- end
- def self.sha1(text)
- Digest::SHA1.hexdigest(text)
- end
- def self.random_string(length)
- chars = ("a".."z").to_a ("A".."Z").to_a ("0".."9").to_a
- random = ""
- 1.upto(length) { |i|
- random << chars[rand(chars.size-1)]
- }
- return random
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement