Skip to content

Commit 89077f4

Browse files
committed
Improved row-level RBAC for records
1 parent 6d7f1b0 commit 89077f4

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

app/concerns/better_together/joinable.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ module Joinable
66
extend ActiveSupport::Concern
77

88
included do
9-
class_attribute :member_role_associations
9+
class_attribute :member_role_associations, :joinable_type
1010
self.member_role_associations = []
1111

1212
def self.joinable(joinable_type:, member_type:, **membership_options) # rubocop:todo Metrics/MethodLength
13+
self.joinable_type = joinable_type
1314
membership_class = "BetterTogether::#{member_type.camelize}#{joinable_type.camelize}Membership"
1415
membership_name = :"#{member_type}_#{joinable_type}_memberships"
1516

app/concerns/better_together/member.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ module Member
66
extend ActiveSupport::Concern
77

88
included do # rubocop:todo Metrics/BlockLength
9-
class_attribute :joinable_role_associations
9+
class_attribute :joinable_role_associations, :joinable_membership_classes
1010
self.joinable_role_associations = []
11+
self.joinable_membership_classes = []
1112

1213
def self.member(joinable_type:, member_type:, **membership_options) # rubocop:todo Metrics/MethodLength
1314
membership_class = "BetterTogether::#{member_type.camelize}#{joinable_type.camelize}Membership"
@@ -31,6 +32,7 @@ def self.member(joinable_type:, member_type:, **membership_options) # rubocop:to
3132

3233
# Register the association name for role retrieval
3334
joinable_role_associations << joinable_roles_association
35+
joinable_membership_classes << membership_class
3436
end
3537

3638
# Cache roles for the current instance
@@ -103,8 +105,7 @@ def record_permission_granted?(resource_permission, record)
103105
# Check if the member has a membership tied explicitly to the record
104106
memberships = membership_class.where(
105107
member: self,
106-
joinable_id: record.id,
107-
joinable_type: record.class.name
108+
joinable_id: record.id
108109
).includes(:role)
109110

110111
memberships.any? do |membership|
@@ -114,9 +115,9 @@ def record_permission_granted?(resource_permission, record)
114115

115116
# Determine the membership class for the record's joinable type
116117
def membership_class_for(record)
117-
joinable_type = record.class.name
118-
membership_class_name = self.class.joinable_role_associations.find do |assoc|
119-
assoc.to_s.include?(joinable_type.underscore)
118+
joinable_type = record.class.joinable_type
119+
membership_class_name = self.class.joinable_membership_classes.find do |assoc|
120+
assoc.to_s.include?(joinable_type.capitalize)
120121
end
121122

122123
membership_class_name&.to_s&.classify&.constantize # rubocop:todo Style/SafeNavigationChainLength

0 commit comments

Comments
 (0)