Skip to content

Commit 5b5f4f2

Browse files
authored
Merge pull request #60 from blocknotes/core-improvements
Core improvements
2 parents 9ab8ca6 + b85ada9 commit 5b5f4f2

File tree

4 files changed

+26
-20
lines changed

4 files changed

+26
-20
lines changed

lib/tiny_admin/basic_app.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,19 @@ 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
1416

1517
plugin :flash
1618
plugin :not_found
1719
plugin :render, engine: "html"
18-
plugin :sessions, secret: SecureRandom.hex(64)
20+
plugin :sessions, secret: ENV.fetch("TINY_ADMIN_SECRET") { SecureRandom.hex(64) }
1921

2022
plugin authentication_plugin, TinyAdmin.settings.authentication
2123

lib/tiny_admin/field.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,26 +54,22 @@ def render_page(page)
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
@@ -101,7 +97,7 @@ def setup_collection_routes(req, slug, options:)
10197
# Index
10298
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
@@ -136,7 +130,7 @@ def setup_member_routes(req, slug, options:)
136130
# Show
137131
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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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
@@ -80,15 +82,18 @@ def load_settings
8082
@store ||= TinyAdmin::Store.new(self)
8183
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"
8587
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)