miau 1.0.3 → 1.1.1

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
  SHA256:
3
- metadata.gz: '008262a0054143f56caffceb6dfa57f195963ff6b98cc07aa6452778f5ad87b5'
4
- data.tar.gz: 1eca9f4196b9569d0515b9c14da47182fecdd1c05b6bbc388b1f16226bb3ee36
3
+ metadata.gz: d207e809bfd3a605612092afc04287df2def3175e71a0a0b2c98c0ce491e1402
4
+ data.tar.gz: 4d5985b0b22a467848c0d5e1bc3fd14a22b3231b928e42fafa922f1faa5520fc
5
5
  SHA512:
6
- metadata.gz: 62497618e0cb83822bb448d9d6a7fbeac80943163284677033dc1e8b555b42440434ff3f16dec7ed16f1b3b1db629815bd42530cf0de1ea457b2eb2ec6cecc96
7
- data.tar.gz: f2d0a731b6a61448790ab745332e2d6f4bf9225fbb9aa947a7fa4438f15bb0a1818df4cbec818c5546ced6b166825a8ecabb35b5fdc060482faee7fae097d18e
6
+ metadata.gz: ba62cbe78a200045d483bb6b3c544fdbd57aa1a7b24a20ed73b8d5a3304d31420d088441edee0c2d16feaa4223c468375f1e985a78e38263c427e6e139173d6f
7
+ data.tar.gz: ff00545b68b70128919cc417ba69906e38eaf5decab73681bed88dc291fc648e8cdfa0c40e32942d08ed82605384ad5c76dd0723514f2a95cf6ec7e8deefdb62
@@ -8,7 +8,7 @@ jobs:
8
8
  strategy:
9
9
  fail-fast: false
10
10
  matrix:
11
- ruby_version: ["2.7", "3.0", "3.2", head]
11
+ ruby_version: ["3.0", "3.2", head]
12
12
  gemfile:
13
13
  - Gemfile
14
14
  - gemfiles/Gemfile.rails-7.1
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- ruby-3.2.2
1
+ ruby-3.3.0
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- miau (1.0.3)
4
+ miau (1.1.1)
5
5
  activesupport
6
6
 
7
7
  GEM
@@ -38,8 +38,8 @@ GEM
38
38
  minitest (5.20.0)
39
39
  mutex_m (0.2.0)
40
40
  observr (1.0.5)
41
- parallel (1.23.0)
42
- parser (3.2.2.4)
41
+ parallel (1.24.0)
42
+ parser (3.3.0.0)
43
43
  ast (~> 2.4.1)
44
44
  racc
45
45
  racc (1.7.3)
@@ -48,7 +48,7 @@ GEM
48
48
  regexp_parser (2.8.3)
49
49
  rexml (3.2.6)
50
50
  ricecream (0.2.1)
51
- rubocop (1.57.2)
51
+ rubocop (1.59.0)
52
52
  json (~> 2.3)
53
53
  language_server-protocol (>= 3.17.0)
54
54
  parallel (~> 1.10)
@@ -56,14 +56,14 @@ GEM
56
56
  rainbow (>= 2.2.2, < 4.0)
57
57
  regexp_parser (>= 1.8, < 3.0)
58
58
  rexml (>= 3.2.5, < 4.0)
59
- rubocop-ast (>= 1.28.1, < 2.0)
59
+ rubocop-ast (>= 1.30.0, < 2.0)
60
60
  ruby-progressbar (~> 1.7)
61
61
  unicode-display_width (>= 2.4.0, < 3.0)
62
62
  rubocop-ast (1.30.0)
63
63
  parser (>= 3.2.1.0)
64
- rubocop-performance (1.19.1)
65
- rubocop (>= 1.7.0, < 2.0)
66
- rubocop-ast (>= 0.4.0)
64
+ rubocop-performance (1.20.1)
65
+ rubocop (>= 1.48.1, < 2.0)
66
+ rubocop-ast (>= 1.30.0, < 2.0)
67
67
  ruby-progressbar (1.13.0)
