Advertisement
Guest User

Untitled

a guest
Jul 31st, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. ## user_controller.rb
  2. class UserController < ApplicationController
  3. before_filter :authorize, :except => :login
  4.  
  5. layout "frontend" # => Show the stylesheet for the user side
  6. def login
  7. User.new
  8. if session[:user_id].nil?
  9. if request.get?
  10. # => If no session exists, create it
  11. @user = User.new
  12. else
  13. # => Create an instance of the model using the form data
  14. @user = User.new(params[:user])
  15.  
  16. # => If either the username or password from the form don't exist, tell the user
  17. if (@user.name.empty? || @user.password.empty?)
  18. flash[:notice] = "Please input username <strong>and</strong> password"
  19. end
  20.  
  21. # => Check if the username exists
  22. if is_user?(@user.name)
  23. # => Username exists, match the password to the user.
  24. if is_password?(@user.name, @user.password)
  25. flash[:notice] = "Password Matched"
  26. else
  27. flash[:notice] = "Password Incorrect"
  28. end
  29. else
  30. # => Username doesn't exist. Notify the user.
  31. flash[:notice] = "Username doesn't exist. Please try again."
  32. end
  33.  
  34. # => Remember data if it user chose to do so
  35. end
  36. end
  37. end
  38. end
  39.  
  40. ## User.rb
  41. require "digest/sha1"
  42. class User < ActiveRecord::Base
  43. # User Levels
  44. # => 0 = Not loggedin
  45. # => 1 = Normal USer
  46. # => 90 = Admin
  47. # => 99 = Super Admin
  48.  
  49. attr_accessor :password
  50. attr_accessible :name, :password, :userlevel, :message => 'is needed'
  51. validates_uniqueness_of :name
  52. validates_presence_of :name, :password
  53. validates_length_of :password, :in => 6..15
  54.  
  55. def before_save
  56. # => Before save or update
  57. self.password = self.hash_password(self.password || "")
  58. end
  59.  
  60. def after_save
  61. # => After save or update
  62. @password = nil
  63. end
  64.  
  65. def is_user?(username)
  66. # => If the user is found, return TRUE, else FALSE
  67. return !User.find_by_name(username).nil?
  68. end
  69.  
  70. def is_password?(username, password)
  71. self.find( :first,
  72. :conditions => ["name = ? and password = ?",
  73. username, hash_password(password)])
  74. end
  75.  
  76. def hash_password(password)
  77. return Digest::SHA1.hexdigest(password) # => SHA1 the password
  78. end
  79. end
  80.  
  81. ## Error
  82. NoMethodError in UserController#login
  83.  
  84. undefined method `is_user?' for #<UserController:0x225a7d0>
  85. RAILS_ROOT: /Users/zachinglis/Sites/rails/heroestheories/public/../config/..
  86.  
  87. Application Trace | Framework Trace | Full Trace
  88. #{RAILS_ROOT}/app/controllers/user_controller.rb:21:in `login'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement