padrino-admin 0.18.1 → 0.18.3

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.
Files changed (29) hide show
  1. checksums.yaml +4 -4
  2. data/lib/padrino-admin/access_control.rb +26 -4
  3. data/lib/padrino-admin/generators/actions.rb +1 -0
  4. data/lib/padrino-admin/generators/admin_app.rb +36 -17
  5. data/lib/padrino-admin/generators/admin_page.rb +2 -1
  6. data/lib/padrino-admin/generators/keep_files.rb +56 -0
  7. data/lib/padrino-admin/generators/orm.rb +16 -2
  8. data/lib/padrino-admin/generators/templates/assets/stylesheets/application.css +0 -3
  9. data/lib/padrino-admin/generators/templates/erb/app/layouts/application.erb.tt +0 -5
  10. data/lib/padrino-admin/generators/templates/erb/app/layouts/error.erb.tt +0 -5
  11. data/lib/padrino-admin/generators/templates/erb/app/sessions/new.erb.tt +0 -4
  12. data/lib/padrino-admin/generators/templates/erb/page/index.erb.tt +2 -2
  13. data/lib/padrino-admin/generators/templates/haml/app/layouts/application.haml.tt +0 -5
  14. data/lib/padrino-admin/generators/templates/haml/app/layouts/error.haml.tt +0 -5
  15. data/lib/padrino-admin/generators/templates/haml/app/sessions/new.haml.tt +0 -4
  16. data/lib/padrino-admin/generators/templates/haml/page/index.haml.tt +2 -2
  17. data/lib/padrino-admin/generators/templates/page/controller.rb.tt +0 -3
  18. data/lib/padrino-admin/generators/templates/slim/app/layouts/application.slim.tt +0 -5
  19. data/lib/padrino-admin/generators/templates/slim/app/layouts/error.slim.tt +0 -5
  20. data/lib/padrino-admin/generators/templates/slim/app/sessions/new.slim.tt +0 -4
  21. data/lib/padrino-admin/generators/templates/slim/page/index.slim.tt +2 -2
  22. data/lib/padrino-admin/helpers/authentication_helpers.rb +3 -1
  23. data/lib/padrino-admin.rb +21 -1
  24. data/padrino-admin.gemspec +8 -3
  25. data/test/generators/test_admin_app_generator.rb +54 -0
  26. data/test/generators/test_admin_page_generator.rb +126 -0
  27. data/test/generators/test_admin_rerun_generator.rb +244 -0
  28. data/test/test_admin_application.rb +305 -0
  29. metadata +15 -9
@@ -50,6 +50,238 @@ describe 'AdminApplication' do
50
50
  assert_equal 'unauthenticated', body
51
51
  end
52
52
 
