protector 0.3.2 → 0.3.3

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: 548944803f26efdf8c5d027636507a7cde1ae2af
4
- data.tar.gz: 47db9641369c7abd7e94a77c42965a7f21d4bfe3
3
+ metadata.gz: 96fab27725783cc22165b845a9c4064f116dbe35
4
+ data.tar.gz: 7a8dbdbffefcb27380c5c9ff0972a9a2704c1be6
5
5
  SHA512:
6
- metadata.gz: d72997d51f020a5fb084f946d217e48cffb94e3e52d56ebcfdcae2f8aef1d66371c5fdbb3c9710d4beec6475a025c7f56cf1d89a23eae504f8ad7368ea64a5dd
7
- data.tar.gz: 63d81d5f539f4159cce19aafdd1f2e32e90e6b8ead2ca051a15644ebcd75ac2d316dcd6f0979f37635dec4dbc0d18cf89ab5ec88578c63ed185e675c7088ded6
6
+ metadata.gz: 82c3d96e96ad45313add53cb1ef48fb7fa3df78bac3a8566c820a2f3db1a7bbe63fa216685780cf813ad80fd9678d583acd921a8a8a50f56253d2685241ae32c
7
+ data.tar.gz: df173a15749af23f7be5e38cf163aa613f42e8750ae2e9fe61a423b3deec66676e9c13c42043775c4539e53572f81f9ce0211fd1fcb2a5517179feed6631ec93
data/README.md CHANGED
@@ -15,7 +15,12 @@ We are working hard to extend the list with:
15
15
 
