You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
asciinema.org/app/controllers/application_controller.rb

84 lines
1.7 KiB
Ruby

require 'authentication/warden_authentication'
13 years ago
class ApplicationController < ActionController::Base
protect_from_forgery
13 years ago
class Unauthorized < Exception; end
13 years ago
class Forbidden < Exception; end
rescue_from ActiveRecord::RecordNotFound, :with => :not_found
13 years ago
rescue_from Unauthorized, :with => :unauthorized
13 years ago
rescue_from Forbidden, :with => :forbidden
helper_method :decorated_current_user
include WardenAuthentication
private
def warden_strategies
[:auth_cookie]
end
def decorated_current_user
current_user && current_user.decorate
end
def ensure_authenticated!
raise Unauthorized unless current_user
end
def omniauth_credentials
OmniAuthCredentials.new(request.env['omniauth.auth'])
end
def store_location
session[:return_to] = request.path
end
def get_stored_location
session.delete(:return_to)
end
def redirect_back_or_to(default, options = nil)
path = get_stored_location || default
if options
redirect_to path, options
else
redirect_to path
end
end
13 years ago
def forbidden
if request.xhr?
13 years ago
render :json => "Forbidden", :status => 403
else
13 years ago
redirect_to root_path, :alert => "This action is forbidden"
end
end
13 years ago
def unauthorized
if request.xhr?
render :json => "Unauthorized", :status => 401
else
store_location
11 years ago
redirect_to login_path, :notice => "Please sign in to proceed"
end
end
def not_found
respond_to do |format|
format.any do
render :text => 'Requested resource not found', :status => 404
end
format.html do
render 'application/not_found', :status => 404, :layout => 'application'
end
end
end
end