Skip to content

Commit 6bb4439

Browse files
authored
fix: smart actions restricted to a segment using segment query should be visible (#510)
* fix: smart actions restricted to a segment using segment query should be visible * test: add rspec tests for all use cases
1 parent cf0fe64 commit 6bb4439

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

app/services/forest_liana/permissions_checker.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,16 @@ def smart_action_allowed?(smart_actions_permissions)
159159

160160
def segment_query_allowed?
161161
segments_queries_permissions = get_segments_in_permissions
162-
162+
# NOTICE: The segmentQuery should be in the segments_queries_permissions
163163
return false unless segments_queries_permissions
164164

165+
# Handle UNION queries made by the FRONT to display available actions on details view
166+
unionQueries = @collection_list_parameters[:segmentQuery].split('/*MULTI-SEGMENTS-QUERIES-UNION*/ UNION ');
167+
if unionQueries.length > 1
168+
# Are unionQueries all included only in the allowed queries
169+
return unionQueries.all? { |unionQuery| segments_queries_permissions.select { |query| query.gsub(/;\s*/i, '') === unionQuery }.length > 0 };
170+
end
171+
165172
# NOTICE: @query_request_info matching an existing segment query
166173
return segments_queries_permissions.include? @collection_list_parameters[:segmentQuery]
167174
end

spec/services/forest_liana/permissions_checker_acl_disabled_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,14 @@ module ForestLiana
481481
end
482482
end
483483

484+
context 'when user has no segments and param segmentQuery is there' do
485+
let(:segmentQuery) { 'SELECT * FROM products;' }
486+
let(:collection_list_parameters) { { :user_id => "1", :segmentQuery => segmentQuery } }
487+
it 'should be authorized' do
488+
expect(subject.is_authorized?).to be false
489+
end
490+
end
491+
484492
context 'when segments are defined' do
485493
let(:segments_permissions) { ['SELECT * FROM products;', 'SELECT * FROM sellers;'] }
486494
let(:collection_list_parameters) { { :user_id => "1", :segmentQuery => segmentQuery } }
@@ -499,6 +507,26 @@ module ForestLiana
499507
end
500508
end
501509

510+
context 'when received union segments NOT passing validation' do
511+
let(:segmentQuery) { 'SELECT * FROM sellers/*MULTI-SEGMENTS-QUERIES-UNION*/ UNION SELECT column_name(s) FROM table1 UNION SELECT column_name(s) FROM table2' }
512+
it 'should return false' do
513+
expect(subject.is_authorized?).to be false
514+
end
515+
end
516+
517+
context 'when received union segments passing validation' do
518+
let(:segmentQuery) { 'SELECT * FROM sellers/*MULTI-SEGMENTS-QUERIES-UNION*/ UNION SELECT * FROM products' }
519+
it 'should return true' do
520+
expect(subject.is_authorized?).to be true
521+
end
522+
end
523+
context 'when received union segments with UNION inside passing validation' do
524+
let(:segmentQuery) { 'SELECT COUNT(*) AS value FROM products/*MULTI-SEGMENTS-QUERIES-UNION*/ UNION SELECT column_name(s) FROM table1 UNION SELECT column_name(s) FROM table2' }
525+
let(:segments_permissions) { ['SELECT COUNT(*) AS value FROM products;', 'SELECT column_name(s) FROM table1 UNION SELECT column_name(s) FROM table2;', 'SELECT * FROM products;', 'SELECT * FROM sellers;'] }
526+
it 'should return true' do
527+
expect(subject.is_authorized?).to be true
528+
end
529+
end
502530
end
503531

504532
context 'when user has not the required permission' do

spec/services/forest_liana/permissions_checker_acl_enabled_spec.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,14 @@ module ForestLiana
458458
end
459459
end
460460

461+
context 'when user has no segments queries permissions and param segmentQuery is there' do
462+
let(:segmentQuery) { 'SELECT * FROM products;' }
463+
let(:collection_list_parameters) { { :user_id => "1", :segmentQuery => segmentQuery } }
464+
it 'should be authorized' do
465+
expect(subject.is_authorized?).to be false
466+
end
467+
end
468+
461469
context 'when segments are defined' do
462470
let(:default_rendering_id) { 1 }
463471
let(:segments_permissions) {
@@ -484,6 +492,36 @@ module ForestLiana
484492
expect(subject.is_authorized?).to be false
485493
end
486494
end
495+
496+
context 'when received union segments NOT passing validation' do
497+
let(:segmentQuery) { 'SELECT * FROM sellers/*MULTI-SEGMENTS-QUERIES-UNION*/ UNION SELECT column_name(s) FROM table1 UNION SELECT column_name(s) FROM table2' }
498+
it 'should return false' do
499+
expect(subject.is_authorized?).to be false
500+
end
501+
end
502+
503+
context 'when received union segments passing validation' do
504+
let(:segmentQuery) { 'SELECT * FROM sellers/*MULTI-SEGMENTS-QUERIES-UNION*/ UNION SELECT * FROM products' }
505+
it 'should return true' do
506+
expect(subject.is_authorized?).to be true
507+
end
508+
end
509+
510+
context 'when received union segments with UNION inside passing validation' do
511+
let(:segmentQuery) { 'SELECT COUNT(*) AS value FROM products/*MULTI-SEGMENTS-QUERIES-UNION*/ UNION SELECT column_name(s) FROM table1 UNION SELECT column_name(s) FROM table2' }
512+
let(:segments_permissions) {
513+
{
514+
default_rendering_id => {
515+
collection_name => {
516+
'segments' => ['SELECT COUNT(*) AS value FROM products;', 'SELECT column_name(s) FROM table1 UNION SELECT column_name(s) FROM table2;', 'SELECT * FROM products;', 'SELECT * FROM sellers;']
517+
}
518+
}
519+
}
520+
}
521+
it 'should return true' do
522+
expect(subject.is_authorized?).to be true
523+
end
524+
end
487525
end
488526
end
489527

0 commit comments

Comments
 (0)