Advertisement
Guest User

Untitled

a guest
Jul 31st, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.19 KB | None | 0 0
  1. ## I have the following mediawiki database.
  2.  
  3. mysql> desc user;
  4. -------------------------- ----------------- ------ ----- --------- ----------------
  5. | Field | Type | Null | Key | Default | Extra |
  6. -------------------------- ----------------- ------ ----- --------- ----------------
  7. | user_id | int(5) unsigned | NO | PRI | NULL | auto_increment |
  8. | user_name | varchar(255) | NO | UNI | | |
  9. | user_real_name | varchar(255) | NO | | | |
  10. | user_password | tinyblob | NO | | | |
  11. | user_newpassword | tinyblob | NO | | | |
  12. | user_email | tinytext | NO | | | |
  13. | user_options | blob | NO | | | |
  14. | user_touched | varchar(14) | NO | | | |
  15. | user_token | varchar(32) | NO | | | |
  16. | user_email_authenticated | varchar(14) | YES | | NULL | |
  17. | user_email_token | varchar(32) | YES | MUL | NULL | |
  18. | user_email_token_expires | varchar(14) | YES | | NULL | |
  19. | user_registration | char(14) | YES | | NULL | |
  20. -------------------------- ----------------- ------ ----- --------- ----------------
  21. 13 rows in set (0.24 sec)
  22.  
  23. mysql>
  24. ## and I am accessing it using the following model:
  25.  
  26. class RemoteUser < ActiveRecord::Base
  27.  
  28. self.table_name = "user"
  29.  
  30. ActiveRecord::Base.establish_connection(
  31. :adapter => "mysql",
  32. :host => "remotewiki.com",
  33. :username => "remote_remote",
  34. :password => "passowrd",
  35. :database => "db"
  36. )
  37.  
  38. end
  39.  
  40. ## How can I use the crypted_hash, id and login within acts_as_authenticated's user.rb model?
  41.  
  42. require 'digest/sha1'
  43. class User < ActiveRecord::Base
  44. has_many :posts
  45. has_many :comments
  46. # Virtual attribute for the unencrypted password
  47. attr_accessor :password
  48.  
  49. validates_presence_of :login, :email
  50. validates_presence_of :password, :if => :password_required?
  51. validates_presence_of :password_confirmation, :if => :password_required?
  52. validates_length_of :password, :within => 4..40, :if => :password_required?
  53. validates_confirmation_of :password, :if => :password_required?
  54. validates_length_of :login, :within => 3..40
  55. validates_length_of :email, :within => 3..100
  56. validates_uniqueness_of :login, :email, :case_sensitive => false
  57. before_save :encrypt_password
  58.  
  59. # Authenticates a user by their login name and unencrypted password. Returns the user or nil.
  60. def self.authenticate(login, password)
  61. u = find_by_login(login) # need to get the salt
  62. u && u.authenticated?(password) ? u : nil
  63. end
  64.  
  65. # Encrypts some data with the salt.
  66. def self.encrypt(password, salt)
  67. Digest::SHA1.hexdigest("--#{salt}--#{password}--")
  68. end
  69.  
  70. # Encrypts the password with the user salt
  71. def encrypt(password)
  72. self.class.encrypt(password, salt)
  73. end
  74.  
  75. def authenticated?(password)
  76. crypted_password == encrypt(password)
  77. end
  78.  
  79. def remember_token?
  80. remember_token_expires_at && Time.now.utc < remember_token_expires_at
  81. end
  82.  
  83. # These create and unset the fields required for remembering users between browser closes
  84. def remember_me
  85. self.remember_token_expires_at = 2.weeks.from_now.utc
  86. self.remember_token = encrypt("#{email}--#{remember_token_expires_at}")
  87. save(false)
  88. end
  89.  
  90. def forget_me
  91. self.remember_token_expires_at = nil
  92. self.remember_token = nil
  93. save(false)
  94. end
  95.  
  96. protected
  97. # before filter
  98. def encrypt_password
  99. return if password.blank?
  100. self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{login}--") if new_record?
  101. self.crypted_password = encrypt(password)
  102. end
  103.  
  104. def password_required?
  105. crypted_password.blank? || !password.blank?
  106. end
  107. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement