Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Add a method to check if Membership is possible

Klaus Zanderse0d99127f918
diff --git a/app/models/member.rb b/app/models/member.rb
index 6e557b64918..8f41d70f580 100644
--- a/app/models/member.rb
+++ b/app/models/member.rb
@@ -96,6 +96,16 @@ class Member < ApplicationRecord
user? && principal&.invited? && principal.memberships.none? { |m| m.project_id != project_id }
end
+ def self.can_be_member_of?(entity_or_class)
+ checked_class = if entity_or_class.is_a?(Class)
+ entity_or_class.name
+ else
+ entity_or_class.class.name
+ end
+
+ ALLOWED_ENTITIES.include?(checked_class)
+ end
+
protected
attr_accessor :prune_watchers_on_destruction
diff --git a/spec/models/member_spec.rb b/spec/models/member_spec.rb
index ea4104919a8..1a65765b6ab 100644
--- a/spec/models/member_spec.rb
+++ b/spec/models/member_spec.rb
@@ -92,4 +92,26 @@ RSpec.describe Member do
expect(project_membership).not_to be_deletable_role(project_role)
end
end
+
+ describe '.can_be_member_of?' do
+ it 'returns true when a whitelisted entity is passed in' do
+ result = described_class.can_be_member_of?(build(:work_package))
+ expect(result).to be_truthy
+ end
+
+ it 'returns true when the class name of a whitelisted entity is passed in' do
+ result = described_class.can_be_member_of?(WorkPackage)
+ expect(result).to be_truthy
+ end
+
+ it 'returns false when a non-whitelisted entity is passed in' do
+ result = described_class.can_be_member_of?(build(:user))
+ expect(result).to be_falsey
+ end
+
+ it 'returns false when the class name of a whitelisted entity is passed in' do
+ result = described_class.can_be_member_of?(User)
+ expect(result).to be_falsey
+ end
+ end
end