cancancan 1.17.0 → 3.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/cancancan.gemspec +10 -11
- data/init.rb +2 -0
- data/lib/cancan/ability/actions.rb +93 -0
- data/lib/cancan/ability/rules.rb +96 -0
- data/lib/cancan/ability/strong_parameter_support.rb +41 -0
- data/lib/cancan/ability.rb +87 -198
- data/lib/cancan/class_matcher.rb +30 -0
- data/lib/cancan/conditions_matcher.rb +147 -0
- data/lib/cancan/config.rb +101 -0
- data/lib/cancan/controller_additions.rb +13 -30
- data/lib/cancan/controller_resource.rb +33 -225
- data/lib/cancan/controller_resource_builder.rb +26 -0
- data/lib/cancan/controller_resource_finder.rb +42 -0
- data/lib/cancan/controller_resource_loader.rb +120 -0
- data/lib/cancan/controller_resource_name_finder.rb +23 -0
- data/lib/cancan/controller_resource_sanitizer.rb +32 -0
- data/lib/cancan/exceptions.rb +24 -4
- data/lib/cancan/matchers.rb +12 -1
- data/lib/cancan/model_adapters/abstract_adapter.rb +22 -1
- data/lib/cancan/model_adapters/active_record_4_adapter.rb +25 -44
- data/lib/cancan/model_adapters/active_record_5_adapter.rb +61 -0
- data/lib/cancan/model_adapters/active_record_adapter.rb +157 -83
- data/lib/cancan/model_adapters/conditions_extractor.rb +75 -0
- data/lib/cancan/model_adapters/conditions_normalizer.rb +49 -0
- data/lib/cancan/model_adapters/default_adapter.rb +2 -0
- data/lib/cancan/model_adapters/sti_normalizer.rb +47 -0
- data/lib/cancan/model_adapters/strategies/base.rb +40 -0
- data/lib/cancan/model_adapters/strategies/joined_alias_each_rule_as_exists_subquery.rb +93 -0
- data/lib/cancan/model_adapters/strategies/joined_alias_exists_subquery.rb +31 -0
- data/lib/cancan/model_adapters/strategies/left_join.rb +11 -0
- data/lib/cancan/model_adapters/strategies/subquery.rb +18 -0
- data/lib/cancan/model_additions.rb +6 -2
- data/lib/cancan/parameter_validators.rb +9 -0
- data/lib/cancan/relevant.rb +29 -0
- data/lib/cancan/rule.rb +67 -90
- data/lib/cancan/rules_compressor.rb +23 -0
- data/lib/cancan/sti_detector.rb +12 -0
- data/lib/cancan/unauthorized_message_resolver.rb +24 -0
- data/lib/cancan/version.rb +3 -1
- data/lib/cancan.rb +15 -10
- data/lib/cancancan.rb +2 -0
- data/lib/generators/cancan/ability/ability_generator.rb +3 -1
- data/lib/generators/cancan/ability/templates/ability.rb +9 -9
- metadata +64 -86
- data/.gitignore +0 -15
- data/.rspec +0 -1
- data/.rubocop.yml +0 -39
- data/.rubocop_todo.yml +0 -54
- data/.travis.yml +0 -39
- data/Appraisals +0 -105
- data/CHANGELOG.rdoc +0 -536
- data/CONTRIBUTING.md +0 -23
- data/Gemfile +0 -3
- data/LICENSE +0 -22
- data/README.md +0 -234
- data/Rakefile +0 -13
- data/gemfiles/activerecord_3.2.gemfile +0 -18
- data/gemfiles/activerecord_4.0.gemfile +0 -19
- data/gemfiles/activerecord_4.1.gemfile +0 -19
- data/gemfiles/activerecord_4.2.gemfile +0 -21
- data/gemfiles/activerecord_5.0.gemfile +0 -20
- data/gemfiles/mongoid_2.x.gemfile +0 -18
- data/gemfiles/sequel_3.x.gemfile +0 -18
- data/lib/cancan/inherited_resource.rb +0 -20
- data/lib/cancan/model_adapters/active_record_3_adapter.rb +0 -16
- data/lib/cancan/model_adapters/mongoid_adapter.rb +0 -80
- data/lib/cancan/model_adapters/sequel_adapter.rb +0 -87
- data/spec/README.rdoc +0 -27
- data/spec/cancan/ability_spec.rb +0 -553
- data/spec/cancan/controller_additions_spec.rb +0 -164
- data/spec/cancan/controller_resource_spec.rb +0 -645
- data/spec/cancan/exceptions_spec.rb +0 -58
- data/spec/cancan/inherited_resource_spec.rb +0 -71
- data/spec/cancan/matchers_spec.rb +0 -29
- data/spec/cancan/model_adapters/active_record_4_adapter_spec.rb +0 -160
- data/spec/cancan/model_adapters/active_record_adapter_spec.rb +0 -415
- data/spec/cancan/model_adapters/default_adapter_spec.rb +0 -7
- data/spec/cancan/model_adapters/mongoid_adapter_spec.rb +0 -246
- data/spec/cancan/model_adapters/sequel_adapter_spec.rb +0 -129
- data/spec/cancan/rule_spec.rb +0 -52
- data/spec/matchers.rb +0 -13
- data/spec/spec.opts +0 -2
- data/spec/spec_helper.rb +0 -27
- data/spec/support/ability.rb +0 -6
@@ -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
|
data/spec/cancan/rule_spec.rb
DELETED
@@ -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
|
data/spec/spec.opts
DELETED
data/spec/spec_helper.rb
DELETED
@@ -1,27 +0,0 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'bundler/setup'
|
3
|
-
|
4
|
-
Bundler.require
|
5
|
-
|
6
|
-
require 'matchers'
|
7
|
-
require 'cancan/matchers'
|
8
|
-
|
9
|
-
# I8n setting to fix deprecation.
|
10
|
-
if defined?(I18n) && I18n.respond_to?('enforce_available_locales=')
|
11
|
-
I18n.enforce_available_locales = false
|
12
|
-
end
|
13
|
-
|
14
|
-
# Add support to load paths
|
15
|
-
$LOAD_PATH.unshift File.expand_path('../support', __FILE__)
|
16
|
-
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
|
17
|
-
|
18
|
-
RSpec.configure do |config|
|
19
|
-
config.filter_run focus: true
|
20
|
-
config.run_all_when_everything_filtered = true
|
21
|
-
config.mock_with :rspec
|
22
|
-
config.order = 'random'
|
23
|
-
|
24
|
-
config.expect_with :rspec do |c|
|
25
|
-
c.syntax = :expect
|
26
|
-
end
|
27
|
-
end
|