active_scaffold 4.4.7 → 4.4.9

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 (41) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.rdoc +18 -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 +115 -14
  18. data/lib/active_scaffold/actions/field_search.rb +8 -1
  19. data/lib/active_scaffold/actions/nested.rb +3 -1
  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 +19 -7
  27. data/lib/active_scaffold/helpers/association_helpers.rb +1 -0
  28. data/lib/active_scaffold/helpers/controller_helpers.rb +65 -2
  29. data/lib/active_scaffold/helpers/form_column_helpers.rb +3 -2
  30. data/lib/active_scaffold/helpers/form_ui_helpers.rb +1 -0
  31. data/lib/active_scaffold/helpers/id_helpers.rb +3 -3
  32. data/lib/active_scaffold/helpers/list_column_helpers.rb +5 -5
  33. data/lib/active_scaffold/helpers/pagination_helpers.rb +1 -1
  34. data/lib/active_scaffold/helpers/search_column_helpers.rb +1 -1
  35. data/lib/active_scaffold/helpers/tabs_helpers.rb +1 -1
  36. data/lib/active_scaffold/helpers/view_helpers.rb +1 -1
  37. data/lib/active_scaffold/parameter_filter_warning.rb +24 -0
  38. data/lib/active_scaffold/parameter_signing.rb +34 -0
  39. data/lib/active_scaffold/version.rb +1 -1
  40. data/lib/active_scaffold.rb +2 -0
  41. metadata +4 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a456ae9c095faa9ee26c7634275b7517bc896af01f132882d44671b7479253d9
4
- data.tar.gz: 8330b0c66cbbe8ff6ee7b258c65aa70a40486ce99b8a5c24ca17b3fbe4b9cea2
3
+ metadata.gz: b13a771c5fb245d7667dff89246184834a35ba6a26b989f66c99ef56ff54d178
4
+ data.tar.gz: fad0f981ddf6a6da05246f7c54cf1d9a88cd18d338a3ed6e591ed9c7fd248e78
5
5
  SHA512:
6
- metadata.gz: 1be8db3d0d117c3ed52dbce11e75479e70e3b7c91769a8bdb0902d917d0ca8916dc4d8df4235abd205a643b1c783f04346d31b2e605aaa049142a30b3118ace9
7
- data.tar.gz: 7abf7b3d41488919571eb5ea182a4bbf8f8abd48bfe8cf9f5813de3401822f34dfe1219faf4c3a5bcaecd29912817c9d591d86b411ae50802d6465ef586144a6
6
+ metadata.gz: 0b085fbe9ae5662e2fc19eaa037efc5ba071d350aad59d45afb3d605ff95f3d69c419decea0994b404a1a800459339ff5080e0525be79e74be3a59af2bd6e19e
7
+ data.tar.gz: 233eb12a8cfbb0294285a27125d09b7faf14642ce24b1bf22c385ef0def4bb14595ed06233a6a28bff2bd36c35c3fb6bd258893ffda2f1d8a31984bc96ca774e
data/CHANGELOG.rdoc CHANGED
@@ -1,3 +1,21 @@
1
+ = 4.4.9
2
+ - Fix opening +field_search+ after a text search when both +search+ and +field_search+ actions are enabled.
3
+ - Ensure generated IDs are always clean, so they are safe to use in JS views
4
+ - Warn once per controller when URL parameters implicitly filter database columns and both +conditions_from_params_allowed_columns+ and +conditions_from_params_excluded_columns+ are nil. Unrestricted filtering can reveal hidden or unauthorized values through matching records or counts. Exclusions now default to nil so an explicit empty exclusion list can retain legacy filtering without warnings. In 4.5 the allowed list will default to +[]+, disabling implicit parameter filters unless applications opt in.
5
+
6
+ = 4.4.8
7
+ - 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.
8
+ - 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.
9
+ - 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.
10
+ - 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.
11
+ - 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.
12
+ - 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.
13
+ - Restrict grouped field searches to configured +group_options+ values, preventing requests from choosing arbitrary grouping columns or functions.
14
+ - 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+.
15
+ - 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.
16
+ - 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.
17
+ - 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.
18
+
1
19
  = 4.4.7
2
20
  - Apply per-record column read authorization to JSON and XML responses, preventing fields hidden by instance-level authorization from being disclosed through API formats.
3
21
 
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,12 +1,15 @@
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
5
7
  class AuthorizedApiResponse
6
- def initialize(response_object, columns_names, config)
8
+ def initialize(response_object, columns_names, config, association_options: nil)
7
9
  @response_object = response_object
8
10
  @columns_names = columns_names
9
11
  @config = config
12
+ @association_serializer = ActiveScaffold::ApiAssociationSerializer.new(association_options)
10
13
  end
11
14
 
12
15
  def to_json(options = {})
@@ -14,13 +17,17 @@ module ActiveScaffold::Actions
14
17
  end
15
18
 
16
19
  def to_xml(options = {})
17
- return serialized_response(:to_xml, options) unless collection?
20
+ return serialized_response(:to_xml, options) if !collection? && unsafe_associations?
18
21
 
19
22
  xml_options = options.dup
20
- records = response_records
21
- if records.present? && records.all?(records.first.class)
22
- record_name = ActiveSupport::Inflector.underscore(records.first.class.name).tr('/', '_')
23
- xml_options[:root] ||= ActiveSupport::Inflector.pluralize(record_name)
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)
24
31
  end
25
32
  serialized_response(:serializable_hash).to_xml(xml_options)
26
33
  end
@@ -30,11 +37,54 @@ module ActiveScaffold::Actions
30
37
  def serialized_response(method, options = {})
31
38
  if collection?
32
39
  response_records.map do |record|
