Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- This is in my AdminController
- def createuser
- @user = User.new(params[:user])
- @user.password = params[:post][:password_first]
- @user.password_hash
- @user.password_salt
- if @user.save
- flash[:notice] = "The user was successfully created."
- redirect_to :action => "listusers"
- else
- render :action => "newuser"
- end
- Here is my user model
- require 'digest/sha2'
- class User < ActiveRecord::Base
- has_and_belongs_to_many :groups
- 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
- raise "Please provide a valid username and password and we will send you right along."
- end
- user
- end
- 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 has_right_for?(action_name, controller_name)
- groups.detect{ |group| group.has_right_for?(action_name, controller_name) }
- end
- # Fun easy validation
- validates_uniqueness_of :username, :on => :create, :message => "is already in the system."
- validates_uniqueness_of :username, :on => :update, :message => "is already in the system."
- validates_uniqueness_of :email, :on => :create, :message => "is already in the system."
- validates_uniqueness_of :email, :on => :update, :message => "is already in the system."
- validates_format_of :username, :with => /^[A-Za-z][A-Za-z0-9\-\_]{2,39}$/, :message => "can only consist of letters, numbers, -, and _"
- validates_format_of :first_name,:with => /^[A-Za-z0-9\-\s]*$/
- validates_format_of :middle_name, :with => /^[A-Za-z0-9\-\s]*$/
- validates_format_of :last_name,:with => /^[A-Za-z0-9\-\s]*$/
- validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/, :message => "is not valid."
- validates_length_of :email, :maximum=> 100, :message => "may only be a maximum of 100 charactars."
- validates_length_of :ip_address, :maximum => 15, :message => "is too long (format: ###.###.###.###)."
- validates_length_of :first_name, :maximum => 40, :allow_nil => true
- validates_length_of :middle_name, :maximum => 40, :allow_nil => true
- validates_length_of :last_name, :maximum => 40, :allow_nil => true
- validates_presence_of :username, :email, :ipaddr
- validates_numericality_of :confirmed, :on => :create
- validates_numericality_of :confirmed, :on => :update
- end
Add Comment
Please, Sign In to add comment