68
68
  ruby2_keywords (0.0.5)
69
69
  simplecov (0.22.0)
@@ -72,24 +72,25 @@ GEM
72
72
  simplecov_json_formatter (~> 0.1)
73
73
  simplecov-html (0.12.3)
74
74
  simplecov_json_formatter (0.1.4)
75
- standard (1.32.1)
75
+ standard (1.33.0)
76
76
  language_server-protocol (~> 3.17.0.2)
77
77
  lint_roller (~> 1.0)
78
- rubocop (~> 1.57.2)
78
+ rubocop (~> 1.59.0)
79
79
  standard-custom (~> 1.0.0)
80
- standard-performance (~> 1.2)
80
+ standard-performance (~> 1.3)
81
81
  standard-custom (1.0.2)
82
82
  lint_roller (~> 1.0)
83
83
  rubocop (~> 1.50)
84
- standard-performance (1.2.1)
84
+ standard-performance (1.3.0)
85
85
  lint_roller (~> 1.1)
86
- rubocop-performance (~> 1.19.1)
86
+ rubocop-performance (~> 1.20.1)
87
87
  thor (1.3.0)
88
88
  tzinfo (2.0.6)
89
89
  concurrent-ruby (~> 1.0)
90
90
  unicode-display_width (2.5.0)
91
91
 
92
92
  PLATFORMS
93
+ ruby
93
94
  x86_64-linux
94
95
 
95
96
  DEPENDENCIES
@@ -98,10 +99,9 @@ DEPENDENCIES
98
99
  miau!
99
100
  minitest
100
101
  observr
101
- rake
102
102
  ricecream
103
103
  simplecov
104
104
  standard
105
105
 
106
106
  BUNDLED WITH
107
- 2.4.22
107
+ 2.5.3
data/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2021-2023 Dittmar Krall (www.matiq.com)
3
+ Copyright (c) 2021-2024 Dittmar Krall (www.matiq.com)
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -1,5 +1,5 @@
1
- Miau
2
- ====
1
+ # Miau
2
+
3
3
  [![Gem Version](https://badge.fury.io/rb/miau.png)](http://badge.fury.io/rb/miau)
4
4
  [![GEM Downloads](https://img.shields.io/gem/dt/miau?color=168AFE&logo=ruby&logoColor=FE1616)](https://rubygems.org/gems/miau)
5
5
 
@@ -60,8 +60,7 @@ in case a policy returns "false" or isn't available.
60
60
 
61
61
  "app/policies/application_policy.rb" is included in the gem.
62
62
 
63
- Internals
64
- ---------
63
+ ## Internals
65
64
 
66
65
  At the bottom line based on a "policy" and an "action"
67
66
  a corresponding policy method is called.
@@ -93,8 +92,7 @@ A full blown sample :
93
92
  authorize! article, policy: :posts, action: :show
94
93
  ```
95
94
 
96
- Usage (more elaborated)
97
- -----------------------
95
+ ## Usage (more elaborated)
98
96
 
99
97
  ```ruby
100
98
  # app/models/application_controller.rb
@@ -127,8 +125,27 @@ Rescue's may be inserted previously in the exception chain.
127
125
 
128
126
  "verify_authorized" checks that an "authorize!" has been called.
129
127
 
130
- DRYing
131
- ------
128
+ ## Authorize Controller
129
+
130
+ Sometimes a whole controller can be authorized,
131
+ e.g. all actions requires admin priviledges.
132
+
133
+ The corresponding authorization is triggered by
134
+ a "before_action :authorize_controller!".
135
+ The corresponding policy is
136
+ defined by (e.g):
137
+
138
+ ```ruby
139
+ class PostsPolicy < ApplicationPolicy
140
+ ...
141
+ def controller
142
+ user.is_admin?
143
+ end
144
+ ...
145
+ end
146
+ ```
147
+
148
+ ## DRYing
132
149
 
133
150
  ```ruby
134
151
  # app/policies/posts_policy.rb --> # app/policies/posts_policy.rb
@@ -148,8 +165,8 @@ DRYing
148
165
  end
149
166
  ```
150
167
 
151
- PORO
152
- ----
168
+ ## PORO
169
+
153
170
  Miau is a small gem, it just provides a few helpers.
154
171
  All of the policy classes are just plain Ruby classes,
155
172
  allowing DRY, encapsulation, aliasing and inheritance.
@@ -159,5 +176,5 @@ Just the embedding in Rails required some specific knowledge.
159
176
 
160
177
  ## Miscellaneous
161
178
 
162
- Copyright (c) 2021-2023 Dittmar Krall (www.matiq.com),
179
+ Copyright (c) 2021-2024 Dittmar Krall (www.matiq.com),
163
180
  released under the [MIT license](https://opensource.org/licenses/MIT).
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- miau (1.0.2)
4
+ miau (1.0.3)
5
5
  activesupport
6
6
 
7
7
  GEM
@@ -98,21 +98,21 @@ GEM
98
98
  method_source (1.0.0)
99
99
  mini_mime (1.1.5)
100
100
  minitest (5.20.0)
101
- net-imap (0.4.8)
101
+ net-imap (0.4.9.1)
102
102
  date
103
103
  net-protocol
104
104
  net-pop (0.1.2)
105
105
  net-protocol
106
106
  net-protocol (0.2.2)
107
107
  timeout
108
- net-smtp (0.4.0)
108
+ net-smtp (0.4.0.1)
109
109
  net-protocol
110
110
  nio4r (2.7.0)
111
- nokogiri (1.15.5-x86_64-linux)
111
+ nokogiri (1.16.0-x86_64-linux)
112
112
  racc (~> 1.4)
113
113
  observr (1.0.5)
114
- parallel (1.23.0)
115
- parser (3.2.2.4)
114
+ parallel (1.24.0)
115
+ parser (3.3.0.0)
116
116
  ast (~> 2.4.1)
117
117
  racc
118
118
  racc (1.7.3)
@@ -152,7 +152,7 @@ GEM
152
152
  regexp_parser (2.8.3)
153
153
  rexml (3.2.6)
154
154
  ricecream (0.2.1)
155
- rubocop (1.57.2)
155
+ rubocop (1.59.0)
156
156
  json (~> 2.3)
157
157
  language_server-protocol (>= 3.17.0)
158
158
  parallel (~> 1.10)
@@ -160,14 +160,14 @@ GEM
160
160
  rainbow (>= 2.2.2, < 4.0)
161
161
  regexp_parser (>= 1.8, < 3.0)
162
162
  rexml (>= 3.2.5, < 4.0)
163
- rubocop-ast (>= 1.28.1, < 2.0)
163
+ rubocop-ast (>= 1.30.0, < 2.0)
164
164
  ruby-progressbar (~> 1.7)
165
165
  unicode-display_width (>= 2.4.0, < 3.0)
166
166
  rubocop-ast (1.30.0)
167
167
  parser (>= 3.2.1.0)
168
- rubocop-performance (1.19.1)
169
- rubocop (>= 1.7.0, < 2.0)
170
- rubocop-ast (>= 0.4.0)
168
+ rubocop-performance (1.20.1)
169
+ rubocop (>= 1.48.1, < 2.0)
170
+ rubocop-ast (>= 1.30.0, < 2.0)
171
171
  ruby-progressbar (1.13.0)
172
172
  simplecov (0.22.0)
173
173
  docile (~> 1.1)
@@ -182,18 +182,18 @@ GEM
182
182
  actionpack (>= 5.2)
183
183
  activesupport (>= 5.2)
184
184
  sprockets (>= 3.0.0)
185
- standard (1.32.1)
185
+ standard (1.33.0)
186
186
  language_server-protocol (~> 3.17.0.2)
187
187
  lint_roller (~> 1.0)
188
- rubocop (~> 1.57.2)
188
+ rubocop (~> 1.59.0)
189
189
  standard-custom (~> 1.0.0)
190
- standard-performance (~> 1.2)
190
+ standard-performance (~> 1.3)
191
191
  standard-custom (1.0.2)
192
192
  lint_roller (~> 1.0)
193
193
  rubocop (~> 1.50)
194
- standard-performance (1.2.1)
194
+ standard-performance (1.3.0)
195
195
  lint_roller (~> 1.1)
196
- rubocop-performance (~> 1.19.1)
196
+ rubocop-performance (~> 1.20.1)
197
197
  thor (1.3.0)
198
198
  timeout (0.4.1)
199
199
  tzinfo (2.0.6)
@@ -215,10 +215,9 @@ DEPENDENCIES
215
215
  minitest
216
216
  observr
217
217
  rails (~> 6.1)
218
- rake
219
218
  ricecream
220
219
  simplecov
221
220
  standard
222
221
 
223
222
  BUNDLED WITH
224
- 2.4.22
223
+ 2.5.3
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- miau (1.0.2)
4
+ miau (1.0.3)
5
5
  activesupport
6
6
 
7
7
  GEM
@@ -103,8 +103,8 @@ GEM
103
103
  activesupport (>= 6.1)
104
104
  i18n (1.14.1)
105
105
  concurrent-ruby (~> 1.0)
106
- io-console (0.7.0)
107
- irb (1.10.1)
106
+ io-console (0.7.1)
107
+ irb (1.11.0)
108
108
  rdoc
109
109
  reline (>= 0.3.8)
110
110
  json (2.7.1)
@@ -122,24 +122,24 @@ GEM
122
122
  mini_mime (1.1.5)
123
123
  minitest (5.20.0)
124
124
  mutex_m (0.2.0)
125
- net-imap (0.4.8)
125
+ net-imap (0.4.9.1)
126
126
  date
127
127
  net-protocol
128
128
  net-pop (0.1.2)
129
129
  net-protocol
130
130
  net-protocol (0.2.2)
131
131
  timeout
132
- net-smtp (0.4.0)
132
+ net-smtp (0.4.0.1)
133
133
  net-protocol
134
134
  nio4r (2.7.0)
135
- nokogiri (1.15.5-x86_64-linux)
135
+ nokogiri (1.16.0-x86_64-linux)
136
136
  racc (~> 1.4)
137
137
  observr (1.0.5)
138
- parallel (1.23.0)
139
- parser (3.2.2.4)
138
+ parallel (1.24.0)
139
+ parser (3.3.0.0)
140
140
  ast (~> 2.4.1)
141
141
  racc
142
- psych (5.1.1.1)
142
+ psych (5.1.2)
143
143
  stringio
144
144
  racc (1.7.3)
145
145
  rack (3.0.8)
@@ -181,14 +181,14 @@ GEM
181
181
  zeitwerk (~> 2.6)
182
182
  rainbow (3.1.1)
183
183
  rake (13.1.0)
184
- rdoc (6.6.1)
184
+ rdoc (6.6.2)
185
185
  psych (>= 4.0.0)
186
186
  regexp_parser (2.8.3)
187
- reline (0.4.1)
187
+ reline (0.4.2)
188
188
  io-console (~> 0.5)
189
189
  rexml (3.2.6)
190
190
  ricecream (0.2.1)
191
- rubocop (1.57.2)
191
+ rubocop (1.59.0)
192
192
  json (~> 2.3)
193
193
  language_server-protocol (>= 3.17.0)
194
194
  parallel (~> 1.10)
@@ -196,14 +196,14 @@ GEM
196
196
  rainbow (>= 2.2.2, < 4.0)
197
197
  regexp_parser (>= 1.8, < 3.0)
198
198
  rexml (>= 3.2.5, < 4.0)
199
- rubocop-ast (>= 1.28.1, < 2.0)
199
+ rubocop-ast (>= 1.30.0, < 2.0)
200
200
  ruby-progressbar (~> 1.7)
201
201
  unicode-display_width (>= 2.4.0, < 3.0)
202
202
  rubocop-ast (1.30.0)
203
203
  parser (>= 3.2.1.0)
204
- rubocop-performance (1.19.1)
205
- rubocop (>= 1.7.0, < 2.0)
206
- rubocop-ast (>= 0.4.0)
204
+ rubocop-performance (1.20.1)
205
+ rubocop (>= 1.48.1, < 2.0)
206
+ rubocop-ast (>= 1.30.0, < 2.0)
207
207
  ruby-progressbar (1.13.0)
208
208
  ruby2_keywords (0.0.5)
209
209
  simplecov (0.22.0)
@@ -212,18 +212,18 @@ GEM
212
212
  simplecov_json_formatter (~> 0.1)
213
213
  simplecov-html (0.12.3)
214
214
  simplecov_json_formatter (0.1.4)
215
- standard (1.32.1)
215
+ standard (1.33.0)
216
216
  language_server-protocol (~> 3.17.0.2)
217
217
  lint_roller (~> 1.0)
218
- rubocop (~> 1.57.2)
218
+ rubocop (~> 1.59.0)
219
219
  standard-custom (~> 1.0.0)
220
- standard-performance (~> 1.2)
220
+ standard-performance (~> 1.3)
221
221
  standard-custom (1.0.2)
222
222
  lint_roller (~> 1.0)
223
223
  rubocop (~> 1.50)
224
- standard-performance (1.2.1)
224
+ standard-performance (1.3.0)
225
225
  lint_roller (~> 1.1)
226
- rubocop-performance (~> 1.19.1)
226
+ rubocop-performance (~> 1.20.1)
227
227
  stringio (3.1.0)
228
228
  thor (1.3.0)
229
229
  timeout (0.4.1)
@@ -247,10 +247,9 @@ DEPENDENCIES
247
247
  minitest
248
248
  observr
249
249
  rails (~> 7.0)
250
- rake
251
250
  ricecream
252
251
  simplecov
253
252
  standard
254
253
 
255
254
  BUNDLED WITH
256
- 2.4.22
255
+ 2.5.3
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- miau (1.0.2)
4
+ miau (1.0.3)
5
5
  activesupport
6
6
 
7
7
  GEM
@@ -102,8 +102,8 @@ GEM
102
102
  activesupport (>= 6.1)
103
103
  i18n (1.14.1)
104
104
  concurrent-ruby (~> 1.0)
105
- io-console (0.7.0)
106
- irb (1.10.1)
105
+ io-console (0.7.1)
106
+ irb (1.11.0)
107
107
  rdoc
108
108
  reline (>= 0.3.8)
109
109
  json (2.7.1)
@@ -121,24 +121,24 @@ GEM
121
121
  mini_mime (1.1.5)
122
122
  minitest (5.20.0)
123
123
  mutex_m (0.2.0)
124
- net-imap (0.4.8)
124
+ net-imap (0.4.9.1)
125
125
  date
126
126
  net-protocol
127
127
  net-pop (0.1.2)
128
128
  net-protocol
129
129
  net-protocol (0.2.2)
130
130
  timeout
131
- net-smtp (0.4.0)
131
+ net-smtp (0.4.0.1)
132
132
  net-protocol
133
133
  nio4r (2.7.0)
134
- nokogiri (1.15.5-x86_64-linux)
134
+ nokogiri (1.16.0-x86_64-linux)
135
135
  racc (~> 1.4)
136
136
  observr (1.0.5)
137
- parallel (1.23.0)
138
- parser (3.2.2.4)
137
+ parallel (1.24.0)
138
+ parser (3.3.0.0)
139
139
  ast (~> 2.4.1)
140
140
  racc
141
- psych (5.1.1.1)
141
+ psych (5.1.2)
142
142
  stringio
143
143
  racc (1.7.3)
144
144
  rack (3.0.8)
@@ -180,14 +180,14 @@ GEM
180
180
  zeitwerk (~> 2.6)
181
181
  rainbow (3.1.1)
182
182
  rake (13.1.0)
183
- rdoc (6.6.1)
183
+ rdoc (6.6.2)
184
184
  psych (>= 4.0.0)
185
185
  regexp_parser (2.8.3)
186
- reline (0.4.1)
186
+ reline (0.4.2)
187
187
  io-console (~> 0.5)
188
188
  rexml (3.2.6)
189
189
  ricecream (0.2.1)
190
- rubocop (1.57.2)
190
+ rubocop (1.59.0)
191
191
  json (~> 2.3)
192
192
  language_server-protocol (>= 3.17.0)
193
193
  parallel (~> 1.10)
@@ -195,14 +195,14 @@ GEM
195
195
  rainbow (>= 2.2.2, < 4.0)
196
196
  regexp_parser (>= 1.8, < 3.0)
197
197
  rexml (>= 3.2.5, < 4.0)
198
- rubocop-ast (>= 1.28.1, < 2.0)
198
+ rubocop-ast (>= 1.30.0, < 2.0)
199
199
  ruby-progressbar (~> 1.7)
200
200
  unicode-display_width (>= 2.4.0, < 3.0)
201
201
  rubocop-ast (1.30.0)
202
202
  parser (>= 3.2.1.0)
203
- rubocop-performance (1.19.1)
204
- rubocop (>= 1.7.0, < 2.0)
205
- rubocop-ast (>= 0.4.0)
203
+ rubocop-performance (1.20.1)
204
+ rubocop (>= 1.48.1, < 2.0)
205
+ rubocop-ast (>= 1.30.0, < 2.0)
206
206
  ruby-progressbar (1.13.0)
207
207
  ruby2_keywords (0.0.5)
208
208
  simplecov (0.22.0)
@@ -211,18 +211,18 @@ GEM
211
211
  simplecov_json_formatter (~> 0.1)
212
212
  simplecov-html (0.12.3)
213
213
  simplecov_json_formatter (0.1.4)
214
- standard (1.32.1)
214
+ standard (1.33.0)
215
215
  language_server-protocol (~> 3.17.0.2)
216
216
  lint_roller (~> 1.0)
217
- rubocop (~> 1.57.2)
217
+ rubocop (~> 1.59.0)
218
218
  standard-custom (~> 1.0.0)
219
- standard-performance (~> 1.2)
219
+ standard-performance (~> 1.3)
220
220
  standard-custom (1.0.2)
221
221
  lint_roller (~> 1.0)
222
222
  rubocop (~> 1.50)
223
- standard-performance (1.2.1)
223
+ standard-performance (1.3.0)
224
224
  lint_roller (~> 1.1)
225
- rubocop-performance (~> 1.19.1)
225
+ rubocop-performance (~> 1.20.1)
226
226
  stringio (3.1.0)
227
227
  thor (1.3.0)
228
228
  timeout (0.4.1)
@@ -245,10 +245,9 @@ DEPENDENCIES
245
245
  minitest
246
246
  observr
247
247
  rails (~> 7.1)
248
- rake
249
248
  ricecream
250
249
  simplecov
251
250
  standard
252
251
 
253
252
  BUNDLED WITH
254
- 2.4.22
253
+ 2.5.3
@@ -4,8 +4,9 @@ class ApplicationPolicy
4
4
  attr_accessor :user, :resource
5
5
 
6
6
  def self.miau(actions, meth = nil, &block)
7
+ kls = name.underscore[0..-8] # remove "_policy"
7
8
  [actions].flatten.each { |action|
8
- Miau::PolicyStorage.instance.add(self, action, meth)
9
+ Miau::PolicyStorage.instance.add_policy(kls, action, meth)
9
10
  }
10
11
  end
11
12
  end
data/lib/miau/error.rb CHANGED
@@ -11,4 +11,7 @@ module Miau
11
11
 
12
12
  class AuthorizationNotPerformedError < Error
13
13
  end
14
+
15
+ class OverwriteError < Error
16
+ end
14
17
  end
data/lib/miau/run.rb ADDED
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "singleton"
4
+ require "yaml"
5
+
6
+ module Miau
7
+ class PolicyRun
8
+ include Singleton
9
+
10
+ # return instance of policy (may be nil) and the method
11
+ # klass and action are symbols
12
+ # Priority:
13
+ # - method of <klass>Policy
14
+ # - method of <klass>Policy specified by "miau action, method"
15
+ # - method of ApplicationPolicy (independent of klass)
16
+ # - method of ApplicationPolicy specified by "miau action, method"
17
+ # - nil
18
+ # returns method_name[s]
19
+
20
+ def find_policy(policy, klass, action)
21
+ return action if policy.respond_to?(action)
22
+
23
+ hsh = PolicyStorage.instance.policies[klass]
24
+ return nil unless hsh
25
+
26
+ hsh[action]
27
+ end
28
+
29
+ def run(klass, action, user, resource)
30
+ policy = PolicyStorage.instance.find_or_create_policy(klass)
31
+ meth = find_policy policy, klass, action if policy
32
+ meth ||= find_policy ApplicationPolicy, :application, action
33
+
34
+ unless meth
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)
43
+ }
44
+ true
45
+ end
46
+ end
47
+ end
data/lib/miau/storage.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "singleton"
4
+ require "yaml"
4
5
 
5
6
  module Miau
6
7
  class PolicyStorage
@@ -28,70 +29,35 @@ module Miau
28
29
  @instances = {}
29
30
  end
30
31
 
31
- def add(klass, action, meth)
32
- kls = klass.name.underscore[0..-8] # remove "_policy"
32
+ def add_policy(kls, action, meth)
33
33
  kls = kls.to_sym
34
+ action = action.to_sym
34
35
  @policies[kls] ||= {}
35
- @instances[kls] ||= klass.new
36
- @policies[kls][action.to_sym] = meth.to_sym
37
- end
38
-
39
- # return instance of policy (may be nil) and the method
40
- # klass and action are symbols
41
- # Priority:
42
- # - method of <klass>Policy
43
- # - method of <klass>Policy specified by "miau action, method"
44
- # - method of ApplicationPolicy (independent of klass)
45
- # - method of ApplicationPolicy specified by "miau action, method"
46
- # - nil
47
-
48
- # returns policy: [instance, method]
49
- def find_policy(klass, action)
50
- kls = instance_of(klass)
51
- act = policy_method(klass, action)
52
- return [kls, act] if kls.respond_to?(act)
53
-
54
- klass = :application
55
- kls = instance_of(klass)
56
- act = policy_method(klass, action)
57
- [kls, act] if kls.respond_to?(act)
58
- end
59
-
60
- def run(klass, action, user, resource)
61
- arr = find_policy(klass, action)
62
- unless arr
63
- msg = "class <#{klass}> action <#{action}>"
64
- raise Miau::NotDefinedError, msg
36
+ if @policies[kls][action]
37
+ raise OverwriteError, "Can't overwrite policy(#{kls}, #{action})"
65
38
  end
66
39
 
67
- policy, meth = arr
68
- policy.user = user
69
- policy.resource = resource
70
- policy.send(meth)
71
- end
72
-
73
- def to_yaml
74
- "# === @policies ===\n" + YAML.dump(@policies) +
75
- "# === @instances ===\n" + YAML.dump(@instances)
40
+ if meth.is_a?(Array)
41
+ meths = [meth].flatten.collect { |m| m.to_sym }
42
+ @policies[kls][action] = meths
43
+ else
44
+ @policies[kls][action] = meth.to_sym
45
+ end
76
46
  end
77
47
 
78
- private
79
-
80
- def instance_of(klass)
48
+ def find_or_create_policy(klass)
81
49
  res = @instances[klass]
82
- return res if res
50
+ return res unless res.nil?
83
51
 
84
52
  name = "#{klass.to_s.camelcase}Policy"
85
53
  return nil unless Object.const_defined?(name)
86
54
 
87
- @instances[klass] = name.constantize.new
55
+ instances[klass] = name.constantize.new
88
56
  end
89
57
 
90
- def policy_method(klass, action)
91
- act = @policies[klass]
92
- return action unless act
93
-
94
- act[action] || action
58
+ def to_yaml
59
+ "# === @policies ===\n" + YAML.dump(@policies) +
60
+ "# === @instances ===\n" + YAML.dump(@instances)
95
61
  end
96
62
  end
97
63
  end
data/lib/miau/version.rb CHANGED
@@ -1,7 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Miau
4
- VERSION = "1.0.3" # 2023-12-13
4
+ VERSION = "1.1.1" # 2024-01-06
5
+ # VERSION = "1.1.0" # 2024-01-06
6
+ # VERSION = "1.0.3" # 2023-12-13
5
7
  # VERSION = "1.0.2" # 2023-11-05
6
8
  # VERSION = "1.0.1" # 2023-04-24
7
9
  # VERSION = "1.0.0" # 2022-01-23
data/lib/miau.rb CHANGED
@@ -4,6 +4,7 @@ require "active_support/concern"
4
4
  require "miau/version"
5
5
  require "miau/error"
6
6
  require "miau/storage"
7
+ require "miau/run"
7
8
  require "miau/application_policy"
8
9
 
9
10
  module Miau
@@ -20,24 +21,20 @@ module Miau
20
21
  @_miau_authorization_performed = true
21
22
  return true if authorized?(resource, hsh)
22
23
 
23
- klass, action = klass_action(hsh)
24
+ klass, action = klass_action
24
25
  msg = "class <#{klass} action <#{action}>"
25
- raise Miau::NotAuthorizedError, msg
26
+ raise NotAuthorizedError, msg
26
27
  end
27
28
 
28
29
  def authorized?(resource = nil, hsh = {})
29
- klass, action = klass_action(hsh)
30
- Miau::PolicyStorage.instance.run(klass, action, miau_user, resource)
30
+ klass, action = klass_action
31
+ PolicyRun.instance.run(klass, action, miau_user, resource)
31
32
  end
32
33
 
33
34
  def miau_user
34
35
  current_user
35
36
  end
36
37
 
37
- def skip_authorization
38
- @_miau_authorization_performed = true
39
- end
40
-
41
38
  def verify_authorized
42
39
  raise AuthorizationNotPerformedError unless miau_authorization_performed?
43
40
  end
@@ -46,13 +43,17 @@ module Miau
46
43
  !!@_miau_authorization_performed
47
44
  end
48
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
+
49
54
  private
50
55
 
51
- def klass_action(hsh)
52
- klass = hsh[:class]
53
- klass ||= params[:controller]
54
- action = hsh[:action]
55
- action ||= params[:action]
56
- [klass.to_sym, action.to_sym]
56
+ def klass_action
57
+ [params[:controller].to_sym, params[:action].to_sym]
57
58
  end
58
59
  end
data/miau.gemspec CHANGED
@@ -22,7 +22,6 @@ Gem::Specification.new do |s|
22
22
  s.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|features)/}) }
