2019-08-14 09:48:44 +00:00
|
|
|
class ApplicationController < ActionController::Base
|
|
|
|
include DeviseTokenAuth::Concerns::SetUserByToken
|
|
|
|
include Pundit
|
2020-11-30 10:54:54 +00:00
|
|
|
include SwitchLocale
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2021-07-14 13:10:24 +00:00
|
|
|
skip_before_action :verify_authenticity_token
|
2019-08-14 09:48:44 +00:00
|
|
|
|
|
|
|
before_action :set_current_user, unless: :devise_controller?
|
2020-11-30 10:54:54 +00:00
|
|
|
around_action :switch_locale
|
2019-08-14 09:48:44 +00:00
|
|
|
around_action :handle_with_exception, unless: :devise_controller?
|
|
|
|
|
|
|
|
rescue_from ActiveRecord::RecordInvalid, with: :render_record_invalid
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def handle_with_exception
|
2019-10-20 08:47:26 +00:00
|
|
|
yield
|
|
|
|
rescue ActiveRecord::RecordNotFound => e
|
2021-08-03 14:41:52 +00:00
|
|
|
Sentry.capture_exception(e)
|
2019-10-20 08:47:26 +00:00
|
|
|
render_not_found_error('Resource could not be found')
|
|
|
|
rescue Pundit::NotAuthorizedError
|
|
|
|
render_unauthorized('You are not authorized to do this action')
|
|
|
|
ensure
|
|
|
|
# to address the thread variable leak issues in Puma/Thin webserver
|
2021-01-14 15:05:22 +00:00
|
|
|
Current.reset
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def set_current_user
|
|
|
|
@user ||= current_user
|
|
|
|
Current.user = @user
|
|
|
|
end
|
|
|
|
|
|
|
|
def current_subscription
|
2020-06-07 08:28:05 +00:00
|
|
|
@subscription ||= Current.account.subscription
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def render_unauthorized(message)
|
|
|
|
render json: { error: message }, status: :unauthorized
|
|
|
|
end
|
|
|
|
|
|
|
|
def render_not_found_error(message)
|
|
|
|
render json: { error: message }, status: :not_found
|
|
|
|
end
|
|
|
|
|
|
|
|
def render_could_not_create_error(message)
|
|
|
|
render json: { error: message }, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
|
|
|
|
def render_internal_server_error(message)
|
|
|
|
render json: { error: message }, status: :internal_server_error
|
|
|
|
end
|
|
|
|
|
|
|
|
def render_record_invalid(exception)
|
2019-10-20 08:47:26 +00:00
|
|
|
render json: {
|
|
|
|
message: exception.record.errors.full_messages.join(', ')
|
2019-08-14 09:48:44 +00:00
|
|
|
}, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
|
|
|
|
def render_error_response(exception)
|
|
|
|
render json: exception.to_hash, status: exception.http_status
|
|
|
|
end
|
|
|
|
|
2020-05-26 17:08:48 +00:00
|
|
|
def pundit_user
|
|
|
|
{
|
|
|
|
user: Current.user,
|
|
|
|
account: Current.account,
|
|
|
|
account_user: Current.account_user
|
|
|
|
}
|
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|