cancancan 1.17.0 → 3.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (85) hide show
  1. checksums.yaml +5 -5
  2. data/cancancan.gemspec +10 -11
  3. data/init.rb +2 -0
  4. data/lib/cancan/ability/actions.rb +93 -0
  5. data/lib/cancan/ability/rules.rb +96 -0
  6. data/lib/cancan/ability/strong_parameter_support.rb +41 -0
  7. data/lib/cancan/ability.rb +87 -198
  8. data/lib/cancan/class_matcher.rb +30 -0
  9. data/lib/cancan/conditions_matcher.rb +147 -0
  10. data/lib/cancan/config.rb +101 -0
  11. data/lib/cancan/controller_additions.rb +13 -30
  12. data/lib/cancan/controller_resource.rb +33 -225
  13. data/lib/cancan/controller_resource_builder.rb +26 -0
  14. data/lib/cancan/controller_resource_finder.rb +42 -0
  15. data/lib/cancan/controller_resource_loader.rb +120 -0
  16. data/lib/cancan/controller_resource_name_finder.rb +23 -0
  17. data/lib/cancan/controller_resource_sanitizer.rb +32 -0
  18. data/lib/cancan/exceptions.rb +24 -4
  19. data/lib/cancan/matchers.rb +12 -1
  20. data/lib/cancan/model_adapters/abstract_adapter.rb +22 -1
  21. data/lib/cancan/model_adapters/active_record_4_adapter.rb +25 -44
  22. data/lib/cancan/model_adapters/active_record_5_adapter.rb +61 -0
  23. data/lib/cancan/model_adapters/active_record_adapter.rb +157 -83
  24. data/lib/cancan/model_adapters/conditions_extractor.rb +75 -0
  25. data/lib/cancan/model_adapters/conditions_normalizer.rb +49 -0
  26. data/lib/cancan/model_adapters/default_adapter.rb +2 -0
  27. data/lib/cancan/model_adapters/sti_normalizer.rb +47 -0
  28. data/lib/cancan/model_adapters/strategies/base.rb +40 -0
  29. data/lib/cancan/model_adapters/strategies/joined_alias_each_rule_as_exists_subquery.rb +93 -0
  30. data/lib/cancan/model_adapters/strategies/joined_alias_exists_subquery.rb +31 -0
  31. data/lib/cancan/model_adapters/strategies/left_join.rb +11 -0
  32. data/lib/cancan/model_adapters/strategies/subquery.rb +18 -0
  33. data/lib/cancan/model_additions.rb +6 -2
  34. data/lib/cancan/parameter_validators.rb +9 -0
  35. data/lib/cancan/relevant.rb +29 -0
  36. data/lib/cancan/rule.rb +67 -90
  37. data/lib/cancan/rules_compressor.rb +23 -0
  38. data/lib/cancan/sti_detector.rb +12 -0
  39. data/lib/cancan/unauthorized_message_resolver.rb +24 -0
  40. data/lib/cancan/version.rb +3 -1
  41. data/lib/cancan.rb +15 -10
  42. data/lib/cancancan.rb +2 -0
  43. data/lib/generators/cancan/ability/ability_generator.rb +3 -1
  44. data/lib/generators/cancan/ability/templates/ability.rb +9 -9
  45. metadata +64 -86
  46. data/.gitignore +0 -15
  47. data/.rspec +0 -1
  48. data/.rubocop.yml +0 -39
  49. data/.rubocop_todo.yml +0 -54
  50. data/.travis.yml +0 -39
  51. data/Appraisals +0 -105
  52. data/CHANGELOG.rdoc +0 -536
  53. data/CONTRIBUTING.md +0 -23
  54. data/Gemfile +0 -3
  55. data/LICENSE +0 -22
  56. data/README.md +0 -234
  57. data/Rakefile +0 -13
  58. data/gemfiles/activerecord_3.2.gemfile +0 -18
  59. data/gemfiles/activerecord_4.0.gemfile +0 -19
  60. data/gemfiles/activerecord_4.1.gemfile +0 -19
  61. data/gemfiles/activerecord_4.2.gemfile +0 -21
  62. data/gemfiles/activerecord_5.0.gemfile +0 -20
  63. data/gemfiles/mongoid_2.x.gemfile +0 -18
  64. data/gemfiles/sequel_3.x.gemfile +0 -18
  65. data/lib/cancan/inherited_resource.rb +0 -20
  66. data/lib/cancan/model_adapters/active_record_3_adapter.rb +0 -16
  67. data/lib/cancan/model_adapters/mongoid_adapter.rb +0 -80
  68. data/lib/cancan/model_adapters/sequel_adapter.rb +0 -87
  69. data/spec/README.rdoc +0 -27
  70. data/spec/cancan/ability_spec.rb +0 -553
  71. data/spec/cancan/controller_additions_spec.rb +0 -164
  72. data/spec/cancan/controller_resource_spec.rb +0 -645
  73. data/spec/cancan/exceptions_spec.rb +0 -58
  74. data/spec/cancan/inherited_resource_spec.rb +0 -71
  75. data/spec/cancan/matchers_spec.rb +0 -29
  76. data/spec/cancan/model_adapters/active_record_4_adapter_spec.rb +0 -160
  77. data/spec/cancan/model_adapters/active_record_adapter_spec.rb +0 -415
  78. data/spec/cancan/model_adapters/default_adapter_spec.rb +0 -7
  79. data/spec/cancan/model_adapters/mongoid_adapter_spec.rb +0 -246
  80. data/spec/cancan/model_adapters/sequel_adapter_spec.rb +0 -129
  81. data/spec/cancan/rule_spec.rb +0 -52
  82. data/spec/matchers.rb +0 -13
  83. data/spec/spec.opts +0 -2
  84. data/spec/spec_helper.rb +0 -27
  85. data/spec/support/ability.rb +0 -6
