Guest User

Untitled

a guest
Jun 14th, 2018
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. ## user excerpt
  2.  
  3. require 'digest/sha1'
  4.  
  5. class User < ActiveRecord::Base
  6.  
  7. # Virtual attribute for the unencrypted password
  8. attr_accessor :password
  9.  
  10. validates_presence_of :firstname, :lastname
  11. validates_presence_of :login, :email
  12. validates_presence_of :password, :if => :password_required?
  13. validates_presence_of :password_confirmation, :if => :password_required?
  14. validates_length_of :password, :within => 4..40, :if => :password_required?
  15. validates_confirmation_of :password, :if => :password_required?
  16. validates_length_of :login, :within => 2..40
  17. validates_length_of :email, :within => 3..100
  18. validates_uniqueness_of :login, :email, :case_sensitive => false
  19. validates_email_format_of :email
  20. validates_presence_of :locale
  21. before_save :encrypt_password
  22. before_create :make_activation_code
  23.  
  24. attr_accessible :title, :firstname, :lastname, :login, :email, :password, :password_confirmation, :locale
  25.  
  26. # Activates the user in the database.
  27. def activate
  28. @activated = true
  29. self.activated_at = Time.now.utc
  30. self.activation_code = nil
  31. save(false)
  32. end
  33.  
  34. # Locks this user by setting its deleted-attribute to the current time.
  35. def lock
  36. self.deleted = Time.now
  37. save(false)
  38. end
  39.  
  40. # Unlocks this user.
  41. def unlock
  42. self.deleted = nil
  43. save(false)
  44. end
  45.  
  46. # Checks if this user has been activated yet.
  47. def active?
  48. # the existence of an activation code means they have not activated yet
  49. activation_code.nil?
  50. end
  51.  
  52. # Authenticates a user by their login name and unencrypted password. Returns the user or nil.
  53. def self.authenticate(login, password)
  54. u = find :first, :conditions => ['login = ? and activated_at IS NOT NULL and deleted IS NULL', login] # need to get the salt
  55. u && u.authenticated?(password) ? u : nil
  56. end
  57.  
  58. # Encrypts some data with the salt.
  59. def self.encrypt(password, salt)
  60. Digest::SHA1.hexdigest("--#{salt}--#{password}--")
  61. end
  62.  
  63. # Encrypts the password with the user salt
  64. def encrypt(password)
  65. self.class.encrypt(password, salt)
  66. end
  67.  
  68. # Checks if the given password matches either the normal password or the
  69. # temporary password and returns true, otherwise false.
  70. def authenticated?(password)
  71. epw = encrypt(password)
  72. case epw
  73. when temp_crypted_password
  74. self.crypted_password = temp_crypted_password
  75. self.temp_crypted_password = nil
  76. save(false)
  77. true
  78. when crypted_password
  79. unless temp_crypted_password.nil?
  80. self.temp_crypted_password = nil
  81. save(false)
  82. end
  83. true
  84. else
  85. false
  86. end
  87. end
  88.  
  89. # Generates a password with eight characters from the current time, the user's
  90. # login and some random values.
  91. def gen_temp_password
  92. temp_password = random_password
  93. self.temp_crypted_password = encrypt(temp_password)
  94. save(false)
  95. temp_password
  96. end
  97.  
  98. def random_password
  99. Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{rand.to_s}--#{login}--")[0...8]
  100. end
  101. ....
  102. end
  103.  
  104. ## controller
  105.  
  106.  
  107. # Renders newpw.html.erb
  108. def newpw
  109. end
  110.  
  111. # Generates a temporary password and sends it to the user's email-address.
  112. def createnewpw
  113. @user = User.find_by_login(params[:login])
  114.  
  115. if @user
  116. temp_password = @user.gen_temp_password
  117. UserMailer.deliver_temp_password(@user, temp_password)
  118. flash[:notice] = 'A new password was mailed to your mail address.'.t
  119. else
  120. flash[:notice] = 'No such user!'.t
  121. end
  122. redirect_to(login_url)
  123. end
Add Comment
Please, Sign In to add comment