16
16
  * [Mongoid](http://mongoid.org/en/mongoid/index.html)
17
17
  * [ROM](https://github.com/rom-rb/rom)
18
- * [DataMapper](http://datamapper.org/) (its undead so it might be skipped)
18
+
19
+ ## Compatibility
20
+
21
+ Protector is an extension and therefore hides deeply inside your ORM library making itself compatible to the most gems you use. Sometimes however, you might need additional integration to take the best from it:
22
+
23
+ * [Protector::SimpleForm](https://github.com/deversus/protector-simple_form)
19
24
 
20
25
  ## Basics
21
26
 
@@ -55,13 +60,15 @@ class Article < ActiveRecord::Base # Fields: title, text, user_id, hidd
55
60
  end
56
61
  ```
57
62
 
63
+ Inside your model, you can have several `protect` calls that will get merged. Using this you can move basic rules to a separate module to keep code DRY.
64
+
58
65
  Now that we have ACL described we can enable it as easy as:
59
66
 
60
67
  ```ruby
61
68
  article.restrict!(current_user) # Assuming article is an instance of Article
62
69
  ```
63
70
 
64
- Now if `current_user` is a guest we will get `nil` from `article.text`. At the same time we will get validation error if we pass any fields but title, text and user_id (equal to our own id) on creation.
71
+ If `current_user` is a guest we will get `nil` from `article.text`. At the same time we will get validation error if we pass any fields but title, text and user_id (equal to our own id) on creation.
65
72
 
66
73
  To make model unsafe again call:
67
74
 
@@ -196,14 +203,10 @@ Protector::Adapters::ActiveRecord.activate!
196
203
 
197
204
  Where "ActiveRecord" is the adapter you are about to use. It can be "Sequel", "DataMapper", "Mongoid".
198
205
 
199
- ## Contributing
206
+ ## Maintainers
200
207
 
201
- 1. Fork it
202
- 2. Create your feature branch (`git checkout -b my-new-feature`)
203
- 3. Commit your changes (`git commit -am 'Add some feature'`)
204
- 4. Push to the branch (`git push origin my-new-feature`)
205
- 5. Create new Pull Request
208
+ * Boris Staal, [@inossidabile](http://staal.io)
206
209
 
207
- ## LICENSE
210
+ ## License
208
211
 
209
212
  It is free software, and may be redistributed under the terms of MIT license.
@@ -10,7 +10,7 @@ GIT
10
10
  PATH
11
11
  remote: /Users/inossidabile/Repos/protector
12
12
  specs:
13
- protector (0.3.2)
13
+ protector (0.3.3)
14
14
  activesupport
15
15
  i18n
16
16
 
@@ -6,7 +6,7 @@ GIT
6
6
  PATH
7
7
  remote: /Users/inossidabile/Repos/protector
8
8
  specs:
9
- protector (0.3.2)
9
+ protector (0.3.3)
10
10
  activesupport
11
11
  i18n
12
12
 
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: /Users/inossidabile/Repos/protector
3
3
  specs:
4
- protector (0.3.2)
4
+ protector (0.3.3)
5
5
  activesupport
6
6
  i18n
7
7
 
@@ -31,7 +31,7 @@ module Protector
31
31
  super
32
32
  end
33
33
 
34
- if Gem::Version.new(::ActiveRecord::VERSION::STRING) < Gem::Version.new('4.0.0')
34
+ unless Protector::Adapters::ActiveRecord.modern?
35
35
  def self.restrict!(subject)
36
36
  scoped.restrict!(subject)
37
37
  end
@@ -114,7 +114,7 @@ module Protector
114
114
 
115
115
  # AR 4 has awfull inconsistency when it comes to method `all`
116
116
  # We have to mimic base class behaviour for relation we get from `unscoped`
117
- if Gem::Version.new(::ActiveRecord::VERSION::STRING) >= Gem::Version.new('4.0.0')
117
+ if Protector::Adapters::ActiveRecord.modern?
118
118
  class <<unscoped
119
119
  def all
120
120
  self
@@ -9,6 +9,8 @@ module Protector
9
9
  module ActiveRecord
10
10
  # YIP YIP! Monkey-Patch the ActiveRecord.
11
11
  def self.activate!
12
+ return false unless defined?(::ActiveRecord)
13
+
12
14
  ::ActiveRecord::Base.send :include, Protector::Adapters::ActiveRecord::Base
13
15
  ::ActiveRecord::Relation.send :include, Protector::Adapters::ActiveRecord::Relation
14
16
  ::ActiveRecord::Associations::SingularAssociation.send :include, Protector::Adapters::ActiveRecord::Association
@@ -16,6 +18,23 @@ module Protector
16
18
  ::ActiveRecord::Associations::Preloader.send :include, Protector::Adapters::ActiveRecord::Preloader
17
19
  ::ActiveRecord::Associations::Preloader::Association.send :include, Protector::Adapters::ActiveRecord::Preloader::Association
18
20
  end
21
+
22
+ def self.modern?
23
+ Gem::Version.new(::ActiveRecord::VERSION::STRING) >= Gem::Version.new('4.0.0')
24
+ end
25
+
26
+ def self.is?(instance)
27
+ instance.is_a?(::ActiveRecord::Relation) ||
28
+ (instance.is_a?(Class) && instance < ActiveRecord::Base)
29
+ end
30
+
31
+ def self.nullify(relation)
32
+ if modern?
33
+ relation.none
34
+ else
35
+ relation.where("1=0")
36
+ end
37
+ end
19
38
  end
20
39
  end
21
40
  end
@@ -91,11 +91,13 @@ module Protector
91
91
 
92
92
  # This is used whenever we fetch data
93
93
  def _associated_dataset(*args)
94
+ return super unless protector_subject?
94
95
  super.restrict!(protector_subject)
95
96
  end
96
97
 
97
98
  # This is used whenever we call counters and existance checkers
98
99
  def _dataset(*args)
100
+ return super unless protector_subject?
99
101
  super.restrict!(protector_subject)
100
102
  end
101
103
  end
@@ -8,10 +8,21 @@ module Protector
8
8
  module Sequel
9
9
  # YIP YIP! Monkey-Patch the Sequel.
10
10
  def self.activate!
11
+ return false unless defined?(::Sequel)
12
+
11
13
  ::Sequel::Model.send :include, Protector::Adapters::Sequel::Model
12
14
  ::Sequel::Dataset.send :include, Protector::Adapters::Sequel::Dataset
13
15
  ::Sequel::Model::Associations::EagerGraphLoader.send :include, Protector::Adapters::Sequel::EagerGraphLoader
14
16
  end
17
+
18
+ def self.is?(instance)
19
+ instance.kind_of?(::Sequel::Dataset) ||
20
+ (instance.kind_of?(Class) && instance < ::Sequel::Model)
21
+ end
22
+
23
+ def self.nullify(relation)
24
+ relation.where("1=0")
25
+ end
15
26
  end
16
27
  end
17
28
  end
data/lib/protector/dsl.rb CHANGED
@@ -61,7 +61,11 @@ module Protector
61
61
  def relation
62
62
  return false unless scoped?
63
63
 
64
- @relation ||= @model.instance_eval(&@scope_proc)
64
+ unless @relation
65
+ @relation = @model.instance_eval(&@scope_proc)
66
+ end
67
+
68
+ @relation
65
69
  end
66
70
 
67
71
  # Enables action for given fields.
@@ -1,4 +1,4 @@
1
1
  module Protector
2
2
  # Gem version
3
- VERSION = "0.3.2"
3
+ VERSION = "0.3.3"
4
4
  end
data/lib/protector.rb CHANGED
@@ -8,16 +8,27 @@ require "protector/adapters/sequel"
8
8
 
9
9
  I18n.load_path += Dir[File.expand_path File.join('..', 'locales', '*.yml'), File.dirname(__FILE__)]
10
10
 
11
- Protector::Adapters::ActiveRecord.activate! if defined?(ActiveRecord)
12
- Protector::Adapters::Sequel.activate! if defined?(Sequel)
13
-
14
11
  module Protector
12
+ class << self
13
+ ADAPTERS = [
14
+ Protector::Adapters::ActiveRecord,
15
+ Protector::Adapters::Sequel
16
+ ]
17
+
18
+ attr_accessor :paranoid
15
19
 
16
- # Allows executing any code having Protector globally disabled
17
- def self.insecurely(&block)
18
- Thread.current[:protector_disabled] = true
19
- yield
20
- ensure
21
- Thread.current[:protector_disabled] = false
20
+ # Allows executing any code having Protector globally disabled
21
+ def insecurely(&block)
22
+ Thread.current[:protector_disabled] = true
23
+ yield
24
+ ensure
25
+ Thread.current[:protector_disabled] = false
26
+ end
27
+
28
+ def activate!
29
+ ADAPTERS.each{|adapter| adapter.activate!}
30
+ end
22
31
  end
23
- end
32
+ end
33
+
34
+ Protector.activate!
@@ -37,6 +37,13 @@ if defined?(ActiveRecord)
37
37
  Fluffy.all.each{|f| Loony.create! fluffy_id: f.id, string: 'zomgstring' }
38
38
  end
39
39
 
40
+ describe Protector::Adapters::ActiveRecord do
41
+ it "finds out whether object is AR relation" do
42
+ Protector::Adapters::ActiveRecord.is?(Dummy).should == true
43
+ Protector::Adapters::ActiveRecord.is?(Dummy.every).should == true
44
+ end
45
+ end
46
+
40
47
  #
41
48
  # Model instance
42
49
  #
@@ -37,6 +37,14 @@ if defined?(Sequel)
37
37
  Fluffy.all.each{|f| Loony.create fluffy_id: f.id, string: 'zomgstring' }
38
38
  end
39
39
 
40
+ describe Protector::Adapters::Sequel do
41
+ it "finds out whether object is Sequel relation" do
42
+ Protector::Adapters::Sequel.is?(Dummy).should == true
43
+ Protector::Adapters::Sequel.is?(Dummy.where).should == true
44
+ end
45
+ end
46
+
47
+
40
48
  #
41
49
  # Model instance
42
50
  #
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.3.2
4
+ version: 0.3.3
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-07-06 00:00:00.000000000 Z
11
+ date: 2013-07-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport