2019-07-30 21:29:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-06-12 20:03:43 +00:00
|
|
|
# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later
|
|
|
|
# Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE
|
2018-03-25 17:30:42 +00:00
|
|
|
class ApplicationController < ActionController::Base
|
2020-05-11 18:38:50 +00:00
|
|
|
include Controllers::Locale
|
|
|
|
include Controllers::Nonprofit::Authorization
|
2019-07-30 21:29:24 +00:00
|
|
|
before_action :set_locale, :redirect_to_maintenance
|
|
|
|
protect_from_forgery
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2021-02-12 00:34:39 +00:00
|
|
|
private
|
2019-07-30 21:29:24 +00:00
|
|
|
def redirect_to_maintenance
|
2020-06-10 22:31:47 +00:00
|
|
|
if Houdini.maintenance&.active && !current_user
|
2019-07-30 21:29:24 +00:00
|
|
|
unless self.class == Users::SessionsController &&
|
2020-06-10 22:31:47 +00:00
|
|
|
((Houdini.maintenance.token && params[:maintenance_token] == Houdini.maintenance.token) || params[:format] == 'json')
|
|
|
|
redirect_to Houdini.maintenance.page
|
2019-07-30 21:29:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def json_saved(model, msg = nil)
|
|
|
|
if model.valid?
|
|
|
|
flash[:notice] = msg if msg
|
|
|
|
render json: model, status: 200
|
|
|
|
else
|
|
|
|
render json: model.errors.full_messages, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
|
|
|
# A response helper for use with the param_validation gem
|
|
|
|
# use like: render_json{ UpdateUsers.update(params[:user]) }
|
|
|
|
# will catch and pretty print exceptions using the rails loggers
|
|
|
|
def render_json(&block)
|
|
|
|
begin
|
2019-07-30 21:29:24 +00:00
|
|
|
result = { status: 200, json: yield(block) }
|
2018-03-25 17:30:42 +00:00
|
|
|
rescue ParamValidation::ValidationError => e
|
|
|
|
logger.info "422: #{e}".red.bold
|
2019-07-30 21:29:24 +00:00
|
|
|
# logger.info ">>".bold.red + " #{{'Failed key name' => e.data[:key], 'Value' => e.data[:val], 'Failed validator' => e.data[:name]}}".red
|
|
|
|
result = { status: 422, json: { error: e.message } }
|
|
|
|
rescue CCOrgError => e
|
|
|
|
logger.info "422: #{e}".red.bold
|
|
|
|
result = { status: 422, json: { error: e.message } }
|
2018-03-25 17:30:42 +00:00
|
|
|
rescue ActiveRecord::RecordNotFound => e
|
|
|
|
logger.info "404: #{e}".red.bold
|
2019-07-30 21:29:24 +00:00
|
|
|
result = { status: 404, json: { error: e.message } }
|
|
|
|
rescue AuthenticationError => e
|
|
|
|
logger.info "401: #{e}".red.bold
|
|
|
|
result = { status: 401, json: { error: e.message } }
|
|
|
|
rescue ExpiredTokenError => e
|
|
|
|
logger.info "422: #{e}".red.bold
|
|
|
|
result = { status: 422, json: { error: e.message } }
|
2018-03-25 17:30:42 +00:00
|
|
|
rescue Exception => e # a non-validation related exception
|
|
|
|
logger.error "500: #{e}".red.bold
|
2019-07-30 21:29:24 +00:00
|
|
|
logger.error e.backtrace.take(5).map { |l| '>>'.red.bold + " #{l}" }.join("\n").red
|
|
|
|
result = { status: 500, json: { error: e.message, backtrace: e.backtrace } }
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
render result
|
|
|
|
end
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
# Test that within the last 5 minutes, the user has confirmed their password
|
|
|
|
def password_was_confirmed(token)
|
|
|
|
session[:pw_token] == token && Chronic.parse(session[:pw_timestamp]) >= 5.minutes.ago.utc
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
# devise config
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
def after_sign_in_path_for(_resource)
|
|
|
|
request.env['omniauth.origin'] || session[:previous_url] || root_path
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
def after_sign_up_path_for(_resource)
|
|
|
|
request.env['omniauth.origin'] || session[:previous_url] || root_path
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
def after_update_path_for(_resource)
|
|
|
|
profile_path(current_user.profile)
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
def after_inactive_sign_up_path_for(_resource)
|
|
|
|
profile_path(current_user.profile)
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|