Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement detection for sti for including subclasses in check #689

Merged
merged 7 commits into from Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/cancan/class_matcher.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require_relative 'sti_detector'

# This class is responsible for matching classes and their subclasses as well as
# upmatching classes to their ancestors.
# This is used to generate sti connections
Expand All @@ -12,6 +14,8 @@ def self.matches_subject_class?(subjects, subject)
def self.matching_class_check(subject, sub, has_subclasses)
matches = matches_class_or_is_related(subject, sub)
if has_subclasses
return matches unless StiDetector.sti_class?(sub)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only include the subclasses in the check if the model is an sti class


matches || subject.subclasses.include?(sub)
else
matches
Expand Down
8 changes: 4 additions & 4 deletions lib/cancan/model_adapters/sti_normalizer.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require_relative '../sti_detector'

# this class is responsible for detecting sti classes and creating new rules for the
# relevant subclasses, using the inheritance_column as a merger
module CanCan
Expand All @@ -20,9 +22,7 @@ def normalize(rules)
private

def update_rule(subject, rule, rules_cache)
return false unless subject.respond_to?(:descends_from_active_record?)
return false if subject == :all || subject.descends_from_active_record?
return false unless subject < ActiveRecord::Base
return false unless StiDetector.sti_class?(subject)

rules_cache.push(build_rule_for_subclass(rule, subject))
true
Expand All @@ -31,7 +31,7 @@ def update_rule(subject, rule, rules_cache)
# create a new rule for the subclasses that links on the inheritance_column
def build_rule_for_subclass(rule, subject)
CanCan::Rule.new(rule.base_behavior, rule.actions, subject.superclass,
rule.conditions.merge(subject.inheritance_column => subject.name), rule.block)
rule.conditions.merge(subject.inheritance_column => subject.sti_name), rule.block)
end
end
end
Expand Down
12 changes: 12 additions & 0 deletions lib/cancan/sti_detector.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

class StiDetector
def self.sti_class?(subject)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A general detection for STI classes

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love its isolation

return false unless defined?(ActiveRecord::Base)
return false unless subject.respond_to?(:descends_from_active_record?)
return false if subject == :all || subject.descends_from_active_record?
return false unless subject < ActiveRecord::Base

true
end
end
29 changes: 29 additions & 0 deletions spec/cancan/model_adapters/active_record_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,35 @@ class JsonTransaction < ActiveRecord::Base
end
end

context 'with rule application to subclass for non sti class' do
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check that rules are no longer applied to parents for non sti

before do
ActiveRecord::Schema.define do
create_table :parents, force: true

create_table :children, force: true
end

class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end

class Parent < ActiveRecord::Base
end

class Child < Parent
end
end

it 'rules are not effecting parent class' do
This conversation was marked as resolved.
Show resolved Hide resolved
u1 = User.create!(name: 'pippo')
ability = Ability.new(u1)
ability.can :manage, Parent
ability.cannot :manage, Child
expect(ability).not_to be_able_to(:index, Child)
expect(ability).to be_able_to(:index, Parent)
end
end

context 'when STI is in use' do
before do
ActiveRecord::Schema.define do
Expand Down