Skip to content

Commit c98ac23

Browse files
authored
fix: raise error when an action is call on a smartcollection (#605)
1 parent 2d95e96 commit c98ac23

File tree

4 files changed

+34
-26
lines changed

4 files changed

+34
-26
lines changed

app/controllers/forest_liana/smart_actions_controller.rb

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,19 @@ class SmartActionsController < ForestLiana::ApplicationController
55
rescue_from ForestLiana::Ability::Exceptions::ActionConditionError, with: :render_error
66
include ForestLiana::Ability
77
if Rails::VERSION::MAJOR < 4
8-
before_filter :smart_action_pre_perform_checks
8+
before_filter :get_smart_action_request
9+
before_filter :find_resource
10+
before_filter :check_permission_for_smart_route
11+
before_filter :ensure_record_ids_in_scope
912
else
10-
before_action :smart_action_pre_perform_checks
13+
before_action :get_smart_action_request
14+
before_action :find_resource
15+
before_action :check_permission_for_smart_route
16+
before_action :ensure_record_ids_in_scope
1117
end
1218

1319
private
1420

15-
def smart_action_pre_perform_checks
16-
get_smart_action_request
17-
find_resource
18-
check_permission_for_smart_route
19-
ensure_record_ids_in_scope
20-
end
21-
2221
def get_smart_action_request
2322
begin
2423
params[:data][:attributes]
@@ -31,18 +30,11 @@ def get_smart_action_request
3130
end
3231

3332
def find_resource
34-
begin
3533
@resource = SchemaUtils.find_model_from_collection_name(@parameters[:data][:attributes][:collection_name])
36-
if @resource.nil? || !SchemaUtils.model_included?(@resource) ||
37-
!@resource.ancestors.include?(ActiveRecord::Base)
38-
render serializer: nil, json: { status: 404 }, status: :not_found
34+
if @resource.nil? || !SchemaUtils.model_included?(@resource) || !@resource.ancestors.include?(ActiveRecord::Base)
35+
raise ForestLiana::Errors::HTTP422Error.new('The conditional smart actions are not supported with Smart Collection. Please contact an administrator.')
3936
end
4037
@resource
41-
rescue => error
42-
FOREST_REPORTER.report error
43-
FOREST_LOGGER.error "Find Collection error: #{error}\n#{format_stacktrace(error)}"
44-
render serializer: nil, json: { status: 404 }, status: :not_found
45-
end
4638
end
4739

4840
def check_permission_for_smart_route

app/services/forest_liana/ability.rb

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,27 @@ module Ability
77
def forest_authorize!(action, user, collection, args = {})
88
case action
99
when 'browse', 'read', 'edit', 'add', 'delete', 'export'
10-
raise ForestLiana::Ability::Exceptions::AccessDenied.new unless is_crud_authorized?(action, user, collection)
10+
raise ForestLiana::Ability::Exceptions::AccessDenied.new unless is_crud_authorized?(action, user, collection)
1111
when 'chart'
12-
if ALLOWED_PERMISSION_LEVELS.exclude?(user['permission_level'])
13-
raise ForestLiana::Errors::HTTP422Error.new('The argument parameters is missing') if args[:parameters].nil?
14-
raise ForestLiana::Ability::Exceptions::AccessDenied.new unless is_chart_authorized?(user, args[:parameters])
15-
end
12+
if ALLOWED_PERMISSION_LEVELS.exclude?(user['permission_level'])
13+
raise ForestLiana::Errors::HTTP422Error.new('The argument parameters is missing') if args[:parameters].nil?
14+
raise ForestLiana::Ability::Exceptions::AccessDenied.new unless is_chart_authorized?(user, args[:parameters])
15+
end
1616
when 'action'
17-
raise ForestLiana::Errors::HTTP422Error.new('You must implement the arguments : parameters, endpoint & http_method') if args[:parameters].nil? || args[:endpoint].nil? || args[:http_method].nil?
18-
is_smart_action_authorized?(user, collection, args[:parameters], args[:endpoint], args[:http_method])
17+
validate_collection collection
18+
raise ForestLiana::Errors::HTTP422Error.new('You must implement the arguments : parameters, endpoint & http_method') if args[:parameters].nil? || args[:endpoint].nil? || args[:http_method].nil?
19+
is_smart_action_authorized?(user, collection, args[:parameters], args[:endpoint], args[:http_method])
1920
else
2021
raise ForestLiana::Ability::Exceptions::AccessDenied.new
2122
end
2223
end
24+
25+
private
26+
27+
def validate_collection(collection)
28+
if collection.nil? || !SchemaUtils.model_included?(collection)
29+
raise ForestLiana::Errors::HTTP422Error.new('The conditional smart actions are not supported with Smart Collection. Please contact an administrator.')
30+
end
31+
end
2332
end
2433
end

app/services/forest_liana/schema_utils.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ def self.many_associations(active_record_class)
3232

3333
def self.find_model_from_collection_name(collection_name, logs = false)
3434
model_found = nil
35-
3635
ForestLiana.models.each do |model|
3736
if model.abstract_class?
3837
model_found = self.find_model_from_abstract_class(model, collection_name)

spec/services/forest_liana/ability/ability_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ module Ability
4242
user['permission_level'] = 'admin'
4343
expect(dummy_class.forest_authorize!('chart', user, Island.first, {parameters: []})).to equal nil
4444
end
45+
46+
it 'should raise error 422 when the collection is nil on action ability' do
47+
expect { dummy_class.forest_authorize!('action', :user, nil) }.to raise_error(ForestLiana::Errors::HTTP422Error, "The conditional smart actions are not supported with Smart Collection. Please contact an administrator.")
48+
end
49+
50+
it 'should raise error 422 when the collection is not a ActiveRecord children on action ability' do
51+
expect { dummy_class.forest_authorize!('action', :user, class Example; end ) }.to raise_error(ForestLiana::Errors::HTTP422Error, "The conditional smart actions are not supported with Smart Collection. Please contact an administrator.")
52+
end
4553
end
4654
end
4755
end

0 commit comments

Comments
 (0)