Guest User

Untitled

a guest
Mar 1st, 2018
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1. ## account_controller.rb
  2.  
  3. def signup
  4. @user = User.new(params[:user])
  5. return unless request.post?
  6. if @user.save
  7. Name.new({:user_id => @user, :name => @user.login}).save!
  8. flash[:notice] = "Your account has been successfully created. Your account must be activated before you can login. Please check your email for your activation code."
  9. redirect_to :action => 'login'
  10. else
  11. render :action => 'signup'
  12. end
  13. end
  14.  
  15. ## user.rb
  16.  
  17. class User < ActiveRecord::Base
  18. before_create :make_activation_code
  19. # Virtual attribute for the unencrypted password
  20. attr_accessor :password
  21. attr_accessor :new_password
  22.  
  23. attr_protected :superuser
  24.  
  25. has_many :user_groups
  26. has_many :groups, :through => :user_groups
  27. has_many :pages
  28. has_many :comments
  29. has_many :sr_characters
  30. has_many :names
  31.  
  32. validates_presence_of :login, :email
  33. validates_presence_of :password, :if => :password_required?
  34. validates_presence_of :password_confirmation, :if => :password_required?
  35. validates_length_of :password, :within => 4..40, :if => :password_required?
  36. validates_confirmation_of :password, :if => :password_required?
  37. validates_length_of :login, :within => 3..40
  38. validates_length_of :email, :within => 3..100
  39. validates_uniqueness_of :login, :email, :case_sensitive => false
  40. before_save :encrypt_password
  41.  
  42. def validate
  43. errors.add(:login, "has already been taken") if Name.find(:all).include? self.login
  44. end
  45.  
  46. def is_superuser?
  47. self.superuser
  48. end
  49.  
  50. # Authenticates a user by their login name and unencrypted password. Returns the user or nil.
  51. def self.authenticate(login, password)
  52. u = find_by_login(login) # need to get the salt
  53. u = find :first, :conditions => ['login = ? and activated_at IS NOT NULL', login]
  54. if u.nil?
  55. return nil
  56. end
  57. return u if u.encrypt(password) == u.crypted_password
  58. nil
  59. end
  60.  
  61. # Activates the user in the database.
  62. def activate
  63. @activated = true
  64. update_attributes(:activated_at => Time.now.utc, :activation_code => nil)
  65. end
  66.  
  67. # Returns true if the user has just been activated.
  68. def recently_activated?
  69. @activated
  70. end
  71.  
  72. # Encrypts some data with the salt.
  73. def self.encrypt(password, salt)
  74. Digest::SHA1.hexdigest("--#{salt}--#{password}--")
  75. end
  76.  
  77. # Encrypts the password with the user salt
  78. def encrypt(password)
  79. self.class.encrypt(password, salt)
  80. end
  81.  
  82. def authenticated?(password)
  83. crypted_password == encrypt(password)
  84. end
  85.  
  86. def remember_token?
  87. remember_token_expires_at && Time.now.utc < remember_token_expires_at
  88. end
  89.  
  90. # These create and unset the fields required for remembering users between browser closes
  91. def remember_me
  92. self.remember_token_expires_at = 2.weeks.from_now.utc
  93. self.remember_token = encrypt("#{email}--#{remember_token_expires_at}")
  94. save(false)
  95. end
  96.  
  97. def forget_me
  98. self.remember_token_expires_at = nil
  99. self.remember_token = nil
  100. save(false)
  101. end
  102.  
  103. def send_new_password
  104. new_pass = User.random_string(10)
  105. self.password = self.password_confirmation = new_pass
  106. self.save
  107. UserNotifier.deliver_forgot_password(self, new_pass)
  108. end
  109.  
  110. def has_permission_to?(action, object)
  111. if self.is_superuser?
  112. true
  113. elsif object.user == self
  114. true
  115. elsif object.permissions[:world][action]
  116. true
  117. elsif object.permissions[:group][action]
  118. if self.is_member_of? object.group
  119. true
  120. else
  121. false
  122. end
  123. else
  124. false
  125. end
  126. end
  127.  
  128. def is_member_of?(group)
  129. group.users.include? self
  130. end
  131.  
  132. protected
  133. # before filter
  134. def encrypt_password
  135. return if password.blank?
  136. self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{login}--") if new_record?
  137. self.crypted_password = encrypt(password)
  138. end
  139.  
  140. def password_required?
  141. crypted_password.blank? || !password.blank?
  142. end
  143.  
  144. def make_activation_code
  145. self.activation_code = Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join )
  146. end
  147.  
  148. def self.random_string(len)
  149. #generat a random password consisting of strings and digits
  150. chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
  151. newpass = ""
  152. 1.upto(len) { |i| newpass << chars[rand(chars.size-1)] }
  153. return newpass
  154. end
  155.  
  156. end
Add Comment
Please, Sign In to add comment