Sha256: da8f50a6bc01246a43ada4ecde7bbb451abd2bc93f04419142b62b4f074a0b04

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

class ActionController::Base
  def self.require_user(options = {})
    raise Exception, "require_user cannot be called on ActionController::Base. Only it's subclasses" if self ==  ActionController::Base
    prepend_before_filter :require_user, options
  end
  
  helper_method :current_user, :current_user?

  protected
  
  # Filters
  
  def require_user
    current_user.present? || deny_access
  end
  
  # Utils
  
  def store_location
    session[:return_to] = request.fullpath
  end
  
  def deny_access(url = nil)
    store_location
    redirect_to url || login_path
  end
  
  def redirect_back_or_default(default)
    redirect_to(session[:return_to] || default)
    session[:return_to] = nil
  end
  
  def current_user
    @current_user ||= if session[:user_id]
      User.first(:conditions => {:id => session[:user_id]})
    elsif cookies[:remember_token]
      User.first(:conditions => {:remember_token => cookies[:remember_token]})
    else
      false
    end
  end
  
  def current_user?
    !!current_user
  end
  
  def current_user=(user)
    user.tap do |user|
      user.remember
      session[:user_id]         = user.id
      cookies[:remember_token]  = user.remember_token
    end
  end
  
  def logout!
    session[:user_id] = nil
    @current_user     = nil
    cookies.delete(:remember_token)
    session[:return_to] = nil
  end
  
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cornflakeomnisocial-0.1.2.3 lib/extensions/action_controller/base.rb