@@ -1,415 +0,0 @@
1
- require 'spec_helper'
2
-
3
- if defined? CanCan::ModelAdapters::ActiveRecordAdapter
4
-
5
- describe CanCan::ModelAdapters::ActiveRecordAdapter do
6
- before :each do
7
- ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
8
- ActiveRecord::Migration.verbose = false
9
- ActiveRecord::Schema.define do
10
- create_table(:categories) do |t|
11
- t.string :name
12
- t.boolean :visible
13
- t.timestamps null: false
14
- end
15
-
16
- create_table(:projects) do |t|
17
- t.string :name
18
- t.timestamps null: false
19
- end
20
-
21
- create_table(:articles) do |t|
22
- t.string :name
23
- t.timestamps null: false
24
- t.boolean :published
25
- t.boolean :secret
26
- t.integer :priority
27
- t.integer :category_id
28
- t.integer :user_id
29
- end
30
-
31
- create_table(:comments) do |t|
32
- t.boolean :spam
33
- t.integer :article_id
34
- t.timestamps null: false
35
- end
36
-
37
- create_table(:legacy_mentions) do |t|
38
- t.integer :user_id
39
- t.integer :article_id
40
- t.timestamps null: false
41
- end
42
-
43
- create_table(:users) do |t|
44
- t.timestamps null: false
45
- end
46
- end
47
-
48
- class Project < ActiveRecord::Base
49
- end
50
-
51
- class Category < ActiveRecord::Base
52
- has_many :articles
53
- end
54
-
55
- class Article < ActiveRecord::Base
56
- belongs_to :category
57
- has_many :comments
58
- has_many :mentions
59
- has_many :mentioned_users, through: :mentions, source: :user
60
- belongs_to :user
61
- end
62
-
63
- class Mention < ActiveRecord::Base
64
- self.table_name = 'legacy_mentions'
65
- belongs_to :user
66
- belongs_to :article
67
- end
68
-
69
- class Comment < ActiveRecord::Base
70
- belongs_to :article
71
- end
72
-
73
- class User < ActiveRecord::Base
74
- has_many :articles
75
- end
76
-
77
- (@ability = double).extend(CanCan::Ability)
78
- @article_table = Article.table_name
79
- @comment_table = Comment.table_name
80
- end
81
-
82
- it 'is for only active record classes' do
83
- if ActiveRecord.respond_to?(:version) &&
84
- ActiveRecord.version > Gem::Version.new('4')
85
- expect(CanCan::ModelAdapters::ActiveRecord4Adapter).to_not be_for_class(Object)
86
- expect(CanCan::ModelAdapters::ActiveRecord4Adapter).to be_for_class(Article)
87
- expect(CanCan::ModelAdapters::AbstractAdapter.adapter_class(Article))
88
- .to eq(CanCan::ModelAdapters::ActiveRecord4Adapter)
89
- else
90
- expect(CanCan::ModelAdapters::ActiveRecord3Adapter).to_not be_for_class(Object)
91
- expect(CanCan::ModelAdapters::ActiveRecord3Adapter).to be_for_class(Article)
92
- expect(CanCan::ModelAdapters::AbstractAdapter.adapter_class(Article))
93
- .to eq(CanCan::ModelAdapters::ActiveRecord3Adapter)
94
- end
95
- end
96
-
97
- it 'finds record' do
98
- article = Article.create!
99
- adapter = CanCan::ModelAdapters::AbstractAdapter.adapter_class(Article)
100
- expect(adapter.find(Article, article.id)).to eq(article)
101
- end
102
-
103
- it 'does not fetch any records when no abilities are defined' do
104
- Article.create!
105
- expect(Article.accessible_by(@ability)).to be_empty
106
- end
107
-
108
- it 'fetches all articles when one can read all' do
109
- @ability.can :read, Article
110
- article = Article.create!
111
- expect(Article.accessible_by(@ability)).to eq([article])
112
- end
113
-
114
- it 'fetches only the articles that are published' do
115
- @ability.can :read, Article, published: true
116
- article1 = Article.create!(published: true)
117
- Article.create!(published: false)
118
- expect(Article.accessible_by(@ability)).to eq([article1])
119
- end
120
-
121
- it 'fetches any articles which are published or secret' do
122
- @ability.can :read, Article, published: true
123
- @ability.can :read, Article, secret: true
124
- article1 = Article.create!(published: true, secret: false)
125
- article2 = Article.create!(published: true, secret: true)
126
- article3 = Article.create!(published: false, secret: true)
127
- Article.create!(published: false, secret: false)
128
- expect(Article.accessible_by(@ability)).to eq([article1, article2, article3])
129
- end
130
-
131
- it 'fetches any articles which we are cited in' do
132
- user = User.create!
133
- cited = Article.create!
134
- Article.create!
135
- cited.mentioned_users << user
136
- @ability.can :read, Article, mentioned_users: { id: user.id }
137
- @ability.can :read, Article, mentions: { user_id: user.id }
138
- expect(Article.accessible_by(@ability)).to eq([cited])
139
- end
140
-
141
- it 'fetches only the articles that are published and not secret' do
142
- @ability.can :read, Article, published: true
143
- @ability.cannot :read, Article, secret: true
144
- article1 = Article.create!(published: true, secret: false)
145
- Article.create!(published: true, secret: true)
146
- Article.create!(published: false, secret: true)
147
- Article.create!(published: false, secret: false)
148
- expect(Article.accessible_by(@ability)).to eq([article1])
149
- end
150
-
151
- it 'only reads comments for articles which are published' do
152
- @ability.can :read, Comment, article: { published: true }
153
- comment1 = Comment.create!(article: Article.create!(published: true))
154
- Comment.create!(article: Article.create!(published: false))
155
- expect(Comment.accessible_by(@ability)).to eq([comment1])
156
- end
157
-
158
- it 'should only read articles which are published or in visible categories' do
159
- @ability.can :read, Article, category: { visible: true }
160
- @ability.can :read, Article, published: true
161
- article1 = Article.create!(published: true)
162
- Article.create!(published: false)
163
- article3 = Article.create!(published: false, category: Category.create!(visible: true))
164
- expect(Article.accessible_by(@ability)).to eq([article1, article3])
165
- end
166
-
167
- it 'should only read categories once even if they have multiple articles' do
168
- @ability.can :read, Category, articles: { published: true }
169
- @ability.can :read, Article, published: true
170
- category = Category.create!
171
- Article.create!(published: true, category: category)
172
- Article.create!(published: true, category: category)
173
- expect(Category.accessible_by(@ability)).to eq([category])
174
- end
175
-
176
- it 'only reads comments for visible categories through articles' do
177
- @ability.can :read, Comment, article: { category: { visible: true } }
178
- comment1 = Comment.create!(article: Article.create!(category: Category.create!(visible: true)))
179
- Comment.create!(article: Article.create!(category: Category.create!(visible: false)))
180
- expect(Comment.accessible_by(@ability)).to eq([comment1])
181
- end
182
-
183
- it 'allows conditions in SQL and merge with hash conditions' do
184
- @ability.can :read, Article, published: true
185
- @ability.can :read, Article, ['secret=?', true]
186
- article1 = Article.create!(published: true, secret: false)
187
- article2 = Article.create!(published: true, secret: true)
188
- article3 = Article.create!(published: false, secret: true)
189
- Article.create!(published: false, secret: false)
190
- expect(Article.accessible_by(@ability)).to eq([article1, article2, article3])
191
- end
192
-
193
- it 'allows a scope for conditions' do
194
- @ability.can :read, Article, Article.where(secret: true)
195
- article1 = Article.create!(secret: true)
196
- Article.create!(secret: false)
197
- expect(Article.accessible_by(@ability)).to eq([article1])
198
- end
199
-
200
- it 'fetches only associated records when using with a scope for conditions' do
201
- @ability.can :read, Article, Article.where(secret: true)
202
- category1 = Category.create!(visible: false)
203
- category2 = Category.create!(visible: true)
204
- article1 = Article.create!(secret: true, category: category1)
205
- Article.create!(secret: true, category: category2)
206
- expect(category1.articles.accessible_by(@ability)).to eq([article1])
207
- end
208
-
209
- it 'raises an exception when trying to merge scope with other conditions' do
210
- @ability.can :read, Article, published: true
211
- @ability.can :read, Article, Article.where(secret: true)
212
- expect(-> { Article.accessible_by(@ability) })
213
- .to raise_error(CanCan::Error,
214
- 'Unable to merge an Active Record scope with other conditions. '\
215
- 'Instead use a hash or SQL for read Article ability.')
216
- end
217
-
218
- it 'does not allow to fetch records when ability with just block present' do
219
- @ability.can :read, Article do
220
- false
221
- end
222
- expect(-> { Article.accessible_by(@ability) }).to raise_error(CanCan::Error)
223
- end
224
-
225
- it 'should support more than one deeply nested conditions' do
226
- @ability.can :read, Comment, article: {
227
- category: {
228
- name: 'foo', visible: true
229
- }
230
- }
231
- expect { Comment.accessible_by(@ability) }.to_not raise_error
232
- end
233
-
234
- it 'does not allow to check ability on object against SQL conditions without block' do
235
- @ability.can :read, Article, ['secret=?', true]
236
- expect(-> { @ability.can? :read, Article.new }).to raise_error(CanCan::Error)
237
- end
238
-
239
- it 'has false conditions if no abilities match' do
240
- expect(@ability.model_adapter(Article, :read).conditions).to eq("'t'='f'")
241
- end
242
-
243
- it 'returns false conditions for cannot clause' do
244
- @ability.cannot :read, Article
245
- expect(@ability.model_adapter(Article, :read).conditions).to eq("'t'='f'")
246
- end
247
-
248
- it 'returns SQL for single `can` definition in front of default `cannot` condition' do
249
- @ability.cannot :read, Article
250
- @ability.can :read, Article, published: false, secret: true
251
- expect(@ability.model_adapter(Article, :read).conditions)
252
- .to orderlessly_match(%("#{@article_table}"."published" = 'f' AND "#{@article_table}"."secret" = 't'))
253
- end
254
-
255
- it 'returns true condition for single `can` definition in front of default `can` condition' do
256
- @ability.can :read, Article
257
- @ability.can :read, Article, published: false, secret: true
258
- expect(@ability.model_adapter(Article, :read).conditions).to eq("'t'='t'")
259
- end
260
-
261
- it 'returns `false condition` for single `cannot` definition in front of default `cannot` condition' do
262
- @ability.cannot :read, Article
263
- @ability.cannot :read, Article, published: false, secret: true
264
- expect(@ability.model_adapter(Article, :read).conditions).to eq("'t'='f'")
265
- end
266
-
267
- it 'returns `not (sql)` for single `cannot` definition in front of default `can` condition' do
268
- @ability.can :read, Article
269
- @ability.cannot :read, Article, published: false, secret: true
270
- expect(@ability.model_adapter(Article, :read).conditions)
271
- .to orderlessly_match(%["not (#{@article_table}"."published" = 'f' AND "#{@article_table}"."secret" = 't')])
272
- end
273
-
274
- it 'returns appropriate sql conditions in complex case' do
275
- @ability.can :read, Article
276
- @ability.can :manage, Article, id: 1
277
- @ability.can :update, Article, published: true
278
- @ability.cannot :update, Article, secret: true
279
- expect(@ability.model_adapter(Article, :update).conditions)
280
- .to eq(%[not ("#{@article_table}"."secret" = 't') ] +
281
- %[AND (("#{@article_table}"."published" = 't') ] +
282
- %[OR ("#{@article_table}"."id" = 1))])
283
- expect(@ability.model_adapter(Article, :manage).conditions).to eq(id: 1)
284
- expect(@ability.model_adapter(Article, :read).conditions).to eq("'t'='t'")
285
- end
286
-
287
- it 'returns appropriate sql conditions in complex case with nested joins' do
288
- @ability.can :read, Comment, article: { category: { visible: true } }
289
- expect(@ability.model_adapter(Comment, :read).conditions).to eq(Category.table_name.to_sym => { visible: true })
290
- end
291
-
292
- it 'returns appropriate sql conditions in complex case with nested joins of different depth' do
293
- @ability.can :read, Comment, article: { published: true, category: { visible: true } }
294
- expect(@ability.model_adapter(Comment, :read).conditions)
295
- .to eq(Article.table_name.to_sym => { published: true }, Category.table_name.to_sym => { visible: true })
296
- end
297
-
298
- it 'does not forget conditions when calling with SQL string' do
299
- @ability.can :read, Article, published: true
300
- @ability.can :read, Article, ['secret=?', false]
301
- adapter = @ability.model_adapter(Article, :read)
302
- 2.times do
303
- expect(adapter.conditions).to eq(%[(secret='f') OR ("#{@article_table}"."published" = 't')])
304
- end
305
- end
306
-
307
- it 'has nil joins if no rules' do
308
- expect(@ability.model_adapter(Article, :read).joins).to be_nil
309
- end
310
-
311
- it 'has nil joins if no nested hashes specified in conditions' do
312
- @ability.can :read, Article, published: false
313
- @ability.can :read, Article, secret: true
314
- expect(@ability.model_adapter(Article, :read).joins).to be_nil
315
- end
316
-
317
- it 'merges separate joins into a single array' do
318
- @ability.can :read, Article, project: { blocked: false }
319
- @ability.can :read, Article, company: { admin: true }
320
- expect(@ability.model_adapter(Article, :read).joins.inspect).to orderlessly_match([:company, :project].inspect)
321
- end
322
-
323
- it 'merges same joins into a single array' do
324
- @ability.can :read, Article, project: { blocked: false }
325
- @ability.can :read, Article, project: { admin: true }
326
- expect(@ability.model_adapter(Article, :read).joins).to eq([:project])
327
- end
328
-
329
- it 'merges nested and non-nested joins' do
330
- @ability.can :read, Article, project: { blocked: false }
331
- @ability.can :read, Article, project: { comments: { spam: true } }
332
- expect(@ability.model_adapter(Article, :read).joins).to eq([{ project: [:comments] }])
333
- end
334
-
335
- it 'merges :all conditions with other conditions' do
336
- user = User.create!
337
- article = Article.create!(user: user)
338
- ability = Ability.new(user)
339
- ability.can :manage, :all
340
- ability.can :manage, Article, user_id: user.id
341
- expect(Article.accessible_by(ability)).to eq([article])
342
- end
343
-
344
- it 'should not execute a scope when checking ability on the class' do
345
- relation = Article.where(secret: true)
346
- @ability.can :read, Article, relation do |article|
347
- article.secret == true
348
- end
349
-
350
- allow(relation).to receive(:count).and_raise('Unexpected scope execution.')
351
-
352
- expect { @ability.can? :read, Article }.not_to raise_error
353
- end
354
-
355
- context 'with namespaced models' do
356
- before :each do
357
- ActiveRecord::Schema.define do
358
- create_table(:table_xes) do |t|
359
- t.timestamps null: false
360
- end
361
-
362
- create_table(:table_zs) do |t|
363
- t.integer :table_x_id
364
- t.integer :user_id
365
- t.timestamps null: false
366
- end
367
- end
368
-
369
- module Namespace
370
- end
371
-
372
- class Namespace::TableX < ActiveRecord::Base
373
- has_many :table_zs
374
- end
375
-
376
- class Namespace::TableZ < ActiveRecord::Base
377
- belongs_to :table_x
378
- belongs_to :user
379
- end
380
- end
381
-
382
- it 'fetches all namespace::table_x when one is related by table_y' do
383
- user = User.create!
384
-
385
- ability = Ability.new(user)
386
- ability.can :read, Namespace::TableX, table_zs: { user_id: user.id }
387
-
388
- table_x = Namespace::TableX.create!
389
- table_x.table_zs.create(user: user)
390
- expect(Namespace::TableX.accessible_by(ability)).to eq([table_x])
391
- end
392
- end
393
-
394
- context 'when conditions are non iterable ranges' do
395
- before :each do
396
- ActiveRecord::Schema.define do
397
- create_table(:courses) do |t|
398
- t.datetime :start_at
399
- end
400
- end
401
-
402
- class Course < ActiveRecord::Base
403
- end
404
- end
405
-
406
- it 'fetches only the valid records' do
407
- @ability.can :read, Course, start_at: 1.day.ago..1.day.from_now
408
- Course.create!(start_at: 10.days.ago)
409
- valid_course = Course.create!(start_at: Time.now)
410
-
411
- expect(Course.accessible_by(@ability)).to eq([valid_course])
412
- end
413
- end
414
- end
415
- end
@@ -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