Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## user_controller.rb
- class UserController < ApplicationController
- before_filter :authorize, :except => :login
- layout "frontend" # => Show the stylesheet for the user side
- def login
- User.new
- if session[:user_id].nil?
- if request.get?
- # => If no session exists, create it
- @user = User.new
- else
- # => Create an instance of the model using the form data
- @user = User.new(params[:user])
- # => If either the username or password from the form don't exist, tell the user
- if (@user.name.empty? || @user.password.empty?)
- flash[:notice] = "Please input username <strong>and</strong> password"
- end
- # => Check if the username exists
- if is_user?(@user.name)
- # => Username exists, match the password to the user.
- if is_password?(@user.name, @user.password)
- flash[:notice] = "Password Matched"
- else
- flash[:notice] = "Password Incorrect"
- end
- else
- # => Username doesn't exist. Notify the user.
- flash[:notice] = "Username doesn't exist. Please try again."
- end
- # => Remember data if it user chose to do so
- end
- end
- end
- end
- ## User.rb
- require "digest/sha1"
- class User < ActiveRecord::Base
- # User Levels
- # => 0 = Not loggedin
- # => 1 = Normal USer
- # => 90 = Admin
- # => 99 = Super Admin
- attr_accessor :password
- attr_accessible :name, :password, :userlevel, :message => 'is needed'
- validates_uniqueness_of :name
- validates_presence_of :name, :password
- validates_length_of :password, :in => 6..15
- def before_save
- # => Before save or update
- self.password = self.hash_password(self.password || "")
- end
- def after_save
- # => After save or update
- @password = nil
- end
- def is_user?(username)
- # => If the user is found, return TRUE, else FALSE
- return !User.find_by_name(username).nil?
- end
- def is_password?(username, password)
- self.find( :first,
- :conditions => ["name = ? and password = ?",
- username, hash_password(password)])
- end
- def hash_password(password)
- return Digest::SHA1.hexdigest(password) # => SHA1 the password
- end
- end
- ## Error
- NoMethodError in UserController#login
- undefined method `is_user?' for #<UserController:0x225a7d0>
- RAILS_ROOT: /Users/zachinglis/Sites/rails/heroestheories/public/../config/..
- Application Trace | Framework Trace | Full Trace
- #{RAILS_ROOT}/app/controllers/user_controller.rb:21:in `login'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement