active_scaffold 4.4.6 → 4.4.8

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 (38) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.rdoc +16 -0
  3. data/README.md +101 -0
  4. data/app/views/active_scaffold_overrides/_add_existing_form.html.erb +2 -2
  5. data/app/views/active_scaffold_overrides/_base_form.html.erb +2 -1
  6. data/app/views/active_scaffold_overrides/_field_search.html.erb +1 -0
  7. data/app/views/active_scaffold_overrides/_form_association_footer.html.erb +13 -11
  8. data/app/views/active_scaffold_overrides/_list.html.erb +1 -1
  9. data/app/views/active_scaffold_overrides/_new_record.js.erb +3 -3
  10. data/app/views/active_scaffold_overrides/_refresh_list.js.erb +1 -1
  11. data/app/views/active_scaffold_overrides/_search.html.erb +1 -0
  12. data/app/views/active_scaffold_overrides/_show.html.erb +1 -1
  13. data/app/views/active_scaffold_overrides/action_confirmation.html.erb +2 -2
  14. data/app/views/active_scaffold_overrides/delete.html.erb +2 -2
  15. data/app/views/active_scaffold_overrides/destroy.js.erb +5 -5
  16. data/app/views/active_scaffold_overrides/on_update.js.erb +1 -1
  17. data/lib/active_scaffold/actions/core.rb +182 -14
  18. data/lib/active_scaffold/actions/field_search.rb +8 -1
  19. data/lib/active_scaffold/actions/nested.rb +5 -3
  20. data/lib/active_scaffold/actions/subform.rb +56 -9
  21. data/lib/active_scaffold/actions/update.rb +2 -2
  22. data/lib/active_scaffold/api_association_serializer.rb +27 -0
  23. data/lib/active_scaffold/config/core.rb +25 -0
  24. data/lib/active_scaffold/extensions/action_view_rendering.rb +4 -9
  25. data/lib/active_scaffold/finder.rb +12 -4
  26. data/lib/active_scaffold/helpers/action_link_helpers.rb +18 -6
  27. data/lib/active_scaffold/helpers/association_helpers.rb +1 -0
  28. data/lib/active_scaffold/helpers/controller_helpers.rb +63 -1
  29. data/lib/active_scaffold/helpers/form_column_helpers.rb +1 -1
  30. data/lib/active_scaffold/helpers/form_ui_helpers.rb +1 -0
  31. data/lib/active_scaffold/helpers/list_column_helpers.rb +4 -4
  32. data/lib/active_scaffold/helpers/pagination_helpers.rb +1 -1
  33. data/lib/active_scaffold/helpers/tabs_helpers.rb +1 -1
  34. data/lib/active_scaffold/helpers/view_helpers.rb +1 -1
  35. data/lib/active_scaffold/parameter_signing.rb +34 -0
  36. data/lib/active_scaffold/version.rb +1 -1
  37. data/lib/active_scaffold.rb +1 -0
  38. metadata +3 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2475af8fa6929537031a900309db3ff1c3b91bcb9e911ccd61676aa06bf3009f
4
- data.tar.gz: 0df3c5893800ffc86159e1a9c73e0b022f5b70a64afcc39e6da59266540f37ba
3
+ metadata.gz: 93a50bab634923deec74bb0777798bbdb11e6a1892d2aecd85602defb7b6352e
4
+ data.tar.gz: 8efd5a4229407e194fbc0f7e901d4467ea1fcd1500c991ecc50d66c5911f9061
5
5
  SHA512:
