mongoid_ability 0.3.6 → 0.3.7
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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d30d247467a000a4905761928d401f3a6adbbc10
|
4
|
+
data.tar.gz: a14eed2b95bd69392acc82b46d192902fef23a66
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 297e80b5fec91daeb637f9394fdac3021df1ba902b20f743855fc1951e0da7adcd401b8207bb034e46357aba1247c3aad20cefbd28507add1a17f0815d66737e
|
7
|
+
data.tar.gz: d8800d4844678c18e62eace9ae607e404fe469986a138fb0b9c3f2625c0986d897f16331b275fbbce266227beb72fce2032e01457ca1c2d9f154035101835890
|
@@ -7,38 +7,27 @@ module MongoidAbility
|
|
7
7
|
# =====================================================================
|
8
8
|
|
9
9
|
def call
|
10
|
-
return base_class.criteria unless and_conditions.present?
|
11
10
|
base_class.criteria.where(conditions)
|
12
11
|
end
|
13
12
|
|
14
13
|
def conditions
|
15
|
-
|
14
|
+
{ '$and' => [{ '$or' => [closed_types_condition, open_types_and_ids_condition] }, closed_ids_condition] }
|
16
15
|
end
|
17
16
|
|
18
17
|
private # =============================================================
|
19
18
|
|
20
|
-
def and_conditions
|
21
|
-
return unless conditions = [or_conditions, closed_ids_condition].compact.presence
|
22
|
-
{ '$and' => conditions }
|
23
|
-
end
|
24
|
-
|
25
|
-
def or_conditions
|
26
|
-
return unless conditions = [closed_types_condition, open_ids_condition].compact.presence
|
27
|
-
{ '$or' => conditions }
|
28
|
-
end
|
29
|
-
|
30
19
|
def closed_types_condition
|
31
|
-
return unless values.closed_types.present?
|
32
20
|
{ _type: { '$nin' => values.closed_types } }
|
33
21
|
end
|
34
22
|
|
35
|
-
def
|
36
|
-
|
37
|
-
|
23
|
+
def open_types_and_ids_condition
|
24
|
+
{
|
25
|
+
_type: { '$in' => values.open_types_and_ids.map(&:type) },
|
26
|
+
_id: { '$in' => values.open_types_and_ids.map(&:id) }
|
27
|
+
}
|
38
28
|
end
|
39
29
|
|
40
30
|
def closed_ids_condition
|
41
|
-
return unless values.closed_ids.present?
|
42
31
|
{ _id: { '$nin' => values.closed_ids } }
|
43
32
|
end
|
44
33
|
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require 'test_helper'
|
2
2
|
|
3
3
|
module MongoidAbility
|
4
4
|
describe Ability do
|
@@ -12,7 +12,7 @@ module MongoidAbility
|
|
12
12
|
describe 'default locks' do
|
13
13
|
before do
|
14
14
|
# NOTE: we might need to use the .default_lock macro in case we propagate down directly
|
15
|
-
MySubject.default_locks = [
|
15
|
+
MySubject.default_locks = [MyLock.new(subject_type: MySubject, action: :update, outcome: true)]
|
16
16
|
MySubject1.default_locks = []
|
17
17
|
MySubject2.default_locks = []
|
18
18
|
end
|
@@ -26,9 +26,9 @@ module MongoidAbility
|
|
26
26
|
|
27
27
|
describe 'when defined for all superclasses' do
|
28
28
|
before do
|
29
|
-
MySubject.default_locks = [
|
30
|
-
MySubject1.default_locks = [
|
31
|
-
MySubject2.default_locks = [
|
29
|
+
MySubject.default_locks = [MyLock.new(subject_type: MySubject, action: :read, outcome: false)]
|
30
|
+
MySubject1.default_locks = [MyLock.new(subject_type: MySubject1, action: :read, outcome: true)]
|
31
|
+
MySubject2.default_locks = [MyLock.new(subject_type: MySubject2, action: :read, outcome: false)]
|
32
32
|
end
|
33
33
|
|
34
34
|
it 'respects the definitions' do
|
@@ -40,9 +40,9 @@ module MongoidAbility
|
|
40
40
|
|
41
41
|
describe 'when defined for some superclasses' do
|
42
42
|
before do
|
43
|
-
MySubject.default_locks = [
|
43
|
+
MySubject.default_locks = [MyLock.new(subject_type: MySubject, action: :read, outcome: false)]
|
44
44
|
MySubject1.default_locks = []
|
45
|
-
MySubject2.default_locks = [
|
45
|
+
MySubject2.default_locks = [MyLock.new(subject_type: MySubject2, action: :read, outcome: true)]
|
46
46
|
end
|
47
47
|
|
48
48
|
it 'propagates default locks to subclasses' do
|
@@ -57,10 +57,10 @@ module MongoidAbility
|
|
57
57
|
describe 'user locks' do
|
58
58
|
describe 'when defined for superclass' do
|
59
59
|
before do
|
60
|
-
MySubject.default_locks = [
|
60
|
+
MySubject.default_locks = [MyLock.new(subject_type: MySubject, action: :read, outcome: false)]
|
61
61
|
MySubject1.default_locks = []
|
62
62
|
MySubject2.default_locks = []
|
63
|
-
owner.my_locks = [
|
63
|
+
owner.my_locks = [MyLock.new(subject_type: MySubject, action: :read, outcome: true)]
|
64
64
|
end
|
65
65
|
|
66
66
|
it 'applies the superclass lock' do
|
@@ -74,10 +74,10 @@ module MongoidAbility
|
|
74
74
|
describe 'inherited owner locks' do
|
75
75
|
describe 'when multiple inherited owners' do
|
76
76
|
before do
|
77
|
-
MySubject.default_locks = [
|
77
|
+
MySubject.default_locks = [MyLock.new(subject_type: MySubject, action: :read, outcome: false)]
|
78
78
|
owner.my_roles = [
|
79
|
-
MyRole.new(my_locks: [
|
80
|
-
MyRole.new(my_locks: [
|
79
|
+
MyRole.new(my_locks: [MyLock.new(subject_type: MySubject, action: :read, outcome: true)]),
|
80
|
+
MyRole.new(my_locks: [MyLock.new(subject_type: MySubject, action: :read, outcome: false)])
|
81
81
|
]
|
82
82
|
end
|
83
83
|
|
@@ -88,10 +88,10 @@ module MongoidAbility
|
|
88
88
|
|
89
89
|
describe 'when defined for superclass' do
|
90
90
|
before do
|
91
|
-
MySubject.default_locks = [
|
91
|
+
MySubject.default_locks = [MyLock.new(subject_type: MySubject, action: :read, outcome: false)]
|
92
92
|
MySubject1.default_locks = []
|
93
93
|
MySubject2.default_locks = []
|
94
|
-
owner.my_roles = [
|
94
|
+
owner.my_roles = [MyRole.new(my_locks: [MyLock.new(subject_type: MySubject, action: :read, outcome: true)])]
|
95
95
|
end
|
96
96
|
|
97
97
|
it 'applies the superclass lock' do
|
@@ -105,9 +105,9 @@ module MongoidAbility
|
|
105
105
|
describe 'combined locks' do
|
106
106
|
describe 'user and role locks' do
|
107
107
|
before do
|
108
|
-
MySubject.default_locks = [
|
109
|
-
owner.my_locks = [
|
110
|
-
owner.my_roles = [
|
108
|
+
MySubject.default_locks = [MyLock.new(subject_type: MySubject, action: :read, outcome: false)]
|
109
|
+
owner.my_locks = [MyLock.new(subject_type: MySubject, action: :read, outcome: false)]
|
110
|
+
owner.my_roles = [MyRole.new(my_locks: [MyLock.new(subject_type: MySubject, action: :read, outcome: true)])]
|
111
111
|
end
|
112
112
|
|
113
113
|
it 'prefers user locks' do
|
@@ -117,8 +117,8 @@ module MongoidAbility
|
|
117
117
|
|
118
118
|
describe 'roles and default locks' do
|
119
119
|
before do
|
120
|
-
MySubject.default_locks = [
|
121
|
-
owner.my_roles = [
|
120
|
+
MySubject.default_locks = [MyLock.new(subject_type: MySubject, action: :read, outcome: false)]
|
121
|
+
owner.my_roles = [MyRole.new(my_locks: [MyLock.new(subject_type: MySubject, action: :read, outcome: true)])]
|
122
122
|
end
|
123
123
|
|
124
124
|
it 'prefers role locks' do
|
@@ -126,6 +126,5 @@ module MongoidAbility
|
|
126
126
|
end
|
127
127
|
end
|
128
128
|
end
|
129
|
-
|
130
129
|
end
|
131
130
|
end
|
@@ -9,7 +9,7 @@ module MongoidAbility
|
|
9
9
|
let(:options) { Hash.new }
|
10
10
|
|
11
11
|
before do
|
12
|
-
MySubject.default_locks = [
|
12
|
+
MySubject.default_locks = [MyLock.new(subject_type: MySubject, action: :read, outcome: true)]
|
13
13
|
end
|
14
14
|
|
15
15
|
subject { AccessibleQueryBuilder.call(base_class, ability, action, options) }
|
@@ -17,5 +17,56 @@ module MongoidAbility
|
|
17
17
|
it 'returns Mongoid::Criteria' do
|
18
18
|
subject.must_be_kind_of Mongoid::Criteria
|
19
19
|
end
|
20
|
+
|
21
|
+
# ---------------------------------------------------------------------
|
22
|
+
|
23
|
+
describe 'closed_types' do
|
24
|
+
let(:my_subject) { MySubject.create! }
|
25
|
+
let(:my_subject_1) { MySubject1.create! }
|
26
|
+
|
27
|
+
before do
|
28
|
+
my_subject; my_subject_1
|
29
|
+
owner.my_locks = [MyLock.new(subject_type: MySubject, action: :read, outcome: false)]
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'does not return subject with that id' do
|
33
|
+
MySubject.accessible_by(ability, :read).wont_include my_subject
|
34
|
+
MySubject.accessible_by(ability, :read).wont_include my_subject_1
|
35
|
+
MySubject1.accessible_by(ability, :read).wont_include my_subject_1
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe 'closed_ids' do
|
40
|
+
let(:my_subject_a) { MySubject.create! }
|
41
|
+
let(:my_subject_b) { MySubject.create! }
|
42
|
+
|
43
|
+
before do
|
44
|
+
my_subject_a; my_subject_b
|
45
|
+
owner.my_locks = [MyLock.new(subject: my_subject_a, action: :read, outcome: false)]
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'does not return subject with that id' do
|
49
|
+
MySubject.accessible_by(ability, :read).wont_include my_subject_a
|
50
|
+
MySubject.accessible_by(ability, :read).must_include my_subject_b
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe 'closed_types & open_ids' do
|
55
|
+
let(:my_subject) { MySubject.create! }
|
56
|
+
let(:my_subject_1) { MySubject1.create! }
|
57
|
+
|
58
|
+
before do
|
59
|
+
owner.my_locks = [
|
60
|
+
MyLock.new(subject_type: MySubject, action: :read, outcome: false),
|
61
|
+
MyLock.new(subject: my_subject, action: :read, outcome: true),
|
62
|
+
MyLock.new(subject: my_subject_1, action: :read, outcome: true)
|
63
|
+
]
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'does not return subject with that id' do
|
67
|
+
MySubject.accessible_by(ability, :read).must_include my_subject
|
68
|
+
MySubject.accessible_by(ability, :read).must_include my_subject_1
|
69
|
+
end
|
70
|
+
end
|
20
71
|
end
|
21
72
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid_ability
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tomáš Celizna
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-03-
|
11
|
+
date: 2016-03-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: cancancan
|