* Refactoring questions asked of user In this pull request, I'm extracting and normalizing role-based questions asked of the user. Prior to this commit, our codebase has asked two very similar questions of our user model: - `user.has_role?(:admin)` - `user.admin?` In asking `has_role?(:admin)` we are relying on implementation details of the rolify gem. In addition, the `has_role?` question asked throughout controllers or views means that it's harder to create hieararchies of permissions. In favoring `user.admin?` as our question, we can use that indirection as an opportunity to discuss and decide "Should someone with the `:super_admin` role be `user.admin? == true`?" The details of this commit is to do three primary things: 1. Ask the `has_role?` questions in "one place" in the code (e.g. the `Authorizer` module) 2. Extract the role based questions that are on the `User` model and provde backwards compatable delegation. 3. Structure the code so that it's harder to accidentally call `user.has_role?` (e.g., make `User#has_role?` and `User#has_any_role?` private). This is related to #15624 and the updates are informed by discussion in PR #15691. This commit supplants #15691. * Refactoring the liquid tag policy tests * Fixing typo * Bump for travis
39 lines
1.2 KiB
Ruby
39 lines
1.2 KiB
Ruby
module Admin
|
|
module Settings
|
|
class BaseController < Admin::ApplicationController
|
|
before_action :authorize_super_admin
|
|
|
|
def create
|
|
result = upsert_config(settings_params)
|
|
|
|
if result.success?
|
|
Audit::Logger.log(:internal, current_user, params.dup)
|
|
render json: { message: "Successfully updated settings." }, status: :ok
|
|
else
|
|
render json: { error: result.errors.to_sentence }, status: :unprocessable_entity
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
# 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
|
|
|
|
def authorize_super_admin
|
|
raise Pundit::NotAuthorizedError unless current_user.super_admin?
|
|
end
|
|
end
|
|
end
|
|
end
|