6
- metadata.gz: e37f5f28f8bade8de9c86d69e68166a0ed7d12acde4c0e908c6c92626505b24511578cfb8394d238393659162ef7cc1dcb3916e693630a9192d03ea5acd8fa3e
7
- data.tar.gz: 2aaae36010f59a87cf009a2ddca97c1f45f620d0493d100ff997f185d732592514acce727b1a929c2411028e6964e20de9007673f874deec8f2eb191a97cb1ae
6
+ metadata.gz: bb56102fc32fa3e594d666eec2446b553ed82ad8ad302d055fd8e5b4e6e1305fda0b54017f4f87ce86069a04a436e219423701831d9977b023f27ed08c444d96
7
+ data.tar.gz: 9072cd0c201e25156e01b9d757acca94f91bc365a8d5d6cb03492f6309b63063bc7f9259bdba491cdfa48ca1454dc31554ffca28add6aec9336cf1e959bacec3
data/CHANGELOG.rdoc CHANGED
@@ -1,3 +1,19 @@
1
+ = 4.4.8
2
+ - Add authorized API association exports containing the primary key and +to_label+, with an overridable +api_association_options+ controller method. Associated record read authorization is checked automatically; applications can return Rails serialization options, including explicit nested +include:+ options, and must authorize the additional fields and nested records they request. In 4.4, +unsafe_api_associations = nil+ preserves legacy full-attribute exports with a deprecation warning, +true+ preserves them without warning, and +false+ enables safe exports. The unsafe behavior and compatibility setting will be removed in 4.5.
3
+ - Authenticate embedded scaffold parameters with a signature in +eid+, bound to the target controller. Reject unsigned or modified conditions and constraints before using them to query or assign records.
4
+ - Authenticate +return_to+ destinations before rendering links or redirecting, preventing request parameters from supplying unverified return URLs. Preserve the application's policy for redirects to external hosts.
5
+ - Authenticate tab selections before assigning associated records, binding the signature to the controller, parent form, association, and grouping value. Validate tab groups against their configured grouping field and resolve association classes according to polymorphism.
6
+ - Authorize existing records selected through add-existing and subform actions. Add +associated_record_scope+ so applications can restrict selectable records using the candidate class and parent association column.
7
+ - Require inline editing to be enabled for the requested column before rendering its editor or invoking update overrides, preventing inline endpoints from updating columns outside the configured inline-edit interface.
8
+ - Restrict grouped field searches to configured +group_options+ values, preventing requests from choosing arbitrary grouping columns or functions.
9
+ - Restrict date trend search units to supported date and time durations before invoking duration methods. Add the overridable +valid_datetime_trend_unit?+ finder method so applications can allow additional duration units when extending +active_scaffold_search_datetime_trend_units+.
10
+ - Add +conditions_from_params_allowed_columns+ and +conditions_from_params_excluded_columns+ to control filtering through URL parameters. Excluded columns take precedence; a nil allowed list preserves existing behavior, while an empty allowed list disables parameter filtering.
11
+ - Make +default_formats+ configurable globally and per controller so applications can restrict the response formats exposed by scaffold actions. Defaults remain HTML, JavaScript, JSON, and XML.
12
+ - Apply the selected tab grouping to existing subform records as well as newly built records. Require update authorization when changing an existing record's grouping and stage association changes until the parent form is saved.
13
+
14
+ = 4.4.7
15
+ - Apply per-record column read authorization to JSON and XML responses, preventing fields hidden by instance-level authorization from being disclosed through API formats.
16
+
1
17
  = 4.4.6
2
18
  - Execute iframe responses by injecting a nonce-aware script into the parent document, avoiding +eval()+ under a strict Content Security Policy
3
19
  - Use RecordSelect's named callback registry for the add-existing subform link when available, avoiding +eval()+ under a strict Content Security Policy while retaining compatibility with older RecordSelect releases.
data/README.md CHANGED
@@ -182,6 +182,22 @@ Configuration
182
182
  -------------
183
183
  See Wiki for instructions on customising ActiveScaffold and to find the full API details.
184
184
 
185
+ The default response formats are `html`, `js`, `json`, and `xml`. To change them for the application, configure the defaults in an initializer before controllers are loaded:
186
+
187
+ ```ruby
188
+ ActiveScaffold::Config::Core.default_formats = %i[html js]
189
+ ```
190
+
191
+ Override these defaults for an individual controller inside its `active_scaffold` block:
192
+
193
+ ```ruby
194
+ active_scaffold do |config|
195
+ config.default_formats = %i[html js]
196
+ end
197
+ ```
198
+
199
+ The existing `config.formats` and action settings such as `config.list.formats` add formats to these defaults. Removing a format from `default_formats` does not disable it if it is explicitly added elsewhere. Actions with a fixed set of formats, such as JavaScript subform rendering, keep their own formats.
200
+
185
201
  Credits
186
202
  -------
187
203
  ActiveScaffold grew out of a project named Ajaxscaffold dating back to 2006. It has had numerous contributors including:
@@ -219,3 +235,88 @@ Happy translating!
219
235
 
220
236
  ### Powered by
