mongoid_ability 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/mongoid_ability/subject.rb +4 -4
- data/lib/mongoid_ability/version.rb +1 -1
- data/test/mongoid_ability/subject_test.rb +127 -39
- data/test/test_helper.rb +12 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f40f0a70a06a2ef26a9e545cd18a940cd46e4be5
|
4
|
+
data.tar.gz: 0f105daba66547732f737fb1928ab7615d2e9162
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c74b4310a0a87557bd113f71b067116137a3b9f6f09d0804ed3fb4757f6c090887520c2cedaac882c60e96cfbc76ee96e7e29193cf08d32a13952a55c3bd1215
|
7
|
+
data.tar.gz: 999a48cd89887c46366621f781a359208db66cd50fc512aeee4c812c52556d20abf6c4628852a59274fd83c4782f4f46c792b7efe157d03da2a0e73c4c0702e3
|
@@ -43,9 +43,9 @@ module MongoidAbility
|
|
43
43
|
# ---------------------------------------------------------------------
|
44
44
|
|
45
45
|
def accessible_by ability, action=:read
|
46
|
-
|
46
|
+
cr = self.criteria
|
47
47
|
|
48
|
-
return
|
48
|
+
return cr unless ability.user.present?
|
49
49
|
|
50
50
|
id_locks = [
|
51
51
|
ability.user,
|
@@ -55,7 +55,7 @@ module MongoidAbility
|
|
55
55
|
}.flatten
|
56
56
|
|
57
57
|
if ability.can?(action, self)
|
58
|
-
|
58
|
+
cr.nin({
|
59
59
|
_id: id_locks.map(&:subject_id).select do |subject_id|
|
60
60
|
subject = self.new
|
61
61
|
subject.id = subject_id
|
@@ -63,7 +63,7 @@ module MongoidAbility
|
|
63
63
|
end
|
64
64
|
})
|
65
65
|
else
|
66
|
-
|
66
|
+
cr.in({
|
67
67
|
_id: id_locks.map(&:subject_id).select do |subject_id|
|
68
68
|
subject = self.new
|
69
69
|
subject.id = subject_id
|
@@ -3,24 +3,29 @@ require 'test_helper'
|
|
3
3
|
module MongoidAbility
|
4
4
|
describe Subject do
|
5
5
|
|
6
|
-
|
7
|
-
|
6
|
+
def default_lock subject_cls, outcome
|
7
|
+
TestLock.new(subject_type: subject_cls.to_s, action: :read, outcome: outcome)
|
8
|
+
end
|
8
9
|
|
9
|
-
|
10
|
-
|
10
|
+
def subject_lock subject, outcome
|
11
|
+
TestLock.new(subject: subject, action: :read, outcome: outcome)
|
12
|
+
end
|
11
13
|
|
12
|
-
|
13
|
-
let(:closed_lock) { TestLock.new(outcome: false, action: :read, subject: subject) }
|
14
|
+
# ---------------------------------------------------------------------
|
14
15
|
|
15
|
-
|
16
|
+
subject { TestSubject.new }
|
16
17
|
|
17
|
-
|
18
|
-
|
18
|
+
let(:subject_test_1) { TestSubject.create! }
|
19
|
+
let(:subject_test_2) { TestSubject.create! }
|
19
20
|
|
20
|
-
|
21
|
+
let(:subject_super) { TestSubjectSuper.new }
|
21
22
|
|
22
|
-
|
23
|
-
|
23
|
+
let(:embedded_test_subject_1) { EmbeddedTestSubject.new }
|
24
|
+
let(:embedded_test_subject_2) { EmbeddedTestSubject.new }
|
25
|
+
let(:embedded_test_subject_owner) { EmbeddedTestSubjectOwner.new(embedded_test_subjects: [ embedded_test_subject_1, embedded_test_subject_2 ]) }
|
26
|
+
|
27
|
+
let(:user) { TestUser.new }
|
28
|
+
let(:ability) { Ability.new(user) }
|
24
29
|
|
25
30
|
# =====================================================================
|
26
31
|
|
@@ -44,8 +49,8 @@ module MongoidAbility
|
|
44
49
|
end
|
45
50
|
end
|
46
51
|
|
47
|
-
#
|
48
|
-
|
52
|
+
# =====================================================================
|
53
|
+
|
49
54
|
describe '.ancestors_with_default_locks' do
|
50
55
|
it 'lists ancestors with default locks' do
|
51
56
|
subject.class.ancestors_with_default_locks.must_equal [TestSubjectSuper]
|
@@ -58,45 +63,128 @@ module MongoidAbility
|
|
58
63
|
end
|
59
64
|
end
|
60
65
|
|
61
|
-
#
|
62
|
-
|
66
|
+
# =====================================================================
|
67
|
+
|
63
68
|
describe '.accessible_by' do
|
69
|
+
|
64
70
|
it 'returns Mongoid::Criteria' do
|
65
71
|
subject.class.accessible_by(ability).must_be_kind_of Mongoid::Criteria
|
72
|
+
embedded_test_subject_1.class.accessible_by(ability).must_be_kind_of Mongoid::Criteria
|
73
|
+
end
|
74
|
+
|
75
|
+
describe 'embedded relations' do
|
76
|
+
it 'returns correct criteria type' do
|
77
|
+
embedded_test_subject_owner.embedded_test_subjects.accessible_by(ability).embedded?.must_equal true
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
# ---------------------------------------------------------------------
|
82
|
+
|
83
|
+
describe 'default locks' do
|
84
|
+
describe 'referenced relations' do
|
85
|
+
it 'returns everything when open' do
|
86
|
+
subject_test_1
|
87
|
+
subject_test_2
|
88
|
+
|
89
|
+
subject.class.stub(:default_locks, [ default_lock(subject_test_1.class, true) ]) do
|
90
|
+
subject.class.accessible_by(ability).must_include subject_test_1
|
91
|
+
subject.class.accessible_by(ability).must_include subject_test_2
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
it 'returns nothing when closed' do
|
96
|
+
subject.class.stub(:default_locks, [ default_lock(subject_test_1.class, false) ]) do
|
97
|
+
subject.class.accessible_by(ability).must_be :empty?
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
describe 'embedded relations' do
|
103
|
+
it 'returns everything when open' do
|
104
|
+
subject.class.stub(:default_locks, [ default_lock(embedded_test_subject_1.class, true) ]) do
|
105
|
+
embedded_test_subject_owner.embedded_test_subjects.accessible_by(ability).must_include embedded_test_subject_1
|
106
|
+
embedded_test_subject_owner.embedded_test_subjects.accessible_by(ability).must_include embedded_test_subject_2
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
it 'returns nothing when closed' do
|
111
|
+
subject.class.stub(:default_locks, [ default_lock(embedded_test_subject_1.class, false) ]) do
|
112
|
+
embedded_test_subject_owner.embedded_test_subjects.accessible_by(ability).must_be :empty?
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
66
116
|
end
|
67
117
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
118
|
+
# ---------------------------------------------------------------------
|
119
|
+
|
120
|
+
describe 'id locks' do
|
121
|
+
describe 'referenced relations' do
|
122
|
+
it 'excludes subject when closed' do
|
123
|
+
user.test_locks = [ subject_lock(subject_test_1, true), subject_lock(subject_test_2, false) ]
|
124
|
+
subject.class.accessible_by(ability).must_include subject_test_1
|
125
|
+
subject.class.accessible_by(ability).wont_include subject_test_2
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
describe 'embedded relations' do
|
130
|
+
it 'excludes subject when closed' do
|
131
|
+
user.test_locks = [ subject_lock(embedded_test_subject_1, true), subject_lock(embedded_test_subject_2, false) ]
|
132
|
+
embedded_test_subject_owner.embedded_test_subjects.accessible_by(ability).must_include embedded_test_subject_1
|
133
|
+
embedded_test_subject_owner.embedded_test_subjects.accessible_by(ability).wont_include embedded_test_subject_2
|
134
|
+
end
|
72
135
|
end
|
73
136
|
end
|
74
137
|
|
75
|
-
#
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
138
|
+
# ---------------------------------------------------------------------
|
139
|
+
|
140
|
+
describe 'default locks & id locks' do
|
141
|
+
describe 'referenced relations' do
|
142
|
+
describe 'default open' do
|
143
|
+
it 'excludes subject when id lock closed' do
|
144
|
+
subject.class.stub(:default_locks, [ default_lock(subject_test_1.class, true) ]) do
|
145
|
+
user.test_locks = [ subject_lock(subject_test_2, false) ]
|
146
|
+
subject.class.accessible_by(ability).must_include subject_test_1
|
147
|
+
subject.class.accessible_by(ability).wont_include subject_test_2
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
81
151
|
|
82
|
-
|
83
|
-
|
84
|
-
|
152
|
+
describe 'default closed' do
|
153
|
+
it 'includes subject when id lock open' do
|
154
|
+
subject.class.stub(:default_locks, [ default_lock(subject_test_1.class, false) ]) do
|
155
|
+
user.test_locks = [ subject_lock(subject_test_2, true) ]
|
156
|
+
subject.class.accessible_by(ability).wont_include subject_test_1
|
157
|
+
subject.class.accessible_by(ability).must_include subject_test_2
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
85
161
|
end
|
86
|
-
|
87
|
-
|
88
|
-
|
162
|
+
|
163
|
+
describe 'embedded relations' do
|
164
|
+
describe 'default open' do
|
165
|
+
it 'excludes subject when id lock closed' do
|
166
|
+
subject.class.stub(:default_locks, [ default_lock(embedded_test_subject_1.class, true) ]) do
|
167
|
+
user.test_locks = [ subject_lock(embedded_test_subject_2, false) ]
|
168
|
+
embedded_test_subject_owner.embedded_test_subjects.accessible_by(ability).must_include embedded_test_subject_1
|
169
|
+
embedded_test_subject_owner.embedded_test_subjects.accessible_by(ability).wont_include embedded_test_subject_2
|
170
|
+
end
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
describe 'default closed' do
|
175
|
+
it 'includes subject when id lock open' do
|
176
|
+
subject.class.stub(:default_locks, [ default_lock(embedded_test_subject_1.class, false) ]) do
|
177
|
+
user.test_locks = [ subject_lock(embedded_test_subject_2, true) ]
|
178
|
+
embedded_test_subject_owner.embedded_test_subjects.accessible_by(ability).wont_include embedded_test_subject_1
|
179
|
+
embedded_test_subject_owner.embedded_test_subjects.accessible_by(ability).must_include embedded_test_subject_2
|
180
|
+
end
|
181
|
+
end
|
89
182
|
end
|
90
183
|
end
|
91
184
|
end
|
92
|
-
|
185
|
+
|
93
186
|
end
|
94
187
|
end
|
95
188
|
end
|
96
189
|
|
97
|
-
|
98
|
-
|
99
|
-
describe 'instance methods' do
|
100
|
-
end
|
101
|
-
|
102
|
-
end
|
190
|
+
end
|
data/test/test_helper.rb
CHANGED
@@ -70,7 +70,7 @@ class TestOwner < TestOwnerSuper
|
|
70
70
|
end
|
71
71
|
|
72
72
|
# ---------------------------------------------------------------------
|
73
|
-
|
73
|
+
|
74
74
|
class TestSubjectSuper
|
75
75
|
include Mongoid::Document
|
76
76
|
include MongoidAbility::Subject
|
@@ -80,6 +80,17 @@ class TestSubject < TestSubjectSuper
|
|
80
80
|
default_lock :read, true
|
81
81
|
end
|
82
82
|
|
83
|
+
class EmbeddedTestSubjectOwner
|
84
|
+
include Mongoid::Document
|
85
|
+
include MongoidAbility::Subject
|
86
|
+
|
87
|
+
embeds_many :embedded_test_subjects
|
88
|
+
end
|
89
|
+
|
90
|
+
class EmbeddedTestSubject < TestSubject
|
91
|
+
embedded_in :embedded_test_subject_owner
|
92
|
+
end
|
93
|
+
|
83
94
|
# ---------------------------------------------------------------------
|
84
95
|
|
85
96
|
class TestAbilityResolverSubject
|
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.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tomas Celizna
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-06-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: cancancan
|