53
+ it 'should require login for a path a protect rule covers, with an encoded letter' do
54
+ mock_app do
55
+ register Padrino::Admin::AccessControl
56
+ enable :sessions
57
+
58
+ access_control.roles_for :any do |role|
59
+ role.protect '/foo'
60
+ end
61
+
62
+ get('/foo') { 'foo' }
63
+ get('/bar') { 'bar' }
64
+ end
65
+
66
+ ['/%66oo', '/f%6Fo', '/%66oo/'].each do |path|
67
+ get path
68
+ assert_equal "You don't have permission for this resource", body, path
69
+ end
70
+
71
+ get '/%62ar'
72
+ assert_equal 'bar', body
73
+ end
74
+
75
+ it 'should keep an encoded slash inside the path segment it is in' do
76
+ mock_app do
77
+ register Padrino::Admin::AccessControl
78
+ enable :sessions
79
+
80
+ access_control.roles_for :any do |role|
81
+ role.protect '/'
82
+ role.allow '/sessions'
83
+ end
84
+
85
+ get('/sessions/:id') { 'session' }
86
+ get('/:page') { "page #{params[:page]}" }
87
+ end
88
+
89
+ get '/sessions/new'
90
+ assert_equal 'session', body
91
+ ['/sessions%2Fnew', '/sessions%2fnew', '/%73essions%2Fnew'].each do |path|
92
+ get path
93
+ assert_equal "You don't have permission for this resource", body, path
94
+ end
95
+ assert @app.access_control.allowed?(nil, '/sessions/a%2Fb')
96
+ refute @app.access_control.allowed?(nil, '/sessions%2F')
97
+ end
98
+
99
+ it 'should deny a path a protect rule covers, whose encoded slash a splat route decodes' do
100
+ mock_app do
101
+ register Padrino::Admin::AccessControl
102
+ enable :sessions
103
+
104
+ access_control.roles_for :any do |role|
105
+ role.protect '/files/report'
106
+ end
107
+
108
+ get('/files/:name') { "name #{params[:name]}" }
109
+ get('/*path') { "file #{params[:path]}" }
110
+ end
111
+
112
+ ['/files/report', '/files%2Freport', '/files%2freport', '/%66iles%2Freport/1',
113
+ '/files%252Freport', '/files/%2572eport', '/%2566iles/report', '/files/rep%256Frt'].each do |path|
114
+ get path
115
+ assert_equal "You don't have permission for this resource", body, path
116
+ refute @app.access_control.allowed?(nil, path), path
117
+ end
118
+ get '/files/public'
119
+ assert_equal 'name public', body
120
+ end
121
+
122
+ describe 'with Regexp rules' do
123
+ def denied
124
+ "You don't have permission for this resource"
125
+ end
126
+
127
+ def mock_filtered_app(&rules)
128
+ mock_app do
129
+ register Padrino::Admin::AccessControl
130
+ enable :sessions
131
+ access_control.roles_for(:any, &rules)
132
+ get('/billing/:a/:b') { 'billing page' }
133
+ get('/admin/:id') { 'admin page' }
134
+ get('/:x/:y') { 'two' }
135
+ get('/:x') { 'one' }
136
+ end
137
+ end
138
+
139
+ def assert_not_served(*paths)
140
+ paths.each do |path|
141
+ get path
142
+ assert_includes [401, 404], status, path
143
+ refute_includes ['one', 'two', 'billing page', 'admin page'], body, path
144
+ end
145
+ end
146
+
147
+ it 'should not serve a path that decodes to a newline with an allow Regexp' do
148
+ mock_filtered_app do |role|
149
+ role.protect '/'
150
+ role.allow %r{\A/public$}
151
+ role.allow %r{/lobby$}
152
+ end
153
+ get '/public'
154
+ assert_equal 'one', body
155
+ get '/lobby'
156
+ assert_equal 'one', body
157
+ get '/public/billing'
158
+ assert_equal [401, denied], [status, body]
159
+ assert_not_served '/public%0A/billing', '/lobby%0A/billing'
160
+ end
161
+
162
+ it 'should not serve a path that decodes to a newline with an allow Regexp in a path a protect rule covers' do
163
+ mock_filtered_app do |role|
164
+ role.protect %r{\A/billing/}
165
+ role.allow %r{\A/billing/ok$}
166
+ end
167
+ get '/billing/ok'
168
+ assert_equal 'two', body
169
+ get '/billing/ok/x'
170
+ assert_equal [401, denied], [status, body]
171
+ assert_not_served '/billing/ok%0A/x'
172
+ end
173
+
174
+ it 'should not serve a path that decodes to a newline with a protect Regexp' do
175
+ mock_filtered_app do |role|
176
+ role.protect %r{\A/admin/.*\z}
177
+ role.protect %r{^/staff/.+$}
178
+ role.protect %r{\A/billing\z}
179
+ end
180
+ get '/other/x'
181
+ assert_equal 'two', body
182
+ assert_not_served '/admin/1', '/admin/a%0Ab', '/staff/x', '/staff/%0Ab', '/billing', '/billing%0A'
183
+ end
184
+
185
+ it 'should deny the path with a trailing slash for a protect Regexp' do
186
+ mock_filtered_app do |role|
187
+ role.protect %r{\A/billing\z}
188
+ role.protect %r{\A/files/[^/]+\z}
189
+ end
190
+ ['/billings', '/billing/x', '/filesx'].each do |path|
191
+ get path
192
+ assert_equal 200, status, path
193
+ end
194
+ ['/billing', '/billing/', '/billing//', '/%62illing/', '/files/a/', '/files/a//'].each do |path|
195
+ get path
196
+ assert_equal [401, denied], [status, body], path
197
+ refute @app.access_control.allowed?(nil, path), path
198
+ end
199
+ end
200
+
201
+ it 'should deny the path with a trailing slash for an app mounted under a sub-path' do
202
+ mock_filtered_app do |role|
203
+ role.protect %r{\A/billing\z}
204
+ role.allow %r{\A/public$}
205
+ end
206
+ request = Rack::MockRequest.new(Padrino::Router.new({ path: '/blog', to: @app }, { path: '/', to: @app }))
207
+ assert_equal 'one', request.get('/blog/public').body
208
+ assert_equal 401, request.get('/blog/billing/').status
209
+ ['/blog/public%0A/billing', '/blog/billing%0A'].each do |path|
210
+ assert_equal 401, request.get(path).status, path
211
+ end
212
+ end
213
+
214
+ it 'should deny the path with a format for a protect Regexp' do
215
+ mock_app do
216
+ register Padrino::Admin::AccessControl
217
+ enable :sessions
218
+ access_control.roles_for :any do |role|
219
+ role.protect %r{\A/billing\z}
220
+ role.protect %r{\A/stock\z}
221
+ end
222
+ get(:billing, map: '/billing', provides: %i[html json]) { 'billing' }
223
+ get('/stock(.:format)?') { 'stock' }
224
+ get('/:x') { 'one' }
225
+ end
226
+ ['/billings', '/stocks', '/billing-day', '/billing-json', '/billing_x'].each do |path|
227
+ get path
228
+ assert_equal 'one', body, path
229
+ assert @app.access_control.allowed?(nil, path), path
230
+ end
231
+ ['/billing', '/billing/', '/billing.json', '/billing.json/', '/%62illing.json', '/stock', '/stock.json', '/stock.a.b'].each do |path|
232
+ get path
233
+ assert_equal [401, denied], [status, body], path
234
+ refute @app.access_control.allowed?(nil, path), path
235
+ end
236
+ end
237
+
238
+ it 'should deny a path with too many forms to check when a protect Regexp exists' do
239
+ mock_app do
240
+ register Padrino::Admin::AccessControl
241
+ enable :sessions
242
+ access_control.roles_for(:any) { |role| role.protect %r{\A/reports/x\z} }
243
+ get('/:name') { 'page' }
244
+ end
245
+ get "/other#{'.a' * 10}"
246
+ assert_equal 'page', body
247
+ get "/other#{'.a' * 70}"
248
+ assert_equal [401, denied], [status, body]
249
+ end
250
+
251
+ it 'should answer allowed? for a path with a newline from its deny rules only' do
252
+ mock_app do
253
+ register Padrino::Admin::AccessControl
254
+ access_control.roles_for(:any) do |role|
255
+ role.protect '/admin'
256
+ role.allow %r{\A/public$}
257
+ role.allow '/lobby'
258
+ end
259
+ end
260
+ ["/public\n/admin", '/public%0A/admin', '/lobby%0A', '/lobby/a%250A', '/other%0A'].each do |path|
261
+ refute @app.access_control.allowed?(nil, path), path
262
+ end
263
+ assert @app.access_control.allowed?(nil, '/public')
264
+ mock_app do
265
+ register Padrino::Admin::AccessControl
266
+ access_control.roles_for(:any) { |role| role.allow %r{\A/public$} }
267
+ end
268
+ assert @app.access_control.allowed?(nil, '/public%0A/admin')
269
+ end
270
+
271
+ it 'should keep the allow rules of a role for a path with a control character' do
272
+ mock_app do
273
+ register Padrino::Admin::AccessControl
274
+ access_control.roles_for(:any) { |role| role.protect '/' }
275
+ access_control.roles_for(:editor) { |role| role.allow '/posts' }
276
+ access_control.roles_for(:admin) { |role| role.allow '/accounts' }
277
+ end
278
+ editor = Struct.new(:role).new('editor')
279
+ assert @app.access_control.allowed?(editor, '/posts/a%09b')
280
+ assert @app.access_control.allowed?(editor, '/posts/a%0Db')
281
+ refute @app.access_control.allowed?(editor, '/accounts/a%09b')
282
+ end
283
+ end
284
+
53
285
  it 'should set basic roles with store location and login page' do
