miau 1.1.1 → 1.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/rake.yml +1 -1
- data/.gitignore +7 -4
- data/Gemfile +5 -3
- data/Gemfile.lock +181 -9
- data/README.md +7 -6
- data/Rakefile +4 -6
- data/gemfiles/rails_6.1.gemfile +3 -2
- data/gemfiles/rails_7.0.gemfile +3 -2
- data/gemfiles/rails_7.1.gemfile +3 -2
- data/lib/miau/application_policy.rb +1 -1
- data/lib/miau/run.rb +16 -15
- data/lib/miau/storage.rb +2 -2
- data/lib/miau/version.rb +2 -1
- data/lib/miau.rb +27 -19
- data/miau.gemspec +6 -8
- data/test/authorization_test.rb +28 -0
- data/test/benchmark_test.rb +34 -0
- data/test/controller_test.rb +58 -0
- data/test/controllers/orders_controller_test.rb +47 -0
- data/test/internal/app/controllers/application_controller.rb +7 -0
- data/test/internal/app/controllers/orders_controller.rb +61 -0
- data/test/internal/app/controllers/posts_controller.rb +10 -0
- data/test/internal/app/models/application_record.rb +3 -0
- data/test/internal/app/models/order.rb +2 -0
- data/test/internal/app/models/post.rb +2 -0
- data/test/internal/app/policies/orders_policy.rb +16 -0
- data/test/internal/app/policies/posts_policy.rb +18 -0
- data/test/internal/app/views/orders/new.html.erb +5 -0
- data/test/internal/config/database.yml +3 -0
- data/test/internal/config/routes.rb +3 -0
- data/test/internal/db/migrate/20141016161801_create_orders.rb +10 -0
- data/test/internal/db/schema.rb +8 -0
- data/test/miau_test.rb +46 -0
- data/test/run_test.rb +69 -0
- data/test/storage_test.rb +51 -0
- data/test/test_helper.rb +17 -0
- metadata +42 -10
- data/gemfiles/rails_6.1.gemfile.lock +0 -223
- data/gemfiles/rails_7.0.gemfile.lock +0 -255
- data/gemfiles/rails_7.1.gemfile.lock +0 -253
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4ff4fcf400151bdd20347e5bc2cd8f215a11e8ea6b519840c6032ed3ac18a57b
|
4
|
+
data.tar.gz: bc678ee4c5c2029d66a5f9d7970ae81e0eb334bb4a493cde94f3445f5d1a9e25
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a2f4c5d1a4b5947fe6d5ff836291909d5428de7e0745d7e50654327637b173c573646446c4846ca62789b1f590e13fe43f057fa82eac7a03c85faf7483115014
|
7
|
+
data.tar.gz: f87d22f71705710792a13a2490d71a632a3c55fd0f40bd7cf5e342d58b58c6066777e23dab777af0a0e674103182dec768caf04a73d9f6db58a35ed5453f2e9a
|
data/.github/workflows/rake.yml
CHANGED
data/.gitignore
CHANGED
data/Gemfile
CHANGED
@@ -1,10 +1,12 @@
|
|
1
1
|
source "https://rubygems.org"
|
2
|
-
|
3
2
|
gemspec
|
4
3
|
|
4
|
+
gem "rails"
|
5
|
+
|
5
6
|
group :test do
|
7
|
+
gem "benchmark-ips"
|
6
8
|
gem "observr"
|
7
|
-
gem "
|
9
|
+
gem "ricecream"
|
8
10
|
gem "simplecov", require: false
|
9
|
-
gem "
|
11
|
+
gem "standard", require: false
|
10
12
|
end
|
data/Gemfile.lock
CHANGED
@@ -1,12 +1,75 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
miau (1.1.
|
5
|
-
activesupport
|
4
|
+
miau (1.1.6)
|
6
5
|
|
7
6
|
GEM
|
8
7
|
remote: https://rubygems.org/
|
9
8
|
specs:
|
9
|
+
actioncable (7.1.2)
|
10
|
+
actionpack (= 7.1.2)
|
11
|
+
activesupport (= 7.1.2)
|
12
|
+
nio4r (~> 2.0)
|
13
|
+
websocket-driver (>= 0.6.1)
|
14
|
+
zeitwerk (~> 2.6)
|
15
|
+
actionmailbox (7.1.2)
|
16
|
+
actionpack (= 7.1.2)
|
17
|
+
activejob (= 7.1.2)
|
18
|
+
activerecord (= 7.1.2)
|
19
|
+
activestorage (= 7.1.2)
|
20
|
+
activesupport (= 7.1.2)
|
21
|
+
mail (>= 2.7.1)
|
22
|
+
net-imap
|
23
|
+
net-pop
|
24
|
+
net-smtp
|
25
|
+
actionmailer (7.1.2)
|
26
|
+
actionpack (= 7.1.2)
|
27
|
+
actionview (= 7.1.2)
|
28
|
+
activejob (= 7.1.2)
|
29
|
+
activesupport (= 7.1.2)
|
30
|
+
mail (~> 2.5, >= 2.5.4)
|
31
|
+
net-imap
|
32
|
+
net-pop
|
33
|
+
net-smtp
|
34
|
+
rails-dom-testing (~> 2.2)
|
35
|
+
actionpack (7.1.2)
|
36
|
+
actionview (= 7.1.2)
|
37
|
+
activesupport (= 7.1.2)
|
38
|
+
nokogiri (>= 1.8.5)
|
39
|
+
racc
|
40
|
+
rack (>= 2.2.4)
|
41
|
+
rack-session (>= 1.0.1)
|
42
|
+
rack-test (>= 0.6.3)
|
43
|
+
rails-dom-testing (~> 2.2)
|
44
|
+
rails-html-sanitizer (~> 1.6)
|
45
|
+
actiontext (7.1.2)
|
46
|
+
actionpack (= 7.1.2)
|
47
|
+
activerecord (= 7.1.2)
|
48
|
+
activestorage (= 7.1.2)
|
49
|
+
activesupport (= 7.1.2)
|
50
|
+
globalid (>= 0.6.0)
|
51
|
+
nokogiri (>= 1.8.5)
|
52
|
+
actionview (7.1.2)
|
53
|
+
activesupport (= 7.1.2)
|
54
|
+
builder (~> 3.1)
|
55
|
+
erubi (~> 1.11)
|
56
|
+
rails-dom-testing (~> 2.2)
|
57
|
+
rails-html-sanitizer (~> 1.6)
|
58
|
+
activejob (7.1.2)
|
59
|
+
activesupport (= 7.1.2)
|
60
|
+
globalid (>= 0.3.6)
|
61
|
+
activemodel (7.1.2)
|
62
|
+
activesupport (= 7.1.2)
|
63
|
+
activerecord (7.1.2)
|
64
|
+
activemodel (= 7.1.2)
|
65
|
+
activesupport (= 7.1.2)
|
66
|
+
timeout (>= 0.4.0)
|
67
|
+
activestorage (7.1.2)
|
68
|
+
actionpack (= 7.1.2)
|
69
|
+
activejob (= 7.1.2)
|
70
|
+
activerecord (= 7.1.2)
|
71
|
+
activesupport (= 7.1.2)
|
72
|
+
marcel (~> 1.0)
|
10
73
|
activesupport (7.1.2)
|
11
74
|
base64
|
12
75
|
bigdecimal
|
@@ -25,27 +88,116 @@ GEM
|
|
25
88
|
base64 (0.2.0)
|
26
89
|
benchmark-ips (2.13.0)
|
27
90
|
bigdecimal (3.1.5)
|
91
|
+
builder (3.2.4)
|
92
|
+
combustion (1.3.7)
|
93
|
+
activesupport (>= 3.0.0)
|
94
|
+
railties (>= 3.0.0)
|
95
|
+
thor (>= 0.14.6)
|
28
96
|
concurrent-ruby (1.2.2)
|
29
97
|
connection_pool (2.4.1)
|
98
|
+
crass (1.0.6)
|
99
|
+
date (3.3.4)
|
30
100
|
docile (1.4.0)
|
31
101
|
drb (2.2.0)
|
32
102
|
ruby2_keywords
|
103
|
+
erubi (1.12.0)
|
104
|
+
globalid (1.2.1)
|
105
|
+
activesupport (>= 6.1)
|
33
106
|
i18n (1.14.1)
|
34
107
|
concurrent-ruby (~> 1.0)
|
108
|
+
io-console (0.7.1)
|
109
|
+
irb (1.11.1)
|
110
|
+
rdoc
|
111
|
+
reline (>= 0.4.2)
|
35
112
|
json (2.7.1)
|
36
113
|
language_server-protocol (3.17.0.3)
|
37
114
|
lint_roller (1.1.0)
|
38
|
-
|
115
|
+
loofah (2.22.0)
|
116
|
+
crass (~> 1.0.2)
|
117
|
+
nokogiri (>= 1.12.0)
|
118
|
+
mail (2.8.1)
|
119
|
+
mini_mime (>= 0.1.1)
|
120
|
+
net-imap
|
121
|
+
net-pop
|
122
|
+
net-smtp
|
123
|
+
marcel (1.0.2)
|
124
|
+
mini_mime (1.1.5)
|
125
|
+
minitest (5.21.1)
|
39
126
|
mutex_m (0.2.0)
|
127
|
+
net-imap (0.4.9.1)
|
128
|
+
date
|
129
|
+
net-protocol
|
130
|
+
net-pop (0.1.2)
|
131
|
+
net-protocol
|
132
|
+
net-protocol (0.2.2)
|
133
|
+
timeout
|
134
|
+
net-smtp (0.4.0.1)
|
135
|
+
net-protocol
|
136
|
+
nio4r (2.7.0)
|
137
|
+
nokogiri (1.16.0-aarch64-linux)
|
138
|
+
racc (~> 1.4)
|
139
|
+
nokogiri (1.16.0-arm-linux)
|
140
|
+
racc (~> 1.4)
|
141
|
+
nokogiri (1.16.0-arm64-darwin)
|
142
|
+
racc (~> 1.4)
|
143
|
+
nokogiri (1.16.0-x86-linux)
|
144
|
+
racc (~> 1.4)
|
145
|
+
nokogiri (1.16.0-x86_64-darwin)
|
146
|
+
racc (~> 1.4)
|
147
|
+
nokogiri (1.16.0-x86_64-linux)
|
148
|
+
racc (~> 1.4)
|
40
149
|
observr (1.0.5)
|
41
150
|
parallel (1.24.0)
|
42
|
-
parser (3.3.0.
|
151
|
+
parser (3.3.0.3)
|
43
152
|
ast (~> 2.4.1)
|
44
153
|
racc
|
154
|
+
psych (5.1.2)
|
155
|
+
stringio
|
45
156
|
racc (1.7.3)
|
157
|
+
rack (3.0.8)
|
158
|
+
rack-session (2.0.0)
|
159
|
+
rack (>= 3.0.0)
|
160
|
+
rack-test (2.1.0)
|
161
|
+
rack (>= 1.3)
|
162
|
+
rackup (2.1.0)
|
163
|
+
rack (>= 3)
|
164
|
+
webrick (~> 1.8)
|
165
|
+
rails (7.1.2)
|
166
|
+
actioncable (= 7.1.2)
|
167
|
+
actionmailbox (= 7.1.2)
|
168
|
+
actionmailer (= 7.1.2)
|
169
|
+
actionpack (= 7.1.2)
|
170
|
+
actiontext (= 7.1.2)
|
171
|
+
actionview (= 7.1.2)
|
172
|
+
activejob (= 7.1.2)
|
173
|
+
activemodel (= 7.1.2)
|
174
|
+
activerecord (= 7.1.2)
|
175
|
+
activestorage (= 7.1.2)
|
176
|
+
activesupport (= 7.1.2)
|
177
|
+
bundler (>= 1.15.0)
|
178
|
+
railties (= 7.1.2)
|
179
|
+
rails-dom-testing (2.2.0)
|
180
|
+
activesupport (>= 5.0.0)
|
181
|
+
minitest
|
182
|
+
nokogiri (>= 1.6)
|
183
|
+
rails-html-sanitizer (1.6.0)
|
184
|
+
loofah (~> 2.21)
|
185
|
+
nokogiri (~> 1.14)
|
186
|
+
railties (7.1.2)
|
187
|
+
actionpack (= 7.1.2)
|
188
|
+
activesupport (= 7.1.2)
|
189
|
+
irb
|
190
|
+
rackup (>= 1.0.0)
|
191
|
+
rake (>= 12.2)
|
192
|
+
thor (~> 1.0, >= 1.2.2)
|
193
|
+
zeitwerk (~> 2.6)
|
46
194
|
rainbow (3.1.1)
|
47
195
|
rake (13.1.0)
|
48
|
-
|
196
|
+
rdoc (6.6.2)
|
197
|
+
psych (>= 4.0.0)
|
198
|
+
regexp_parser (2.9.0)
|
199
|
+
reline (0.4.2)
|
200
|
+
io-console (~> 0.5)
|
49
201
|
rexml (3.2.6)
|
50
202
|
ricecream (0.2.1)
|
51
203
|
rubocop (1.59.0)
|
@@ -61,7 +213,7 @@ GEM
|
|
61
213
|
unicode-display_width (>= 2.4.0, < 3.0)
|
62
214
|
rubocop-ast (1.30.0)
|
63
215
|
parser (>= 3.2.1.0)
|
64
|
-
rubocop-performance (1.20.
|
216
|
+
rubocop-performance (1.20.2)
|
65
217
|
rubocop (>= 1.48.1, < 2.0)
|
66
218
|
rubocop-ast (>= 1.30.0, < 2.0)
|
67
219
|
ruby-progressbar (1.13.0)
|
@@ -72,6 +224,12 @@ GEM
|
|
72
224
|
simplecov_json_formatter (~> 0.1)
|
73
225
|
simplecov-html (0.12.3)
|
74
226
|
simplecov_json_formatter (0.1.4)
|
227
|
+
sqlite3 (1.7.0-aarch64-linux)
|
228
|
+
sqlite3 (1.7.0-arm-linux)
|
229
|
+
sqlite3 (1.7.0-arm64-darwin)
|
230
|
+
sqlite3 (1.7.0-x86-linux)
|
231
|
+
sqlite3 (1.7.0-x86_64-darwin)
|
232
|
+
sqlite3 (1.7.0-x86_64-linux)
|
75
233
|
standard (1.33.0)
|
76
234
|
language_server-protocol (~> 3.17.0.2)
|
77
235
|
lint_roller (~> 1.0)
|
@@ -81,26 +239,40 @@ GEM
|
|
81
239
|
standard-custom (1.0.2)
|
82
240
|
lint_roller (~> 1.0)
|
83
241
|
rubocop (~> 1.50)
|
84
|
-
standard-performance (1.3.
|
242
|
+
standard-performance (1.3.1)
|
85
243
|
lint_roller (~> 1.1)
|
86
|
-
rubocop-performance (~> 1.20.
|
244
|
+
rubocop-performance (~> 1.20.2)
|
245
|
+
stringio (3.1.0)
|
87
246
|
thor (1.3.0)
|
247
|
+
timeout (0.4.1)
|
88
248
|
tzinfo (2.0.6)
|
89
249
|
concurrent-ruby (~> 1.0)
|
90
250
|
unicode-display_width (2.5.0)
|
251
|
+
webrick (1.8.1)
|
252
|
+
websocket-driver (0.7.6)
|
253
|
+
websocket-extensions (>= 0.1.0)
|
254
|
+
websocket-extensions (0.1.5)
|
255
|
+
zeitwerk (2.6.12)
|
91
256
|
|
92
257
|
PLATFORMS
|
93
|
-
|
258
|
+
aarch64-linux
|
259
|
+
arm-linux
|
260
|
+
arm64-darwin
|
261
|
+
x86-linux
|
262
|
+
x86_64-darwin
|
94
263
|
x86_64-linux
|
95
264
|
|
96
265
|
DEPENDENCIES
|
97
266
|
appraisal
|
98
267
|
benchmark-ips
|
268
|
+
combustion
|
99
269
|
miau!
|
100
270
|
minitest
|
101
271
|
observr
|
272
|
+
rails
|
102
273
|
ricecream
|
103
274
|
simplecov
|
275
|
+
sqlite3
|
104
276
|
standard
|
105
277
|
|
106
278
|
BUNDLED WITH
|
data/README.md
CHANGED
@@ -44,13 +44,12 @@ end
|
|
44
44
|
```ruby
|
45
45
|
# app/policies/application_policy.rb # app/policies/posts_policy.rb
|
46
46
|
class ApplicationPolicy class PostsPolicy < ApplicationPolicy
|
47
|
-
attr_reader :user, :resource ...
|
47
|
+
attr_reader :user, :resource, :action ...
|
48
48
|
def update
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
end
|
49
|
+
... user.admin? && resource.published?
|
50
|
+
end end
|
51
|
+
...
|
52
|
+
end
|
54
53
|
```
|
55
54
|
|
56
55
|
"authorize!" will raise an exception (which can be handled by "rescue")
|
@@ -67,6 +66,8 @@ a corresponding policy method is called.
|
|
67
66
|
|
68
67
|
The policy method has access to the "user" and the "resource".
|
69
68
|
|
69
|
+
The "controller" policy method has access to the "user" and the "action".
|
70
|
+
|
70
71
|
"user" is set by the default method "miau_user" (can be overwritten) as:
|
71
72
|
|
72
73
|
```ruby
|
data/Rakefile
CHANGED
@@ -1,12 +1,10 @@
|
|
1
|
-
require "rubygems"
|
2
|
-
require "bundler/gem_tasks"
|
3
1
|
require "rake/testtask"
|
4
2
|
|
5
|
-
desc "Run
|
3
|
+
desc "Run the tests."
|
6
4
|
Rake::TestTask.new do |t|
|
7
|
-
t.libs
|
8
|
-
t.pattern = "test
|
5
|
+
t.libs << "test"
|
6
|
+
t.pattern = "test/**/*_test.rb"
|
7
|
+
t.verbose = false
|
9
8
|
end
|
10
9
|
|
11
|
-
desc "Default: run unit tests."
|
12
10
|
task default: :test
|
data/gemfiles/rails_6.1.gemfile
CHANGED
@@ -6,10 +6,11 @@ gem "rails", "~> 6.1"
|
|
6
6
|
gem "dryer-config", "~> 6.0"
|
7
7
|
|
8
8
|
group :test do
|
9
|
+
gem "benchmark-ips"
|
9
10
|
gem "observr"
|
10
|
-
gem "
|
11
|
+
gem "ricecream"
|
11
12
|
gem "simplecov", require: false
|
12
|
-
gem "
|
13
|
+
gem "standard", require: false
|
13
14
|
end
|
14
15
|
|
15
16
|
gemspec path: "../"
|
data/gemfiles/rails_7.0.gemfile
CHANGED
@@ -6,10 +6,11 @@ gem "rails", "~> 7.0"
|
|
6
6
|
gem "dryer-config", "~> 7.0"
|
7
7
|
|
8
8
|
group :test do
|
9
|
+
gem "benchmark-ips"
|
9
10
|
gem "observr"
|
10
|
-
gem "
|
11
|
+
gem "ricecream"
|
11
12
|
gem "simplecov", require: false
|
12
|
-
gem "
|
13
|
+
gem "standard", require: false
|
13
14
|
end
|
14
15
|
|
15
16
|
gemspec path: "../"
|
data/gemfiles/rails_7.1.gemfile
CHANGED
@@ -5,10 +5,11 @@ source "https://rubygems.org"
|
|
5
5
|
gem "rails", "~> 7.1"
|
6
6
|
|
7
7
|
group :test do
|
8
|
+
gem "benchmark-ips"
|
8
9
|
gem "observr"
|
9
|
-
gem "
|
10
|
+
gem "ricecream"
|
10
11
|
gem "simplecov", require: false
|
11
|
-
gem "
|
12
|
+
gem "standard", require: false
|
12
13
|
end
|
13
14
|
|
14
15
|
gemspec path: "../"
|
data/lib/miau/run.rb
CHANGED
@@ -7,7 +7,7 @@ module Miau
|
|
7
7
|
class PolicyRun
|
8
8
|
include Singleton
|
9
9
|
|
10
|
-
# return
|
10
|
+
# return method[s]
|
11
11
|
# klass and action are symbols
|
12
12
|
# Priority:
|
13
13
|
# - method of <klass>Policy
|
@@ -17,7 +17,7 @@ module Miau
|
|
17
17
|
# - nil
|
18
18
|
# returns method_name[s]
|
19
19
|
|
20
|
-
def
|
20
|
+
def find_methods(policy, klass, action)
|
21
21
|
return action if policy.respond_to?(action)
|
22
22
|
|
23
23
|
hsh = PolicyStorage.instance.policies[klass]
|
@@ -26,22 +26,23 @@ module Miau
|
|
26
26
|
hsh[action]
|
27
27
|
end
|
28
28
|
|
29
|
-
def
|
30
|
-
|
31
|
-
|
32
|
-
meth ||= find_policy ApplicationPolicy, :application, action
|
29
|
+
def runs(policy, actions)
|
30
|
+
[actions].flatten.each { |action|
|
31
|
+
raise_undef(policy, action) unless policy&.respond_to?(action)
|
33
32
|
|
34
|
-
|
35
|
-
msg = "class <#{klass}> action <#{action}>"
|
36
|
-
raise NotDefinedError, msg
|
37
|
-
end
|
38
|
-
|
39
|
-
policy.user = user
|
40
|
-
policy.resource = resource
|
41
|
-
[meth].flatten.each { |m|
|
42
|
-
return false unless policy.send(m)
|
33
|
+
return false unless policy.send(action)
|
43
34
|
}
|
44
35
|
true
|
45
36
|
end
|
37
|
+
|
38
|
+
def raise_undef(policy, action)
|
39
|
+
msg = "NotDefined policy <#{policy}> action <#{action}>"
|
40
|
+
raise NotDefinedError, msg
|
41
|
+
end
|
42
|
+
|
43
|
+
def raise_authorize(controller, action)
|
44
|
+
msg = "NotAuthorized controller <#{controller}> action <#{action}>"
|
45
|
+
raise NotAuthorizedError, msg
|
46
|
+
end
|
46
47
|
end
|
47
48
|
end
|
data/lib/miau/storage.rb
CHANGED
@@ -18,7 +18,7 @@ module Miau
|
|
18
18
|
# }
|
19
19
|
# }
|
20
20
|
attr_reader :policies
|
21
|
-
attr_reader :instances # { posts: PostsPolicy.new }
|
21
|
+
attr_reader :instances # e.g. { posts: PostsPolicy.new }
|
22
22
|
|
23
23
|
def initialize
|
24
24
|
reset
|
@@ -38,7 +38,7 @@ module Miau
|
|
38
38
|
end
|
39
39
|
|
40
40
|
if meth.is_a?(Array)
|
41
|
-
meths = [meth].flatten.collect
|
41
|
+
meths = [meth].flatten.collect(&:to_sym)
|
42
42
|
@policies[kls][action] = meths
|
43
43
|
else
|
44
44
|
@policies[kls][action] = meth.to_sym
|
data/lib/miau/version.rb
CHANGED
data/lib/miau.rb
CHANGED
@@ -21,14 +21,36 @@ module Miau
|
|
21
21
|
@_miau_authorization_performed = true
|
22
22
|
return true if authorized?(resource, hsh)
|
23
23
|
|
24
|
-
|
25
|
-
|
26
|
-
|
24
|
+
controller = params[:controller].to_sym
|
25
|
+
action = params[:action].to_sym
|
26
|
+
PolicyRun.instance.raise_authorize(controller, action)
|
27
27
|
end
|
28
28
|
|
29
29
|
def authorized?(resource = nil, hsh = {})
|
30
|
-
|
31
|
-
|
30
|
+
controller = params[:controller].to_sym
|
31
|
+
action = params[:action].to_sym
|
32
|
+
policy = PolicyStorage.instance.find_or_create_policy(controller)
|
33
|
+
PolicyRun.instance.raise_undef(policy, action) unless policy
|
34
|
+
|
35
|
+
policy.user = miau_user
|
36
|
+
policy.resource = resource
|
37
|
+
methods = PolicyRun.instance.find_methods(policy, controller, action)
|
38
|
+
PolicyRun.instance.raise_undef(policy, action) unless methods
|
39
|
+
|
40
|
+
PolicyRun.instance.runs(policy, methods)
|
41
|
+
end
|
42
|
+
|
43
|
+
def authorize_controller!
|
44
|
+
controller = params[:controller].to_sym
|
45
|
+
action = params[:action].to_sym
|
46
|
+
policy = PolicyStorage.instance.find_or_create_policy(controller)
|
47
|
+
policy.user = miau_user
|
48
|
+
policy.action = action
|
49
|
+
|
50
|
+
@_miau_authorization_performed = true
|
51
|
+
return true if PolicyRun.instance.runs(policy, :controller)
|
52
|
+
|
53
|
+
PolicyRun.instance.raise_authorize policy, action
|
32
54
|
end
|
33
55
|
|
34
56
|
def miau_user
|
@@ -42,18 +64,4 @@ module Miau
|
|
42
64
|
def miau_authorization_performed?
|
43
65
|
!!@_miau_authorization_performed
|
44
66
|
end
|
45
|
-
|
46
|
-
def authorize_controller!
|
47
|
-
name = params[:controller].to_sym
|
48
|
-
policy = PolicyStorage.instance.find_or_create_policy(name)
|
49
|
-
raise NotDefinedError unless policy&.respond_to?(:controller)
|
50
|
-
|
51
|
-
policy.send(:controller)
|
52
|
-
end
|
53
|
-
|
54
|
-
private
|
55
|
-
|
56
|
-
def klass_action
|
57
|
-
[params[:controller].to_sym, params[:action].to_sym]
|
58
|
-
end
|
59
67
|
end
|
data/miau.gemspec
CHANGED
@@ -8,22 +8,20 @@ Gem::Specification.new do |s|
|
|
8
8
|
s.license = "MIT"
|
9
9
|
|
10
10
|
s.description = <<~EOS
|
11
|
-
MIAU (MIcro AUthorization) provides
|
12
|
-
|
11
|
+
MIAU (MIcro AUthorization) provides some helpers which
|
12
|
+
raises an exception if a given user violates a policy.
|
13
13
|
EOS
|
14
14
|
|
15
|
-
s.executables = s.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
16
|
-
s.require_paths = ["lib"]
|
17
|
-
|
18
15
|
s.authors = ["Dittmar Krall"]
|
19
16
|
s.email = "dittmar.krall@matiq.com"
|
20
17
|
s.homepage = "https://github.com/matique/miau"
|
21
18
|
|
22
|
-
s.files = `git ls-files
|
23
|
-
|
24
|
-
s.add_runtime_dependency "activesupport"
|
19
|
+
s.files = `git ls-files`.split("\n")
|
20
|
+
s.require_paths = ["lib"]
|
25
21
|
|
26
22
|
s.add_development_dependency "appraisal"
|
23
|
+
s.add_development_dependency "combustion"
|
27
24
|
s.add_development_dependency "minitest"
|
28
25
|
s.add_development_dependency "ricecream"
|
26
|
+
s.add_development_dependency "sqlite3"
|
29
27
|
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
describe Miau, "performed authorization" do
|
4
|
+
let(:user) { "User" }
|
5
|
+
let(:post) { Post.new(user, 1) }
|
6
|
+
let(:params) { {action: "si", controller: "posts"} }
|
7
|
+
let(:posts_controller) { PostsController.new(user, params) }
|
8
|
+
|
9
|
+
def test_does_nothing_when_authorized
|
10
|
+
posts_controller.authorize!(post)
|
11
|
+
posts_controller.verify_authorized
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_exception_when_not_authorized
|
15
|
+
assert_raises(Miau::AuthorizationNotPerformedError) {
|
16
|
+
posts_controller.verify_authorized
|
17
|
+
}
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_authorization_performed
|
21
|
+
posts_controller.authorize!(post)
|
22
|
+
assert posts_controller.miau_authorization_performed?
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_authorization_not_performed
|
26
|
+
refute posts_controller.miau_authorization_performed?
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
require "benchmark"
|
3
|
+
require "benchmark/ips"
|
4
|
+
|
5
|
+
describe "Internal timings" do
|
6
|
+
let(:user) { "User" }
|
7
|
+
let(:params) { {action: "si", controller: "posts"} }
|
8
|
+
let(:posts_controller) { PostsController.new(user, params) }
|
9
|
+
let(:storage) { Miau::PolicyStorage.instance }
|
10
|
+
|
11
|
+
# run_flag = true
|
12
|
+
run_flag ||= false
|
13
|
+
|
14
|
+
it "times ips" do
|
15
|
+
return unless run_flag
|
16
|
+
|
17
|
+
Benchmark.ips do |x|
|
18
|
+
x.report("empty ") {}
|
19
|
+
x.report("authorize! ") { posts_controller.authorize! }
|
20
|
+
x.report("authorized? ") { posts_controller.authorized? }
|
21
|
+
# x.report("PostsPolicy.new") { PostsPolicy.new }
|
22
|
+
# x.report("find_or_create_policy") {
|
23
|
+
# storage.find_or_create_policy "application"
|
24
|
+
# }
|
25
|
+
# x.report("name & constantize.new") {
|
26
|
+
# klass = :posts
|
27
|
+
# name = "#{klass.to_s.camelcase}Policy"
|
28
|
+
# name.constantize.new
|
29
|
+
# }
|
30
|
+
|
31
|
+
x.compare!
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|