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

Fix querying for global roles

Klaus Zanders0abc083b8d8f
diff --git a/app/models/users/project_role_cache.rb b/app/models/users/project_role_cache.rb
index 99169cf674d..20b812aa3f2 100644
--- a/app/models/users/project_role_cache.rb
+++ b/app/models/users/project_role_cache.rb
@@ -34,40 +34,24 @@ class Users::ProjectRoleCache
end
def fetch(project: nil, entity: nil)
- if project
- project_cache[project] ||= roles(project:)
- elsif entity
- entity_cache[entity] ||= roles(entity:)
- else
- []
- end
+ cache[entity || project] ||= roles(project:, entity:)
end
private
def roles(project: nil, entity: nil)
- if project
- # Project is nil if checking global role
- # No roles on archived projects, unless the active state is being changed
- return [] if archived?(project)
- # Return all roles if user is admin
- return all_givable_roles if user.admin?
+ # Return all roles if user is admin
+ return all_givable_roles if user.admin?
- ::Authorization.roles(user, project:).eager_load(:role_permissions)
- elsif entity
- # Return all roles if user is admin
- return all_givable_roles if user.admin?
+ # Project is nil if checking global role
+ # No roles on archived projects, unless the active state is being changed
+ return [] if project && archived?(project)
- ::Authorization.roles(user, entity:).eager_load(:role_permissions)
- end
+ ::Authorization.roles(user, project:, entity:).eager_load(:role_permissions)
end
- def project_cache
- @project_cache ||= {}
- end
-
- def entity_cache
- @entity_cache ||= {}
+ def cache
+ @cache ||= {}
end
def all_givable_roles
diff --git a/app/services/authorization/user_allowed_service.rb b/app/services/authorization/user_allowed_service.rb
index 1a9e00c7002..95c87093432 100644
--- a/app/services/authorization/user_allowed_service.rb
+++ b/app/services/authorization/user_allowed_service.rb
@@ -69,8 +69,6 @@ class Authorization::UserAllowedService
allowed_to_globally?(action)
elsif context.is_a? Project
allowed_to_in_project?(action, context)
- elsif supported_entity?(context)
- allowed_to_in_entity?(action, context)
elsif context.respond_to?(:to_a)
allowed_to_in_all_projects?(action, context)
else
@@ -79,12 +77,13 @@ class Authorization::UserAllowedService
end
def allowed_to_in_entity?(action, entity)
- # Short circuit: When the user is already allowed to execute the action on the project,
- # there's no need to do a check on the entity
- return true if entity.respond_to?(:project) && allowed_to_in_project?(action, entity.project)
-
# Inactive users are never authorized
return false unless authorizable_user?
+
+ # Short circuit: When the user is already allowed to execute the action baed
+ # on the project, there's no need to do a check on the entity
+ return true if entity.respond_to?(:project) && allowed_to_in_project?(action, entity.project)
+
# Admin users are authorized for anything else
# unless the permission is explicitly flagged not to be granted to admins.
return true if granted_to_admin?(action)
@@ -165,7 +164,6 @@ class Authorization::UserAllowedService
def supported_context?(context, global:)
(context.nil? && global) ||
context.is_a?(Project) ||
- supported_entity?(context) ||
(!context.nil? && context.respond_to?(:to_a))
end
diff --git a/spec/models/users/allowed_to_spec.rb b/spec/models/users/allowed_to_spec.rb
index 868cf454a98..2c3069d187a 100644
--- a/spec/models/users/allowed_to_spec.rb
+++ b/spec/models/users/allowed_to_spec.rb
@@ -36,23 +36,11 @@ RSpec.describe User, 'allowed_to?' do
let(:role) { build(:role) }
let(:role2) { build(:role) }
let(:anonymous_role) { build(:anonymous_role) }
- let(:member) do
- build(:member, project:,
- roles: [role],
- principal: user)
- end
- let(:member2) do
- build(:member, project: project2,
- roles: [role2],
- principal: user)
- end
- let(:global_permission) { OpenProject::AccessControl.permissions.find { |p| p.global? } }
+ let(:member) { build(:member, project:, roles: [role], principal: user) }
+ let(:member2) { build(:member, project: project2, roles: [role2], principal: user) }
+ let(:global_permission) { OpenProject::AccessControl.permissions.find(&:global?) }
let(:global_role) { build(:global_role, permissions: [global_permission.name]) }
- let(:global_member) do
- build(:global_member,
- principal: user,
- roles: [global_role])
- end
+ let(:global_member) { build(:global_member, principal: user, roles: [global_role]) }
before do
anonymous_role.save!
@@ -60,7 +48,7 @@ RSpec.describe User, 'allowed_to?' do
user.save!
end
- shared_examples_for 'w/ inquiring for project' do
+ shared_examples_for 'when inquiring for project' do
let(:permission) { :add_work_packages }
let(:final_setup_step) {}
@@ -480,7 +468,7 @@ RSpec.describe User, 'allowed_to?' do
end
end
- shared_examples_for 'w/ inquiring globally' do
+ shared_examples_for 'when inquiring globally' do
let(:permission) { :add_work_packages }
let(:final_setup_step) {}
@@ -662,13 +650,13 @@ RSpec.describe User, 'allowed_to?' do
end
end
- context 'w/o preloaded permissions' do
- it_behaves_like 'w/ inquiring for project'
- it_behaves_like 'w/ inquiring globally'
+ context 'without preloaded permissions' do
+ it_behaves_like 'when inquiring for project'
+ it_behaves_like 'when inquiring globally'
end
- context 'w/ preloaded permissions' do
- it_behaves_like 'w/ inquiring for project' do
+ context 'with preloaded permissions' do
+ it_behaves_like 'when inquiring for project' do
let(:final_setup_step) do
user.preload_projects_allowed_to(permission)
end