cancancan 1.17.0 → 2.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/cancancan.gemspec +4 -6
  3. data/lib/cancan.rb +2 -11
  4. data/lib/cancan/ability.rb +1 -1
  5. data/lib/cancan/controller_additions.rb +3 -24
  6. data/lib/cancan/controller_resource.rb +9 -25
  7. data/lib/cancan/matchers.rb +7 -1
  8. data/lib/cancan/model_adapters/active_record_4_adapter.rb +1 -3
  9. data/lib/cancan/model_adapters/active_record_adapter.rb +2 -2
  10. data/lib/cancan/rule.rb +2 -2
  11. data/lib/cancan/version.rb +1 -1
  12. metadata +22 -69
  13. data/.gitignore +0 -15
  14. data/.rspec +0 -1
  15. data/.rubocop.yml +0 -39
  16. data/.rubocop_todo.yml +0 -54
  17. data/.travis.yml +0 -39
  18. data/Appraisals +0 -105
  19. data/CHANGELOG.rdoc +0 -536
  20. data/CONTRIBUTING.md +0 -23
  21. data/Gemfile +0 -3
  22. data/LICENSE +0 -22
  23. data/README.md +0 -234
  24. data/Rakefile +0 -13
  25. data/gemfiles/activerecord_3.2.gemfile +0 -18
  26. data/gemfiles/activerecord_4.0.gemfile +0 -19
  27. data/gemfiles/activerecord_4.1.gemfile +0 -19
  28. data/gemfiles/activerecord_4.2.gemfile +0 -21
  29. data/gemfiles/activerecord_5.0.gemfile +0 -20
  30. data/gemfiles/mongoid_2.x.gemfile +0 -18
  31. data/gemfiles/sequel_3.x.gemfile +0 -18
  32. data/lib/cancan/inherited_resource.rb +0 -20
  33. data/lib/cancan/model_adapters/active_record_3_adapter.rb +0 -16
  34. data/lib/cancan/model_adapters/mongoid_adapter.rb +0 -80
  35. data/lib/cancan/model_adapters/sequel_adapter.rb +0 -87
  36. data/spec/README.rdoc +0 -27
  37. data/spec/cancan/ability_spec.rb +0 -553
  38. data/spec/cancan/controller_additions_spec.rb +0 -164
  39. data/spec/cancan/controller_resource_spec.rb +0 -645
  40. data/spec/cancan/exceptions_spec.rb +0 -58
  41. data/spec/cancan/inherited_resource_spec.rb +0 -71
  42. data/spec/cancan/matchers_spec.rb +0 -29
  43. data/spec/cancan/model_adapters/active_record_4_adapter_spec.rb +0 -160
  44. data/spec/cancan/model_adapters/active_record_adapter_spec.rb +0 -415
  45. data/spec/cancan/model_adapters/default_adapter_spec.rb +0 -7
  46. data/spec/cancan/model_adapters/mongoid_adapter_spec.rb +0 -246
  47. data/spec/cancan/model_adapters/sequel_adapter_spec.rb +0 -129
  48. data/spec/cancan/rule_spec.rb +0 -52
  49. data/spec/matchers.rb +0 -13
  50. data/spec/spec.opts +0 -2
  51. data/spec/spec_helper.rb +0 -27
  52. data/spec/support/ability.rb +0 -6
