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

Allow querying for the roles for entities

Klaus Zanders8bfb2172d1ff
diff --git a/app/models/users/project_role_cache.rb b/app/models/users/project_role_cache.rb
index 69e25e164dc..f869d5e53cf 100644
--- a/app/models/users/project_role_cache.rb
+++ b/app/models/users/project_role_cache.rb
@@ -33,25 +33,39 @@ class Users::ProjectRoleCache
self.user = user
end
- def fetch(project)
- cache[project] ||= roles(project)
+ def fetch(project: nil, entity: nil)
+ if project
+ project_cache[project] ||= roles(project:)
+ elsif entity
+ entity_cache[entity] ||= roles(entity:)
+ else
+ []
+ end
end
private
- def roles(project)
- # Project is nil if checking global role
- # No roles on archived projects, unless the active state is being changed
- return [] if project && archived?(project)
-
+ def roles(project: nil, entity: nil)
# Return all roles if user is admin
return all_givable_roles if user.admin?
- ::Authorization.roles(user, project:).eager_load(:role_permissions)
+ 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)
+
+ ::Authorization.roles(user, project:).eager_load(:role_permissions)
+ elsif entity
+ ::Authorization.roles(user, entity:).eager_load(:role_permissions)
+ end
+ end
+
+ def project_cache
+ @project_cache ||= {}
end
- def cache
- @cache ||= {}
+ def entity_cache
+ @entity_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 756f2b8ecfa..1a9e00c7002 100644
--- a/app/services/authorization/user_allowed_service.rb
+++ b/app/services/authorization/user_allowed_service.rb
@@ -79,8 +79,17 @@ class Authorization::UserAllowedService
end
def allowed_to_in_entity?(action, entity)
- # TODO: Check in entity
- entity.respond_to?(:project) && allowed_to_in_project?(action, entity.project)
+ # 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?
+ # 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)
+
+ has_authorized_role?(action, entity:)
end
def allowed_to_in_project?(action, project)
@@ -98,7 +107,7 @@ class Authorization::UserAllowedService
# unless the permission is explicitly flagged not to be granted to admins.
return true if granted_to_admin?(action)
- has_authorized_role?(action, project)
+ has_authorized_role?(action, project:)
end
# Authorize if user is authorized on every element of the array
@@ -132,9 +141,9 @@ class Authorization::UserAllowedService
user.admin? && OpenProject::AccessControl.grant_to_admin?(action)
end
- def has_authorized_role?(action, project = nil)
+ def has_authorized_role?(action, project: nil, entity: nil)
project_role_cache
- .fetch(project)
+ .fetch(project:, entity:)
.any? do |role|
role.allowed_to?(action)
end