54
286
  mock_app do
55
287
  set :app_name, :basic_app
@@ -402,6 +634,79 @@ describe 'AdminApplication' do
402
634
  assert_raises(Padrino::Admin::AccessControlError) { access_control.roles_for }
403
635
  end
404
636
 
637
+ describe 'admin model' do
638
+ def mock_admin_app(admin_model)
639
+ mock_app do
640
+ register Padrino::Admin::AccessControl
641
+ set :admin_model, admin_model
642
+ get('/current') { current_account.inspect }
643
+ end
644
+ end
645
+
646
+ it 'should say a missing admin model must be defined' do
647
+ mock_admin_app 'MissingAdminSample::Account'
648
+ error = assert_raises(Padrino::Admin::AccessControlError) { get '/current' }
649
+ assert_equal 'You must define an MissingAdminSample::Account Model', error.message
650
+ end
651
+
652
+ it 'should say a missing admin model must be defined when its name repeats a namespace' do
653
+ Object.const_set(:RepeatedAdminSample, Module.new)
654
+ mock_admin_app 'RepeatedAdminSample::RepeatedAdminSample'
655
+ error = assert_raises(Padrino::Admin::AccessControlError) { get '/current' }
656
+ assert_equal 'You must define an RepeatedAdminSample::RepeatedAdminSample Model', error.message
657
+ ensure
658
+ Object.send(:remove_const, :RepeatedAdminSample) if Object.const_defined?(:RepeatedAdminSample, false)
659
+ end
660
+
661
+ it 'should raise the error of a top-level admin model whose body names another constant like it' do
662
+ Dir.mktmpdir do |dir|
663
+ File.write("#{dir}/colliding_top_account.rb", <<~RUBY)
664
+ module CollidingTopMixins; end
665
+ class CollidingTopAccount
666
+ include CollidingTopMixins::CollidingTopAccount
667
+ end
668
+ RUBY
669
+ Object.autoload(:CollidingTopAccount, "#{dir}/colliding_top_account.rb")
670
+ mock_admin_app 'CollidingTopAccount'
671
+ error = assert_raises(NameError) { get '/current' }
672
+ assert_match(/CollidingTopMixins::CollidingTopAccount/, error.message)
673
+ end
674
+ ensure
675
+ %i[CollidingTopAccount CollidingTopMixins].each { |name| Object.send(:remove_const, name) if Object.const_defined?(name, false) }
676
+ end
677
+
678
+ it 'should raise the error of an admin model whose body names another constant like it' do
679
+ Dir.mktmpdir do |dir|
680
+ File.write("#{dir}/colliding_admin_sample.rb", <<~RUBY)
681
+ module CollidingAdminSample
682
+ module Mixins; end
683
+ class Account
684
+ include Mixins::Account
685
+ end
686
+ end
687
+ RUBY
688
+ Object.autoload(:CollidingAdminSample, "#{dir}/colliding_admin_sample.rb")
689
+ mock_admin_app 'CollidingAdminSample::Account'
690
+ error = assert_raises(NameError) { get '/current' }
691
+ assert_match(/Mixins::Account/, error.message)
692
+ end
693
+ ensure
694
+ Object.send(:remove_const, :CollidingAdminSample) if Object.const_defined?(:CollidingAdminSample, false)
695
+ end
696
+
697
+ it 'should raise the error of an admin model that fails to load' do
698
+ Dir.mktmpdir do |dir|
699
+ File.write("#{dir}/broken_admin_sample.rb", "module BrokenAdminSample; class Account; MissingConcern; end; end\n")
700
+ Object.autoload(:BrokenAdminSample, "#{dir}/broken_admin_sample.rb")
701
+ mock_admin_app 'BrokenAdminSample::Account'
702
+ error = assert_raises(NameError) { get '/current' }
703
+ assert_match(/MissingConcern/, error.message)
704
+ end
705
+ ensure
706
+ Object.send(:remove_const, :BrokenAdminSample) if Object.const_defined?(:BrokenAdminSample, false)
707
+ end
708
+ end
709
+
405
710
  it 'should humanize project module names without ActiveSupport' do