23
23
 
24
24
  s.add_runtime_dependency "activesupport"
25
- s.add_development_dependency "rake"
26
25
 
27
26
  s.add_development_dependency "appraisal"
28
27
  s.add_development_dependency "minitest"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: miau
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dittmar Krall
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-12-13 00:00:00.000000000 Z
11
+ date: 2024-01-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -24,20 +24,6 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
- - !ruby/object:Gem::Dependency
28
- name: rake
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: '0'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: '0'
41
27
  - !ruby/object:Gem::Dependency
42
28
  name: appraisal
43
29
  requirement: !ruby/object:Gem::Requirement
@@ -107,6 +93,7 @@ files:
107
93
  - lib/miau.rb
108
94
  - lib/miau/application_policy.rb
109
95
  - lib/miau/error.rb
96
+ - lib/miau/run.rb
110
97
  - lib/miau/storage.rb
111
98
  - lib/miau/version.rb
112
99
  - miau.gemspec
@@ -129,7 +116,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
129
116
  - !ruby/object:Gem::Version
130
117
  version: '0'
131
118
  requirements: []
132
- rubygems_version: 3.4.20
119
+ rubygems_version: 3.5.3
133
120
  signing_key:
134
121
  specification_version: 4
135
122
  summary: Simple and lightweight authorization solution for Rails.