docbrown/app/controllers/admin/settings/base_controller.rb
Michael Kohl dcf942e3a2
Add Admin:Settings controllers (#13851)
* Add Admin:Settings::BaseController and refactor

* Further simplify controllers

* Add comments, more cleanup

* Update mandatory settings controller

* Update spec

* Update service object and spec

* Update controller

* Update app/services/settings/general/upsert.rb

Co-authored-by: Jamie Gaskins <jamie@forem.com>

Co-authored-by: Jamie Gaskins <jamie@forem.com>
2021-06-08 10:13:55 +07:00

51 lines
1.7 KiB
Ruby

module Admin
module Settings
class BaseController < Admin::ApplicationController
MISMATCH_ERROR = "The confirmation key does not match".freeze
before_action :extra_authorization_and_confirmation, only: [:create]
def create
result = upsert_config(settings_params)
if result.success?
Audit::Logger.log(:internal, current_user, params.dup)
redirect_to admin_config_path, notice: "Successfully updated settings."
else
redirect_to admin_config_path, alert: "😭 #{result.errors.to_sentence}"
end
end
private
def extra_authorization_and_confirmation
not_authorized unless current_user.has_role?(:super_admin)
raise_confirmation_mismatch_error unless confirmation_text_valid?
end
def confirmation_text_valid?
params.require(:confirmation) ==
"My username is @#{current_user.username} and this action is 100% safe and appropriate."
end
def raise_confirmation_mismatch_error
raise ActionController::BadRequest.new, MISMATCH_ERROR
end
# Override this method if you need to call a custom class for upserting.
# Ideally such a class eventually calls out to Settings::Upsert and returns
# the result of that service.
def upsert_config(settings)
::Settings::Upsert.call(settings, authorization_resource)
end
# Override this if you need additional params or need to make other changes,
# e.g. a different require key.
def settings_params
params
.require(:"settings_#{authorization_resource.name.demodulize.underscore}")
.permit(*authorization_resource.keys)
end
end
end
end