-
Notifications
You must be signed in to change notification settings - Fork 897
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13948 from abellotti/api_fine_grained_settings
API Enhancement to support fine-grained settings whitelisting
- Loading branch information
Showing
2 changed files
with
128 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,36 @@ | ||
module Api | ||
class SettingsController < BaseController | ||
def index | ||
render_resource :settings, slice_settings(exposed_settings) | ||
render_resource :settings, whitelist_settings(settings_hash) | ||
end | ||
|
||
def show | ||
raise NotFoundError, "Settings category #{@req.c_id} not found" unless exposed_settings.include?(@req.c_id) | ||
render_resource :settings, slice_settings(@req.c_id) | ||
settings_value = entry_value(whitelist_settings(settings_hash), @req.c_suffix) | ||
|
||
raise NotFoundError, "Settings entry #{@req.c_suffix} not found" if settings_value.nil? | ||
render_resource :settings, settings_entry_to_hash(@req.c_suffix, settings_value) | ||
end | ||
|
||
private | ||
|
||
def exposed_settings | ||
ApiConfig.collections[:settings][:categories] | ||
def whitelist_settings(settings) | ||
result_hash = {} | ||
ApiConfig.collections[:settings][:categories].each do |category_path| | ||
result_hash.deep_merge!(settings_entry_to_hash(category_path, entry_value(settings, category_path))) | ||
end | ||
result_hash | ||
end | ||
|
||
def settings_hash | ||
@settings_hash ||= Settings.to_hash.deep_stringify_keys | ||
end | ||
|
||
def entry_value(settings, path) | ||
settings.fetch_path(path.split('/')) | ||
end | ||
|
||
def slice_settings(keys) | ||
::Settings.to_hash.slice(*Array(keys).collect(&:to_sym)) | ||
def settings_entry_to_hash(path, value) | ||
{}.tap { |h| h.store_path(path.split("/"), value) } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters