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 plan specific features for enterprise tokens

Klaus Zandersc5232435f928
diff --git a/app/services/authorization/enterprise_service.rb b/app/services/authorization/enterprise_service.rb
index e070d85775c..d0737bd93b9 100644
--- a/app/services/authorization/enterprise_service.rb
+++ b/app/services/authorization/enterprise_service.rb
@@ -31,7 +31,7 @@
class Authorization::EnterpriseService
attr_accessor :token
- GUARDED_ACTIONS = %i[
+ ENTERPRISE_PLAN_ACTIONS = %i[
baseline_comparison
board_view
conditional_highlighting
@@ -57,6 +57,15 @@ class Authorization::EnterpriseService
work_package_subject_generation
].freeze
+ CORPORATE_PLAN_ACTIONS = %i[
+ time_tracking_enforcement
+ ].freeze
+
+ ACTIONS_PER_PLAN = {
+ enterprise: ENTERPRISE_PLAN_ACTIONS,
+ corporate: CORPORATE_PLAN_ACTIONS + ENTERPRISE_PLAN_ACTIONS
+ }.freeze
+
def initialize(token)
self.token = token
end
@@ -75,9 +84,17 @@ class Authorization::EnterpriseService
private
+ def token_plan
+ # fallback to enterprise if no token is provided
+ (token.token_object.plan || OpenProject::Token::DEFAULT_PLAN).to_sym
+ end
+
+ def allowed_actions
+ @allowed_actions ||= ACTIONS_PER_PLAN[token_plan] + (token.token_object.features || [])
+ end
+
def process(action)
- # Every non-expired token
- GUARDED_ACTIONS.include?(action.to_sym)
+ allowed_actions.include?(action.to_sym)
end
def result(bool)
diff --git a/spec/services/authorization/enterprise_service_spec.rb b/spec/services/authorization/enterprise_service_spec.rb
index d2cfb8a3b6e..c8a26cf6ac5 100644
--- a/spec/services/authorization/enterprise_service_spec.rb
+++ b/spec/services/authorization/enterprise_service_spec.rb
@@ -34,11 +34,26 @@ RSpec.describe Authorization::EnterpriseService do
let(:token_object) { OpenProject::Token.new }
let(:expired?) { false }
- describe "GUARDED_ACTIONS" do
+ describe "ENTERPRISE_PLAN_ACTIONS" do
it "is in alphabetical order" do
- guarded_actions = described_class::GUARDED_ACTIONS
+ enterprise_actions = described_class::ENTERPRISE_PLAN_ACTIONS
+ expect(enterprise_actions).to eq(enterprise_actions.sort)
+ end
+ end
+
+ describe "CORPORATE_PLAN_ACTIONS" do
+ it "is in alphabetical order" do
+ corporate_actions = described_class::CORPORATE_PLAN_ACTIONS
+ expect(corporate_actions).to eq(corporate_actions.sort)
+ end
+ end
- expect(guarded_actions).to eq(guarded_actions.sort)
+ describe "ACTONS_PER_PLAN" do
+ it "includes all available plans" do
+ available_plans = OpenProject::Token::PLANS.map(&:to_sym)
+ plans = described_class::ACTIONS_PER_PLAN.keys
+
+ expect(plans).to match_array(available_plans)
end
end
@@ -68,7 +83,7 @@ RSpec.describe Authorization::EnterpriseService do
end
shared_examples "false result for any action" do
- guarded_action = described_class::GUARDED_ACTIONS.sample
+ guarded_action = described_class::ENTERPRISE_PLAN_ACTIONS.sample
context "for known action #{guarded_action}" do
let(:action) { guarded_action }
@@ -83,18 +98,72 @@ RSpec.describe Authorization::EnterpriseService do
end
end
- context "for a valid token" do
- described_class::GUARDED_ACTIONS.each do |guarded_action|
+ context "with an enterprise plan" do
+ context "for a valid token" do
+ described_class::ENTERPRISE_PLAN_ACTIONS.each do |guarded_action|
+ context "for known action #{guarded_action}" do
+ let(:action) { guarded_action }
+
+ include_examples "true result"
+ end
+ end
+
+ context "for unknown action" do
+ let(:action) { "foo" }
+
+ include_examples "false result"
+ end
+
+ described_class::ACTIONS_PER_PLAN.each do |plan, actions|
+ next if plan == :enterprise
+
+ context "for actions belonging to the #{plan} plan" do
+ actions.each do |action|
+ next if described_class::ENTERPRISE_PLAN_ACTIONS.include?(action)
+
+ context "for known action #{action}" do
+ let(:action) { action }
+
+ include_examples "false result"
+ end
+ end
+ end
+ end
+ end
+ end
+
+ context "with a corporate plan" do
+ let(:token_object) { OpenProject::Token.new(plan: "corporate") }
+
+ allowed_actions = described_class::ENTERPRISE_PLAN_ACTIONS + described_class::CORPORATE_PLAN_ACTIONS
+
+ allowed_actions.each do |guarded_action|
context "for known action #{guarded_action}" do
let(:action) { guarded_action }
include_examples "true result"
end
+
+ context "for unknown action" do
+ let(:action) { "foo" }
+
+ include_examples "false result"
+ end
end
+ end
- context "for unknown action" do
+ context "with a plan with an additional feature" do
+ let(:token_object) { OpenProject::Token.new(features: ["foo"]) }
+
+ context "for the additionally defined feature" do
let(:action) { "foo" }
+ include_examples "true result"
+ end
+
+ context "for unknown action" do
+ let(:action) { "foobar" }
+
include_examples "false result"
end
end