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

reevaluate module availability on each call

Christophe Bliard76f9aa760d84
diff --git a/lib/open_project/access_control.rb b/lib/open_project/access_control.rb
index eb0515b575f..bbc8f1db5e0 100644
--- a/lib/open_project/access_control.rb
+++ b/lib/open_project/access_control.rb
@@ -99,20 +99,24 @@ module OpenProject
end
def available_project_modules
- @available_project_modules ||=
+ project_modules
+ .reject { |name| disabled_project_modules.include? name }
+ end
+
+ def disabled_project_modules
+ modules
+ .select { |entry| entry[:if].respond_to?(:call) && !entry[:if].call }
+ .map { |entry| entry[:name].to_sym }
+ end
+
+ def project_modules
+ @project_modules ||=
@permissions
.reject(&:global?)
.map(&:project_module)
.including(@project_modules_without_permissions)
.uniq
.compact
- .reject { |name| disabled_project_modules.include? name }
- end
-
- def disabled_project_modules
- @disabled_project_modules ||= modules
- .select { |entry| entry[:if].respond_to?(:call) && !entry[:if].call }
- .map { |entry| entry[:name].to_sym }
end
def modules_permissions(modules)
@@ -147,12 +151,11 @@ module OpenProject
end
def clear_caches
- @available_project_modules = nil
- @disabled_project_modules = nil
- @public_permissions = nil
- @members_only_permissions = nil
- @loggedin_only_permissions = nil
@contract_actions_map = nil
+ @loggedin_only_permissions = nil
+ @members_only_permissions = nil
+ @project_modules = nil
+ @public_permissions = nil
end
end
end
diff --git a/modules/bim/spec/lib/open_project/access_control_spec.rb b/modules/bim/spec/lib/open_project/access_control_spec.rb
index 1a30daed46c..6ff073f82a5 100644
--- a/modules/bim/spec/lib/open_project/access_control_spec.rb
+++ b/modules/bim/spec/lib/open_project/access_control_spec.rb
@@ -29,14 +29,6 @@
require 'spec_helper'
describe OpenProject::AccessControl do
- before do
- described_class.clear_caches
- end
-
- after do
- described_class.clear_caches
- end
-
describe '.sorted_module_names' do
context 'with bim disabled' do
before do
diff --git a/spec/lib/open_project/access_control_spec.rb b/spec/lib/open_project/access_control_spec.rb
index 0c50ed0b187..ac16e53e011 100644
--- a/spec/lib/open_project/access_control_spec.rb
+++ b/spec/lib/open_project/access_control_spec.rb
@@ -206,17 +206,17 @@ describe OpenProject::AccessControl do
it { expect(described_class.available_project_modules).not_to include(:global_module) }
it { expect(described_class.available_project_modules).to include(:mixed_module) }
- context 'when a module :if is returning false' do
+ context 'when a module specifies :if' do
before do
described_class.map do |map|
- map.project_module :dynamic_module, if: ->(*) { module_available } do |mod|
+ map.project_module :dynamic_module, if: if_proc do |mod|
mod.permission :perm_d1, { dont: :care }
end
end
end
context 'with if: true' do
- let(:module_available) { true }
+ let(:if_proc) { ->(*) { true } }
it 'is considered available' do
described_class.available_project_modules
@@ -225,12 +225,25 @@ describe OpenProject::AccessControl do
end
context 'with if: false' do
- let(:module_available) { false }
+ let(:if_proc) { ->(*) { false } }
it 'is not considered available anymore' do
expect(described_class.available_project_modules).not_to include(:dynamic_module)
end
end
+
+ context 'with if: dynamically changing' do
+ let(:if_proc) { ->(*) { if_state[:available] } }
+ let(:if_state) { { available: true } }
+
+ it 'reevaluates module availability each time' do
+ if_state[:available] = true
+ expect(described_class.available_project_modules).to include(:dynamic_module)
+
+ if_state[:available] = false
+ expect(described_class.available_project_modules).not_to include(:dynamic_module)
+ end
+ end
end
end
diff --git a/spec/support/shared/with_config.rb b/spec/support/shared/with_config.rb
index a8b16c80d88..0fe6a87cd1f 100644
--- a/spec/support/shared/with_config.rb
+++ b/spec/support/shared/with_config.rb
@@ -48,14 +48,14 @@ class WithConfig
#
# @config [Hash] Hash containing the configurations with keys as seen in `configuration.rb`.
def before(example, config)
- allow(OpenProject::Configuration).to receive(:[]).and_call_original
-
aggregate_mocked_configuration(example, config)
.with_indifferent_access
.each { |k, v| stub_key(k, v) }
end
def stub_key(key, value)
+ allow_original_implementation_for_unstubbed_keys
+
allow(OpenProject::Configuration)
.to receive(:[])
.with(key.to_s)
@@ -78,10 +78,19 @@ class WithConfig
config
end
+
+ private
+
+ def allow_original_implementation_for_unstubbed_keys
+ return if @call_original_implementation_done
+
+ allow(OpenProject::Configuration).to receive(:[]).and_call_original
+ @call_original_implementation_done = true
+ end
end
RSpec.configure do |config|
- config.before(:each) do |example|
+ config.before do |example|
with_config = example.metadata[:with_config]
WithConfig.new(self).before example, with_config if with_config.present?