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

Refactor to use one context instead of multiple arguments

Klaus Zanders163e0965bde9
diff --git a/app/models/users/project_role_cache.rb b/app/models/users/project_role_cache.rb
index 20b812aa3f2..9b29cdae1c7 100644
--- a/app/models/users/project_role_cache.rb
+++ b/app/models/users/project_role_cache.rb
@@ -33,21 +33,21 @@ class Users::ProjectRoleCache
self.user = user
end
- def fetch(project: nil, entity: nil)
- cache[entity || project] ||= roles(project:, entity:)
+ def fetch(cacheable)
+ cache[cacheable] ||= roles(cacheable)
end
private
- def roles(project: nil, entity: nil)
+ def roles(context)
# 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)
+ return [] if context.is_a?(Project) && archived?(context)
- ::Authorization.roles(user, project:, entity:).eager_load(:role_permissions)
+ ::Authorization.roles(user, context).eager_load(:role_permissions)
end
def cache
diff --git a/app/services/authorization.rb b/app/services/authorization.rb
index a95787d775c..96a4217387f 100644
--- a/app/services/authorization.rb
+++ b/app/services/authorization.rb
@@ -40,11 +40,11 @@ module Authorization
end
# Returns all roles a user has in a certain project, for a specific entity or globally
- def roles(user, project: nil, entity: nil)
- if entity
- Authorization::UserEntityRolesQuery.query(user, entity)
- elsif project
+ def roles(user, context = nil)
+ if context.is_a?(Project)
Authorization::UserProjectRolesQuery.query(user, project)
+ elsif Member.can_be_member_of?(context)
+ Authorization::UserEntityRolesQuery.query(user, entity)
else
Authorization::UserGlobalRolesQuery.query(user)
end
diff --git a/docs/development/concepts/permissions/README.md b/docs/development/concepts/permissions/README.md
index 285b002f9eb..ba812faebf8 100644
--- a/docs/development/concepts/permissions/README.md
+++ b/docs/development/concepts/permissions/README.md
@@ -94,8 +94,8 @@ When a set of records is to be returned, e.g. for an index action, it is best to
|---------- | ------| ------ |
| All projects a user is allowed a permission in | `Authorization.projects(permission, user)` | `Authorization.projects(:view_work_packages, User.current)` |
| All users granted a permission in a project | `Authorization.users(permission, project: project)` | `Authorization.users(:view_work_packages, project: project)` |
-| All roles a user has in a project | `Authorization.roles(user, project:)` | `Authorization.roles(User.current, project: project)` |
-| All roles a user has for a specific resource | `Authorization.roles(user, entity:)` | `Authorization.roles(User.current, entity: work_package)` |
+| All roles a user has in a project | `Authorization.roles(user, project)` | `Authorization.roles(User.current, project: project)` |
+| All roles a user has for a specific resource | `Authorization.roles(user, entity:` | `Authorization.roles(User.current, entity: work_package)` |
| All roles a user has globally | `Authorization.roles(user)` | `Authorization.roles(User.current)` |
Most of the time, a developer will not witness those queries as they are the embedded deeply within the existing scopes. E.g. the `visible` scopes defined for most AR models, under the hood rely on `Authorization.projects(permission, user)` by checking that the `project_id` attribute of the record is within that set of projects.