2018-10-13 20:16:35 +02:00
|
|
|
class Api::V1::BaseController < ApplicationController
|
2019-02-05 20:53:02 +01:00
|
|
|
include Concerns::AuthApi
|
|
|
|
|
2018-10-13 20:16:35 +02:00
|
|
|
protect_from_forgery with: :null_session
|
|
|
|
|
|
|
|
before_action :skip_session
|
2018-10-13 15:15:43 +02:00
|
|
|
before_action :authenticate
|
|
|
|
|
2018-10-13 20:16:35 +02:00
|
|
|
rescue_from ActiveRecord::RecordNotFound, with: :not_found_handler
|
|
|
|
rescue_from ActiveRecord::RecordNotSaved, with: :not_acceptable_handler
|
|
|
|
rescue_from ActiveRecord::RecordInvalid, with: :not_acceptable_handler
|
|
|
|
rescue_from Api::Errors::PermissionRequired, with: :permission_required_handler
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# @return [Ordergroup] Current user's ordergroup, or +nil+ if no valid token or user has no ordergroup.
|
|
|
|
def current_ordergroup
|
|
|
|
current_user.try(:ordergroup)
|
|
|
|
end
|
|
|
|
|
|
|
|
def require_ordergroup
|
|
|
|
authenticate
|
2023-05-12 13:01:12 +02:00
|
|
|
return if current_ordergroup.present?
|
|
|
|
|
|
|
|
raise Api::Errors::PermissionRequired, 'Forbidden, must be in an ordergroup'
|
2018-10-13 20:16:35 +02:00
|
|
|
end
|
|
|
|
|
2021-02-18 10:55:51 +01:00
|
|
|
def require_minimum_balance
|
|
|
|
minimum_balance = FoodsoftConfig[:minimum_balance] or return
|
2023-05-12 13:01:12 +02:00
|
|
|
return unless current_ordergroup.account_balance < minimum_balance
|
|
|
|
|
|
|
|
raise Api::Errors::PermissionRequired, t('application.controller.error_minimum_balance', min: minimum_balance)
|
2021-02-18 10:55:51 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def require_enough_apples
|
2023-05-12 13:01:12 +02:00
|
|
|
return unless current_ordergroup.not_enough_apples?
|
|
|
|
|
|
|
|
s = t('group_orders.messages.not_enough_apples', apples: current_ordergroup.apples,
|
|
|
|
stop_ordering_under: FoodsoftConfig[:stop_ordering_under])
|
|
|
|
raise Api::Errors::PermissionRequired, s
|
2021-02-18 10:55:51 +01:00
|
|
|
end
|
|
|
|
|
2021-02-18 11:17:33 +01:00
|
|
|
def require_config_enabled(config)
|
2023-05-12 13:01:12 +02:00
|
|
|
return if FoodsoftConfig[config]
|
|
|
|
|
|
|
|
raise Api::Errors::PermissionRequired, t('application.controller.error_not_enabled', config: config)
|
2021-02-18 11:17:33 +01:00
|
|
|
end
|
|
|
|
|
2018-10-13 20:16:35 +02:00
|
|
|
def skip_session
|
|
|
|
request.session_options[:skip] = true
|
|
|
|
end
|
|
|
|
|
|
|
|
def not_found_handler(e)
|
|
|
|
# remove where-clauses from error message (not suitable for end-users)
|
2021-03-01 15:27:26 +01:00
|
|
|
msg = e.message.try { |m| m.sub(/\s*\[.*?\]\s*$/, '') } || 'Not found'
|
2023-05-12 13:01:12 +02:00
|
|
|
render status: :not_found, json: { error: 'not_found', error_description: msg }
|
2018-10-13 20:16:35 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def not_acceptable_handler(e)
|
2019-02-05 20:53:02 +01:00
|
|
|
msg = e.message || 'Data not acceptable'
|
2023-05-12 13:01:12 +02:00
|
|
|
render status: :unprocessable_entity, json: { error: 'not_acceptable', error_description: msg }
|
2018-10-13 20:16:35 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def doorkeeper_unauthorized_render_options(error:)
|
2021-03-01 15:27:26 +01:00
|
|
|
{ json: { error: error.name, error_description: error.description } }
|
2018-10-13 20:16:35 +02:00
|
|
|
end
|
|
|
|
|
2019-02-05 20:53:02 +01:00
|
|
|
def doorkeeper_forbidden_render_options(error:)
|
2021-03-01 15:27:26 +01:00
|
|
|
{ json: { error: error.name, error_description: error.description } }
|
2019-02-05 20:53:02 +01:00
|
|
|
end
|
|
|
|
|
2018-10-13 20:16:35 +02:00
|
|
|
def permission_required_handler(e)
|
|
|
|
msg = e.message || 'Forbidden, user has no access'
|
2023-05-12 13:01:12 +02:00
|
|
|
render status: :forbidden, json: { error: 'forbidden', error_description: msg }
|
2018-10-13 20:16:35 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# @todo something with ApplicationHelper#show_user
|
2023-05-12 13:01:12 +02:00
|
|
|
def show_user(user = current_user, **_options)
|
2018-10-13 20:16:35 +02:00
|
|
|
user.display
|
|
|
|
end
|
|
|
|
end
|