From f09ef892dc8e8bce123edb2296ef72927d47cbda Mon Sep 17 00:00:00 2001 From: wvengen Date: Fri, 27 Jun 2014 09:07:47 +0200 Subject: [PATCH] add foodsoft_config protection whitelisting --- lib/foodsoft_config.rb | 9 ++++++++- spec/lib/foodsoft_config_spec.rb | 18 ++++++++++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/lib/foodsoft_config.rb b/lib/foodsoft_config.rb index 3f377586..d6d8257c 100644 --- a/lib/foodsoft_config.rb +++ b/lib/foodsoft_config.rb @@ -28,6 +28,9 @@ # shared_lists: false # allow database connection override # use_messages: true # foodcoops can't disable the use of messages # +# When you like to whitelist protected attributes, define an entry +all: true+, +# then you can whitelist specific attributes setting them to +false+. +# class FoodsoftConfig # @!attribute scope @@ -152,7 +155,11 @@ class FoodsoftConfig # @return [Boolean] Whether this key may be set in the database def allowed_key?(key) # fast check for keys without nesting - return !self.config[:protected][key] + if self.config[:protected].include? key + return !self.config[:protected][key] + else + return !self.config[:protected][:all] + end # @todo allow to check nested keys as well end diff --git a/spec/lib/foodsoft_config_spec.rb b/spec/lib/foodsoft_config_spec.rb index 6b35a3c2..20ab28f5 100644 --- a/spec/lib/foodsoft_config_spec.rb +++ b/spec/lib/foodsoft_config_spec.rb @@ -5,11 +5,11 @@ describe FoodsoftConfig do let(:other_name) { Faker::Lorem.words(rand(2..4)).join(' ') } it 'returns a default value' do - expect(FoodsoftConfig[:protected][:database]).to be_true + expect(FoodsoftConfig[:protected][:database]).to be true end it 'returns an empty default value' do - expect(FoodsoftConfig[:protected][:LIUhniuyGNKUQTWfbiOQIWYexngo78hqexul]).to be_false + expect(FoodsoftConfig[:protected][:LIUhniuyGNKUQTWfbiOQIWYexngo78hqexul]).to be nil end it 'returns a configuration value' do @@ -60,6 +60,20 @@ describe FoodsoftConfig do end end + it 'can protect all values' do + old_name = FoodsoftConfig[:name] + FoodsoftConfig.config[:protected][:all] = true + FoodsoftConfig[:name] = name + expect(FoodsoftConfig[:name]).to eq old_name + end + + it 'can whitelist a value' do + FoodsoftConfig.config[:protected][:all] = true + FoodsoftConfig.config[:protected][:name] = false + FoodsoftConfig[:name] = name + expect(FoodsoftConfig[:name]).to eq name + end + describe 'has indifferent access', type: :feature do it 'with symbol' do FoodsoftConfig[:name] = name