Advertisement
Guest User

Untitled

a guest
Jul 30th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. ## create_users.rb
  2.  
  3. class CreateUsers < ActiveRecord::Migration
  4. def self.up
  5. create_table :users do |t|
  6. t.column :email_address, :string, :null => false
  7. t.column :hashed_password, :string, :null => false
  8. t.column :screen_name, :string, :limit => 20, :null => false
  9. t.column :real_name, :string, :limit => 50
  10. t.column :salt, :string, :null => false
  11. end
  12. end
  13.  
  14. def self.down
  15. drop_table :users
  16. end
  17. end
  18.  
  19. ## User Modle
  20.  
  21. require 'digest/sha1'
  22.  
  23. class User < ActiveRecord::Base
  24.  
  25. attr_accessor :password
  26. attr_accessor :password_confirmation
  27. message_order = "new, created_on ASC"
  28. #has_many :messages, :class_name => 'Message', :foreign_key => 'author_id'
  29. validates_presence_of :email_address, :screen_name, :real_name
  30. validates_uniqueness_of :email_address
  31. validates_length_of :screen_name, :maximum => 20
  32. validates_length_of :real_name, :maximum => 50
  33. validates_length_of :password, :within => 5..25
  34. validates_confirmation_of :password, :message => "did not match the confirmation"
  35. validates_format_of :email_address, :with => /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/
  36.  
  37.  
  38. def after_validation_on_create
  39. return false unless self.password
  40. create_salt
  41. self.hashed_password = User.encrypt_password(self.password, self.salt)
  42. #self.password, self.password_confirmation = nil
  43. end
  44.  
  45. def self.login(email_address, password)
  46. user = self.find_by_email_address(email_address)
  47. unless user && (User.encrypt_password(password, user.salt) == user.hashed_password)
  48. user = nil
  49. end
  50. user
  51. end
  52.  
  53. def mailbox
  54. MailBox.new(self)
  55. end
  56.  
  57. def is_authorized?
  58. !self.is_guest?
  59. end
  60.  
  61. def is_guest?
  62. self.new_record?
  63. end
  64.  
  65. private
  66. def create_salt
  67. self.salt = self.object_id.to_s rand.to_s
  68. end
  69.  
  70. def self.encrypt_password(password, salt)
  71. string_to_hash = password "blync" salt
  72. Digest::SHA1.hexdigest(string_to_hash)
  73. end
  74.  
  75. def set_session_key
  76.  
  77. end
  78. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement