From aacae1129b77a793a682b3c3051520dcf509cebf Mon Sep 17 00:00:00 2001 From: krzyczak Date: Tue, 25 Jan 2022 17:51:07 +0000 Subject: [PATCH 1/2] fix: Updated property matcher to be able to take multiple values --- .../model_adapters/active_graph_adapter.rb | 6 +++++- .../model_adapters/neo4j_active_graph_spec.rb | 19 ++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/cancancan/model_adapters/active_graph_adapter.rb b/lib/cancancan/model_adapters/active_graph_adapter.rb index b599ba4..0bd0d23 100644 --- a/lib/cancancan/model_adapters/active_graph_adapter.rb +++ b/lib/cancancan/model_adapters/active_graph_adapter.rb @@ -44,7 +44,11 @@ def self.property_matches?(subject, property, value) if subject.is_a?(ActiveGraph::Node::HasN::AssociationProxy) subject.where(property => value).exists? else - subject.send(property) == value + if value.is_a?(Enumerable) + subject.class.where(property => value, uuid: subject.uuid).exists? + else + subject.send(property) == value + end end end diff --git a/spec/cancancan/model_adapters/neo4j_active_graph_spec.rb b/spec/cancancan/model_adapters/neo4j_active_graph_spec.rb index 1bd8c7c..f3f0936 100644 --- a/spec/cancancan/model_adapters/neo4j_active_graph_spec.rb +++ b/spec/cancancan/model_adapters/neo4j_active_graph_spec.rb @@ -12,7 +12,7 @@ ActiveGraph::Base.label_object(Mention.mapped_label_names.first).create_constraint(Mention.id_property_name, type: :unique) ActiveGraph::Base.label_object(Namespace::TableX.mapped_label_names.first).create_constraint(Namespace::TableX.id_property_name, type: :unique) ActiveGraph::Base.label_object(Namespace::TableZ.mapped_label_names.first).create_constraint(Namespace::TableZ.id_property_name, type: :unique) - + Article.delete_all Category.delete_all Comment.delete_all @@ -379,6 +379,23 @@ expect { Comment.accessible_by(@ability) }.to_not raise_error end + it 'should support deeply nested conditions with an Enumerable value' do + @ability.can :read, Comment, article: { category: { name: ["Cars", "Sports"] } } + + category1 = Category.create!(name: "Cars", visible: true) + category2 = Category.create!(name: "Sports", visible: true) + category3 = Category.create!(name: "Books", visible: true) + + comment1 = Comment.create!(article: Article.create!(category: category1)) + comment2 = Comment.create!(article: Article.create!(category: category2)) + comment3 = Comment.create!(article: Article.create!(category: category3)) + + expect(Comment.accessible_by(@ability)).to match_array([comment1, comment2]) + expect(@ability.can?(:read, comment1)).to be(true) + expect(@ability.can?(:read, comment2)).to be(true) + expect(@ability.can?(:read, comment3)).to be(false) + end + it 'returns empty set if no abilities match' do expect(@ability.model_adapter(Article, :read).database_records).to be_empty end From 8601a6303f4d3b5ed71a10851a6271b5c8dc2d55 Mon Sep 17 00:00:00 2001 From: Braden King Date: Thu, 16 Jun 2022 11:17:17 +0100 Subject: [PATCH 2/2] fix: Match based on concrete neo_id not ActiveGraph uuid --- lib/cancancan/model_adapters/active_graph_adapter.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cancancan/model_adapters/active_graph_adapter.rb b/lib/cancancan/model_adapters/active_graph_adapter.rb index 0bd0d23..f1b24c1 100644 --- a/lib/cancancan/model_adapters/active_graph_adapter.rb +++ b/lib/cancancan/model_adapters/active_graph_adapter.rb @@ -45,7 +45,7 @@ def self.property_matches?(subject, property, value) subject.where(property => value).exists? else if value.is_a?(Enumerable) - subject.class.where(property => value, uuid: subject.uuid).exists? + subject.class.where(property => value, neo_id: subject.neo_id).exists? else subject.send(property) == value end