ledermann-rails-settings 2.0.0 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7aa1af77c8b2aba3819e77ba00790f313f143e69
4
- data.tar.gz: c4d85b477105c7f86dae04d5fb5b9245506fd1a2
3
+ metadata.gz: acfe3a67b8daf8e9108c0ddf270e638c4310d2b1
4
+ data.tar.gz: ef9c5a3a455365f5817f56f5476b12d9283d2440
5
5
  SHA512:
6
- metadata.gz: 883960f19b067da03bc8f54d3de59d82d8640eeeb44d31b5ada5949a552a43027f59bec7b3854e9f9bc341c10126deb3c1714c0a6b6d4a5433639a9a1f53ebdb
7
- data.tar.gz: 9786e48162d45e3a07020d268dc8dd9cbab7a2d5dd2bcc290a5ac47121f27bd4d0601fe75054d009773a95bf644390546aeaaf8b8425f5735a7eb06c02cdc2cb
6
+ metadata.gz: 5fd20605f8b1dc5474b94402140baba09d08a5d59429269ebb37bc4bd5b63742088a24ed0c2ccbf6b7cb127c631fb5522b85636735a1a6b28f94853ed97b90e5
7
+ data.tar.gz: 3c4f8c5f8f5732e727cf116a341d04f09e519e8e1aa13df8b94e72f3dfd532a6155a1cfa7c4af0de44ceebce271ca4e28a1e5b129008994a992362fb14de44ea
data/Changelog.md CHANGED
@@ -1,3 +1,8 @@
1
+ Version 2.0.1 (2013-03-08)
2
+
3
+ - Added mass assignment security by protecting all regular attributes
4
+
5
+
1
6
  Version 2.0.0 (2013-03-07)
2
7
 
3
8
  - Complete rewrite
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Settings for Rails 3
2
2
 
