Skip to content

Commit 777b4b9

Browse files
committed
fixup! Handle id_in_scope attribute checks
1 parent 6d1fd84 commit 777b4b9

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

lib/declarative_authorization/authorization.rb

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,7 @@ def permit!(privilege, options = {})
232232
else
233233
puts "Rule has #{rule.attributes.count} attributes, examining them:"
234234

235-
all_attributes_matched = true
236-
any_attribute_matched = false
235+
matching_attributes_count = 0
237236

238237
rule.attributes.each_with_index do |attribute, index|
239238
puts "\n -- Attribute ##{index + 1}: #{attribute.inspect}"
@@ -248,8 +247,9 @@ def permit!(privilege, options = {})
248247

249248
next unless conditions.is_a?(Hash)
250249

251-
puts " Checking conditions against current values:"
252-
current_attribute_matched = true
250+
puts " Checking #{conditions.count} conditions against current values:"
251+
matching_conditions_count = 0
252+
any_condition_failed = false
253253

254254
if conditions.key?(:granular_permissions)
255255
rule_requires = conditions[:granular_permissions][1]
@@ -258,13 +258,14 @@ def permit!(privilege, options = {})
258258
puts " Granular permissions - Rule requires: #{rule_requires}, Actual: #{actual_value}"
259259
if rule_requires == actual_value
260260
puts " ✓ Granular permissions condition matched!"
261+
matching_conditions_count += 1
261262
else
262263
puts " ✗ Granular permissions condition did not match"
263-
current_attribute_matched = false
264+
any_condition_failed = true
264265
end
265266
end
266267

267-
if conditions.key?(:is_renewal) && current_attribute_matched
268+
if conditions.key?(:is_renewal) && !any_condition_failed
268269
rule_requires = conditions[:is_renewal][1]
269270
# If options[:object] has is_renewal, we'll use that value instead of obtaining it from SpiceDB.
270271
# This allows for Blue Moon leases and others to be checked without having to make a SpiceDB call for the value of is_renewal.
@@ -286,13 +287,14 @@ def permit!(privilege, options = {})
286287
puts " Is renewal - Rule requires: #{rule_requires}, Actual: #{actual_value}"
287288
if rule_requires == actual_value
288289
puts " ✓ Is_renewal condition matched!"
290+
matching_conditions_count += 1
289291
else
290292
puts " ✗ Is_renewal condition did not match"
291-
current_attribute_matched = false
293+
any_condition_failed = true
292294
end
293295
end
294296

295-
if conditions.key?(:id) && current_attribute_matched
297+
if conditions.key?(:id) && !any_condition_failed
296298
condition_type = conditions[:id][0]
297299
condition_proc = conditions[:id][1]
298300

@@ -302,27 +304,27 @@ def permit!(privilege, options = {})
302304
puts " User has access to occupancy? #{user_has_access_to_occupancy}"
303305
if user_has_access_to_occupancy
304306
puts " ✓ This attribute matched conditions"
307+
matching_conditions_count += 1
305308
else
306309
puts " ✗ This attribute did not match conditions"
307-
current_attribute_matched = false
310+
any_condition_failed = true
308311
end
309312
end
310313
end
311314

312-
if current_attribute_matched
313-
any_attribute_matched = true
315+
if matching_conditions_count == conditions.count
314316
puts " ✓ All conditions matched for this attribute"
317+
matching_attributes_count += 1
315318
else
316-
all_attributes_matched = false
317319
puts " ✗ Not all conditions matched for this attribute"
318320
end
319321
end
320322

321323
puts " Finished checking attributes, checking if rule is authorized"
322324

323325
if rule.respond_to?(:join_operator) && rule.join_operator == :and
324-
puts " Rule uses AND operator, checking if all attributes matched: #{all_attributes_matched}"
325-
if all_attributes_matched
326+
puts " Rule uses AND operator, checking if all attributes matched: #{matching_attributes_count == rule.attributes.count}"
327+
if matching_attributes_count == rule.attributes.count
326328
puts " All attributes matched, checking SpiceDB permission"
327329
authorized = @auth_service.check_permission(
328330
resource: { type: "vhost", id: vhost_id },
@@ -334,8 +336,8 @@ def permit!(privilege, options = {})
334336
puts " Not all attributes matched, authorization denied for this rule"
335337
end
336338
else
337-
puts " Rule uses OR operator (default), checking if any attribute matched: #{any_attribute_matched}"
338-
if any_attribute_matched
339+
puts " Rule uses OR operator (default), checking if any attribute matched: #{matching_attributes_count > 0}"
340+
if matching_attributes_count > 0
339341
puts " At least one attribute matched, checking SpiceDB permission"
340342
authorized = @auth_service.check_permission(
341343
resource: { type: "vhost", id: vhost_id },

0 commit comments

Comments
 (0)