33
- record.public_send(method, options.merge(serialization_options(record)))
40
+ serialized_record(record, method, options)
34
41
  end
35
42
  else
36
- @response_object.public_send(method, options.merge(serialization_options(@response_object)))
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)
37
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
+ )
38
88
  end
39
89
 
40
90
  def collection?
@@ -74,6 +124,7 @@ module ActiveScaffold::Actions
74
124
 
75
125
  def self.included(base)
76
126
  base.class_eval do
127
+ before_action :verify_return_to_parameter
77
128
  before_action :set_vary_accept_header
78
129
  before_action :check_input_device
79
130
  before_action :register_constraints_with_action_columns, unless: :nested?
@@ -131,13 +182,25 @@ module ActiveScaffold::Actions
131
182
  end
132
183
 
133
184
  def render_field_for_inplace_editing
134
- @column = active_scaffold_config.columns[params[:update_column]]
185
+ @column = inplace_edit_column(params[:update_column])
135
186
  @record = find_if_allowed(params[:id], crud_type: :update, column: params[:update_column])
136
187
  end
137
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
+
138
196
  def add_tab
139
197
  process_render_field_params
140
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
+
141
204
  @record = updated_record_with_form(@main_columns, {}, @scope)
142
205
  end
143
206
 
@@ -367,7 +430,7 @@ module ActiveScaffold::Actions
367
430
  end
368
431
 
369
432
  def default_formats
370
- %i[html js json xml]
433
+ active_scaffold_config.default_formats
371
434
  end
372
435
 
373
436
  # Returns true if the client accepts one of the MIME types passed to it
@@ -397,10 +460,19 @@ module ActiveScaffold::Actions
397
460
  @response_object ||= successful? ? (@record || @records) : @record.errors
398
461
  end
399
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
+
400
469
  def response_to_api(format, columns_names, options = {})
401
470
  response =
402
471
  if successful?
403
- AuthorizedApiResponse.new(response_object, columns_names, active_scaffold_config)
472
+ AuthorizedApiResponse.new(
473
+ response_object, columns_names, active_scaffold_config,
474
+ association_options: method(:api_association_options)
475
+ )
404
476
  else
405
477
  response_object
406
478
  end
@@ -422,7 +494,7 @@ module ActiveScaffold::Actions
422
494
  options = main_path_to_return
423
495
  # use url_for in case main_path_to_return returns Hash with status param,
424
496
  # which would be interpreted as status option to redirect_to instead of url param
425
- redirect_to options.is_a?(Hash) ? url_for(options) : options
497
+ redirect_to options.is_a?(Hash) ? url_for(signed_url_options(options)) : options
426
498
  end
427
499
 
428
500
  def filtered_query
@@ -434,6 +506,12 @@ module ActiveScaffold::Actions
434
506
  active_scaffold_config.model
435
507
  end
436
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
+
437
515
  # Builds search conditions from query parameters whose names match columns.
438
516
  # For example, `contacts/list?company_id=5` matches records with company_id 5.
439
517
  # Append `!` to a column name to negate the condition, as in `company_id!=5`.
@@ -443,17 +521,26 @@ module ActiveScaffold::Actions
443
521
  # Array parameters use the `column[]=value` syntax, for example `company_id[]=5&company_id[]=10`.
444
522
  # Negation may also be combined with ranges or arrays, using `not_between` or `not_in`.
445
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
+
446
526
  @conditions_from_params ||= begin
447
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)
448
530
  params.except(:controller, :action, :page, :sort, :sort_direction, :format, :id).each do |key, value|
449
531
  distinct = key.match?(/!$/)
450
- 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]
451
537
  next unless column
452
538
 
453
539
  key = column.name.to_sym
454
540
  next if active_scaffold_constraints[key]
455
541
  next if nested? && nested.param_name == key
456
542
 
543
+ ActiveScaffold::ParameterFilterWarning.warn_once(self.class) if allowed_columns.nil? && excluded_columns.nil?
457
544
  value = condition_value_from_param(value, column, active_scaffold_config.model)
458
545
  if distinct
459
546
  conditions << distinct_condition(key, value)
@@ -550,7 +637,21 @@ module ActiveScaffold::Actions
550
637
  end
551
638
 
552
639
  def active_scaffold_embedded_params
553
- params[:embedded] || {}
640
+ @active_scaffold_embedded_params ||= begin
641
+ embedded = params[:embedded]
642
+ if embedded.nil?
643
+ {}
644
+ else
645
+ raise ActionController::BadRequest, 'Invalid embedded parameters' unless embedded.is_a?(ActionController::Parameters)
646
+
647
+ payload = ActiveScaffold::ParameterSigning.embedded_payload(embedded, controller: controller_path)
648
+ unless ActiveScaffold::ParameterSigning.valid_signature?(payload, params[:eid], purpose: :embedded)
649
+ raise ActionController::BadRequest, 'Invalid embedded signature'
650
+ end
651
+
652
+ embedded
653
+ end
654
+ end
554
655
  end
555
656
 
556
657
  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
 
@@ -251,7 +251,9 @@ module ActiveScaffold::Actions::Nested
251
251
  # The actual "add_existing" algorithm
252
252
  def do_add_existing
253
253
  parent_record = nested_parent_record(:update)
254
- @record = active_scaffold_config.model.find(params[:associated_id])
254
+ column = nested.parent_scaffold.active_scaffold_config.columns[nested.association.name]
255
+ scope = associated_record_scope(active_scaffold_config.model, column: column)
256
+ @record = find_if_allowed(params[:associated_id], :read, scope)
255
257
  return false unless parent_record && @record
256
258
 
257
259
  association = parent_record.send(nested.association.name)