Sha256: e0e8d9ddb27650481a8987e12bc305227bebacd148815a0fbc29c56400122978

Contents?: true

Size: 1.29 KB

Versions: 4

Compression:

Stored size: 1.29 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.find_by_id(session[:user_id])
    elsif cookies[:remember_token]
      User.find_by_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

4 entries across 4 versions & 1 rubygems

Version Path
cornflakeomnisocial-0.1.2.7 lib/extensions/action_controller/base.rb
cornflakeomnisocial-0.1.2.6 lib/extensions/action_controller/base.rb
cornflakeomnisocial-0.1.2.5 lib/extensions/action_controller/base.rb
cornflakeomnisocial-0.1.2.4 lib/extensions/action_controller/base.rb