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"
- def login
- if session[:user_id].nil?
- if request.get?
- @user = User.new
- else
- @user = User.new(params[:user])
- @authenticate = User.authentication(@user.name,@user.password) # => Add Remember Me?
- unless @authenticate == "Logged In"
- flash[:notice] = @authenticate
- end
- end
- else
- render :action => "action"
- end
- end
- end
- ##user.rb
- require "digest/sha1"
- class User < ActiveRecord::Base
- # User Levels [:level]
- # => 0 = Not loggedin
- # => 1 = Normal USer
- # => 90 = Admin
- # => 99 = Super Admin
- attr_accessor :password
- attr_accessible :name, :password, :level, :message => 'is needed'
- validates_uniqueness_of :name
- validates_presence_of :name, :password
- validates_length_of :password, :in => 6..15
- # => Before save or update
- def before_save
- self.password = self.hash_password(self.password || "")
- end
- # => After save or update
- def after_save
- @password = nil
- end
- def self.authentication(username,password)
- if (username.empty? || password.empty?)
- return "Please input username <strong>and</strong> password"
- end
- unless self.check_is_user?(username)
- return "Can't find this username?"
- end
- @userinfo = self.password_belongs_to_user?(username,password)
- unless @userinfo
- return "That username and password do not match"
- end
- return "Logged In"
- end
- # => If the user is found, return TRUE, else FALSE
- def self.check_is_user?(username)
- return !User.find_by_name(username).nil?
- end
- def self.password_belongs_to_user?(username, password)
- return self.find( :first,
- :conditions => ["name = ? and password = ?",
- username, hash_password(password)])
- end
- def self.hash_password(password)
- return Digest::SHA1.hexdigest(password) # => SHA1 the password
- end
- end
- ##Error
- undefined local variable or method `session' for User:Class
- RAILS_ROOT: /Users/zachinglis/Sites/rails/heroestheories/public/../config/..
- Application Trace | Framework Trace | Full Trace
- /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1129:in `method_missing'
- #{RAILS_ROOT}/app/models/user.rb:35:in `authentication'
- #{RAILS_ROOT}/app/controllers/user_controller.rb:11:in `login'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement