active_scaffold 4.4.6 → 4.4.7

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2475af8fa6929537031a900309db3ff1c3b91bcb9e911ccd61676aa06bf3009f
4
- data.tar.gz: 0df3c5893800ffc86159e1a9c73e0b022f5b70a64afcc39e6da59266540f37ba
3
+ metadata.gz: a456ae9c095faa9ee26c7634275b7517bc896af01f132882d44671b7479253d9
4
+ data.tar.gz: 8330b0c66cbbe8ff6ee7b258c65aa70a40486ce99b8a5c24ca17b3fbe4b9cea2
5
5
  SHA512:
6
- metadata.gz: e37f5f28f8bade8de9c86d69e68166a0ed7d12acde4c0e908c6c92626505b24511578cfb8394d238393659162ef7cc1dcb3916e693630a9192d03ea5acd8fa3e
7
- data.tar.gz: 2aaae36010f59a87cf009a2ddca97c1f45f620d0493d100ff997f185d732592514acce727b1a929c2411028e6964e20de9007673f874deec8f2eb191a97cb1ae
6
+ metadata.gz: 1be8db3d0d117c3ed52dbce11e75479e70e3b7c91769a8bdb0902d917d0ca8916dc4d8df4235abd205a643b1c783f04346d31b2e605aaa049142a30b3118ace9
7
+ data.tar.gz: 7abf7b3d41488919571eb5ea182a4bbf8f8abd48bfe8cf9f5813de3401822f34dfe1219faf4c3a5bcaecd29912817c9d591d86b411ae50802d6465ef586144a6
data/CHANGELOG.rdoc CHANGED
@@ -1,3 +1,6 @@
1
+ = 4.4.7
2
+ - 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
+
1
4
  = 4.4.6
2
5
  - Execute iframe responses by injecting a nonce-aware script into the parent document, avoiding +eval()+ under a strict Content Security Policy
3
6
  - 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.
@@ -2,6 +2,76 @@
2
2
 
3
3
  module ActiveScaffold::Actions
4
4
  module Core
5
+ class AuthorizedApiResponse
6
+ def initialize(response_object, columns_names, config)
7
+ @response_object = response_object
8
+ @columns_names = columns_names
9
+ @config = config
10
+ end
11
+
12
+ def to_json(options = {})
13
+ serialized_response(:as_json).to_json(options)
14
+ end
15
+
16
+ def to_xml(options = {})
17
+ return serialized_response(:to_xml, options) unless collection?
18
+
19
+ 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)
24
+ end
25
+ serialized_response(:serializable_hash).to_xml(xml_options)
26
+ end
27
+
28
+ private
29
+
30
+ def serialized_response(method, options = {})
31
+ if collection?
32
+ response_records.map do |record|
33
+ record.public_send(method, options.merge(serialization_options(record)))
34
+ end
35
+ else
36
+ @response_object.public_send(method, options.merge(serialization_options(@response_object)))
37
+ end
38
+ end
39
+
40
+ def collection?
41
+ @response_object.respond_to?(:to_ary)
42
+ end
43
+
44
+ def response_records
45
+ @response_records ||= @response_object.to_ary
46
+ end
47
+
48
+ def serialization_options(record)
49
+ columns_names =
50
+ if record.respond_to?(:authorized_for?)
51
+ @columns_names.select do |column_name|
52
+ record.authorized_for?(crud_type: :read, column: column_name)
53
+ end
54
+ else
55
+ @columns_names
56
+ end
57
+ {
58
+ only: columns_names + [@config.model.primary_key],
59
+ include: association_columns(columns_names),
60
+ methods: virtual_columns(columns_names)
61
+ }
62
+ end
63
+
64
+ def virtual_columns(columns_names)
65
+ columns_names.reject do |column_name|
66
+ @config._columns_hash[column_name.to_s] || @config.columns[column_name]&.association
67
+ end
68
+ end
69
+
70
+ def association_columns(columns_names)
71
+ columns_names.select { |column_name| @config.columns[column_name]&.association }
72
+ end
73
+ end
74
+
5
75
  def self.included(base)
6
76
  base.class_eval do
7
77
  before_action :set_vary_accept_header
@@ -328,15 +398,13 @@ module ActiveScaffold::Actions
328
398
  end
329
399
 
330
400
  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
- )
401
+ response =
402
+ if successful?
403
+ AuthorizedApiResponse.new(response_object, columns_names, active_scaffold_config)
404
+ else
405
+ response_object
406
+ end
407
+ render(options.reverse_merge(format => response, status: response_status))
340
408
  end
341
409
 
342
410
  # Success is the existence of one or more model objects. Most actions
@@ -197,11 +197,11 @@ module ActiveScaffold::Actions::Nested
197
197
  end
198
198
 
199
199
  def add_existing_respond_to_xml
200
- render xml: response_object, only: active_scaffold_config.list.columns.visible_columns_names, status: response_status
200
+ response_to_api(:xml, active_scaffold_config.list.columns.visible_columns_names)
201
201
  end
202
202
 
203
203
  def add_existing_respond_to_json
204
- render json: response_object, only: active_scaffold_config.list.columns.visible_columns_names, status: response_status
204
+ response_to_api(:json, active_scaffold_config.list.columns.visible_columns_names)
205
205
  end
206
206
 
207
207
  def destroy_existing_respond_to_html
@@ -4,7 +4,7 @@ module ActiveScaffold
4
4
  module Version
5
5
  MAJOR = 4
6
6
  MINOR = 4
7
- PATCH = 6
7
+ PATCH = 7
8
8
  FIX = nil
9
9
 
10
10
  STRING = [MAJOR, MINOR, PATCH, FIX].compact.join('.')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_scaffold
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.4.6
4
+ version: 4.4.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Many, see README