@@ -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
188186end
0 commit comments