Skip to content

Commit f98976d

Browse files
committed
refactor(core): consolidate Utils usage, extract authorize! helper, harden settings
Consolidate to_class into BasicApp.authentication_plugin via Utils include. Use Utils.humanize in Field.create_field instead of inline logic. Extract repeated authorization checks in Router into a single authorize! helper method. Guard Settings#load_settings against nil authentication plugin and make it idempotent with @loaded flag so repeated calls (e.g. per-request) are a no-op.
1 parent 88fd15a commit f98976d

4 files changed

Lines changed: 36 additions & 30 deletions

File tree

lib/tiny_admin/basic_app.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ class BasicApp < Roda
55
include Utils
66

77
class << self
8+
include Utils
9+
810
def authentication_plugin
911
plugin = TinyAdmin.settings.authentication&.dig(:plugin)
10-
plugin_class = plugin.is_a?(String) ? Object.const_get(plugin) : plugin
12+
plugin_class = to_class(plugin) if plugin
1113
plugin_class || TinyAdmin::Plugins::NoAuth
1214
end
1315
end

lib/tiny_admin/field.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ def initialize(name:, title:, type:, options: {})
1212
end
1313

1414
def apply_call_option(target)
15-
messages = (options[:call] || '').split(',').map(&:strip)
15+
messages = (options[:call] || "").split(",").map(&:strip)
1616
messages.inject(target) { |result, msg| result&.send(msg) } if messages.any?
1717
end
1818

1919
def translate_value(value)
2020
if options && options[:method]
21-
method, *args = options[:method].split(',').map(&:strip)
21+
method, *args = options[:method].split(",").map(&:strip)
2222
if options[:converter]
2323
Object.const_get(options[:converter]).send(method, value, options: args || [])
2424
else
@@ -30,10 +30,11 @@ def translate_value(value)
3030
end
3131

3232
class << self
33+
include Utils
34+
3335
def create_field(name:, title: nil, type: nil, options: {})
3436
field_name = name.to_s
35-
field_title = field_name.respond_to?(:humanize) ? field_name.humanize : field_name.tr('_', ' ').capitalize
36-
new(name: field_name, title: title || field_title, type: type || :string, options: options || {})
37+
new(name: field_name, title: title || humanize(field_name), type: type || :string, options: options || {})
3738
end
3839
end
3940
end

lib/tiny_admin/router.rb

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Router < BasicApp
99
route do |r|
1010
TinyAdmin.settings.load_settings
1111

12-
r.on 'auth' do
12+
r.on "auth" do
1313
r.run Authentication
1414
end
1515

@@ -25,7 +25,7 @@ class Router < BasicApp
2525
# :nocov:
2626
end
2727

28-
r.post '' do
28+
r.post "" do
2929
r.redirect TinyAdmin.settings.root_path
3030
end
3131

@@ -48,32 +48,28 @@ def store
4848

4949
def render_page(page)
5050
if page.respond_to?(:messages=)
51-
page.messages = { notices: flash['notices'], warnings: flash['warnings'], errors: flash['errors'] }
51+
page.messages = {notices: flash["notices"], warnings: flash["warnings"], errors: flash["errors"]}
5252
end
5353
render(inline: page.call)
5454
end
5555

5656
def root_route(req)
57-
if authorization.allowed?(current_user, :root)
57+
authorize!(:root) do
5858
if TinyAdmin.settings.root[:redirect]
5959
req.redirect route_for(TinyAdmin.settings.root[:redirect])
6060
else
6161
page_class = to_class(TinyAdmin.settings.root[:page])
6262
attributes = TinyAdmin.settings.root.slice(:content, :title, :widgets)
6363
render_page prepare_page(page_class, attributes: attributes, params: request.params)
6464
end
65-
else
66-
render_page prepare_page(TinyAdmin.settings.page_not_allowed)
6765
end
6866
end
6967

7068
def setup_page_route(req, slug, page_data)
7169
req.get slug do
72-
if authorization.allowed?(current_user, :page, slug)
70+
authorize!(:page, slug) do
7371
attributes = page_data.slice(:content, :title, :widgets)
7472
render_page prepare_page(page_data[:class], slug: slug, attributes: attributes, params: request.params)
75-
else
76-
render_page prepare_page(TinyAdmin.settings.page_not_allowed)
7773
end
7874
end
7975
end
@@ -99,9 +95,9 @@ def setup_collection_routes(req, slug, options:)
9995
)
10096

