2019-07-30 21:29:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-25 16:15:39 +00:00
|
|
|
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
|
2018-03-25 17:30:42 +00:00
|
|
|
class ApplicationController < ActionController::Base
|
2019-07-30 21:29:24 +00:00
|
|
|
before_action :set_locale, :redirect_to_maintenance
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
protect_from_forgery
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
helper_method \
|
|
|
|
:current_role?,
|
|
|
|
:current_nonprofit_user?,
|
|
|
|
:administered_nonprofit,
|
2018-03-25 17:30:42 +00:00
|
|
|
:nonprofit_in_trial?,
|
2019-07-30 21:29:24 +00:00
|
|
|
:current_plan_tier # int
|
|
|
|
|
|
|
|
def set_locale
|
|
|
|
if params[:locale] && Settings.available_locales.include?(params[:locale])
|
|
|
|
I18n.locale = params[:locale]
|
|
|
|
else
|
|
|
|
I18n.locale = Settings.language
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def redirect_to_maintenance
|
|
|
|
if Settings&.maintenance&.maintenance_mode && !current_user
|
|
|
|
unless self.class == Users::SessionsController &&
|
|
|
|
((Settings.maintenance.maintenance_token && params[:maintenance_token] == Settings.maintenance.maintenance_token) || params[:format] == 'json')
|
|
|
|
redirect_to Settings.maintenance.maintenance_page
|
|
|
|
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
|
|
|
def store_location
|
|
|
|
referrer = request.fullpath
|
|
|
|
no_redirects = ['/users', '/signup', '/signin', '/users/sign_in', '/users/sign_up', '/users/password', '/users/sign_out', /.*\.json.*/, %r{.*auth/facebook.*}]
|
|
|
|
unless request.format.symbol == :json || no_redirects.map { |p| referrer.match(p) }.any?
|
|
|
|
session[:previous_url] = referrer
|
|
|
|
end
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
def block_with_sign_in(msg = nil)
|
|
|
|
store_location
|
2018-03-25 17:30:42 +00:00
|
|
|
if current_user
|
|
|
|
flash[:notice] = "It looks like you're not allowed to access that page. If this seems like a mistake, please contact #{Settings.mailer.email}"
|
|
|
|
redirect_to root_path
|
|
|
|
else
|
|
|
|
msg ||= 'We need to sign you in before you can do that.'
|
2019-07-30 21:29:24 +00:00
|
|
|
redirect_to new_user_session_path, flash: { error: msg }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def authenticate_user!(_options = {})
|
|
|
|
block_with_sign_in unless current_user
|
|
|
|
end
|
|
|
|
|
|
|
|
def authenticate_confirmed_user!
|
|
|
|
if !current_user
|
|
|
|
block_with_sign_in
|
|
|
|
elsif !current_user.confirmed? && !current_role?(%i[super_associate super_admin])
|
|
|
|
redirect_to new_user_confirmation_path, flash: { error: 'You need to confirm your account to do that.' }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def authenticate_super_associate!
|
|
|
|
unless current_role?(:super_admin) || current_role?(:super_associate)
|
|
|
|
block_with_sign_in 'Please login.'
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
2019-07-30 21:29:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def authenticate_super_admin!
|
|
|
|
block_with_sign_in 'Please login.' unless current_role?(:super_admin)
|
|
|
|
end
|
|
|
|
|
|
|
|
def current_role?(role_names, host_id = nil)
|
2018-03-25 17:30:42 +00:00
|
|
|
return false unless current_user
|
2019-07-30 21:29:24 +00:00
|
|
|
|
2018-03-25 17:30:42 +00:00
|
|
|
role_names = Array(role_names)
|
2019-07-30 21:29:24 +00:00
|
|
|
key = "current_role_user_#{current_user_id}_names_#{role_names.join('_')}_host_#{host_id}"
|
2018-03-25 17:30:42 +00:00
|
|
|
QueryRoles.user_has_role?(current_user.id, role_names, host_id)
|
2019-07-30 21:29:24 +00:00
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
def nonprofit_in_trial?(npo_id = nil)
|
2018-03-25 17:30:42 +00:00
|
|
|
return false if !npo_id && !administered_nonprofit
|
2019-07-30 21:29:24 +00:00
|
|
|
|
2018-03-25 17:30:42 +00:00
|
|
|
npo_id ||= administered_nonprofit.id
|
|
|
|
key = "in_trial_user_#{current_user_id}_nonprofit_#{npo_id}"
|
|
|
|
QueryBillingSubscriptions.currently_in_trial?(npo_id)
|
|
|
|
end
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
def current_plan_tier(npo_id = nil)
|
2018-03-25 17:30:42 +00:00
|
|
|
return 0 if !npo_id && !administered_nonprofit
|
2019-07-30 21:29:24 +00:00
|
|
|
|
2018-03-25 17:30:42 +00:00
|
|
|
npo_id ||= administered_nonprofit.id
|
2019-07-30 21:29:24 +00:00
|
|
|
return 2 if current_role?(:super_admin)
|
|
|
|
|
|
|
|
key = "plan_tier_user_#{current_user_id}_nonprofit_#{npo_id}"
|
2018-03-25 17:30:42 +00:00
|
|
|
administered_nonprofit ? QueryBillingSubscriptions.plan_tier(npo_id) : 0
|
2019-07-30 21:29:24 +00:00
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
def administered_nonprofit
|
|
|
|
return nil unless current_user
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
key = "administered_nonprofit_user_#{current_user_id}_nonprofit"
|
|
|
|
Nonprofit.where(id: QueryRoles.host_ids(current_user_id, %i[nonprofit_admin nonprofit_associate])).last
|
|
|
|
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
|
|
|
|
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
|
|
|
private
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
def current_user_id
|
|
|
|
current_user&.id
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|