Guest User

Untitled

a guest
Apr 8th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.58 KB | None | 0 0
  1. ## User Model
  2. require 'digest/sha1'
  3. class User < ActiveRecord::Base
  4. # Virtual attribute for the unencrypted password
  5. attr_accessor :password, :captcha
  6.  
  7. validates_presence_of :login, :email, :zip_code, :first_name, :last_name, :gender
  8. validates_presence_of :password, :if => :password_required?
  9. validates_presence_of :password_confirmation, :if => :password_required?
  10. validates_confirmation_of :password, :if => :password_required?
  11. validates_confirmation_of :email, :if => :email_required?
  12. validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
  13. validates_length_of :password, :within => 4..40, :if => :password_required?
  14. validates_length_of :login, :within => 3..40
  15. validates_length_of :email, :within => 3..100
  16. validates_length_of :zip_code, :is => 5
  17. validates_numericality_of :zip_code
  18. validates_uniqueness_of :login, :email, :case_sensitive => false
  19. validates_acceptance_of :captcha, :accept => true, :message => 'must be verified'
  20. before_save :encrypt_password
  21.  
  22. # prevents a user from submitting a crafted form that bypasses activation
  23. # anything else you want your user to change should be added here.
  24. attr_accessible :login, :email, :email_confirmation, :password, :password_confirmation, :zip_code, :first_name, :last_name, :gender, :status, :status_update_at, :file_type
  25.  
  26. # Takes the integer in the gender field and gives it a proper name
  27. def gender_name
  28. genders = ["Male", "Female"];
  29. return genders[attributes['gender']]
  30. end
  31.  
  32. def verify_captcha
  33. # self.errors.add_to_base( "You failed to verify the Captcha image/sound." ) unless cap
  34.  
  35. end
  36.  
  37. # Authenticates a user by their login name and unencrypted password. Returns the user or nil.
  38. def self.authenticate(login, password)
  39. u = find_by_login(login) # need to get the salt
  40. u && u.authenticated?(password) ? u : nil
  41. end
  42.  
  43. # Encrypts some data with the salt.
  44. def self.encrypt(password, salt)
  45. Digest::SHA1.hexdigest("--#{salt}--#{password}--")
  46. end
  47.  
  48. # Encrypts the password with the user salt
  49. def encrypt(password)
  50. self.class.encrypt(password, salt)
  51. end
  52.  
  53. def authenticated?(password)
  54. crypted_password == encrypt(password)
  55. end
  56.  
  57. def remember_token?
  58. remember_token_expires_at && Time.now.utc < remember_token_expires_at
  59. end
  60.  
  61. # These create and unset the fields required for remembering users between browser closes
  62. def remember_me
  63. remember_me_for 2.weeks
  64. end
  65.  
  66. def remember_me_for(time)
  67. remember_me_until time.from_now.utc
  68. end
  69.  
  70. def remember_me_until(time)
  71. self.remember_token_expires_at = time
  72. self.remember_token = encrypt("#{email}--#{remember_token_expires_at}")
  73. save(false)
  74. end
  75.  
  76. def forget_me
  77. self.remember_token_expires_at = nil
  78. self.remember_token = nil
  79. save(false)
  80. end
  81.  
  82. # Returns true if the user has just been activated.
  83. def recently_activated?
  84. @activated
  85. end
  86.  
  87. protected
  88. # before filter
  89. def encrypt_password
  90. return if password.blank?
  91. self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{login}--") if new_record?
  92. self.crypted_password = encrypt(password)
  93. end
  94.  
  95. def password_required?
  96. crypted_password.blank? || !password.blank?
  97. end
  98.  
  99. def email_required?
  100. !email.blank?
  101. end
  102. end
  103.  
  104. ## View
  105. <h2>Sign Up</h2>
  106. <p>Don't have a <strong>slidenation</strong> account yet? You've come to the right place! Just fill in your information below, and you're on your way!</p>
  107. <%= error_messages_for :user %>
  108. <% form_for :user, :url => users_path do |f| -%>
  109. <table class="form">
  110. <tr>
  111. <td class="col1"><label for="user_login">Login:</label></td>
  112. <td class="col2"><%= f.text_field :login, :size => 25 %></td>
  113. </tr>
  114. <tr>
  115. <td class="col1"><label for="user_password">Password:</label></td>
  116. <td class="col2"><%= f.password_field :password, :size => 25 %></td>
  117. </tr>
  118. <tr>
  119. <td class="col1"><label for="user_password_confirmation">Confirm Password:</label></td>
  120. <td class="col2"><%= f.password_field :password_confirmation, :size => 25 %></td>
  121. </tr>
  122. <tr>
  123. <td class="divider" colspan="2"><hr /></td>
  124. </tr>
  125. <tr>
  126. <td class="col1"><label for="user_email">Email Address:</label></td>
  127. <td class="col2"><%= f.text_field :email, :size => 35 %></td>
  128. </tr>
  129. <tr>
  130. <td class="col1"><label for="user_email_confirmation">Confirm Email Address:</label></td>
  131. <td class="col2"><%= f.text_field :email_confirmation, :size => 35 %></td>
  132. </tr>
  133. <tr>
  134. <td class="divider" colspan="2"><hr /></td>
  135. </tr>
  136. <tr>
  137. <td class="col1"><label for="user_first_name">First Name:</label></td>
  138. <td class="col2"><%= f.text_field :first_name, :size => 20 %></td>
  139. </tr>
  140. <tr>
  141. <td class="col1"><label for="user_last_name">Last Name:</label></td>
  142. <td class="col2"><%= f.text_field :last_name, :size => 20 %></td>
  143. </tr>
  144. <tr>
  145. <td class="col1">Gender:</td>
  146. <td class="col2"><%= f.radio_button :gender, 0, :checked => true %><label for="user_gender_0">Male</label>&nbsp;<%= f.radio_button :gender, 1 %><label for="user_gender_1">Female</label></td>
  147. </tr>
  148. <tr>
  149. <td class="col1"><label for="user_zip_code">Zip Code:</label></td>
  150. <td class="col2"><%= f.text_field :zip_code, :size => 5 %></td>
  151. </tr>
  152. <tr>
  153. <td class="divider" colspan="2"><hr /></td>
  154. </tr>
  155. <tr>
  156. <td class="col1"><label for="recaptcha_response_field">Human Verification:</label></td>
  157. <td class="col2"><%= recaptcha_tags :display => { :theme => 'clean' } %></td>
  158. </tr>
  159. <tr>
  160. <td class="divider" colspan="2"><hr /></td>
  161. </tr>
  162. <tr>
  163. <td class="col1">&nbsp;</td>
  164. <td class="col2"><%= submit_tag 'Sign Up!' %></td>
  165. </tr>
  166. </table>
  167. <% end -%>
  168.  
  169. ## Controller
  170. class UsersController < ApplicationController
  171. # Be sure to include AuthenticationSystem in Application Controller instead
  172. include AuthenticatedSystem
  173.  
  174.  
  175. # render new.rhtml
  176. def new
  177. end
  178.  
  179. def create
  180. cookies.delete :auth_token
  181. # protects against session fixation attacks, wreaks havoc with
  182. # request forgery protection.
  183. # uncomment at your own risk
  184. # reset_session
  185. @user = User.new(params[:user])
  186. @user.captcha = verify_recaptcha
  187. @user.save
  188. if @user.errors.empty?
  189. self.current_user = @user
  190. flash[:notice] = "<h3>Skoar!</h3><br />You're all signed up for <strong>slidenation</strong>!"
  191. render :controller => 'sessions', :action => 'new'
  192. else
  193. render :action => 'new'
  194. end
  195. end
  196.  
  197. end
Add Comment
Please, Sign In to add comment