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 checks for the `allowed_in_any_entity?` for non project-scoped entities

Klaus Zanders3f27aa30a93f
diff --git a/app/services/authorization/user_permissible_service.rb b/app/services/authorization/user_permissible_service.rb
index e0dddb4d819..133a16a2652 100644
--- a/app/services/authorization/user_permissible_service.rb
+++ b/app/services/authorization/user_permissible_service.rb
@@ -47,22 +47,16 @@ module Authorization
end
end
- def allowed_in_any_entity?(permission, entity_class, in_project: nil) # rubocop:disable Metrics/PerceivedComplexity
+ def allowed_in_any_entity?(permission, entity_class, in_project: nil)
perms = contextual_permissions(permission, context_name(entity_class))
return false unless authorizable_user?
return false if in_project && !(in_project.active? || in_project.being_archived?)
return true if admin_and_all_granted_to_admin?(perms)
- # entity_class.allowed_to will also check whether the user has the permission via a membership in the project.
- # ^-- still a problem in some cases
- allowed_scope = entity_class.allowed_to(user, perms)
-
- # TODO: Also refactor to allow for checking non-project scoped entities
-
- if in_project
- allowed_in_single_project?(perms, in_project) || allowed_scope.exists?(project: in_project)
+ if entity_is_project_scoped?(entity_class)
+ allowed_in_any_project_scoped_entity?(perms, entity_class, in_project:)
else
- allowed_in_any_project?(perms) || allowed_scope.exists?
+ allowed_in_any_standalone_entity?(perms, entity_class)
end
end
@@ -128,6 +122,22 @@ module Authorization
cached_permissions(entity).intersect?(permissions)
end
+ def allowed_in_any_project_scoped_entity?(permissions, entity_class, in_project:)
+ # entity_class.allowed_to will also check whether the user has the permission via a membership in the project.
+ # ^-- still a problem in some cases
+ allowed_scope = entity_class.allowed_to(user, permissions)
+
+ if in_project
+ allowed_in_single_project?(permissions, in_project) || allowed_scope.exists?(project: in_project)
+ else
+ allowed_in_any_project?(permissions) || allowed_scope.exists?
+ end
+ end
+
+ def allowed_in_any_standalone_entity?(permissions, entity_class)
+ entity_class.allowed_to(user, permissions).exists?
+ end
+
def admin_and_all_granted_to_admin?(permissions)
user.admin? && permissions.all?(&:grant_to_admin?)
end