Advertisement
Guest User

Untitled

a guest
Aug 1st, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.76 KB | None | 0 0
  1. # controller
  2. class UsersController < ApplicationController
  3. # render new.rhtml
  4. def new
  5. end
  6.  
  7. def create
  8. @user = User.new(params[:user])
  9. @user.save!
  10. #self.current_user = @user
  11. reset_session
  12. flash[:notice] = "Thanks for signing up!"
  13. render :action => "waiting"
  14. rescue ActiveRecord::RecordInvalid
  15. render :action => 'new'
  16. end
  17. end
  18.  
  19. # app/views/users/new.rhtml
  20. <h1>register</h1>
  21.  
  22. <%= error_messages_for :user %>
  23.  
  24. <% form_for :user, :url => users_path do |f| -%>
  25. <div class="required">
  26. <label for="user_login"><%= "Username" %></label>
  27. <%= f.text_field :login %>
  28. </div>
  29. <div class="required">
  30. <label for="user_password"><%= "Password" %></label>
  31. <%= f.password_field :password %>
  32. </div>
  33. <div class="required">
  34. <label for="user_password_confirmation"><%= "Confirm" %> <%= "password" %></label>
  35. <%= f.password_field :password_confirmation %>
  36. </div>
  37. <div class="required">
  38. <label for="user_email"><%= "Email" %></label>
  39. <%= f.text_field :email %>
  40. </div>
  41. <div class="required">
  42. <label for="user_email_confirmation"><%= "Confirm" %> <%= "email" %></label>
  43. <%= f.text_field :email_confirmation %>
  44. </div>
  45. <%= submit_tag("Sign up", {:disable_with => "Sign up"}) %>
  46. <% end -%>
  47.  
  48. # model
  49. require 'digest/sha2'
  50. require 'digest/sha1'
  51.  
  52. class User < ActiveRecord::Base
  53.  
  54. class ActivationCodeNotFound < StandardError
  55. end
  56.  
  57. class AlreadyActivated < StandardError
  58. attr_reader :user, :message;
  59. def initialize(user, message=nil)
  60. @message, @user = message, user
  61. end
  62. end
  63.  
  64. # relations
  65. has_one :profile
  66. belongs_to :group
  67.  
  68. # virtual attributes
  69. attr_accessor :password
  70. attr_accessor :email
  71.  
  72. # validation
  73. validates_presence_of :login
  74. validates_length_of :login, :within => 3..20
  75. validates_uniqueness_of :login, :email, :case_sensitive => false
  76.  
  77. validates_presence_of :password, :if => :password_required?
  78. validates_presence_of :password_confirmation, :if => :password_required?
  79. validates_length_of :password, :within => 4..40, :if => :password_required?
  80. validates_confirmation_of :password, :if => :password_required?
  81.  
  82. validates_presence_of :email, :if => :email_required?
  83. validates_presence_of :email_confirmation, :if => :email_required?
  84. validates_length_of :email, :within => 3..100
  85. validates_confirmation_of :email, :if => :email_required?
  86. validates_format_of :email, :with => /\A([^@\s] )@((?:[-a-z0-9] \.) [a-z]{2,})\Z/
  87.  
  88. # callbacks
  89. before_save :encrypt_password
  90. before_create :make_activation_code
  91.  
  92. # check if this user is an admin
  93. def is_admin?
  94. group.name.singularize.downcase.eql?'admin'
  95. end
  96.  
  97. # return the proper name according to the preferences
  98. def screen_name
  99. if profile && !profile.real_name.blank? && profile.always_show_real_name
  100. profile.real_name
  101. else
  102. login
  103. end
  104. end
  105.  
  106. # return the gravar email if set, otherwise use the primary email provided
  107. def gravatar
  108. if profile && !profile.gravatar_email.nil? && !profile.gravatar_email.blank?
  109. profile.gravatar_email
  110. else
  111. email
  112. end
  113. end
  114.  
  115. # lookup the user and check the password
  116. # set user to nil if user doesn't exist or password doesn't match
  117. def self.authenticate(login, password, has_been_activated=true)
  118. u = find_by_login_and_has_been_activated(login, has_been_activated, :include => %w(group profile))
  119. u && u.authenticated?(password) ? u : nil
  120. end
  121.  
  122. # Encrypts some data with the salt.
  123. def self.encrypt(password, salt)
  124. Digest::SHA256.hexdigest("--#{salt}--#{password}--")
  125. end
  126.  
  127. # Encrypts the password with the user salt
  128. def encrypt(password)
  129. self.class.encrypt(password, salt)
  130. end
  131.  
  132. def authenticated?(password)
  133. crypted_password == encrypt(password)
  134. end
  135.  
  136. def remember_token?
  137. remember_token_expires_at && Time.now.utc < remember_token_expires_at
  138. end
  139.  
  140. # These create and unset the fields required for remembering users between browser closes
  141. def remember_me
  142. self.remember_token_expires_at = 2.weeks.from_now.utc
  143. self.remember_token = encrypt("#{email}--#{remember_token_expires_at}")
  144. save(false)
  145. end
  146.  
  147. def forget_me
  148. self.remember_token_expires_at = nil
  149. self.remember_token = nil
  150. save(false)
  151. end
  152.  
  153. # make sure we leave at least one user in the database
  154. def safe_delete
  155. transaction do
  156. destroy
  157. if User.count.zero?
  158. raise "Can't delete last user"
  159. end
  160. end
  161. end
  162.  
  163. # finds the user with the corresponding activation code, activates their account and returns the user.
  164. # raises:
  165. # User::ActivationCodeNotFound if there is no user with the corresponding activation code
  166. # User::AlreadyActivated if the user with the corresponding activation code has already activated their account
  167. def self.find_and_activate!(activation_code)
  168. user = find_by_activation_code(activation_code)
  169. raise ActivationCodeNotFound if !user
  170. raise AlreadyActivated.new(user) if user.has_been_activated?
  171. user.send(:activate!)
  172. user
  173. end
  174.  
  175. protected
  176. # before filter
  177. def encrypt_password
  178. return if password.blank?
  179. self.salt = [Array.new(6){rand(256).chr}.join].pack("m").chomp if new_record?
  180. self.crypted_password = encrypt(password)
  181. end
  182.  
  183. def password_required?
  184. crypted_password.blank? || !password.blank?
  185. end
  186.  
  187. def email_required?
  188. !email.blank?
  189. end
  190.  
  191. def make_activation_code
  192. self.activation_code = Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join )
  193. end
  194.  
  195. # private methods
  196. private
  197.  
  198. # activates a user
  199. def activate!
  200. @activated = true
  201. self.update_attribute(:activated_at, Time.now.utc)
  202. end
  203. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement