asciinema.org/app/controllers/application_controller.rb

51 lines
1.1 KiB
Ruby
Raw Normal View History

2011-11-23 20:46:18 +00:00
class NotFound < StandardError; end
class ApplicationController < ActionController::Base
protect_from_forgery
2011-11-23 21:27:38 +00:00
rescue_from(ActiveRecord::RecordNotFound) { render 'exceptions/not_found' }
2011-11-23 20:46:18 +00:00
2012-03-01 23:25:55 +00:00
class Unauthorized < Exception; end
class Forbiden < Exception; end
2012-03-04 14:54:25 +00:00
rescue_from Unauthorized, :with => :unauthorized
rescue_from Forbiden, :with => :forbiden
2012-03-01 23:25:55 +00:00
helper_method :current_user
def current_user
2012-03-02 21:33:13 +00:00
@current_user ||= User.find_by_id(session[:user_id]) if session[:user_id]
end
def current_user=(user)
if user
@current_user = user
session[:user_id] = user.id
else
@current_user = nil
session[:user_id] = nil
end
end
2012-03-01 23:25:55 +00:00
private
def ensure_authenticated!
raise Unauthorized unless current_user
end
def forbiden
if request.xhr?
render :json => "Forbiden", :status => 403
else
redirect_to root_path, :alert => "This action is forbiden"
end
end
2012-03-04 14:54:25 +00:00
def unauthorized
if request.xhr?
render :json => "Unauthorized", :status => 401
else
redirect_to login_path, :notice => "Please login"
end
end
end