Guest User

Untitled

a guest
Feb 21st, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. This is in my AdminController
  2.  
  3. def createuser
  4. @user = User.new(params[:user])
  5. @user.password = params[:post][:password_first]
  6. @user.password_hash
  7. @user.password_salt
  8. if @user.save
  9. flash[:notice] = "The user was successfully created."
  10. redirect_to :action => "listusers"
  11. else
  12. render :action => "newuser"
  13. end
  14.  
  15.  
  16. Here is my user model
  17.  
  18.  
  19. require 'digest/sha2'
  20. class User < ActiveRecord::Base
  21.  
  22. has_and_belongs_to_many :groups
  23.  
  24. def self.authenticate(username, password)
  25. user = User.find(:first, :conditions => ['username = ?', username])
  26. if user.blank? || Digest::SHA256.hexdigest(password + user.password_salt) != user.password_hash
  27. raise "Please provide a valid username and password and we will send you right along."
  28. end
  29. user
  30. end
  31.  
  32. def password=(pass)
  33. salt = [Array.new(6){rand(256).chr}.join].pack("m").chomp
  34. self.password_salt, self.password_hash = salt, Digest::SHA256.hexdigest(pass + salt)
  35. end
  36.  
  37. def has_right_for?(action_name, controller_name)
  38. groups.detect{ |group| group.has_right_for?(action_name, controller_name) }
  39. end
  40.  
  41. # Fun easy validation
  42. validates_uniqueness_of :username, :on => :create, :message => "is already in the system."
  43. validates_uniqueness_of :username, :on => :update, :message => "is already in the system."
  44. validates_uniqueness_of :email, :on => :create, :message => "is already in the system."
  45. validates_uniqueness_of :email, :on => :update, :message => "is already in the system."
  46.  
  47. validates_format_of :username, :with => /^[A-Za-z][A-Za-z0-9\-\_]{2,39}$/, :message => "can only consist of letters, numbers, -, and _"
  48. validates_format_of :first_name,:with => /^[A-Za-z0-9\-\s]*$/
  49. validates_format_of :middle_name, :with => /^[A-Za-z0-9\-\s]*$/
  50. validates_format_of :last_name,:with => /^[A-Za-z0-9\-\s]*$/
  51. validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/, :message => "is not valid."
  52.  
  53. validates_length_of :email, :maximum=> 100, :message => "may only be a maximum of 100 charactars."
  54. validates_length_of :ip_address, :maximum => 15, :message => "is too long (format: ###.###.###.###)."
  55. validates_length_of :first_name, :maximum => 40, :allow_nil => true
  56. validates_length_of :middle_name, :maximum => 40, :allow_nil => true
  57. validates_length_of :last_name, :maximum => 40, :allow_nil => true
  58.  
  59. validates_presence_of :username, :email, :ipaddr
  60.  
  61. validates_numericality_of :confirmed, :on => :create
  62. validates_numericality_of :confirmed, :on => :update
  63.  
  64. end
Add Comment
Please, Sign In to add comment