Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #The Registration page View
- #--------------------------
- <h1 class="headingMiddleColumn">Register</h1>
- <div class="middleContent" style="margin-left: 15px; margin-right: 15px;">
- <% if flash[:notice] %>
- <span style="margin-top: 50px; clear: left; color: #ff0000;"><br /><%= flash[:notice] %></span><br />
- <% end %>
- <%= flash[:errors] %>
- <%= start_form_tag :action => 'register_user' %><br />
- <p>Please provide the information below and click on 'Submit' to become a registered user.</p><br />
- <p><label for="user_username">Preferred username:</label><br/>
- <%= text_field 'user', 'username' %></p><br />
- <p><label for="user_email">Email:</label><br/>
- <%= text_field 'user', 'email' %></p><br />
- <p><label for="user_firstname">Firstname:</label><br/>
- <%= text_field 'user', 'firstname' %></p><br />
- <p><label for="user_surname">Surname:</label><br/>
- <%= text_field 'user', 'surname' %></p><br />
- <p><label for="user_password">Password:</label><br/>
- <%= password_field 'user', 'password' %></p><br />
- <p><label for="user_password">Re-enter password:</label><br/>
- <%= password_field 'user', 'password_conf' %></p><br />
- <%= submit_tag "Submit" %>
- <%= end_form_tag %>
- <br /><br />
- <h1 class="headingMiddleColumn">Forgotten Password</h1>
- <br />
- <p>Please enter your username and a new password will be emailed to you.</p>
- <%= start_form_tag :action => 'lost_pass'%><br />
- <p><label for="user_username">Username:</label><br/>
- <%= text_field_tag 'username' %></p>
- <br />
- <%= submit_tag "Submit" %>
- <%= end_form_tag %>
- #The controller action
- #---------------------
- def register_user
- if(params[:user][:password] == nil || params[:user][:password_conf] == nil)
- flash[:notice] = "Passwords cannot be empty"
- redirect_to :action => "register"
- elsif(params[:user][:email] == nil )
- flash[:notice] = "Email Address Cannot be empty"
- redirect_to :action => "register"
- elsif(params[:user][:firstname] == nil || params[:user][:surname] == nil)
- flash[:notice] = "Both Firstname and Surname cannot be empty"
- redirect_ro :action => "register"
- else
- if(params[:user][:password] == params[:user][:password_conf])
- @user = User.create(:username => params[:user][:username])
- @user.email = params[:user][:email]
- @user.firstname = params[:user][:firstname]
- @user.surname = params[:user][:surname]
- @user.password = params[:user][:password]
- role = Role.find_by_name("Forum User")
- @user.roles << role
- if @user.save
- flash[:notice] = "Registration successful, please login using your username and password."
- redirect_to :action => "register"
- else
- redirect_to :action => "register"
- end
- else
- flash[:notice] = "Error passwords do not match."
- redirect_to :action => "register"
- end
- end
- end
- #The Model
- #---------
- require 'digest/sha2'
- class User < ActiveRecord::Base
- has_and_belongs_to_many :roles
- validates_uniqueness_of :username
- validates_presence_of :email, :firstname, :surname
- def password=(pass)
- salt = [Array.new(6){rand(256).chr}.join].pack("m").chomp
- self.password_salt, self.password_hash = salt, Digest::SHA256.hexdigest(pass + salt)
- end
- def self.authenticate(username, password)
- user = User.find(:first, :conditions => ['username = ?', username])
- if user.blank? || Digest::SHA256.hexdigest(password + user.password_salt) != user.password_hash
- return false
- end
- user
- end
- def self.random_string(limit)
- Array.new(limit, "").collect{(("a".."z").to_a + ("A".."Z").to_a + ("1".."9").to_a)[rand(61)]}.join
- end
- end
Add Comment
Please, Sign In to add comment