3
- [![Build Status](https://secure.travis-ci.org/ledermann/rails-settings.png)](http://travis-ci.org/ledermann/rails-settings)
3
+ [![Build Status](https://travis-ci.org/ledermann/rails-settings.png?branch=master)](https://travis-ci.org/ledermann/rails-settings)
4
4
  [![Code Climate](https://codeclimate.com/github/ledermann/rails-settings.png)](https://codeclimate.com/github/ledermann/rails-settings)
5
5
 
6
6
  Ruby gem to handle settings for ActiveRecord objects by storing them as serialized Hash in a separate database table. Optional: Defaults and Namespaces.
@@ -7,8 +7,8 @@ ActiveRecord::Base.class_eval do
7
7
  def self.has_settings(*args, &block)
8
8
  RailsSettings::Configuration.new(*args.unshift(self), &block)
9
9
 
10
- include RailsSettings::Base unless self.include?(RailsSettings::Base)
11
- include RailsSettings::Scopes unless self.include?(RailsSettings::Scopes)
10
+ include RailsSettings::Base
11
+ extend RailsSettings::Scopes
12
12
  end
13
13
  end
14
14
 
@@ -12,7 +12,7 @@ module RailsSettings
12
12
  raise ArgumentError unless var.is_a?(Symbol)
13
13
  raise ArgumentError.new("Unknown key: #{var}") unless self.class.default_settings[var]
14
14
 
15
- setting_objects.detect { |s| s.var == var.to_s } || setting_objects.build(:var => var.to_s)
15
+ setting_objects.detect { |s| s.var == var.to_s } || setting_objects.build({ :var => var.to_s }, :without_protection => true)
16
16
  end
17
17
 
18
18
  def settings=(value)
@@ -1,33 +1,29 @@
1
1
  module RailsSettings
2
2
  module Scopes
3
- def self.included(base)
4
- base.class_eval do
5
- scope :with_settings, lambda {
6
- joins("INNER JOIN settings ON #{settings_join_condition}").
7
- uniq
8
- }
3
+ def with_settings
4
+ joins("INNER JOIN settings ON #{settings_join_condition}").
5
+ uniq
6
+ end
9
7
 
10
- scope :with_settings_for, lambda { |var|
11
- raise ArgumentError unless var.is_a?(Symbol)
12
- joins("INNER JOIN settings ON #{settings_join_condition} AND settings.var = '#{var}'")
13
- }
8
+ def with_settings_for(var)
9
+ raise ArgumentError.new('Symbol expected!') unless var.is_a?(Symbol)
10
+ joins("INNER JOIN settings ON #{settings_join_condition} AND settings.var = '#{var}'")
11
+ end
14
12
 
15
- scope :without_settings, lambda {
16
- joins("LEFT JOIN settings ON #{settings_join_condition}").
17
- where('settings.id IS NULL')
18
- }
13
+ def without_settings
14
+ joins("LEFT JOIN settings ON #{settings_join_condition}").
15
+ where('settings.id IS NULL')
16
+ end
19
17
 
20
- scope :without_settings_for, lambda { |var|
21
- raise ArgumentError unless var.is_a?(Symbol)
22
- joins("LEFT JOIN settings ON #{settings_join_condition} AND settings.var = '#{var}'").
23
- where('settings.id IS NULL')
24
- }
18
+ def without_settings_for(var)
19
+ raise ArgumentError.new('Symbol expected!') unless var.is_a?(Symbol)
20
+ joins("LEFT JOIN settings ON #{settings_join_condition} AND settings.var = '#{var}'").
21
+ where('settings.id IS NULL')
22
+ end
25
23
 
26
- def self.settings_join_condition
27
- "settings.target_id = #{table_name}.#{primary_key} AND
28
- settings.target_type = '#{base_class.name}'"
29
- end
30
- end
24
+ def settings_join_condition
25
+ "settings.target_id = #{table_name}.#{primary_key} AND
26
+ settings.target_type = '#{base_class.name}'"
31
27
  end
32
28
  end
33
29
  end
@@ -13,6 +13,10 @@ module RailsSettings
13
13
 
14
14
  serialize :value, Hash
15
15
 
16
+ # attr_protected can not be here used because it touches the database which is not connected yet.
17
+ # So allow no attributes and override <tt>#sanitize_for_mass_assignment</tt>
18
+ attr_accessible
19
+
16
20
  REGEX_SETTER = /\A([a-z]\w+)=\Z/i
17
21
  REGEX_GETTER = /\A([a-z]\w+)\Z/i
18
22
 
@@ -34,6 +38,12 @@ module RailsSettings
34
38
  end
35
39
  end
36
40
 
41
+ protected
42
+ # Simulate attr_protected by removing all regular attributes
43
+ def sanitize_for_mass_assignment(attributes, role = nil)
44
+ attributes.except('id', 'var', 'value', 'target_id', 'target_type', 'created_at', 'updated_at')
45
+ end
46
+
37
47
  private
38
48
  def _get_value(name)
39
49
  value[name] || _target_class.default_settings[var.to_sym][name]
@@ -1,3 +1,3 @@
1
1
  module RailsSettings
2
- VERSION = '2.0.0'
2
+ VERSION = '2.0.1'
3
3
  end
@@ -2,8 +2,8 @@ require 'spec_helper'
2
2
 
3
3
  describe RailsSettings::SettingObject do
4
4
  let(:user) { User.create! :name => 'Mr. Pink' }
5
- let(:new_setting_object) { user.setting_objects.build :var => 'dashboard' }
6
- let(:saved_setting_object) { user.setting_objects.create! :var => 'dashboard', :value => { 'theme' => 'pink', 'filter' => true } }
5
+ let(:new_setting_object) { user.setting_objects.build({ :var => 'dashboard'}, :without_protection => true) }
6
+ let(:saved_setting_object) { user.setting_objects.create!({ :var => 'dashboard', :value => { 'theme' => 'pink', 'filter' => true}}, :without_protection => true) }
7
7
 
8
8
  describe "Getter and Setter" do
9
9
  context "on unsaved settings" do
@@ -92,6 +92,13 @@ describe RailsSettings::SettingObject do
92
92
  it 'should not save blank hash' do
93
93
  new_setting_object.update_attributes({}).should be_false
94
94
  end
95
+
96
+ it 'should not allow changing protected attributes' do
97
+ new_setting_object.update_attributes!(:var => 'calendar', :foo => 42)
98
+
99
+ new_setting_object.var.should eq('dashboard')
100
+ new_setting_object.foo.should eq(42)
101
+ end
95
102
  end
96
103
 
97
104
  describe "save" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ledermann-rails-settings
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Georg Ledermann
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-03-07 00:00:00.000000000 Z
11
+ date: 2013-03-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord