Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## User Model
- require 'digest/sha1'
- class User < ActiveRecord::Base
- # Virtual attribute for the unencrypted password
- attr_accessor :password, :captcha
- validates_presence_of :login, :email, :zip_code, :first_name, :last_name, :gender
- validates_presence_of :password, :if => :password_required?
- validates_presence_of :password_confirmation, :if => :password_required?
- validates_confirmation_of :password, :if => :password_required?
- validates_confirmation_of :email, :if => :email_required?
- validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
- validates_length_of :password, :within => 4..40, :if => :password_required?
- validates_length_of :login, :within => 3..40
- validates_length_of :email, :within => 3..100
- validates_length_of :zip_code, :is => 5
- validates_numericality_of :zip_code
- validates_uniqueness_of :login, :email, :case_sensitive => false
- validates_acceptance_of :captcha, :accept => true, :message => 'must be verified'
- before_save :encrypt_password
- # prevents a user from submitting a crafted form that bypasses activation
- # anything else you want your user to change should be added here.
- attr_accessible :login, :email, :email_confirmation, :password, :password_confirmation, :zip_code, :first_name, :last_name, :gender, :status, :status_update_at, :file_type
- # Takes the integer in the gender field and gives it a proper name
- def gender_name
- genders = ["Male", "Female"];
- return genders[attributes['gender']]
- end
- def verify_captcha
- # self.errors.add_to_base( "You failed to verify the Captcha image/sound." ) unless cap
- end
- # Authenticates a user by their login name and unencrypted password. Returns the user or nil.
- def self.authenticate(login, password)
- u = find_by_login(login) # need to get the salt
- u && u.authenticated?(password) ? u : nil
- end
- # Encrypts some data with the salt.
- def self.encrypt(password, salt)
- Digest::SHA1.hexdigest("--#{salt}--#{password}--")
- end
- # Encrypts the password with the user salt
- def encrypt(password)
- self.class.encrypt(password, salt)
- end
- def authenticated?(password)
- crypted_password == encrypt(password)
- end
- def remember_token?
- remember_token_expires_at && Time.now.utc < remember_token_expires_at
- end
- # These create and unset the fields required for remembering users between browser closes
- def remember_me
- remember_me_for 2.weeks
- end
- def remember_me_for(time)
- remember_me_until time.from_now.utc
- end
- def remember_me_until(time)
- self.remember_token_expires_at = time
- self.remember_token = encrypt("#{email}--#{remember_token_expires_at}")
- save(false)
- end
- def forget_me
- self.remember_token_expires_at = nil
- self.remember_token = nil
- save(false)
- end
- # Returns true if the user has just been activated.
- def recently_activated?
- @activated
- end
- protected
- # before filter
- def encrypt_password
- return if password.blank?
- self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{login}--") if new_record?
- self.crypted_password = encrypt(password)
- end
- def password_required?
- crypted_password.blank? || !password.blank?
- end
- def email_required?
- !email.blank?
- end
- end
- ## View
- <h2>Sign Up</h2>
- <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>
- <%= error_messages_for :user %>
- <% form_for :user, :url => users_path do |f| -%>
- <table class="form">
- <tr>
- <td class="col1"><label for="user_login">Login:</label></td>
- <td class="col2"><%= f.text_field :login, :size => 25 %></td>
- </tr>
- <tr>
- <td class="col1"><label for="user_password">Password:</label></td>
- <td class="col2"><%= f.password_field :password, :size => 25 %></td>
- </tr>
- <tr>
- <td class="col1"><label for="user_password_confirmation">Confirm Password:</label></td>
- <td class="col2"><%= f.password_field :password_confirmation, :size => 25 %></td>
- </tr>
- <tr>
- <td class="divider" colspan="2"><hr /></td>
- </tr>
- <tr>
- <td class="col1"><label for="user_email">Email Address:</label></td>
- <td class="col2"><%= f.text_field :email, :size => 35 %></td>
- </tr>
- <tr>
- <td class="col1"><label for="user_email_confirmation">Confirm Email Address:</label></td>
- <td class="col2"><%= f.text_field :email_confirmation, :size => 35 %></td>
- </tr>
- <tr>
- <td class="divider" colspan="2"><hr /></td>
- </tr>
- <tr>
- <td class="col1"><label for="user_first_name">First Name:</label></td>
- <td class="col2"><%= f.text_field :first_name, :size => 20 %></td>
- </tr>
- <tr>
- <td class="col1"><label for="user_last_name">Last Name:</label></td>
- <td class="col2"><%= f.text_field :last_name, :size => 20 %></td>
- </tr>
- <tr>
- <td class="col1">Gender:</td>
- <td class="col2"><%= f.radio_button :gender, 0, :checked => true %><label for="user_gender_0">Male</label> <%= f.radio_button :gender, 1 %><label for="user_gender_1">Female</label></td>
- </tr>
- <tr>
- <td class="col1"><label for="user_zip_code">Zip Code:</label></td>
- <td class="col2"><%= f.text_field :zip_code, :size => 5 %></td>
- </tr>
- <tr>
- <td class="divider" colspan="2"><hr /></td>
- </tr>
- <tr>
- <td class="col1"><label for="recaptcha_response_field">Human Verification:</label></td>
- <td class="col2"><%= recaptcha_tags :display => { :theme => 'clean' } %></td>
- </tr>
- <tr>
- <td class="divider" colspan="2"><hr /></td>
- </tr>
- <tr>
- <td class="col1"> </td>
- <td class="col2"><%= submit_tag 'Sign Up!' %></td>
- </tr>
- </table>
- <% end -%>
- ## Controller
- class UsersController < ApplicationController
- # Be sure to include AuthenticationSystem in Application Controller instead
- include AuthenticatedSystem
- # render new.rhtml
- def new
- end
- def create
- cookies.delete :auth_token
- # protects against session fixation attacks, wreaks havoc with
- # request forgery protection.
- # uncomment at your own risk
- # reset_session
- @user = User.new(params[:user])
- @user.captcha = verify_recaptcha
- @user.save
- if @user.errors.empty?
- self.current_user = @user
- flash[:notice] = "<h3>Skoar!</h3><br />You're all signed up for <strong>slidenation</strong>!"
- render :controller => 'sessions', :action => 'new'
- else
- render :action => 'new'
- end
- end
- end
Add Comment
Please, Sign In to add comment