2014-03-23 00:22:25 +00:00
|
|
|
class ApplicationController < ActionController::Base
|
2014-04-27 23:16:28 +00:00
|
|
|
include Exceptions
|
|
|
|
|
2014-03-23 00:22:25 +00:00
|
|
|
helper :all
|
|
|
|
helper_method :cuser, :strip, :return_here
|
|
|
|
|
2020-03-16 23:57:47 +00:00
|
|
|
before_action :update_user
|
|
|
|
before_action :set_controller_and_action_names
|
2014-03-23 00:22:25 +00:00
|
|
|
|
2020-04-10 15:32:18 +00:00
|
|
|
# Omniauth has its own CSRF
|
|
|
|
protect_from_forgery :except => [:callback]
|
|
|
|
|
2014-03-23 00:22:25 +00:00
|
|
|
respond_to :html, :js
|
|
|
|
|
|
|
|
def cuser
|
2020-04-10 16:58:36 +00:00
|
|
|
begin
|
|
|
|
@cuser ||= User.find(session[:user])
|
|
|
|
rescue
|
|
|
|
session[:user] = nil
|
|
|
|
@cuser = nil
|
|
|
|
end
|
2014-03-23 00:22:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def return_here
|
|
|
|
session[:return_to] = request.url
|
|
|
|
end
|
|
|
|
|
|
|
|
def return_to
|
|
|
|
addr = session[:return_to]
|
|
|
|
session[:return_to] = nil
|
|
|
|
redirect_to addr
|
|
|
|
end
|
|
|
|
|
2020-04-10 15:32:18 +00:00
|
|
|
def return_back
|
|
|
|
if session[:return_to]
|
|
|
|
return_to
|
|
|
|
elsif request.env["HTTP_REFERER"]
|
|
|
|
redirect_to request.env["HTTP_REFERER"]
|
|
|
|
else
|
|
|
|
redirect_to "/"
|
|
|
|
end
|
|
|
|
rescue
|
|
|
|
redirect_to "/"
|
|
|
|
end
|
|
|
|
|
2014-03-23 00:22:25 +00:00
|
|
|
def redirect_to_back
|
|
|
|
if request.env["HTTP_REFERER"]
|
|
|
|
redirect_to request.env["HTTP_REFERER"]
|
|
|
|
else
|
|
|
|
redirect_to "/"
|
|
|
|
end
|
|
|
|
rescue
|
|
|
|
redirect_to "/"
|
|
|
|
end
|
|
|
|
|
|
|
|
def redirect_to_home
|
2014-04-27 23:16:28 +00:00
|
|
|
redirect_to controller: "articles", action: "news_index"
|
2014-03-23 00:22:25 +00:00
|
|
|
end
|
|
|
|
|
2020-03-17 19:31:57 +00:00
|
|
|
unless Rails.env.production?
|
2014-04-27 23:16:28 +00:00
|
|
|
|
2020-03-17 19:31:57 +00:00
|
|
|
rescue_from AccessError do |exception|
|
|
|
|
render 'errors/403', status: 403, layout: 'errors'
|
|
|
|
end
|
2014-04-27 23:16:28 +00:00
|
|
|
|
2020-03-17 19:31:57 +00:00
|
|
|
rescue_from Error do |exception|
|
2020-03-21 20:33:44 +00:00
|
|
|
render text: exception.message, layout: true, status: 500
|
2020-03-17 19:31:57 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
rescue_from ActiveRecord::StaleObjectError do |exception|
|
|
|
|
render text: t(:application_stale)
|
|
|
|
end
|
2014-03-23 00:22:25 +00:00
|
|
|
|
2020-03-17 19:31:57 +00:00
|
|
|
rescue_from ActiveRecord::RecordNotFound do |exception|
|
|
|
|
render :template => 'errors/404.html', :status => :not_found, :layout => 'errors'
|
|
|
|
end
|
2014-10-11 23:39:14 +00:00
|
|
|
end
|
|
|
|
|
2014-03-23 00:22:25 +00:00
|
|
|
private
|
|
|
|
|
2020-03-17 19:31:57 +00:00
|
|
|
# FIXME: move to model
|
2014-03-23 00:22:25 +00:00
|
|
|
def update_user
|
|
|
|
if cuser
|
|
|
|
Time.zone = cuser.time_zone
|
2020-03-25 01:13:38 +00:00
|
|
|
cuser.update_attribute :lastvisit, Time.now.utc if cuser&.lastvisit < 2.minutes.ago.utc
|
2014-03-23 00:22:25 +00:00
|
|
|
|
|
|
|
if cuser.banned? Ban::TYPE_SITE
|
|
|
|
session[:user] = nil
|
|
|
|
@cuser = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_controller_and_action_names
|
|
|
|
@current_controller = controller_name
|
|
|
|
@current_action = action_name
|
|
|
|
end
|
|
|
|
end
|