406
711
  assert_equal 'Blog posts', Padrino::Admin::AccessControl::ProjectModule.new(:blog_posts, '/blog_posts').human_name
407
712
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: padrino-admin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.18.1
4
+ version: 0.18.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Padrino Team
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2026-10-31 00:00:00.000000000 Z
14
+ date: 2026-11-03 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: padrino-core
@@ -19,28 +19,28 @@ dependencies:
19
19
  requirements:
20
20
  - - '='
21
21
  - !ruby/object:Gem::Version
22
- version: 0.18.1
22
+ version: 0.18.3
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
27
  - - '='
28
28
  - !ruby/object:Gem::Version
29
- version: 0.18.1
29
+ version: 0.18.3
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: padrino-helpers
32
32
  requirement: !ruby/object:Gem::Requirement
33
33
  requirements:
34
34
  - - '='
35
35
  - !ruby/object:Gem::Version
36
- version: 0.18.1
36
+ version: 0.18.3
37
37
  type: :runtime
38
38
  prerelease: false
39
39
  version_requirements: !ruby/object:Gem::Requirement
40
40
  requirements:
41
41
  - - '='
42
42
  - !ruby/object:Gem::Version
43
- version: 0.18.1
43
+ version: 0.18.3
44
44
  description: Admin View for Padrino applications