10197
# Index
102-
if options[:only].include?(:index) || options[:only].include?('index')
98+
if options[:only].include?(:index) || options[:only].include?("index")
10399
req.is do
104-
if authorization.allowed?(current_user, :resource_index, slug)
100+
authorize!(:resource_index, slug) do
105101
context = Context.new(
106102
actions: custom_actions,
107103
repository: repository,
@@ -111,8 +107,6 @@ def setup_collection_routes(req, slug, options:)
111107
)
112108
index_action = TinyAdmin::Actions::Index.new
113109
render_page index_action.call(app: self, context: context, options: action_options)
114-
else
115-
render_page prepare_page(TinyAdmin.settings.page_not_allowed)
116110
end
117111
end
118112
end
@@ -134,9 +128,9 @@ def setup_member_routes(req, slug, options:)
134128
)
135129

136130
# Show
137-
if options[:only].include?(:show) || options[:only].include?('show')
131+
if options[:only].include?(:show) || options[:only].include?("show")
138132
req.is do
139-
if authorization.allowed?(current_user, :resource_show, slug)
133+
authorize!(:resource_show, slug) do
140134
context = Context.new(
141135
actions: custom_actions,
142136
reference: reference,
@@ -147,8 +141,6 @@ def setup_member_routes(req, slug, options:)
147141
)
148142
show_action = TinyAdmin::Actions::Show.new
149143
render_page show_action.call(app: self, context: context, options: action_options)
150-
else
151-
render_page prepare_page(TinyAdmin.settings.page_not_allowed)
152144
end
153145
end
154146
end
@@ -161,7 +153,7 @@ def setup_custom_actions(req, custom_actions = nil, options:, repository:, slug:
161153
action_class = to_class(action)
162154

163155
req.get action_slug.to_s do
164-
if authorization.allowed?(current_user, :custom_action, action_slug.to_s)
156+
authorize!(:custom_action, action_slug.to_s) do
165157
context = Context.new(
166158
actions: {},
167159
reference: reference,
@@ -172,8 +164,6 @@ def setup_custom_actions(req, custom_actions = nil, options:, repository:, slug:
172164
)
173165
custom_action = action_class.new
174166
render_page custom_action.call(app: self, context: context, options: options)
175-
else
176-
render_page prepare_page(TinyAdmin.settings.page_not_allowed)
177167
end
178168
end
179169

@@ -184,5 +174,13 @@ def setup_custom_actions(req, custom_actions = nil, options:, repository:, slug:
184174
def authorization
185175
TinyAdmin.settings.authorization_class
186176
end
177+
178+
def authorize!(action, param = nil)
179+
if authorization.allowed?(current_user, action, param)
180+
yield
181+
else
182+
render_page prepare_page(TinyAdmin.settings.page_not_allowed)
183+
end
184+
end
187185
end
188186
end

lib/tiny_admin/settings.rb

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ class Settings
1919
%i[page_not_found] => Views::Pages::PageNotFound,
2020
%i[record_not_found] => Views::Pages::RecordNotFound,
2121
%i[repository] => Plugins::ActiveRecordRepository,
22-
%i[root_path] => '/admin',
22+
%i[root_path] => "/admin",
2323
%i[root page] => Views::Pages::Root,
24-
%i[root title] => 'TinyAdmin',
24+
%i[root title] => "TinyAdmin",
2525
%i[sections] => []
2626
}.freeze
2727

@@ -67,6 +67,8 @@ def []=(*path, value)
6767
end
6868

6969
def load_settings
70+
return if @loaded
71+
7072
# default values
7173
DEFAULTS.each do |(option, param), default|
7274
if param
@@ -78,17 +80,20 @@ def load_settings
7880
end
7981

8082
@store ||= TinyAdmin::Store.new(self)
81-
self.root_path = '/' if root_path == ''
83+
self.root_path = "/" if root_path == ""
8284

83-
if authentication[:plugin] <= Plugins::SimpleAuth
85+
if authentication[:plugin].is_a?(Module) && authentication[:plugin] <= Plugins::SimpleAuth
8486
logout_path = "#{root_path}/auth/logout"
85-
authentication[:logout] ||= TinyAdmin::Section.new(name: 'logout', slug: 'logout', path: logout_path)
87+
authentication[:logout] ||= TinyAdmin::Section.new(name: "logout", slug: "logout", path: logout_path)
8688
end
8789
store.prepare_sections(sections, logout: authentication[:logout])
90+
@loaded = true
8891
end
8992

9093
def reset!
9194
@options = {}
95+
@store = nil
96+
@loaded = false
9297
end
9398

9499
private

0 commit comments

Comments
 (0)