protector 0.7.0 → 0.7.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -1
- data/.travis.yml +10 -0
- data/Appraisals +9 -1
- data/Gemfile +2 -2
- data/Rakefile +3 -3
- data/gemfiles/AR_3.2.gemfile +18 -0
- data/gemfiles/AR_4.gemfile +18 -0
- data/gemfiles/AR_edge.gemfile +21 -0
- data/gemfiles/Rails_3.2.gemfile +20 -0
- data/gemfiles/Rails_4.gemfile +19 -0
- data/gemfiles/Sequel.gemfile +17 -0
- data/lib/protector/adapters/active_record/association.rb +8 -0
- data/lib/protector/adapters/active_record/base.rb +5 -5
- data/lib/protector/adapters/active_record/relation.rb +15 -3
- data/lib/protector/adapters/active_record/strong_parameters.rb +1 -0
- data/lib/protector/dsl.rb +1 -1
- data/lib/protector/version.rb +1 -1
- data/spec/lib/protector/adapters/active_record_spec.rb +25 -0
- data/spec/lib/protector/dsl_spec.rb +59 -43
- metadata +8 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8046a17b4ab071f2e5af6511c48a213eb46a8c8a
|
4
|
+
data.tar.gz: 789efaa941d0b0fa7590b2d91e6a7cbfac879975
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bc9c1a541de4183af641b8b1d5f498d27eb3804e284d0682f83bba43653ba86475165468f9099dd48a1ccbff266d33724dae82a1737a9c57df5cdc839b3a923b
|
7
|
+
data.tar.gz: c863b7e9044aa4e0b46776280be869df766a2e631746efb048c5ed62ecbd71ccdad82987f0abadeafeb3a5cdd700f45c8a479cde2c73e5d31b1990cfb2e95b01
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
@@ -2,3 +2,13 @@ rvm:
|
|
2
2
|
- 1.9.3
|
3
3
|
- jruby-19mode
|
4
4
|
- 2.0.0
|
5
|
+
|
6
|
+
gemfile:
|
7
|
+
- gemfiles/AR_3.2.gemfile
|
8
|
+
- gemfiles/AR_4.gemfile
|
9
|
+
- gemfiles/AR_edge.gemfile
|
10
|
+
- gemfiles/Rails_3.2.gemfile
|
11
|
+
- gemfiles/Rails_4.gemfile
|
12
|
+
- gemfiles/Sequel.gemfile
|
13
|
+
|
14
|
+
script: bundle exec rspec
|
data/Appraisals
CHANGED
@@ -4,7 +4,15 @@ appraise "AR_3.2" do
|
|
4
4
|
end
|
5
5
|
|
6
6
|
appraise "AR_4" do
|
7
|
-
gem "activerecord", "4.0
|
7
|
+
gem "activerecord", "4.0", require: "active_record"
|
8
|
+
gem "activerecord-jdbcsqlite3-adapter", platform: :jruby, github: "jruby/activerecord-jdbc-adapter"
|
9
|
+
end
|
10
|
+
|
11
|
+
appraise "AR_edge" do
|
12
|
+
gem "activerecord", require: "active_record", github: "rails/rails"
|
13
|
+
gem "activemodel", github: "rails/rails"
|
14
|
+
gem "activesupport", github: "rails/rails"
|
15
|
+
gem "arel", github: "rails/arel"
|
8
16
|
gem "activerecord-jdbcsqlite3-adapter", platform: :jruby, github: "jruby/activerecord-jdbc-adapter"
|
9
17
|
end
|
10
18
|
|
data/Gemfile
CHANGED
@@ -8,9 +8,9 @@ gem 'rspec'
|
|
8
8
|
gem 'simplecov', require: false
|
9
9
|
gem 'simplecov-summary'
|
10
10
|
|
11
|
-
gem 'appraisal'
|
11
|
+
gem 'appraisal', github: 'thoughtbot/appraisal'
|
12
12
|
|
13
13
|
gem 'sqlite3', platform: :ruby
|
14
14
|
gem 'jdbc-sqlite3', platform: :jruby, require: 'jdbc/sqlite3'
|
15
15
|
|
16
|
-
gem 'ruby-prof', platform: :ruby
|
16
|
+
gem 'ruby-prof', platform: :ruby
|
data/Rakefile
CHANGED
@@ -8,8 +8,8 @@ RSpec::Core::RakeTask.new(:spec)
|
|
8
8
|
task :default => :all
|
9
9
|
|
10
10
|
desc 'Test the plugin under all supported Rails versions.'
|
11
|
-
task :all
|
12
|
-
exec('
|
11
|
+
task :all do |t|
|
12
|
+
exec('bundle exec appraisal install; bundle exec appraisal rspec')
|
13
13
|
end
|
14
14
|
|
15
15
|
task :perf do
|
@@ -23,4 +23,4 @@ task :perf do
|
|
23
23
|
Perf.load a.underscore
|
24
24
|
end
|
25
25
|
end
|
26
|
-
end
|
26
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# This file was generated by Appraisal
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
gem "rake"
|
6
|
+
gem "colored"
|
7
|
+
gem "pry"
|
8
|
+
gem "rspec"
|
9
|
+
gem "simplecov", :require=>false
|
10
|
+
gem "simplecov-summary"
|
11
|
+
gem "appraisal", :github=>"thoughtbot/appraisal"
|
12
|
+
gem "sqlite3", :platform=>:ruby
|
13
|
+
gem "jdbc-sqlite3", :platform=>:jruby, :require=>"jdbc/sqlite3"
|
14
|
+
gem "ruby-prof", :platform=>:ruby
|
15
|
+
gem "activerecord", "3.2.9", :require=>"active_record"
|
16
|
+
gem "activerecord-jdbcsqlite3-adapter", :platform=>:jruby, :github=>"jruby/activerecord-jdbc-adapter"
|
17
|
+
|
18
|
+
gemspec :path=>".././"
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# This file was generated by Appraisal
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
gem "rake"
|
6
|
+
gem "colored"
|
7
|
+
gem "pry"
|
8
|
+
gem "rspec"
|
9
|
+
gem "simplecov", :require=>false
|
10
|
+
gem "simplecov-summary"
|
11
|
+
gem "appraisal", :github=>"thoughtbot/appraisal"
|
12
|
+
gem "sqlite3", :platform=>:ruby
|
13
|
+
gem "jdbc-sqlite3", :platform=>:jruby, :require=>"jdbc/sqlite3"
|
14
|
+
gem "ruby-prof", :platform=>:ruby
|
15
|
+
gem "activerecord", "4.0", :require=>"active_record"
|
16
|
+
gem "activerecord-jdbcsqlite3-adapter", :platform=>:jruby, :github=>"jruby/activerecord-jdbc-adapter"
|
17
|
+
|
18
|
+
gemspec :path=>".././"
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# This file was generated by Appraisal
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
gem "rake"
|
6
|
+
gem "colored"
|
7
|
+
gem "pry"
|
8
|
+
gem "rspec"
|
9
|
+
gem "simplecov", :require=>false
|
10
|
+
gem "simplecov-summary"
|
11
|
+
gem "appraisal", :github=>"thoughtbot/appraisal"
|
12
|
+
gem "sqlite3", :platform=>:ruby
|
13
|
+
gem "jdbc-sqlite3", :platform=>:jruby, :require=>"jdbc/sqlite3"
|
14
|
+
gem "ruby-prof", :platform=>:ruby
|
15
|
+
gem "activerecord", :require=>"active_record", :github=>"rails/rails"
|
16
|
+
gem "activemodel", :github=>"rails/rails"
|
17
|
+
gem "activesupport", :github=>"rails/rails"
|
18
|
+
gem "arel", :github=>"rails/arel"
|
19
|
+
gem "activerecord-jdbcsqlite3-adapter", :platform=>:jruby, :github=>"jruby/activerecord-jdbc-adapter"
|
20
|
+
|
21
|
+
gemspec :path=>".././"
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# This file was generated by Appraisal
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
gem "rake"
|
6
|
+
gem "colored"
|
7
|
+
gem "pry"
|
8
|
+
gem "rspec"
|
9
|
+
gem "simplecov", :require=>false
|
10
|
+
gem "simplecov-summary"
|
11
|
+
gem "appraisal", :github=>"thoughtbot/appraisal"
|
12
|
+
gem "sqlite3", :platform=>:ruby
|
13
|
+
gem "jdbc-sqlite3", :platform=>:jruby, :require=>"jdbc/sqlite3"
|
14
|
+
gem "ruby-prof", :platform=>:ruby
|
15
|
+
gem "combustion", :github=>"pat/combustion", :ref=>"50a946b5a7ab3d9249f0e5fcebbb73488a91b1e5"
|
16
|
+
gem "rails", "3.2.13"
|
17
|
+
gem "strong_parameters"
|
18
|
+
gem "activerecord-jdbcsqlite3-adapter", :platform=>:jruby, :github=>"jruby/activerecord-jdbc-adapter"
|
19
|
+
|
20
|
+
gemspec :path=>".././"
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# This file was generated by Appraisal
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
gem "rake"
|
6
|
+
gem "colored"
|
7
|
+
gem "pry"
|
8
|
+
gem "rspec"
|
9
|
+
gem "simplecov", :require=>false
|
10
|
+
gem "simplecov-summary"
|
11
|
+
gem "appraisal", :github=>"thoughtbot/appraisal"
|
12
|
+
gem "sqlite3", :platform=>:ruby
|
13
|
+
gem "jdbc-sqlite3", :platform=>:jruby, :require=>"jdbc/sqlite3"
|
14
|
+
gem "ruby-prof", :platform=>:ruby
|
15
|
+
gem "combustion", :github=>"pat/combustion", :ref=>"50a946b5a7ab3d9249f0e5fcebbb73488a91b1e5"
|
16
|
+
gem "rails", "4.0.0"
|
17
|
+
gem "activerecord-jdbcsqlite3-adapter", :platform=>:jruby, :github=>"jruby/activerecord-jdbc-adapter"
|
18
|
+
|
19
|
+
gemspec :path=>".././"
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# This file was generated by Appraisal
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
gem "rake"
|
6
|
+
gem "colored"
|
7
|
+
gem "pry"
|
8
|
+
gem "rspec"
|
9
|
+
gem "simplecov", :require=>false
|
10
|
+
gem "simplecov-summary"
|
11
|
+
gem "appraisal", :github=>"thoughtbot/appraisal"
|
12
|
+
gem "sqlite3", :platform=>:ruby
|
13
|
+
gem "jdbc-sqlite3", :platform=>:jruby, :require=>"jdbc/sqlite3"
|
14
|
+
gem "ruby-prof", :platform=>:ruby
|
15
|
+
gem "sequel", "3.30.0"
|
16
|
+
|
17
|
+
gemspec :path=>".././"
|
@@ -13,6 +13,8 @@ module Protector
|
|
13
13
|
alias_method 'scope_without_protector', 'scoped'
|
14
14
|
alias_method 'scoped', 'scope_with_protector'
|
15
15
|
end
|
16
|
+
|
17
|
+
alias_method_chain :build, :protector
|
16
18
|
end
|
17
19
|
|
18
20
|
# Wraps every association with current subject
|
@@ -21,6 +23,12 @@ module Protector
|
|
21
23
|
scope = scope.restrict!(owner.protector_subject) if owner.protector_subject?
|
22
24
|
scope
|
23
25
|
end
|
26
|
+
|
27
|
+
# Forwards protection subject to the new instance
|
28
|
+
def build_with_protector(*args, &block)
|
29
|
+
return build_without_protector(*args, &block) unless owner.protector_subject?
|
30
|
+
build_without_protector(*args, &block).restrict!(owner.protector_subject)
|
31
|
+
end
|
24
32
|
end
|
25
33
|
end
|
26
34
|
end
|
@@ -17,12 +17,12 @@ module Protector
|
|
17
17
|
end
|
18
18
|
|
19
19
|
validate do
|
20
|
-
|
20
|
+
if protector_subject?
|
21
|
+
method = new_record? ? :first_uncreatable_field : :first_unupdatable_field
|
22
|
+
field = protector_meta.send(method, protector_changed)
|
21
23
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
errors[:base] << I18n.t('protector.invalid', field: field) if field
|
24
|
+
errors[:base] << I18n.t('protector.invalid', field: field) if field
|
25
|
+
end
|
26
26
|
end
|
27
27
|
|
28
28
|
before_destroy do
|
@@ -42,7 +42,9 @@ module Protector
|
|
42
42
|
end
|
43
43
|
|
44
44
|
def protector_relation
|
45
|
-
|
45
|
+
result = self.clone
|
46
|
+
result = protector_meta.eval_scope_procs(result) if protector_meta.relation
|
47
|
+
result
|
46
48
|
end
|
47
49
|
|
48
50
|
# @note Unscoped relation drops properties and therefore should be re-restricted
|
@@ -92,7 +94,13 @@ module Protector
|
|
92
94
|
Protector::ActiveRecord::StrongParameters.sanitize! args, true, protector_meta
|
93
95
|
end
|
94
96
|
|
95
|
-
|
97
|
+
unless block_given?
|
98
|
+
new_without_protector(*args).restrict!(protector_subject)
|
99
|
+
else
|
100
|
+
new_without_protector(*args) do |instance|
|
101
|
+
block.call instance.restrict!(protector_subject)
|
102
|
+
end
|
103
|
+
end
|
96
104
|
end
|
97
105
|
|
98
106
|
def create_with_protector(*args, &block)
|
@@ -136,7 +144,11 @@ module Protector
|
|
136
144
|
|
137
145
|
# Now we have @records restricted properly so let's preload associations!
|
138
146
|
associations.each do |association|
|
139
|
-
::ActiveRecord::Associations::Preloader.
|
147
|
+
if ::ActiveRecord::Associations::Preloader.method_defined? :preload
|
148
|
+
::ActiveRecord::Associations::Preloader.new.preload(@records, association)
|
149
|
+
else
|
150
|
+
::ActiveRecord::Associations::Preloader.new(@records, association).run
|
151
|
+
end
|
140
152
|
end
|
141
153
|
|
142
154
|
@loaded = true
|
data/lib/protector/dsl.rb
CHANGED
data/lib/protector/version.rb
CHANGED
@@ -57,6 +57,7 @@ if defined?(ActiveRecord)
|
|
57
57
|
describe Protector::Adapters::ActiveRecord::Base do
|
58
58
|
let(:dummy) do
|
59
59
|
Class.new(ActiveRecord::Base) do
|
60
|
+
def self.name; 'Dummy'; end
|
60
61
|
def self.model_name; ActiveModel::Name.new(self, nil, "dummy"); end
|
61
62
|
self.table_name = "dummies"
|
62
63
|
scope :none, where('1 = 0') unless respond_to?(:none)
|
@@ -92,6 +93,29 @@ if defined?(ActiveRecord)
|
|
92
93
|
|
93
94
|
expect { dummy.restrict!('!').create!(string: 'test').delete }.to raise_error
|
94
95
|
end
|
96
|
+
|
97
|
+
it "validates on new{}" do
|
98
|
+
dummy.instance_eval do
|
99
|
+
protect do; end
|
100
|
+
end
|
101
|
+
|
102
|
+
result = dummy.restrict!('!').new do |instance|
|
103
|
+
instance.protector_subject.should == '!'
|
104
|
+
end
|
105
|
+
|
106
|
+
result.protector_subject.should == '!'
|
107
|
+
end
|
108
|
+
|
109
|
+
it "finds with scope on id column" do
|
110
|
+
dummy.instance_eval do
|
111
|
+
protect do
|
112
|
+
scope { where(id: 1) }
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
expect { dummy.restrict!('!').find(1) }.to_not raise_error
|
117
|
+
expect { dummy.restrict!('!').find(2) }.to raise_error
|
118
|
+
end
|
95
119
|
end
|
96
120
|
|
97
121
|
#
|
@@ -112,6 +136,7 @@ if defined?(ActiveRecord)
|
|
112
136
|
Dummy.restrict!('!').where(number: 999).first.protector_subject.should == '!'
|
113
137
|
Dummy.restrict!('!').where(number: 999).to_a.first.protector_subject.should == '!'
|
114
138
|
Dummy.restrict!('!').new.protector_subject.should == '!'
|
139
|
+
Dummy.restrict!('!').first.fluffies.new.protector_subject.should == '!'
|
115
140
|
end
|
116
141
|
|
117
142
|
it "checks creatability" do
|
@@ -80,7 +80,7 @@ describe Protector::DSL do
|
|
80
80
|
|
81
81
|
describe Protector::DSL::Meta do
|
82
82
|
context "basic methods" do
|
83
|
-
l = lambda {|x| x > 4
|
83
|
+
l = lambda {|x| x > 4}
|
84
84
|
|
85
85
|
before :each do
|
86
86
|
@meta = Protector::DSL::Meta.new(nil, nil){%w(field1 field2 field3 field4 field5)}
|
@@ -129,55 +129,59 @@ describe Protector::DSL do
|
|
129
129
|
end
|
130
130
|
end
|
131
131
|
|
132
|
-
|
133
|
-
data
|
134
|
-
|
135
|
-
|
132
|
+
context "evaluated" do
|
133
|
+
let(:data) { @meta.evaluate('user', 'entry') }
|
134
|
+
|
135
|
+
it "sets relation" do
|
136
|
+
data.relation.should == 'relation'
|
137
|
+
end
|
136
138
|
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
139
|
+
it "sets access" do
|
140
|
+
data.access.should == {
|
141
|
+
update: {
|
142
|
+
"field1" => nil,
|
143
|
+
"field2" => nil,
|
144
|
+
"field3" => 1,
|
145
|
+
"field4" => 0..5,
|
146
|
+
"field5" => l
|
147
|
+
},
|
148
|
+
read: {
|
149
|
+
"field1" => nil,
|
150
|
+
"field2" => nil,
|
151
|
+
"field3" => nil
|
152
|
+
}
|
151
153
|
}
|
152
|
-
|
153
|
-
end
|
154
|
+
end
|
154
155
|
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
end
|
156
|
+
it "marks destroyable" do
|
157
|
+
data.destroyable?.should == true
|
158
|
+
data.can?(:destroy).should == true
|
159
|
+
end
|
160
160
|
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
161
|
+
context "marks updatable" do
|
162
|
+
it "with defaults" do
|
163
|
+
data.updatable?.should == true
|
164
|
+
data.can?(:update).should == true
|
165
|
+
end
|
166
166
|
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
167
|
+
it "respecting lambda", dev: true do
|
168
|
+
data.updatable?('field5' => 5).should == true
|
169
|
+
data.updatable?('field5' => 3).should == false
|
170
|
+
end
|
171
|
+
end
|
171
172
|
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
173
|
+
it "gets first unupdatable field" do
|
174
|
+
data.first_unupdatable_field('field1' => 1, 'field6' => 2, 'field7' => 3).should == 'field6'
|
175
|
+
end
|
176
|
+
|
177
|
+
it "marks creatable" do
|
178
|
+
data.creatable?.should == false
|
179
|
+
data.can?(:create).should == false
|
180
|
+
end
|
177
181
|
|
178
|
-
|
179
|
-
|
180
|
-
|
182
|
+
it "gets first uncreatable field" do
|
183
|
+
data.first_uncreatable_field('field1' => 1, 'field6' => 2).should == 'field1'
|
184
|
+
end
|
181
185
|
end
|
182
186
|
end
|
183
187
|
|
@@ -222,5 +226,17 @@ describe Protector::DSL do
|
|
222
226
|
box.can?(:eat).should == true
|
223
227
|
end
|
224
228
|
end
|
229
|
+
|
230
|
+
it "avoids lambdas recursion" do
|
231
|
+
base = Class.new{ include Protector::DSL::Base }
|
232
|
+
meta = Protector::DSL::Meta.new(nil, nil){%w(field1)}
|
233
|
+
|
234
|
+
meta << lambda {
|
235
|
+
can :create, field1: lambda {|x| x.protector_subject?.should == false}
|
236
|
+
}
|
237
|
+
|
238
|
+
box = meta.evaluate('context', 'instance')
|
239
|
+
box.creatable?('field1' => base.new.restrict!(nil))
|
240
|
+
end
|
225
241
|
end
|
226
242
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: protector
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.7.
|
4
|
+
version: 0.7.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Boris Staal
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-12-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -56,6 +56,12 @@ files:
|
|
56
56
|
- LICENSE.txt
|
57
57
|
- README.md
|
58
58
|
- Rakefile
|
59
|
+
- gemfiles/AR_3.2.gemfile
|
60
|
+
- gemfiles/AR_4.gemfile
|
61
|
+
- gemfiles/AR_edge.gemfile
|
62
|
+
- gemfiles/Rails_3.2.gemfile
|
63
|
+
- gemfiles/Rails_4.gemfile
|
64
|
+
- gemfiles/Sequel.gemfile
|
59
65
|
- lib/protector.rb
|
60
66
|
- lib/protector/adapters/active_record.rb
|
61
67
|
- lib/protector/adapters/active_record/association.rb
|