forest_liana 9.22.17 → 9.22.19
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 +4 -4
- data/app/controllers/forest_liana/application_controller.rb +29 -1
- data/app/serializers/forest_liana/serializer_factory.rb +3 -1
- data/app/services/forest_liana/ability/permission.rb +6 -0
- data/app/services/forest_liana/belongs_to_updater.rb +6 -2
- data/app/services/forest_liana/resources_getter.rb +2 -0
- data/app/services/forest_liana/schema_adapter.rb +1 -8
- data/app/services/forest_liana/schema_utils.rb +24 -5
- data/app/services/forest_liana/scope_manager.rb +70 -16
- data/lib/forest_liana/version.rb +1 -1
- data/spec/lib/forest_liana/lookup_cache_spec.rb +14 -5
- data/spec/requests/csv_export_relation_spec.rb +59 -0
- data/spec/requests/polymorphic_relation_namesake_spec.rb +98 -0
- data/spec/requests/polymorphic_target_read_check_spec.rb +115 -0
- data/spec/requests/projection_header_spec.rb +5 -0
- data/spec/requests/resources_spec.rb +83 -0
- data/spec/services/forest_liana/ability/permission/smart_action_checker_spec.rb +49 -0
- data/spec/services/forest_liana/belongs_to_updater_spec.rb +35 -0
- data/spec/services/forest_liana/has_many_getter_spec.rb +3 -0
- data/spec/services/forest_liana/resources_getter_spec.rb +31 -0
- data/spec/services/forest_liana/schema_adapter_spec.rb +21 -0
- data/spec/services/forest_liana/schema_utils_spec.rb +79 -0
- data/spec/services/forest_liana/scope_manager_spec.rb +162 -0
- metadata +96 -93
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 40f4ba483ce36ae181d7065df01a5d9b2984d70d3f4020c0f4079afaa95c6cce
|
|
4
|
+
data.tar.gz: 5fbe9bf4fd893807d7b0d1df3fe01abb5a2c0146d7970dd1ca9d3f942002ff1f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9131cea95f456d1f6dd4c792fdafb25d7e7348588fe612de6df1fdd5c8aa2592039608571d957a121bf859955c6f67f060e719332936f97863bf3f7b0d9d5771
|
|
7
|
+
data.tar.gz: 2e1c398ffac85a0a50da4f10c7ce4a9dd3c164394b28f91cee3b29274f5812f7eed44f51b96d59d47444c43e525934881cb39d9d399feda23c11792be7db2617
|
|
@@ -57,17 +57,37 @@ module ForestLiana
|
|
|
57
57
|
def serialize_model(record, options = {})
|
|
58
58
|
options[:is_collection] = false
|
|
59
59
|
options[:context] = { unoptimized: options[:fields].nil? }.merge(options[:context] || {})
|
|
60
|
+
readable = polymorphic_target_readable
|
|
61
|
+
options[:context][:polymorphic_target_readable] ||= readable if readable
|
|
60
62
|
|
|
61
63
|
json = ForestAdmin::JSONAPI::Serializer.serialize(record, options)
|
|
62
64
|
|
|
63
65
|
force_utf8_encoding(json)
|
|
64
66
|
end
|
|
65
67
|
|
|
68
|
+
# The read check passes on the targets a polymorphic relation declares, yet a row may point at
|
|
69
|
+
# any model: the target each row actually points at is checked too, before it is serialized.
|
|
70
|
+
def polymorphic_target_readable
|
|
71
|
+
return unless forest_user && respond_to?(:readable_collection?, true)
|
|
72
|
+
|
|
73
|
+
@polymorphic_target_readable ||= lambda do |relation, target|
|
|
74
|
+
checked = SchemaUtils.polymorphic_models(relation).find { |model| target.is_a?(model) }
|
|
75
|
+
readable_collection?(forest_user, ForestLiana.name_for(checked || target.class))
|
|
76
|
+
rescue ForestLiana::Errors::PermissionsUnavailableError
|
|
77
|
+
# A CSV row is serialized after its 200 went out: withholding the target beats truncating
|
|
78
|
+
# the file.
|
|
79
|
+
FOREST_LOGGER.warn "Polymorphic target withheld, its read permission being unavailable: #{target.class.name}"
|
|
80
|
+
false
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
|
|
66
84
|
def serialize_models(records, options = {}, fields_searched = [])
|
|
67
85
|
options[:is_collection] = true
|
|
68
86
|
if options[:fields].nil?
|
|
69
87
|
options[:context] = { unoptimized: true }.merge(options[:context] || {})
|
|
70
88
|
end
|
|
89
|
+
readable = polymorphic_target_readable
|
|
90
|
+
options[:context] = (options[:context] || {}).merge(polymorphic_target_readable: readable) if readable
|
|
71
91
|
|
|
72
92
|
json = ForestAdmin::JSONAPI::Serializer.serialize(records, options)
|
|
73
93
|
|
|
@@ -317,6 +337,9 @@ module ForestLiana
|
|
|
317
337
|
# column instead of 403ing the file whenever it doesn't error there either.
|
|
318
338
|
requested_fields = fields_per_model(params[:fields], model)
|
|
319
339
|
fields_to_serialize = redact_fields(forest_user, model, requested_fields, named_collections: [])
|
|
340
|
+
# The rows are serialized while the response streams, once the request cache is gone: the
|
|
341
|
+
# read permission of every model a polymorphic row may point at is answered now.
|
|
342
|
+
read_permissions(forest_user, ForestLiana.models.map { |candidate| ForestLiana.name_for(candidate) })
|
|
320
343
|
|
|
321
344
|
set_headers_file
|
|
322
345
|
set_headers_streaming
|
|
@@ -353,6 +376,11 @@ module ForestLiana
|
|
|
353
376
|
elsif record_relationships[field_name] &&
|
|
354
377
|
record_relationships[field_name]['data']
|
|
355
378
|
relationship_id = record_relationships[field_name]['data']['id']
|
|
379
|
+
# The front names the reference field first, then any sub-field a segment condition
|
|
380
|
+
# reads; it names none for a polymorphic relation, whose export falls back to the id.
|
|
381
|
+
reference_field = params[:fields][field_name].to_s.split(',').first
|
|
382
|
+
next relationship_id if reference_field.blank?
|
|
383
|
+
|
|
356
384
|
relationship_type = record_relationships[field_name]['data']['type']
|
|
357
385
|
relationship_object = included.select do |object|
|
|
358
386
|
object['id'] == relationship_id && object['type'] == relationship_type
|
|
@@ -360,7 +388,7 @@ module ForestLiana
|
|
|
360
388
|
|
|
361
389
|
relationship_object = relationship_object.first
|
|
362
390
|
if relationship_object && relationship_object['attributes']
|
|
363
|
-
relationship_object['attributes'][
|
|
391
|
+
relationship_object['attributes'][reference_field]
|
|
364
392
|
else
|
|
365
393
|
nil
|
|
366
394
|
end
|
|
@@ -407,7 +407,9 @@ module ForestLiana
|
|
|
407
407
|
serializer.send(serializer_association(a), a.name) {
|
|
408
408
|
if [:has_one, :belongs_to].include?(a.macro)
|
|
409
409
|
begin
|
|
410
|
-
object.send(a.name)
|
|
410
|
+
target = object.send(a.name)
|
|
411
|
+
readable = @context[:polymorphic_target_readable]
|
|
412
|
+
target && readable && !readable.call(a, target) ? nil : target
|
|
411
413
|
rescue ActiveRecord::RecordNotFound
|
|
412
414
|
nil
|
|
413
415
|
end
|
|
@@ -66,6 +66,12 @@ module ForestLiana
|
|
|
66
66
|
SmartActionChecker.new(parameters, nil, smart_action, user_data, schema_action.type, user).can_execute?
|
|
67
67
|
end
|
|
68
68
|
|
|
69
|
+
# Asked once per serialized row: a collection already answered allocates nothing.
|
|
70
|
+
def readable_collection?(user, collection_name)
|
|
71
|
+
read_permissions(user, [collection_name]) unless @read_permissions_cache&.key?(collection_name)
|
|
72
|
+
@read_permissions_cache[collection_name]
|
|
73
|
+
end
|
|
74
|
+
|
|
69
75
|
# +refetch+ drops the cached permissions first. Only a caller about to refuse on a denial asks
|
|
70
76
|
# for it (a stale, up to TTL-old cache may sit behind a permission granted moments ago): the
|
|
71
77
|
# refetch rewrites the cluster-wide `forest.collections` entry and this request's copy, so a
|
|
@@ -27,8 +27,12 @@ module ForestLiana
|
|
|
27
27
|
if @data.nil?
|
|
28
28
|
new_value = nil
|
|
29
29
|
else
|
|
30
|
-
association_klass = SchemaUtils.polymorphic_models(@association).
|
|
31
|
-
|
|
30
|
+
association_klass = SchemaUtils.polymorphic_models(@association).find { |model| model.name == @data[:type] }
|
|
31
|
+
unless association_klass
|
|
32
|
+
raise ForestLiana::Errors::HTTP422Error.new("#{@data[:type]} is not a target of #{@association.name}")
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
new_value = association_klass.find(@data[:id]) if @data[:id]
|
|
32
36
|
end
|
|
33
37
|
else
|
|
34
38
|
new_value = @association.klass.find(@data[:id]) if @data && @data[:id]
|
|
@@ -231,6 +231,8 @@ module ForestLiana
|
|
|
231
231
|
end
|
|
232
232
|
|
|
233
233
|
def apply_live_query_segment
|
|
234
|
+
ScopeManager.assert_can_execute_segment_query!(@user, @collection_name, @params[:segmentQuery])
|
|
235
|
+
|
|
234
236
|
LiveQueryChecker.new(@params[:segmentQuery], 'Live Query Segment').validate
|
|
235
237
|
|
|
236
238
|
begin
|
|
@@ -315,14 +315,7 @@ module ForestLiana
|
|
|
315
315
|
end
|
|
316
316
|
|
|
317
317
|
def get_polymorphic_types(relation)
|
|
318
|
-
|
|
319
|
-
ForestLiana.models.each do |model|
|
|
320
|
-
unless model.reflect_on_all_associations.select { |association| association.options[:as] == relation.name.to_sym }.empty?
|
|
321
|
-
types << model.name
|
|
322
|
-
end
|
|
323
|
-
end
|
|
324
|
-
|
|
325
|
-
types
|
|
318
|
+
SchemaUtils.polymorphic_models(relation).map(&:name)
|
|
326
319
|
end
|
|
327
320
|
|
|
328
321
|
def automatic_inverse_of(association)
|
|
@@ -94,15 +94,34 @@ module ForestLiana
|
|
|
94
94
|
|
|
95
95
|
end
|
|
96
96
|
|
|
97
|
+
# Keyed on the reflection itself, not [owner, name]: a hit must not allocate (see LookupCache).
|
|
97
98
|
def self.polymorphic_models(relation)
|
|
98
|
-
|
|
99
|
-
LookupCache.fetch(:polymorphic_models, name) { scan_polymorphic_models(name).freeze }
|
|
99
|
+
LookupCache.fetch(:polymorphic_models, relation) { scan_polymorphic_models(relation).freeze }
|
|
100
100
|
end
|
|
101
101
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
102
|
+
# A relation whose real targets declare no inverse falls back to the namesakes' targets, the
|
|
103
|
+
# only ones it ever had: an empty list would drop it from every response.
|
|
104
|
+
def self.scan_polymorphic_models(relation)
|
|
105
|
+
name = relation.name.to_sym
|
|
106
|
+
owner = relation.active_record
|
|
107
|
+
|
|
108
|
+
inverses = ForestLiana.models.each_with_object({}) do |model, acc|
|
|
109
|
+
matching = model.reflect_on_all_associations.select { |association| association.options[:as] == name }
|
|
110
|
+
acc[model] = matching if matching.any?
|
|
105
111
|
end
|
|
112
|
+
|
|
113
|
+
owned = inverses.select { |_model, associations| associations.any? { |association| targets_owner?(association, owner) } }.keys
|
|
114
|
+
owned.empty? ? inverses.keys : owned
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
# Both directions for STI: the belongs_to may sit on a subclass while the inverse targets the
|
|
118
|
+
# base class, or the other way round.
|
|
119
|
+
def self.targets_owner?(association, owner)
|
|
120
|
+
target = association.klass
|
|
121
|
+
target <= owner || owner <= target
|
|
122
|
+
rescue NameError
|
|
123
|
+
FOREST_LOGGER.warn "Unknown association #{association.name} on class #{association.active_record.name}"
|
|
124
|
+
false
|
|
106
125
|
end
|
|
107
126
|
|
|
108
127
|
|
|
@@ -2,6 +2,7 @@ module ForestLiana
|
|
|
2
2
|
class ScopeManager
|
|
3
3
|
# 5 minutes expiration cache
|
|
4
4
|
@@scope_cache_expiration_delta = 300
|
|
5
|
+
SEGMENT_UNION_SEPARATOR = '/*MULTI-SEGMENTS-QUERIES-UNION*/ UNION '
|
|
5
6
|
|
|
6
7
|
def self.apply_scopes_on_records(records, user, collection_name, timezone)
|
|
7
8
|
scope_filters = get_scope(collection_name, user)
|
|
@@ -65,25 +66,69 @@ module ForestLiana
|
|
|
65
66
|
end
|
|
66
67
|
|
|
67
68
|
def self.invalidate_scope_cache(rendering_id)
|
|
68
|
-
Rails.cache.delete(
|
|
69
|
-
RequestCache.delete(
|
|
70
|
-
end
|
|
71
|
-
|
|
72
|
-
def self.
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
69
|
+
Rails.cache.delete(scope_cache_key(rendering_id))
|
|
70
|
+
RequestCache.delete(scope_cache_key(rendering_id))
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def self.assert_can_execute_segment_query!(user, collection_name, query)
|
|
74
|
+
return if Ability::ALLOWED_PERMISSION_LEVELS.include?(user['permission_level'])
|
|
75
|
+
raise Ability::Exceptions::AccessDenied.new unless query.is_a?(String)
|
|
76
|
+
return if segment_query_allowed?(segment_scopes(user['rendering_id']), collection_name, query)
|
|
77
|
+
return if segment_query_allowed?(segment_scopes(user['rendering_id'], force_fetch: true), collection_name, query)
|
|
78
|
+
|
|
79
|
+
raise Ability::Exceptions::AccessDenied.new
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def self.segment_query_allowed?(scopes, collection_name, query)
|
|
83
|
+
allowed_queries = (scopes['segments'] || {})[collection_name]
|
|
84
|
+
return false if allowed_queries.nil?
|
|
85
|
+
|
|
86
|
+
allowed_set = allowed_queries.map { |allowed| normalize_segment_query(allowed) }
|
|
87
|
+
return true if allowed_set.include?(normalize_segment_query(query))
|
|
88
|
+
|
|
89
|
+
parts = query.split(SEGMENT_UNION_SEPARATOR)
|
|
90
|
+
parts.any? && parts.all? { |part| allowed_set.include?(normalize_segment_query(part)) }
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def self.normalize_segment_query(query)
|
|
94
|
+
query.sub(/[\s;]+\z/, '').strip
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def self.fetch_scopes(rendering_id, force_fetch: false)
|
|
98
|
+
key = scope_cache_key(rendering_id)
|
|
99
|
+
return RequestCache.write(key, cached_scopes(key, rendering_id, force_fetch)) if force_fetch
|
|
100
|
+
|
|
101
|
+
RequestCache.fetch(key) { cached_scopes(key, rendering_id, force_fetch) }
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def self.segment_scopes(rendering_id, force_fetch: false)
|
|
105
|
+
fetch_scopes(rendering_id, force_fetch: force_fetch)
|
|
106
|
+
rescue StandardError
|
|
107
|
+
raise Errors::PermissionsUnavailableError.new
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def self.cached_scopes(key, rendering_id, force_fetch)
|
|
111
|
+
Rails.cache.fetch(key, expires_in: @@scope_cache_expiration_delta, force: force_fetch) do
|
|
112
|
+
load_scopes(rendering_id)
|
|
84
113
|
end
|
|
85
114
|
end
|
|
86
115
|
|
|
116
|
+
def self.load_scopes(rendering_id)
|
|
117
|
+
response = ForestLiana::ForestApiRequester.get("/liana/v4/permissions/renderings/#{rendering_id}")
|
|
118
|
+
raise 'Unable to fetch scopes' unless response.is_a?(Net::HTTPOK)
|
|
119
|
+
parsed_response = JSON.parse(response.body)
|
|
120
|
+
|
|
121
|
+
{
|
|
122
|
+
'scopes' => decode_scope(parsed_response['collections']),
|
|
123
|
+
'segments' => decode_segments(parsed_response['collections']),
|
|
124
|
+
'team' => parsed_response['team']
|
|
125
|
+
}
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def self.scope_cache_key(rendering_id)
|
|
129
|
+
"forest.scopes.#{rendering_id}"
|
|
130
|
+
end
|
|
131
|
+
|
|
87
132
|
def self.decode_scope(raw_scopes)
|
|
88
133
|
scopes = {}
|
|
89
134
|
raw_scopes.each do |collection_name, value|
|
|
@@ -92,5 +137,14 @@ module ForestLiana
|
|
|
92
137
|
|
|
93
138
|
scopes
|
|
94
139
|
end
|
|
140
|
+
|
|
141
|
+
def self.decode_segments(raw_collections)
|
|
142
|
+
raw_collections.each_with_object({}) do |(collection_name, value), segments|
|
|
143
|
+
segments[collection_name] = value['segments'] unless value['segments'].nil?
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
private_class_method :segment_query_allowed?, :normalize_segment_query, :segment_scopes, :cached_scopes,
|
|
148
|
+
:load_scopes, :scope_cache_key
|
|
95
149
|
end
|
|
96
150
|
end
|
data/lib/forest_liana/version.rb
CHANGED
|
@@ -64,27 +64,36 @@ describe 'Lookups by name over the schema' do
|
|
|
64
64
|
let(:addressable) { Address.reflect_on_association(:addressable) }
|
|
65
65
|
|
|
66
66
|
it 'scans the models once for repeated lookups of a relation' do
|
|
67
|
-
expect(ForestLiana::SchemaUtils).to receive(:scan_polymorphic_models).with(
|
|
67
|
+
expect(ForestLiana::SchemaUtils).to receive(:scan_polymorphic_models).with(addressable).once.and_call_original
|
|
68
68
|
|
|
69
69
|
3.times { expect(ForestLiana::SchemaUtils.polymorphic_models(addressable)).to eq [User] }
|
|
70
70
|
end
|
|
71
71
|
|
|
72
|
+
it 'answers a remembered relation without allocating' do
|
|
73
|
+
ForestLiana::SchemaUtils.polymorphic_models(addressable)
|
|
74
|
+
|
|
75
|
+
allocated = GC.stat(:total_allocated_objects)
|
|
76
|
+
1_000.times { ForestLiana::SchemaUtils.polymorphic_models(addressable) }
|
|
77
|
+
|
|
78
|
+
expect(GC.stat(:total_allocated_objects) - allocated).to be < 100
|
|
79
|
+
end
|
|
80
|
+
|
|
72
81
|
it 'returns a frozen list' do
|
|
73
82
|
expect(ForestLiana::SchemaUtils.polymorphic_models(addressable)).to be_frozen
|
|
74
83
|
end
|
|
75
84
|
|
|
76
85
|
it 'remembers a relation no model targets' do
|
|
77
|
-
orphan = double(name: :orphan)
|
|
78
|
-
expect(ForestLiana::SchemaUtils).to receive(:scan_polymorphic_models).with(
|
|
86
|
+
orphan = double(name: :orphan, active_record: Address)
|
|
87
|
+
expect(ForestLiana::SchemaUtils).to receive(:scan_polymorphic_models).with(orphan).once.and_call_original
|
|
79
88
|
|
|
80
89
|
2.times { expect(ForestLiana::SchemaUtils.polymorphic_models(orphan)).to eq [] }
|
|
81
90
|
end
|
|
82
91
|
|
|
83
92
|
it 'finds a target added to the models after a first lookup' do
|
|
84
|
-
target = Class.new(ActiveRecord::Base) do
|
|
93
|
+
target = stub_const('AddressableTree', Class.new(ActiveRecord::Base) do
|
|
85
94
|
self.table_name = 'trees'
|
|
86
95
|
has_many :addresses, as: :addressable
|
|
87
|
-
end
|
|
96
|
+
end)
|
|
88
97
|
expect(ForestLiana::SchemaUtils.polymorphic_models(addressable)).to eq [User]
|
|
89
98
|
|
|
90
99
|
ForestLiana.models << target
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
require 'rails_helper'
|
|
2
|
+
|
|
3
|
+
describe 'Exporting a belongsTo to CSV', type: :request do
|
|
4
|
+
let(:auth_headers) do
|
|
5
|
+
token = JWT.encode({ id: 1, email: 'michael.kelso@that70.show', first_name: 'Michael',
|
|
6
|
+
last_name: 'Kelso', team: 'Operations', rendering_id: 16,
|
|
7
|
+
exp: Time.now.to_i + 2.weeks.to_i, permission_level: 'admin' },
|
|
8
|
+
ForestLiana.auth_secret, 'HS256')
|
|
9
|
+
{ 'Accept' => 'application/json', 'Content-Type' => 'application/json', 'Authorization' => "Bearer #{token}" }
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
let!(:owner) { User.create!(name: 'Michel') }
|
|
13
|
+
let!(:tree) { Tree.create!(name: 'Lemon Tree', owner: owner, cutter: owner) }
|
|
14
|
+
|
|
15
|
+
before do
|
|
16
|
+
Rails.cache.clear
|
|
17
|
+
Rails.cache.write('forest.users', { '1' => { 'id' => 1, 'roleId' => 1, 'rendering_id' => '1' } })
|
|
18
|
+
Rails.cache.write('forest.has_permission', true)
|
|
19
|
+
enabled = { 'roles' => [1] }
|
|
20
|
+
allow_any_instance_of(ForestLiana::Ability::Fetch).to receive(:get_permissions)
|
|
21
|
+
.with('/liana/v4/permissions/environment').and_return(
|
|
22
|
+
'collections' => %w[Tree User].to_h do |name|
|
|
23
|
+
[name, { 'collection' => %w[browseEnabled readEnabled editEnabled addEnabled deleteEnabled exportEnabled]
|
|
24
|
+
.to_h { |key| [key, enabled] }, 'actions' => {} }]
|
|
25
|
+
end
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
allow(ForestLiana::IpWhitelist).to receive(:retrieve) { true }
|
|
29
|
+
allow(ForestLiana::IpWhitelist).to receive(:is_ip_whitelist_retrieved) { true }
|
|
30
|
+
allow(ForestLiana::IpWhitelist).to receive(:is_ip_valid) { true }
|
|
31
|
+
allow_any_instance_of(ForestLiana::Ability).to receive(:forest_authorize!) { true }
|
|
32
|
+
allow(ForestLiana::ScopeManager).to receive(:fetch_scopes)
|
|
33
|
+
.and_return('scopes' => {}, 'team' => { 'id' => '1', 'name' => 'Operations' })
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
after do
|
|
37
|
+
Tree.destroy_all
|
|
38
|
+
User.destroy_all
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def export(owner_fields)
|
|
42
|
+
get '/forest/Tree.csv',
|
|
43
|
+
params: { fields: { 'Tree' => 'id,name,owner', 'owner' => owner_fields },
|
|
44
|
+
header: 'id,name,owner', timezone: 'Europe/Paris' },
|
|
45
|
+
headers: auth_headers
|
|
46
|
+
|
|
47
|
+
expect(response.status).to eq(200)
|
|
48
|
+
response.body.lines.second.strip
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
it 'exports the reference field of the target' do
|
|
52
|
+
expect(export('name')).to eq("#{tree.id},Lemon Tree,Michel")
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# A basic segment condition on another sub-field of the relation adds it after the reference field.
|
|
56
|
+
it 'exports the reference field when the front also names other sub-fields' do
|
|
57
|
+
expect(export('name,title')).to eq("#{tree.id},Lemon Tree,Michel")
|
|
58
|
+
end
|
|
59
|
+
end
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
require 'rails_helper'
|
|
2
|
+
|
|
3
|
+
# PRD-2267: a polymorphic relation was given the targets of every namesake on another owner, so a
|
|
4
|
+
# role reading its real targets still saw it refused.
|
|
5
|
+
describe 'A polymorphic relation sharing its name with another owner\'s', type: :request do
|
|
6
|
+
token = JWT.encode({ id: 1, email: 'michael.kelso@that70.show', first_name: 'Michael',
|
|
7
|
+
last_name: 'Kelso', team: 'Operations', rendering_id: 16,
|
|
8
|
+
exp: Time.now.to_i + 2.weeks.to_i, permission_level: 'admin' },
|
|
9
|
+
ForestLiana.auth_secret, 'HS256')
|
|
10
|
+
headers = { 'Accept' => 'application/json', 'Content-Type' => 'application/json',
|
|
11
|
+
'Authorization' => "Bearer #{token}" }
|
|
12
|
+
|
|
13
|
+
let!(:user) { User.create!(name: 'Michel') }
|
|
14
|
+
let!(:address) { Address.create!(line1: '1 Palm Street', city: 'Papeete', zipcode: '98713', addressable: user) }
|
|
15
|
+
let(:namesakes) do
|
|
16
|
+
[
|
|
17
|
+
stub_const('PolyNote', Class.new(ActiveRecord::Base) do
|
|
18
|
+
self.table_name = 'articles'
|
|
19
|
+
belongs_to :addressable, polymorphic: true
|
|
20
|
+
end),
|
|
21
|
+
stub_const('PolyHarbor', Class.new(ActiveRecord::Base) do
|
|
22
|
+
self.table_name = 'harbors'
|
|
23
|
+
has_many :poly_notes, as: :addressable
|
|
24
|
+
end)
|
|
25
|
+
]
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
before do
|
|
29
|
+
# Rails 6.1 remembers the class a name resolved to, across examples.
|
|
30
|
+
ActiveSupport::Dependencies::Reference.clear! if defined?(ActiveSupport::Dependencies::Reference)
|
|
31
|
+
ForestLiana.models.push(*namesakes)
|
|
32
|
+
|
|
33
|
+
Rails.cache.clear
|
|
34
|
+
Rails.cache.write('forest.users', { '1' => { 'id' => 1, 'roleId' => 1, 'rendering_id' => '1' } })
|
|
35
|
+
Rails.cache.write('forest.has_permission', true)
|
|
36
|
+
enabled = { 'roles' => [1] }
|
|
37
|
+
allow_any_instance_of(ForestLiana::Ability::Fetch).to receive(:get_permissions)
|
|
38
|
+
.with('/liana/v4/permissions/environment').and_return(
|
|
39
|
+
'collections' => %w[Address User].to_h do |name|
|
|
40
|
+
[name, { 'collection' => %w[browseEnabled readEnabled editEnabled addEnabled deleteEnabled exportEnabled]
|
|
41
|
+
.to_h { |key| [key, enabled] }, 'actions' => {} }]
|
|
42
|
+
end
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
allow(ForestLiana::IpWhitelist).to receive(:retrieve) { true }
|
|
46
|
+
allow(ForestLiana::IpWhitelist).to receive(:is_ip_whitelist_retrieved) { true }
|
|
47
|
+
allow(ForestLiana::IpWhitelist).to receive(:is_ip_valid) { true }
|
|
48
|
+
allow_any_instance_of(ForestLiana::Ability).to receive(:forest_authorize!) { true }
|
|
49
|
+
allow(ForestLiana::ScopeManager).to receive(:fetch_scopes)
|
|
50
|
+
.and_return('scopes' => {}, 'team' => { 'id' => '1', 'name' => 'Operations' })
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
after do
|
|
54
|
+
namesakes.each { |model| ForestLiana.models.delete(model) }
|
|
55
|
+
Address.destroy_all
|
|
56
|
+
User.destroy_all
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
it 'serves the relation to a role that reads its own targets' do
|
|
60
|
+
get "/forest/Address/#{address.id}",
|
|
61
|
+
params: { fields: { 'Address' => 'id,line1,addressable', 'addressable' => 'id' }, timezone: 'Europe/Paris' },
|
|
62
|
+
headers: headers
|
|
63
|
+
|
|
64
|
+
expect(response.status).to eq(200)
|
|
65
|
+
relationship = JSON.parse(response.body)['data']['relationships']['addressable']
|
|
66
|
+
expect(relationship['data']).to include('id' => user.id.to_s)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
it 'keeps the relation column in the CSV export' do
|
|
70
|
+
get '/forest/Address.csv',
|
|
71
|
+
params: { fields: { 'Address' => 'id,line1,addressable', 'addressable' => 'id' },
|
|
72
|
+
header: 'id,line1,addressable', timezone: 'Europe/Paris' },
|
|
73
|
+
headers: headers
|
|
74
|
+
|
|
75
|
+
expect(response.status).to eq(200)
|
|
76
|
+
expect(response.body.lines.first.strip).to eq('id,line1,addressable')
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
it 'exports the id of the target, as the list shows it, when no field of the target is named' do
|
|
80
|
+
get '/forest/Address.csv',
|
|
81
|
+
params: { fields: { 'Address' => 'id,line1,addressable' },
|
|
82
|
+
header: 'id,line1,addressable', timezone: 'Europe/Paris' },
|
|
83
|
+
headers: headers
|
|
84
|
+
|
|
85
|
+
expect(response.status).to eq(200)
|
|
86
|
+
expect(response.body.lines.second.strip).to eq("#{address.id},1 Palm Street,#{user.id}")
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
it 'still exports the named field of the target' do
|
|
90
|
+
get '/forest/Address.csv',
|
|
91
|
+
params: { fields: { 'Address' => 'id,line1,addressable', 'addressable' => 'name' },
|
|
92
|
+
header: 'id,line1,addressable', timezone: 'Europe/Paris' },
|
|
93
|
+
headers: headers
|
|
94
|
+
|
|
95
|
+
expect(response.status).to eq(200)
|
|
96
|
+
expect(response.body.lines.second.strip).to eq("#{address.id},1 Palm Street,Michel")
|
|
97
|
+
end
|
|
98
|
+
end
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
require 'rails_helper'
|
|
2
|
+
|
|
3
|
+
# Rails does not require the targets of a polymorphic relation to declare an inverse, so a row may
|
|
4
|
+
# point at a model the relation's read check never covered.
|
|
5
|
+
describe 'Serving the target a polymorphic relation points at', type: :request do
|
|
6
|
+
let(:auth_headers) do
|
|
7
|
+
token = JWT.encode({ id: 1, email: 'michael.kelso@that70.show', first_name: 'Michael',
|
|
8
|
+
last_name: 'Kelso', team: 'Operations', rendering_id: 16,
|
|
9
|
+
exp: Time.now.to_i + 2.weeks.to_i, permission_level: 'admin' },
|
|
10
|
+
ForestLiana.auth_secret, 'HS256')
|
|
11
|
+
{ 'Accept' => 'application/json', 'Content-Type' => 'application/json', 'Authorization' => "Bearer #{token}" }
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
let(:readable) { %w[Address User] }
|
|
15
|
+
let!(:user) { User.create!(name: 'Michel') }
|
|
16
|
+
let!(:island) { Island.create!(name: 'Lemon Island') }
|
|
17
|
+
let!(:to_user) { Address.create!(line1: '1 Palm Street', city: 'Papeete', zipcode: '98713', addressable: user) }
|
|
18
|
+
let!(:to_island) { Address.create!(line1: '2 Palm Street', city: 'Papeete', zipcode: '98713', addressable: island) }
|
|
19
|
+
|
|
20
|
+
before do
|
|
21
|
+
Rails.cache.clear
|
|
22
|
+
Rails.cache.write('forest.users', { '1' => { 'id' => 1, 'roleId' => 1, 'rendering_id' => '1' } })
|
|
23
|
+
Rails.cache.write('forest.has_permission', true)
|
|
24
|
+
enabled = { 'roles' => [1] }
|
|
25
|
+
allow_any_instance_of(ForestLiana::Ability::Fetch).to receive(:get_permissions)
|
|
26
|
+
.with('/liana/v4/permissions/environment').and_return(
|
|
27
|
+
'collections' => readable.to_h do |name|
|
|
28
|
+
[name, { 'collection' => %w[browseEnabled readEnabled editEnabled addEnabled deleteEnabled exportEnabled]
|
|
29
|
+
.to_h { |key| [key, enabled] }, 'actions' => {} }]
|
|
30
|
+
end
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
allow(ForestLiana::IpWhitelist).to receive(:retrieve) { true }
|
|
34
|
+
allow(ForestLiana::IpWhitelist).to receive(:is_ip_whitelist_retrieved) { true }
|
|
35
|
+
allow(ForestLiana::IpWhitelist).to receive(:is_ip_valid) { true }
|
|
36
|
+
allow_any_instance_of(ForestLiana::Ability).to receive(:forest_authorize!) { true }
|
|
37
|
+
allow(ForestLiana::ScopeManager).to receive(:fetch_scopes)
|
|
38
|
+
.and_return('scopes' => {}, 'team' => { 'id' => '1', 'name' => 'Operations' })
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
after do
|
|
42
|
+
Rails.cache.clear
|
|
43
|
+
Address.destroy_all
|
|
44
|
+
Island.destroy_all
|
|
45
|
+
User.destroy_all
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def get_one(address)
|
|
49
|
+
get "/forest/Address/#{address.id}",
|
|
50
|
+
params: { fields: { 'Address' => 'id,line1,addressable', 'addressable' => 'id' }, timezone: 'Europe/Paris' },
|
|
51
|
+
headers: auth_headers
|
|
52
|
+
expect(response.status).to eq(200)
|
|
53
|
+
JSON.parse(response.body)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def served_addressable(body)
|
|
57
|
+
body['data'].dig('relationships', 'addressable', 'data')
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def export_rows
|
|
61
|
+
get '/forest/Address.csv',
|
|
62
|
+
params: { fields: { 'Address' => 'id,addressable' }, header: 'id,addressable', timezone: 'Europe/Paris' },
|
|
63
|
+
headers: auth_headers
|
|
64
|
+
expect(response.status).to eq(200)
|
|
65
|
+
response.body.lines.drop(1).map(&:strip)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
it 'serves a target of a model the relation declares' do
|
|
69
|
+
expect(served_addressable(get_one(to_user))).to include('type' => 'User', 'id' => user.id.to_s)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
it 'withholds a target of a model the role cannot read, though the relation never declared it' do
|
|
73
|
+
body = get_one(to_island)
|
|
74
|
+
|
|
75
|
+
expect(served_addressable(body)).to be_nil
|
|
76
|
+
expect(body.fetch('included', [])).to be_empty
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
context 'when the role reads that model' do
|
|
80
|
+
let(:readable) { %w[Address User Island] }
|
|
81
|
+
|
|
82
|
+
it 'serves the target' do
|
|
83
|
+
expect(served_addressable(get_one(to_island))).to include('type' => 'Island', 'id' => island.id.to_s)
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
context 'with skip_relation_read_permissions' do
|
|
88
|
+
before { allow(ForestLiana).to receive(:skip_relation_read_permissions?).and_return(true) }
|
|
89
|
+
|
|
90
|
+
it 'serves a target of any exposed model' do
|
|
91
|
+
expect(served_addressable(get_one(to_island))).to include('type' => 'Island', 'id' => island.id.to_s)
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
it 'leaves the cell of a withheld target empty in the CSV export' do
|
|
96
|
+
expect(export_rows).to contain_exactly("#{to_user.id},#{user.id}", "#{to_island.id},")
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# The rows of an export are serialized while the response streams, after the request cache is
|
|
100
|
+
# gone: a permission asked for then could reach the Forest API mid-file.
|
|
101
|
+
it 'answers the read permission of every model before the CSV export streams' do
|
|
102
|
+
expect_any_instance_of(ForestLiana::ResourcesController).to receive(:read_permissions)
|
|
103
|
+
.with(anything, array_including('Island', 'User')).and_call_original
|
|
104
|
+
allow_any_instance_of(ForestLiana::ResourcesController).to receive(:read_permissions).and_call_original
|
|
105
|
+
|
|
106
|
+
export_rows
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
it 'withholds the target rather than failing when its read permission is unavailable' do
|
|
110
|
+
allow_any_instance_of(ForestLiana::ResourcesController).to receive(:readable_collection?)
|
|
111
|
+
.and_raise(ForestLiana::Errors::PermissionsUnavailableError)
|
|
112
|
+
|
|
113
|
+
expect(export_rows).to contain_exactly("#{to_user.id},", "#{to_island.id},")
|
|
114
|
+
end
|
|
115
|
+
end
|
|
@@ -19,6 +19,11 @@ describe 'Requesting resources with the Forest-Projection header', :type => :req
|
|
|
19
19
|
allow_any_instance_of(ForestLiana::Ability).to receive(:forest_authorize!) { true }
|
|
20
20
|
allow(ForestLiana::ScopeManager).to receive(:fetch_scopes)
|
|
21
21
|
.and_return({ 'scopes' => {}, 'team' => { 'id' => '1', 'name' => 'Operations' } })
|
|
22
|
+
|
|
23
|
+
# The permissions another spec left in Rails.cache would otherwise decide these requests.
|
|
24
|
+
Rails.cache.clear
|
|
25
|
+
allow_any_instance_of(ForestLiana::Ability::Fetch).to receive(:get_permissions)
|
|
26
|
+
.with('/liana/v4/permissions/environment').and_return(true)
|
|
22
27
|
end
|
|
23
28
|
|
|
24
29
|
let(:auth_headers) do
|