@@ -1,7 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe CanCan::ModelAdapters::DefaultAdapter do
4
- it 'is default for generic classes' do
5
- expect(CanCan::ModelAdapters::AbstractAdapter.adapter_class(Object)).to eq(CanCan::ModelAdapters::DefaultAdapter)
6
- end
7
- end
@@ -1,246 +0,0 @@
1
- require 'spec_helper'
2
-
3
- if defined? CanCan::ModelAdapters::MongoidAdapter
4
-
5
- class MongoidCategory
6
- include Mongoid::Document
7
-
8
- references_many :mongoid_projects
9
- end
10
-
11
- class MongoidProject
12
- include Mongoid::Document
13
-
14
- referenced_in :mongoid_category
15
- references_many :mongoid_sub_projects
16
- end
17
-
18
- class MongoidSubProject
19
- include Mongoid::Document
20
-
21
- referenced_in :mongoid_project
22
- end
23
-
24
- Mongoid.configure do |config|
25
- config.master = Mongo::Connection.new('127.0.0.1', 27_017).db('cancan_mongoid_spec')
26
- end
27
-
28
- describe CanCan::ModelAdapters::MongoidAdapter do
29
- context 'Mongoid defined' do
30
- before(:each) do
31
- (@ability = double).extend(CanCan::Ability)
32
- end
33
-
34
- after(:each) do
35
- Mongoid.master.collections.select do |collection|
36
- collection.name !~ /system/
37
- end.each(&:drop)
38
- end
39
-
40
- it 'is for only Mongoid classes' do
41
- expect(CanCan::ModelAdapters::MongoidAdapter).not_to be_for_class(Object)
42
- expect(CanCan::ModelAdapters::MongoidAdapter).to be_for_class(MongoidProject)
43
- expect(CanCan::ModelAdapters::AbstractAdapter.adapter_class(MongoidProject))
44
- .to eq(CanCan::ModelAdapters::MongoidAdapter)
45
- end
46
-
47
- it 'finds record' do
48
- project = MongoidProject.create
49
- expect(CanCan::ModelAdapters::MongoidAdapter.find(MongoidProject, project.id)).to eq(project)
50
- end
51
-
52
- it 'compares properties on mongoid documents with the conditions hash' do
53
- model = MongoidProject.new
54
- @ability.can :read, MongoidProject, id: model.id
55
- expect(@ability).to be_able_to(:read, model)
56
- end
57
-
58
- it 'is able to read hashes when field is array' do
59
- one_to_three = MongoidProject.create(numbers: %w(one two three))
60
- two_to_five = MongoidProject.create(numbers: %w(two three four five))
61
-
62
- @ability.can :foo, MongoidProject, numbers: 'one'
63
- expect(@ability).to be_able_to(:foo, one_to_three)
64
- expect(@ability).not_to be_able_to(:foo, two_to_five)
65
- end
66
-
67
- it 'returns [] when no ability is defined so no records are found' do
68
- MongoidProject.create(title: 'Sir')
69
- MongoidProject.create(title: 'Lord')
70
- MongoidProject.create(title: 'Dude')
71
-
72
- expect(MongoidProject.accessible_by(@ability, :read).entries).to eq([])
73
- end
74
-
75
- it 'returns the correct records based on the defined ability' do
76
- @ability.can :read, MongoidProject, title: 'Sir'
77
- sir = MongoidProject.create(title: 'Sir')
78
- MongoidProject.create(title: 'Lord')
79
- MongoidProject.create(title: 'Dude')
80
-
81
- expect(MongoidProject.accessible_by(@ability, :read).entries).to eq([sir])
82
- end
83
-
84
- it 'returns the correct records when a mix of can and cannot rules in defined ability' do
85
- @ability.can :manage, MongoidProject, title: 'Sir'
86
- @ability.cannot :destroy, MongoidProject
87
-
88
- sir = MongoidProject.create(title: 'Sir')
89
- MongoidProject.create(title: 'Lord')
90
- MongoidProject.create(title: 'Dude')
91
-
92
- expect(MongoidProject.accessible_by(@ability, :destroy).entries).to eq([sir])
93
- end
94
-
95
- it 'is able to mix empty conditions and hashes' do
96
- @ability.can :read, MongoidProject
97
- @ability.can :read, MongoidProject, title: 'Sir'
98
- MongoidProject.create(title: 'Sir')
99
- MongoidProject.create(title: 'Lord')
100
-
101
- expect(MongoidProject.accessible_by(@ability, :read).count).to eq(2)
102
- end
103
-
104
- it 'returns everything when the defined ability is access all' do
105
- @ability.can :manage, :all
106
- sir = MongoidProject.create(title: 'Sir')
107
- lord = MongoidProject.create(title: 'Lord')
108
- dude = MongoidProject.create(title: 'Dude')
109
-
110
- expect(MongoidProject.accessible_by(@ability, :read).entries).to eq([sir, lord, dude])
111
- end
112
-
113
- it 'allows a scope for conditions' do
114
- @ability.can :read, MongoidProject, MongoidProject.where(title: 'Sir')
115
- sir = MongoidProject.create(title: 'Sir')
116
- MongoidProject.create(title: 'Lord')
117
- MongoidProject.create(title: 'Dude')
118
-
119
- expect(MongoidProject.accessible_by(@ability, :read).entries).to eq([sir])
120
- end
121
-
122
- describe 'Mongoid::Criteria where clause Symbol extensions using MongoDB expressions' do
123
- it 'handles :field.in' do
124
- obj = MongoidProject.create(title: 'Sir')
125
- @ability.can :read, MongoidProject, :title.in => %w(Sir Madam)
126
- expect(@ability.can?(:read, obj)).to eq(true)
127
- expect(MongoidProject.accessible_by(@ability, :read)).to eq([obj])
128
-
129
- obj2 = MongoidProject.create(title: 'Lord')
130
- expect(@ability.can?(:read, obj2)).to be(false)
131
- end
132
-
133
- describe 'activates only when there are Criteria in the hash' do
134
- it 'Calls where on the model class when there are criteria' do
135
- obj = MongoidProject.create(title: 'Bird')
136
- @conditions = { :title.nin => %w(Fork Spoon) }
137
-
138
- @ability.can :read, MongoidProject, @conditions
139
- expect(@ability).to be_able_to(:read, obj)
140
- end
141
- it 'Calls the base version if there are no mongoid criteria' do
142
- obj = MongoidProject.new(title: 'Bird')
143
- @conditions = { id: obj.id }
144
- @ability.can :read, MongoidProject, @conditions
145
- expect(@ability).to be_able_to(:read, obj)
146
- end
147
- end
148
-
149
- it 'handles :field.nin' do
150
- obj = MongoidProject.create(title: 'Sir')
151
- @ability.can :read, MongoidProject, :title.nin => %w(Lord Madam)
152
- expect(@ability.can?(:read, obj)).to eq(true)
153
- expect(MongoidProject.accessible_by(@ability, :read)).to eq([obj])
154
-
155
- obj2 = MongoidProject.create(title: 'Lord')
156
- expect(@ability.can?(:read, obj2)).to be(false)
157
- end
158
-
159
- it 'handles :field.size' do
160
- obj = MongoidProject.create(titles: %w(Palatin Margrave))
161
- @ability.can :read, MongoidProject, :titles.size => 2
162
- expect(@ability.can?(:read, obj)).to eq(true)
163
- expect(MongoidProject.accessible_by(@ability, :read)).to eq([obj])
164
-
165
- obj2 = MongoidProject.create(titles: %w(Palatin Margrave Marquis))
166
- expect(@ability.can?(:read, obj2)).to be(false)
167
- end
168
-
169
- it 'handles :field.exists' do
170
- obj = MongoidProject.create(titles: %w(Palatin Margrave))
171
- @ability.can :read, MongoidProject, :titles.exists => true
172
- expect(@ability.can?(:read, obj)).to eq(true)
173
- expect(MongoidProject.accessible_by(@ability, :read)).to eq([obj])
174
-
175
- obj2 = MongoidProject.create
176
- expect(@ability.can?(:read, obj2)).to be(false)
177
- end
178
-
179
- it 'handles :field.gt' do
180
- obj = MongoidProject.create(age: 50)
181
- @ability.can :read, MongoidProject, :age.gt => 45
182
- expect(@ability.can?(:read, obj)).to eq(true)
183
- expect(MongoidProject.accessible_by(@ability, :read)).to eq([obj])
184
-
185
- obj2 = MongoidProject.create(age: 40)
186
- expect(@ability.can?(:read, obj2)).to be(false)
187
- end
188
-
189
- it 'handles instance not saved to database' do
190
- obj = MongoidProject.new(title: 'Sir')
191
- @ability.can :read, MongoidProject, :title.in => %w(Sir Madam)
192
- expect(@ability.can?(:read, obj)).to eq(true)
193
-
194
- # accessible_by only returns saved records
195
- expect(MongoidProject.accessible_by(@ability, :read).entries).to eq([])
196
-
197
- obj2 = MongoidProject.new(title: 'Lord')
198
- expect(@ability.can?(:read, obj2)).to be(false)
199
- end
200
- end
201
-
202
- it 'calls where with matching ability conditions' do
203
- obj = MongoidProject.create(foo: { bar: 1 })
204
- @ability.can :read, MongoidProject, foo: { bar: 1 }
205
- expect(MongoidProject.accessible_by(@ability, :read).entries.first).to eq(obj)
206
- end
207
-
208
- it 'excludes from the result if set to cannot' do
209
- obj = MongoidProject.create(bar: 1)
210
- MongoidProject.create(bar: 2)
211
- @ability.can :read, MongoidProject
212
- @ability.cannot :read, MongoidProject, bar: 2
213
- expect(MongoidProject.accessible_by(@ability, :read).entries).to eq([obj])
214
- end
215
-
216
- it 'combines the rules' do
217
- obj = MongoidProject.create(bar: 1)
218
- obj2 = MongoidProject.create(bar: 2)
219
- MongoidProject.create(bar: 3)
220
- @ability.can :read, MongoidProject, bar: 1
221
- @ability.can :read, MongoidProject, bar: 2
222
- expect(MongoidProject.accessible_by(@ability, :read).entries).to match_array([obj, obj2])
223
- end
224
-
225
- it 'does not allow to fetch records when ability with just block present' do
226
- @ability.can :read, MongoidProject do
227
- false
228
- end
229
- expect do
230
- MongoidProject.accessible_by(@ability)
231
- end.to raise_error(CanCan::Error)
232
- end
233
-
234
- it 'can handle nested queries for accessible_by' do
235
- @ability.can :read, MongoidSubProject, mongoid_project: { mongoid_category: { name: 'Authorization' } }
236
- cat1 = MongoidCategory.create name: 'Authentication'
237
- cat2 = MongoidCategory.create name: 'Authorization'
238
- proj1 = cat1.mongoid_projects.create name: 'Proj1'
239
- proj2 = cat2.mongoid_projects.create name: 'Proj2'
240
- sub1 = proj1.mongoid_sub_projects.create name: 'Sub1'
241
- proj2.mongoid_sub_projects.create name: 'Sub2'
242
- expect(MongoidSubProject.accessible_by(@ability)).to match_array([sub1])
243
- end
244
- end
245
- end
246
- end
@@ -1,129 +0,0 @@
1
- require 'spec_helper'
2
-
3
- if defined? CanCan::ModelAdapters::SequelAdapter
4
- describe CanCan::ModelAdapters::SequelAdapter do
5
- DB = if RUBY_PLATFORM == 'java'
6
- Sequel.connect('jdbc:sqlite:db.sqlite3')
7
- else
8
- Sequel.sqlite
9
- end
10
-
11
- DB.create_table :users do
12
- primary_key :id
13
- String :name
14
- end
15
-
16
- class User < Sequel::Model
17
- one_to_many :articles
18
- end
19
-
20
- DB.create_table :articles do
21
- primary_key :id
22
- String :name
23
- TrueClass :published
24
- TrueClass :secret
25
- Integer :priority
26
- foreign_key :user_id, :users
27
- end
28
-
29
- class Article < Sequel::Model
30
- many_to_one :user
31
- one_to_many :comments
32
- end
33
-
34
- DB.create_table :comments do
35
- primary_key :id
36
- TrueClass :spam
37
- foreign_key :article_id, :articles
38
- end
39
-
40
- class Comment < Sequel::Model
41
- many_to_one :article
42
- end
43
-
44
- before(:each) do
45
- Comment.dataset.delete
46
- Article.dataset.delete
47
- User.dataset.delete
48
- (@ability = double).extend(CanCan::Ability)
49
- end
50
-
51
- it 'should be for only sequel model classes' do
52
- expect(CanCan::ModelAdapters::SequelAdapter).to_not be_for_class(Object)
53
- expect(CanCan::ModelAdapters::SequelAdapter).to be_for_class(Article)
54
- expect(CanCan::ModelAdapters::AbstractAdapter.adapter_class(Article)).to eq CanCan::ModelAdapters::SequelAdapter
55
- end
56
-
57
- it 'should find record' do
58
- article = Article.create
59
- expect(CanCan::ModelAdapters::SequelAdapter.find(Article, article.id)).to eq article
60
- end
61
-
62
- it 'should not fetch any records when no abilities are defined' do
63
- Article.create
64
- expect(Article.accessible_by(@ability).all).to be_empty
65
- end
66
-
67
- it 'should fetch all articles when one can read all' do
68
- @ability.can :read, Article
69
- article = Article.create
70
- expect(Article.accessible_by(@ability).all).to eq [article]
71
- end
72
-
73
- it 'should fetch only the articles that are published' do
74
- @ability.can :read, Article, published: true
75
- article1 = Article.create(published: true)
76
- Article.create(published: false)
77
- expect(Article.accessible_by(@ability).all).to eq [article1]
78
- end
79
-
80
- it 'should fetch any articles which are published or secret' do
81
- @ability.can :read, Article, published: true
82
- @ability.can :read, Article, secret: true
83
- article1 = Article.create(published: true, secret: false)
84
- article2 = Article.create(published: true, secret: true)
85
- article3 = Article.create(published: false, secret: true)
86
- Article.create(published: false, secret: false)
87
- expect(Article.accessible_by(@ability).all).to eq([article1, article2, article3])
88
- end
89
-
90
- it 'should fetch only the articles that are published and not secret' do
91
- @ability.can :read, Article, published: true
92
- @ability.cannot :read, Article, secret: true
93
- article1 = Article.create(published: true, secret: false)
94
- Article.create(published: true, secret: true)
95
- Article.create(published: false, secret: true)
96
- Article.create(published: false, secret: false)
97
- expect(Article.accessible_by(@ability).all).to eq [article1]
98
- end
99
-
100
- it 'should only read comments for articles which are published' do
101
- @ability.can :read, Comment, article: { published: true }
102
- comment1 = Comment.create(article: Article.create(published: true))
103
- Comment.create(article: Article.create(published: false))
104
- expect(Comment.accessible_by(@ability).all).to eq [comment1]
105
- end
106
-
107
- it "should only read comments for articles which are published and user is 'me'" do
108
- @ability.can :read, Comment, article: { user: { name: 'me' }, published: true }
109
- user1 = User.create(name: 'me')
110
- comment1 = Comment.create(article: Article.create(published: true, user: user1))
111
- Comment.create(article: Article.create(published: true))
112
- Comment.create(article: Article.create(published: false, user: user1))
113
- expect(Comment.accessible_by(@ability).all).to eq [comment1]
114
- end
115
-
116
- it 'should allow conditions in SQL and merge with hash conditions' do
117
- @ability.can :read, Article, published: true
118
- @ability.can :read, Article, ['secret=?', true], &:secret
119
- @ability.cannot :read, Article, 'priority > 1' do |article|
120
- article.priority > 1
121
- end
122
- article1 = Article.create(published: true, secret: false, priority: 1)
123
- article2 = Article.create(published: true, secret: true, priority: 1)
124
- Article.create(published: true, secret: true, priority: 2)
125
- Article.create(published: false, secret: false, priority: 2)
126
- expect(Article.accessible_by(@ability).all).to eq [article1, article2]
127
- end
128
- end
129
- end
@@ -1,52 +0,0 @@
1
- require 'spec_helper'
2
- require 'ostruct' # for OpenStruct below
3
-
4
- # Most of Rule functionality is tested in Ability specs
5
- describe CanCan::Rule do
6
- before(:each) do
7
- @conditions = {}
8
- @rule = CanCan::Rule.new(true, :read, Integer, @conditions, nil)
9
- end
10
-
11
- it 'returns no association joins if none exist' do
12
- expect(@rule.associations_hash).to eq({})
13
- end
14
-
15
- it 'returns no association for joins if just attributes' do
16
- @conditions[:foo] = :bar
17
- expect(@rule.associations_hash).to eq({})
18
- end
19
-
20
- it 'returns single association for joins' do
21
- @conditions[:foo] = { bar: 1 }
22
- expect(@rule.associations_hash).to eq(foo: {})
23
- end
24
-
25
- it 'returns multiple associations for joins' do
26
- @conditions[:foo] = { bar: 1 }
27
- @conditions[:test] = { 1 => 2 }
28
- expect(@rule.associations_hash).to eq(foo: {}, test: {})
29
- end
30
-
31
- it 'returns nested associations for joins' do
32
- @conditions[:foo] = { bar: { 1 => 2 } }
33
- expect(@rule.associations_hash).to eq(foo: { bar: {} })
34
- end
35
-
36
- it 'returns no association joins if conditions is nil' do
37
- rule = CanCan::Rule.new(true, :read, Integer, nil, nil)
38
- expect(rule.associations_hash).to eq({})
39
- end
40
-
41
- it 'is not mergeable if conditions are not simple hashes' do
42
- meta_where = OpenStruct.new(name: 'metawhere', column: 'test')
43
- @conditions[meta_where] = :bar
44
-
45
- expect(@rule).to be_unmergeable
46
- end
47
-
48
- it 'is not mergeable if conditions is an empty hash' do
49
- @conditions = {}
50
- expect(@rule).to_not be_unmergeable
51
- end
52
- end
data/spec/matchers.rb DELETED
@@ -1,13 +0,0 @@
1
- RSpec::Matchers.define :orderlessly_match do |original_string|
2
- match do |given_string|
3
- original_string.split('').sort == given_string.split('').sort
4
- end
5
-
6
- failure_message do |given_string|
7
- "expected \"#{given_string}\" to have the same characters as \"#{original_string}\""
8
- end
9
-
10
- failure_message_when_negated do |given_string|
11
- "expected \"#{given_string}\" not to have the same characters as \"#{original_string}\""
12
- end
13
- end