Skip to content

Commit f61dd2b

Browse files
authored
fix: explicit authorization (#3453)
* fix: explicit authorization * .
1 parent 49265e8 commit f61dd2b

File tree

4 files changed

+15
-7
lines changed

4 files changed

+15
-7
lines changed

Diff for: app/controllers/avo/associations_controller.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,13 @@ def reflection_class
155155
end
156156

157157
def authorize_if_defined(method, record = @record)
158+
return unless Avo.configuration.authorization_enabled?
159+
158160
@authorization.set_record(record)
159161

160162
if @authorization.has_method?(method.to_sym)
161163
@authorization.authorize_action method.to_sym
162-
elsif !@authorization.is_a?(Avo::Services::AuthorizationService) && Avo.configuration.explicit_authorization
164+
elsif Avo.configuration.explicit_authorization
163165
raise Avo::NotAuthorizedError.new
164166
end
165167
end

Diff for: lib/avo/concerns/checks_assoc_authorization.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ module ChecksAssocAuthorization
55

66
# Ex: A Post has many Comments
77
def authorize_association_for(policy_method)
8+
return true unless Avo.configuration.authorization_enabled?
9+
810
# Use the related_name as the base of the association
911
association_name = @reflection&.name
1012
return true if association_name.blank?
@@ -34,10 +36,8 @@ def authorize_association_for(policy_method)
3436

3537
if service.has_method?(method_name, raise_exception: false)
3638
service.authorize_action(method_name, record:, raise_exception: false)
37-
elsif !service.is_a?(Avo::Services::AuthorizationService)
38-
!Avo.configuration.explicit_authorization
3939
else
40-
true
40+
!Avo.configuration.explicit_authorization
4141
end
4242
end
4343
end

Diff for: lib/avo/configuration.rb

+6
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ def initialize
123123
@associations_lookup_list_limit = 1000
124124
end
125125

126+
# Authorization is enabled when:
127+
# (avo-pro gem is installed) AND (authorization_client is NOT nil)
128+
def authorization_enabled?
129+
@authorization_enabled ||= Avo.plugin_manager.installed?(:avo_pro) && !authorization_client.nil?
130+
end
131+
126132
def current_user_method(&block)
127133
@current_user = block if block.present?
128134
end

Diff for: lib/avo/fields/has_base_field.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,15 @@ def component_for_view(view = Avo::ViewInquirer.new("index"))
9494
end
9595

9696
def authorized?
97+
return true unless Avo.configuration.authorization_enabled?
98+
9799
method = :"view_#{id}?"
98100
service = field_resource.authorization
99101

100102
if service.has_method? method
101103
service.authorize_action(method, raise_exception: false)
102-
elsif !service.is_a?(Avo::Services::AuthorizationService)
103-
!Avo.configuration.explicit_authorization
104104
else
105-
true
105+
!Avo.configuration.explicit_authorization
106106
end
107107
end
108108

0 commit comments

Comments
 (0)