45
45
  email: padrinorb@gmail.com
46
46
  executables: []
@@ -59,6 +59,7 @@ files:
59
59
  - lib/padrino-admin/generators/actions.rb
60
60
  - lib/padrino-admin/generators/admin_app.rb
61
61
  - lib/padrino-admin/generators/admin_page.rb
62
+ - lib/padrino-admin/generators/keep_files.rb
62
63
  - lib/padrino-admin/generators/orm.rb
63
64
  - lib/padrino-admin/generators/templates/account/activerecord.rb.tt
64
65
  - lib/padrino-admin/generators/templates/account/mongoid.rb.tt
@@ -159,13 +160,18 @@ files:
159
160
  - test/generators/test_account_model_generator.rb
160
161
  - test/generators/test_admin_app_generator.rb
161
162
  - test/generators/test_admin_page_generator.rb
163
+ - test/generators/test_admin_rerun_generator.rb
162
164
  - test/helper.rb
163
165
  - test/test_admin_application.rb
164
166
  - test/test_locale.rb
165
- homepage: http://www.padrinorb.com
167
+ homepage: https://padrinorb.com
166
168
  licenses:
167
169
  - MIT
168
- metadata: {}
170
+ metadata:
171
+ bug_tracker_uri: https://github.com/padrino/padrino-framework/issues
172
+ changelog_uri: https://github.com/padrino/padrino-framework/blob/master/CHANGES.rdoc
173
+ documentation_uri: https://padrinorb.com/guides/
174
+ source_code_uri: https://github.com/padrino/padrino-framework
169
175
  post_install_message:
170
176
  rdoc_options:
171
177
  - "--charset=UTF-8"
@@ -175,7 +181,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
175
181
  requirements:
176
182
  - - ">="
177
183
  - !ruby/object:Gem::Version
178
- version: '0'
184
+ version: 2.7.8
179
185
  required_rubygems_version: !ruby/object:Gem::Requirement
180
186
  requirements:
181
187
  - - ">="