221
237
  [![RubyMine logo](https://resources.jetbrains.com/storage/products/company/brand/logos/RubyMine.png)](https://jb.gg/OpenSource)
238
+
239
+ ### API association exports
240
+
241
+ In 4.4, associations in JSON and XML responses retain their legacy serialization by default.
242
+ This exports all database attributes on associated records without checking their record or
243
+ column read authorization, even when the associated scaffold hides those attributes.
244
+ A deprecation warning is emitted when associations are exported.
245
+
246
+ Enable the safe output globally:
247
+
248
+ ```ruby
249
+ ActiveScaffold::Config::Core.unsafe_api_associations = false
250
+ ```
251
+
252
+ Or configure an individual scaffold:
253
+
254
+ ```ruby
255
+ active_scaffold do |config|
256
+ config.unsafe_api_associations = false
257
+ end
258
+ ```
259
+
260
+ Safe output exports each readable associated record as a hash containing its primary key
261
+ and `to_label`. Unreadable singular records become `null`; unreadable records in collections
262
+ are omitted. The primary key and label do not require column authorization checks. By default,
263
+ associations are not expanded recursively, and no associated scaffold configuration is loaded.
264
+
265
+ Override `api_association_options(column, record)` to return the options passed directly to
266
+ Rails' `serializable_hash`, including `only:`, `except:`, `methods:`, and `include:`. For example:
267
+
268
+ ```ruby
269
+ protected
270
+
271
+ def api_association_options(column, record)
272
+ if column.name == :company
273
+ {only: [record.class.primary_key, :name], methods: [:to_label]}
274
+ else
275
+ super
276
+ end
277
+ end
278
+ ```
279
+
280
+ The serializer checks associated record read authorization but does not authorize the fields
281
+ selected by these options. If additional fields need column read authorization, filter them
282
+ explicitly in the override:
283
+
284
+ ```ruby
285
+ protected
286
+
287
+ def api_association_options(column, record)
288
+ options = super
289
+ if column.name == :company
290
+ columns = %i[name email].select do |name|
291
+ record.authorized_for?(crud_type: :read, column: name)
292
+ end
293
+ options = options.merge(only: Array(options[:only]) + columns)
294
+ end
295
+ options
296
+ end
297
+ ```
298
+
299
+ This preserves the default primary key and label and checks permissions only for the extra
300
+ fields. Choose label and virtual methods that expose data appropriate for your API.
301
+
302
+ Applications can explicitly request nested data using `include:` (singular):
303
+
304
+ ```ruby
305
+ def api_association_options(column, record)
306
+ options = super
307
+ if column.name == :company
308
+ options = options.merge(include: {address: {only: [:city, :country]}})
309
+ end
310
+ options
311
+ end
312
+ ```
313
+
314
+ Nested includes use Rails' normal serialization. Applications are responsible for authorizing
315
+ all nested records and fields they request. `serializable_hash` does not check those permissions;
316
+ using an unrestricted `include:` can export every database attribute of nested records.
317
+ For JSON, the parent uses `as_json` to preserve its model root, while associated records use
318
+ `serializable_hash`, matching Rails' nested `include:` behavior and avoiding extra model roots.
319
+
320
+ Setting `unsafe_api_associations = true` explicitly retains the legacy unsafe output without
321
+ warnings in 4.4. Leaving it `nil` retains the output with a warning. Both the unsafe behavior
322
+ and this compatibility setting will be removed in 4.5; configure `false` now to migrate.
@@ -6,7 +6,7 @@ options = {id: element_form_id(action: :add_existing),
6
6
  method: :post,
7
7
  'data-loading' => true}
8
8
  options[:remote] = true if xhr
9
- form_tag url_options, options -%>
9
+ form_tag signed_url_options(url_options), options -%>
10
10
 
11
11
  <%= as_element :form_title, active_scaffold_config.nested.label -%>
12
12
 
@@ -27,7 +27,7 @@ options = {id: element_form_id(action: :add_existing),
27
27
 
28
28
  <%= as_element :form_footer, class: 'form-footer' do %>
29
29
  <%= submit_tag as_(:add), as_element_attributes(:form_submit) %>
30
- <%= link_to as_(:cancel), main_path_to_return, as_element_attributes(:form_cancel, class: 'as_cancel', remote: true) %>
30
+ <%= link_to as_(:cancel), signed_url_options(main_path_to_return), as_element_attributes(:form_cancel, class: 'as_cancel', remote: true) %>
31
31
  <%= loading_indicator_tag(action: :add_existing, id: params[:id]) %>
32
32
  <% end %>
33
33
 
@@ -46,6 +46,7 @@ options = as_element_attributes(
46
46
  data: {loading: defined?(loading) ? loading : true}
47
47
  )
48
48
 
49
+ url_options = signed_url_options(url_options)
49
50
  if xhr && multipart # file_uploads
50
51
  form_remote_upload_tag url_options.merge(iframe: true), options
51
52
  else
@@ -71,7 +72,7 @@ end
71
72
  <%= as_element :form_footer, class: 'form-footer' do %>
72
73
  <%= submit_tag as_(submit_text), as_element_attributes(:form_submit) if !persistent || persistent == :optional %>
73
74
  <%= submit_tag as_(apply_text), as_element_attributes(:form_apply, name: 'dont_close') if persistent %>
74
- <%= link_to as_(cancel_text), cancel_url, as_element_attributes(:form_cancel, **cancel_options) if cancel_link %>
75
+ <%= link_to as_(cancel_text), signed_url_options(cancel_url), as_element_attributes(:form_cancel, **cancel_options) if cancel_link %>
75
76
  <%= loading_indicator_tag action: form_action, id: @record&.id %>
76
77
  <%= render footer_extension, form_action: form_action if footer_extension %>
77
78
  <% end %>
@@ -11,6 +11,7 @@
11
11
  method: :get
12
12
  )
13
13
 
14
+ url_options = signed_url_options(url_options)
14
15
  hidden_params = url_options.except(:controller, :action, :id, :search).to_query.split(Rack::Utils::DEFAULT_SEP)
15
16
  -%>
16
17
 
@@ -14,17 +14,19 @@ temporary_id = generated_id(parent_record) if parent_record.new_record?
14
14
  controller_path = active_scaffold_controller_for(parent_record.class).controller_path
15
15
  parent_controller = (main_form_controller || controller).controller_path
16
16
  url_options = {controller: controller_path, action: 'edit_associated', child_association: column.name, scope: scope, id: parent_record.to_param, generated_id: temporary_id, parent_controller: parent_controller}
17
- edit_associated_url = params_for(url_options.merge(associated_id: '--ID--')) if show_add_existing
18
- if show_add_new
19
- if local_assigns[:tabbed_by]
20
- url_options.merge! tabbed_by: tabbed_by, value: tab_value
21
- if (tabbed_association = tabbed_by_association(column, tabbed_by))
22
- url_options[:value] = tab_value.id
23
- url_options[:value_type] = tab_value.class.base_class.sti_name if tabbed_association.polymorphic?
24
- end
17
+ # Tab selection parameters are one-use context. Only the values constructed
18
+ # below from the rendered tab may be signed, never inherited request values.
19
+ tab_parameters = ActiveScaffold::ParameterSigning::TAB_SELECTION_PARAMETERS
20
+ if local_assigns[:tabbed_by]
21
+ url_options.merge! tabbed_by: tabbed_by, value: tab_value
22
+ if (tabbed_association = tabbed_by_association(column, tabbed_by))
23
+ url_options[:value] = tab_value.id
24
+ url_options[:value_type] = tab_value.class.base_class.sti_name if tabbed_association.polymorphic?
25
25
  end
26
- add_new_url = params_for(url_options)
27
26
  end
27
+ tab_options = url_options.slice(:tabbed_by, :value, :value_type)
28
+ edit_associated_url = params_for(url_options.merge(associated_id: '--ID--')).except(*tab_parameters).merge(tab_options) if show_add_existing
29
+ add_new_url = params_for(url_options).except(*tab_parameters).merge(tab_options) if show_add_new
28
30
 
29
31
  -%>
30
32
  <div class="footer-wrapper">
@@ -40,7 +42,7 @@ end
40
42
  end
41
43
  add_button_attrs = {id: "#{subform_div_id}-create-another", remote: true, class: "as-js-button as_#{add_class}", style: 'display: none;'}
42
44
  %>
43
- <%= link_to add_label, add_new_url, as_element_attributes(:"subform_#{add_class}", **add_button_attrs) %>
45
+ <%= link_to add_label, signed_tab_selection_options(signed_url_options(add_new_url)), as_element_attributes(:"subform_#{add_class}", **add_button_attrs) %>
44
46
  <% end -%>
45
47
 
46
48
  <%= '|' if show_add_new && show_add_existing %>
@@ -56,7 +58,7 @@ end
56
58
  add_existing_attrs = {id: "#{subform_div_id}-add-existing", remote: true, class: "as-js-button as_#{add_existing_label}", style: 'display: none;'}
57
59
  %>
58
60
  <%= select_tag 'associated_id', content_tag(:option, as_(:_select_), value: '') + select_options %>
59
- <%= link_to as_(add_existing_label), edit_associated_url, as_element_attributes(:subform_add_existing, **add_existing_attrs) %>
61
+ <%= link_to as_(add_existing_label), signed_tab_selection_options(signed_url_options(edit_associated_url)), as_element_attributes(:subform_add_existing, **add_existing_attrs) %>
60
62
  <% end -%>
61
63
  <% end -%>
62
64
  <% end %>
@@ -18,7 +18,7 @@
18
18
  </tr>
19
19
  </thead>
20
20
  <%= render 'list_messages', columns: columns %>
21
- <tbody <%= as_element_attributes :list_records, class: 'records', id: active_scaffold_tbody_id, data: {refresh_record: url_for(params_for(action: :index, id: '--ID--'))} %>>
21
+ <tbody <%= as_element_attributes :list_records, class: 'records', id: active_scaffold_tbody_id, data: {refresh_record: url_for(signed_url_options(params_for(action: :index, id: '--ID--')))} %>>
22
22
  <% if !@page.empty? -%>
23
23
  <%= render partial: list_record_view, collection: @page.items, locals: {hidden: false, columns: columns, action_links: active_scaffold_config.action_links.member} %>
24
24
  <% end -%>
@@ -2,9 +2,9 @@
2
2
  <% if nested_singular_association? %>
3
3
  if (action_link) action_link.close(true);
4
4
  <% elsif params[:parent_sti] && render_parent_action == :row %>
5
- ActiveScaffold.create_record_row_from_url(action_link, '<%=raw url_for(render_parent_options) %>', <%= {insert_at: insert_at}.to_json.html_safe %>);
5
+ ActiveScaffold.create_record_row_from_url(action_link, '<%= escape_javascript(url_for(signed_url_options(render_parent_options))) %>', <%= {insert_at: insert_at}.to_json.html_safe %>);
6
6
  <% else %>
7
- ActiveScaffold.reload('<%=raw url_for render_parent_options %>');
7
+ ActiveScaffold.reload('<%= escape_javascript(url_for(signed_url_options(render_parent_options))) %>');
8
8
  <% end %>
9
9
  <% elsif active_scaffold_config.create.refresh_list %>
10
10
  <%= render 'refresh_list', no_history: true %>
@@ -14,4 +14,4 @@
14
14
  <% new_row = render list_record_view, record: @saved_record || @record %>
15
15
  ActiveScaffold.create_record_row(action_link ? action_link.scaffold() : '<%= active_scaffold_id %>', '<%= escape_javascript(new_row) %>', <%= {insert_at: insert_at}.to_json.html_safe %>);
16
16
  <%= render 'update_calculations' %>
17
- <% end %>
17
+ <% end %>
@@ -3,7 +3,7 @@
3
3
  ActiveScaffold.append('<%= active_scaffold_tbody_id %>', '<%= escape_javascript(render(partial: list_record_view, collection: @page.items, locals: {hidden: false, columns: list_columns, action_links: active_scaffold_config.action_links.member})) %>');
4
4
 
5
5
  <% if @page.pager.infinite? || @page.number < @page.pager.number_of_pages -%>
6
- ActiveScaffold.auto_load_page('<%=raw url_for main_path_to_return %>', {auto_pagination: true, page: <%= @page.number + 1 %>});
6
+ ActiveScaffold.auto_load_page('<%= escape_javascript(url_for(signed_url_options(main_path_to_return))) %>', {auto_pagination: true, page: <%= @page.number + 1 %>});
7
7
  <% else %>
8
8
  ActiveScaffold.hide('<%= loading_indicator_id(action: :pagination) %>');
9
9
  <% end # @page.pager.infinite?... %>
@@ -12,6 +12,7 @@
12
12
  method: :get
13
13
  )
14
14
 
15
+ url_options = signed_url_options(url_options)
15
16
  hidden_params = url_options.except(:controller, :action, :id, :search).to_query.split(Rack::Utils::DEFAULT_SEP)
16
17
  -%>
17
18
 
@@ -3,6 +3,6 @@
3
3
  <%= render 'show_columns', columns: active_scaffold_config.show.columns, record: @record -%>
4
4
 
5
5
  <%= as_element :show_footer do %>
6
- <%= link_to as_(:close), main_path_to_return, as_element_attributes(:show_cancel, class: 'as_cancel', remote: request.xhr?) %>
6
+ <%= link_to as_(:close), signed_url_options(main_path_to_return), as_element_attributes(:show_cancel, class: 'as_cancel', remote: request.xhr?) %>
7
7
  <%= loading_indicator_tag(action: :create, id: params[:id]) %>
8
8
  <% end %>
@@ -1,11 +1,11 @@
1
1
  <div class="active-scaffold">
2
2
  <div class="delete-view view">
3
- <%= form_tag params_for(action: link.action, id: params[:id]), {method: link.method} %>
3
+ <%= form_tag signed_url_options(params_for(action: link.action, id: params[:id])), {method: link.method} %>
4
4
  <h4><%= link.confirm(h(record&.to_label)) -%></h4>
5
5
 
6
6
  <p class="form-footer">
7
7
  <%= submit_tag as_(link.label(record)), class: 'submit' %>
8
- <%= link_to as_(:cancel), main_path_to_return, class: 'cancel' %>
8
+ <%= link_to as_(:cancel), signed_url_options(main_path_to_return), class: 'cancel' %>
9
9
  </p>
10
10
 
11
11
  </form>
@@ -1,11 +1,11 @@
1
1
  <div class="active-scaffold">
2
2
  <div class="delete-view view">
3
- <%= form_tag params_for(action: :destroy, id: params[:id]), {method: :delete} %>
3
+ <%= form_tag signed_url_options(params_for(action: :destroy, id: params[:id])), {method: :delete} %>
4
4
  <h4><%= as_(:are_you_sure_to_delete, label: h(@record&.to_label)) -%></h4>
5
5
 
6
6
  <p class="form-footer">
7
7
  <%= submit_tag as_(:delete), class: 'submit' %>
8
- <%= link_to as_(:cancel), main_path_to_return, class: 'cancel' %>
8
+ <%= link_to as_(:cancel), signed_url_options(main_path_to_return), class: 'cancel' %>
9
9
  </p>
10
10
 
11
11
  </form>
@@ -3,14 +3,14 @@
3
3
  <% if render_parent? %>
4
4
  <% if render_parent_action == :row %>
5
5
  <% current_id = controller_id(params[:eid] || params[:parent_sti]) -%>
6
- ActiveScaffold.delete_record_row('<%= element_row_id(controller_id: current_id, action: 'list', id: params[:id]) %>', '<%=raw url_for params_for(render_parent_options.merge(id: nil, page: [active_scaffold_config.list.user.page.to_i - 1, 1].max)) %>');
6
+ ActiveScaffold.delete_record_row('<%= element_row_id(controller_id: current_id, action: 'list', id: params[:id]) %>', '<%= escape_javascript(url_for(signed_url_options(params_for(render_parent_options.merge(id: nil, page: [active_scaffold_config.list.user.page.to_i - 1, 1].max))))) %>');
7
7
  <% messages_id = active_scaffold_messages_id(controller_id: current_id) %>
8
8
  <%= render 'update_calculations', calculations_id: active_scaffold_calculations_id(controller_id: current_id) %>
9
9
  <% elsif render_parent_action == :index %>
10
10
  <% if controller.respond_to?(:render_component_into_view, true) %>
11
- <%= escape_javascript(controller.send(:render_component_into_view, render_parent_options)) %>
11
+ <%= escape_javascript(controller.send(:render_component_into_view, signed_url_options(render_parent_options))) %>
12
12
  <% else %>
13
- ActiveScaffold.reload('<%=raw url_for render_parent_options %>');
13
+ ActiveScaffold.reload('<%= escape_javascript(url_for(signed_url_options(render_parent_options))) %>');
14
14
  <% end %>
15
15
  <% end %>
16
16
  <% elsif active_scaffold_config.delete.refresh_list %>
@@ -20,10 +20,10 @@
20
20
  if active_scaffold_config.actions.exclude?(:list) || active_scaffold_config.list.pagination
21
21
  url = main_path_to_return
22
22
  url[:page] = [active_scaffold_config.list.user.page.to_i - 1, 1].max if url.is_a?(Hash) && active_scaffold_config.actions.include?(:list)
23
- url = url_for(url)
23
+ url = url_for(signed_url_options(url))
24
24
  end
25
25
  %>
26
- ActiveScaffold.delete_record_row('<%= element_row_id(action: 'list', id: params[:id]) %>', '<%= url %>');
26
+ ActiveScaffold.delete_record_row('<%= element_row_id(action: 'list', id: params[:id]) %>', '<%= escape_javascript(url) %>');
27
27
  <%= render 'update_calculations' %>
28
28
  <% end %>
29
29
  <% else %>
@@ -16,7 +16,7 @@ if (action_link) action_link.update_flash_messages('<%= escape_javascript(render
16
16
  <% if nested_singular_association? || render_parent_action == :row %>
17
17
  if (action_link) action_link.close(true);
18
18
  <% else %>
19
- ActiveScaffold.reload('<%=raw url_for render_parent_options %>');
19
+ ActiveScaffold.reload('<%= escape_javascript(url_for(signed_url_options(render_parent_options))) %>');
20
20
  <% end %>
21
21
  <% elsif update_refresh_list? %>
22
22
  <%= render 'refresh_list', no_history: true %>
@@ -1,9 +1,130 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'active_scaffold/api_association_serializer'
4
+
3
5
  module ActiveScaffold::Actions
4
6
  module Core
7
+ class AuthorizedApiResponse
8
+ def initialize(response_object, columns_names, config, association_options: nil)
9
+ @response_object = response_object
10
+ @columns_names = columns_names
11
+ @config = config
12
+ @association_serializer = ActiveScaffold::ApiAssociationSerializer.new(association_options)
13
+ end
14
+
15
+ def to_json(options = {})
16
+ serialized_response(:as_json).to_json(options)
17
+ end
18
+
19
+ def to_xml(options = {})
20
+ return serialized_response(:to_xml, options) if !collection? && unsafe_associations?
21
+
22
+ xml_options = options.dup
23
+ if collection?
24
+ records = response_records
25
+ if records.present? && records.all?(records.first.class)
26
+ record_name = ActiveSupport::Inflector.underscore(records.first.class.name).tr('/', '_')
27
+ xml_options[:root] ||= ActiveSupport::Inflector.pluralize(record_name)
28
+ end
29
+ else
30
+ xml_options.reverse_merge!(root: @response_object.class.model_name.singular)
31
+ end
32
+ serialized_response(:serializable_hash).to_xml(xml_options)
33
+ end
34
+
35
+ private
36
+
37
+ def serialized_response(method, options = {})
38
+ if collection?
39
+ response_records.map do |record|
40
+ serialized_record(record, method, options)
41
+ end
42
+ else
43
+ serialized_record(@response_object, method, options)
44
+ end
45
+ end
46
+
47
+ def serialized_record(record, method, options)
48
+ options = options.merge(serialization_options(record))
49
+ associations = options[:include]
50
+ if unsafe_associations?
51
+ warn_unsafe_associations if associations.any?
52
+ return record.public_send(method, options)
53
+ end
54
+
55
+ options[:include] = []
56
+ result = record.public_send(method, options)
57
+ attributes = serialized_attributes(result, record, method, options)
58
+ associations.each do |name|
59
+ column = @config.columns[name]
60
+ attributes[name.to_s] = @association_serializer.serialize(record.public_send(name), column)
61
+ end
62
+ result
63
+ end
64
+
65
+ def serialized_attributes(result, record, method, options)
66
+ return result unless method == :as_json
67
+
68
+ root = options.key?(:root) ? options[:root] : record.include_root_in_json
69
+ root = record.model_name.element if root == true
70
+ root ? result.fetch(root) : result
71
+ end
72
+
73
+ def unsafe_associations?
74
+ @config.unsafe_api_associations != false
75
+ end
76
+
77
+ def warn_unsafe_associations
78
+ return if @config.unsafe_api_associations || @warned_unsafe_associations
79
+
80
+ @warned_unsafe_associations = true
81
+ ActiveScaffold.deprecator.warn(
82
+ 'API association serialization currently exports all associated database attributes without ' \
83
+ 'checking associated record or column read authorization. This unsafe behavior and the ' \
84
+ 'unsafe_api_associations setting will be removed in ActiveScaffold 4.5. Set ' \
85
+ 'unsafe_api_associations = false to export authorized association IDs and labels, or true ' \
86
+ 'to explicitly retain unsafe serialization without this warning until 4.5.'
87
+ )
88
+ end
89
+
90
+ def collection?
91
+ @response_object.respond_to?(:to_ary)
92
+ end
93
+
94
+ def response_records
95
+ @response_records ||= @response_object.to_ary
96
+ end
97
+
98
+ def serialization_options(record)
99
+ columns_names =
100
+ if record.respond_to?(:authorized_for?)
101
+ @columns_names.select do |column_name|
102
+ record.authorized_for?(crud_type: :read, column: column_name)
103
+ end
104
+ else
105
+ @columns_names
106
+ end
107
+ {
108
+ only: columns_names + [@config.model.primary_key],
109
+ include: association_columns(columns_names),
110
+ methods: virtual_columns(columns_names)
111
+ }
112
+ end
113
+
114
+ def virtual_columns(columns_names)
115
+ columns_names.reject do |column_name|
116
+ @config._columns_hash[column_name.to_s] || @config.columns[column_name]&.association
117
+ end
118
+ end
119
+
120
+ def association_columns(columns_names)
121
+ columns_names.select { |column_name| @config.columns[column_name]&.association }
122
+ end
123
+ end
124
+
5
125
  def self.included(base)
6
126
  base.class_eval do
127
+ before_action :verify_return_to_parameter
7
128
  before_action :set_vary_accept_header
8
129
  before_action :check_input_device
9
130
  before_action :register_constraints_with_action_columns, unless: :nested?
@@ -61,13 +182,25 @@ module ActiveScaffold::Actions
61
182
  end
62
183
 
63
184
  def render_field_for_inplace_editing
64
- @column = active_scaffold_config.columns[params[:update_column]]
185
+ @column = inplace_edit_column(params[:update_column])
65
186
  @record = find_if_allowed(params[:id], crud_type: :update, column: params[:update_column])
66
187
  end
67
188
 
189
+ def inplace_edit_column(name)
190
+ column = active_scaffold_config.columns[name] if name.present?
191
+ raise ActiveScaffold::ActionNotAllowed unless column&.inplace_edit
192
+
193
+ column
194
+ end
195
+
68
196
  def add_tab
69
197
  process_render_field_params
70
198
  @column = @main_columns.find_by_name(params[:column])
199
+ unless @column.is_a?(ActiveScaffold::DataStructures::ActionColumns) && @column.tabbed_by.present? &&
200
+ @column.tabbed_by.to_s == params[:tabbed_by]
201
+ raise ::ActionController::BadRequest, 'Invalid tab group'
202
+ end
203
+
71
204
  @record = updated_record_with_form(@main_columns, {}, @scope)
72
205
  end
73
206
 
@@ -297,7 +430,7 @@ module ActiveScaffold::Actions
297
430
  end
298
431
 
299
432
  def default_formats
300
- %i[html js json xml]
433
+ active_scaffold_config.default_formats
301
434
  end
302
435
 
303
436
  # Returns true if the client accepts one of the MIME types passed to it
@@ -327,16 +460,23 @@ module ActiveScaffold::Actions
327
460
  @response_object ||= successful? ? (@record || @records) : @record.errors
328
461
  end
329
462
 
463
+ # Override the options passed to serializable_hash for an associated record.
464
+ # Applications must authorize any additional fields or nested includes they select.
465
+ def api_association_options(column, record)
466
+ {only: [record.class.primary_key], methods: [:to_label]}
467
+ end
468
+
330
469
  def response_to_api(format, columns_names, options = {})
331
- render(
332
- options.reverse_merge(
333
- format => response_object,
334
- only: columns_names + [active_scaffold_config.model.primary_key],
335
- include: association_columns(columns_names),
336
- methods: virtual_columns(columns_names),
337
- status: response_status
338
- )
339
- )
470
+ response =
471
+ if successful?
472
+ AuthorizedApiResponse.new(
473
+ response_object, columns_names, active_scaffold_config,
474
+ association_options: method(:api_association_options)
475
+ )
476
+ else
477
+ response_object
478
+ end
479
+ render(options.reverse_merge(format => response, status: response_status))
340
480
  end
341
481
 
342
482
  # Success is the existence of one or more model objects. Most actions
@@ -354,7 +494,7 @@ module ActiveScaffold::Actions
354
494
  options = main_path_to_return
355
495
  # use url_for in case main_path_to_return returns Hash with status param,
356
496
  # which would be interpreted as status option to redirect_to instead of url param
357
- redirect_to options.is_a?(Hash) ? url_for(options) : options
497
+ redirect_to options.is_a?(Hash) ? url_for(signed_url_options(options)) : options
358
498
  end
359
499
 
360
500
  def filtered_query
@@ -366,6 +506,12 @@ module ActiveScaffold::Actions
366
506
  active_scaffold_config.model
367
507
  end
368
508
 
509
+ # Override to scope existing records that can be selected for an association.
510
+ # The column belongs to the parent scaffold, which may use a different model.
511
+ def associated_record_scope(klass, column: nil)
512
+ klass
513
+ end
514
+
369
515
  # Builds search conditions from query parameters whose names match columns.
370
516
  # For example, `contacts/list?company_id=5` matches records with company_id 5.
371
517
  # Append `!` to a column name to negate the condition, as in `company_id!=5`.
@@ -375,11 +521,19 @@ module ActiveScaffold::Actions
375
521
  # Array parameters use the `column[]=value` syntax, for example `company_id[]=5&company_id[]=10`.
376
522
  # Negation may also be combined with ranges or arrays, using `not_between` or `not_in`.
377
523
  def conditions_from_params
524
+ return [{}] if active_scaffold_config.conditions_from_params_allowed_columns&.empty? # rubocop:disable Lint/SafeNavigationWithEmpty -- nil allows all columns
525
+
378
526
  @conditions_from_params ||= begin
379
527
  conditions = [{}]
528
+ allowed_columns = active_scaffold_config.conditions_from_params_allowed_columns&.map(&:to_s)
529
+ excluded_columns = active_scaffold_config.conditions_from_params_excluded_columns.map(&:to_s)
380
530
  params.except(:controller, :action, :page, :sort, :sort_direction, :format, :id).each do |key, value|
381
531
  distinct = key.match?(/!$/)
382
- column = active_scaffold_config._columns_hash[key.to_s[0..(distinct ? -2 : -1)]]
532
+ column_name = key.to_s[0..(distinct ? -2 : -1)]
533
+ next if allowed_columns&.exclude?(column_name)
534
+ next if excluded_columns.include?(column_name)
535
+
536
+ column = active_scaffold_config._columns_hash[column_name]
383
537
  next unless column
384
538
 
385
539
  key = column.name.to_sym
@@ -482,7 +636,21 @@ module ActiveScaffold::Actions
482
636
  end
483
637
 
484
638
  def active_scaffold_embedded_params
485
- params[:embedded] || {}
639
+ @active_scaffold_embedded_params ||= begin
640
+ embedded = params[:embedded]
641
+ if embedded.nil?
642
+ {}
643
+ else
644
+ raise ActionController::BadRequest, 'Invalid embedded parameters' unless embedded.is_a?(ActionController::Parameters)
645
+
646
+ payload = ActiveScaffold::ParameterSigning.embedded_payload(embedded, controller: controller_path)
647
+ unless ActiveScaffold::ParameterSigning.valid_signature?(payload, params[:eid], purpose: :embedded)
648
+ raise ActionController::BadRequest, 'Invalid embedded signature'
649
+ end
650
+
651
+ embedded
652
+ end
653
+ end
486
654
  end
487
655
 
488
656
  def clear_storage
@@ -41,10 +41,17 @@ module ActiveScaffold::Actions
41
41
  end
42
42
 
43
43
  def grouped_search?
44
- field_search_params.present? && field_search_params['active_scaffold_group'].present?
44
+ group = field_search_params['active_scaffold_group']
45
+ return false if group.blank?
46
+
47
+ Array(active_scaffold_config.field_search.group_options).any? do |text, value|
48
+ (value || text).to_s == group.to_s
49
+ end
45
50
  end
46
51
 
47
52
  def setup_search_group
53
+ return unless grouped_search?
54
+
48
55
  @_search_group_name, @_search_group_function = field_search_params['active_scaffold_group'].to_s.split('#')
49
56
  end
50
57