docbrown/app/controllers/admin/creator_settings_controller.rb
Jeremy Friesen a40efc6bbd
Refactoring questions asked of user (#15762)
* 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
2021-12-21 12:45:12 -05:00

50 lines
1.7 KiB
Ruby

module Admin
class CreatorSettingsController < Admin::ApplicationController
after_action :bust_content_change_caches, only: %i[create]
ALLOWED_PARAMS = %i[checked_code_of_conduct checked_terms_and_conditions community_name
invite_only_mode logo primary_brand_color_hex public].freeze
def new
@creator_settings_form = CreatorSettingsForm.new(
community_name: ::Settings::Community.community_name,
public: ::Settings::UserExperience.public,
invite_only_mode: ::Settings::Authentication.invite_only_mode,
primary_brand_color_hex: ::Settings::UserExperience.primary_brand_color_hex,
checked_code_of_conduct: current_user.checked_code_of_conduct,
checked_terms_and_conditions: current_user.checked_terms_and_conditions,
)
@max_file_size = LogoUploader::MAX_FILE_SIZE
@logo_allowed_types = LogoUploader::ALLOWED_TYPES
end
def create
extra_authorization
@creator_settings_form = CreatorSettingsForm.new(settings_params)
current_user.update!(
checked_code_of_conduct: @creator_settings_form.checked_code_of_conduct,
checked_terms_and_conditions: @creator_settings_form.checked_terms_and_conditions,
)
@creator_settings_form.save
if @creator_settings_form.success
current_user.update!(saw_onboarding: true)
redirect_to root_path
else
flash[:error] = @creator_settings_form.errors.full_messages
redirect_to new_admin_creator_setting_path
end
end
private
def extra_authorization
not_authorized unless current_user.creator?
end
def settings_params
params.require(:creator_settings_form).permit(ALLOWED_PARAMS)
end
end
end