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

Start implementation

Klaus Zanders40650ca5b702
diff --git a/app/models/users/project_role_cache.rb b/app/models/users/project_role_cache.rb
index 8301a61c9f8..69e25e164dc 100644
--- a/app/models/users/project_role_cache.rb
+++ b/app/models/users/project_role_cache.rb
@@ -47,7 +47,7 @@ class Users::ProjectRoleCache
# Return all roles if user is admin
return all_givable_roles if user.admin?
- ::Authorization.roles(user, project).eager_load(:role_permissions)
+ ::Authorization.roles(user, project:).eager_load(:role_permissions)
end
def cache
diff --git a/app/services/authorization.rb b/app/services/authorization.rb
index f9fb6610da3..0bb6c224659 100644
--- a/app/services/authorization.rb
+++ b/app/services/authorization.rb
@@ -40,8 +40,10 @@ module Authorization
end
# Returns all roles a user has in a certain project or globally
- def roles(user, project = nil)
- if project
+ def roles(user, project: nil, entity: nil)
+ if entity
+ Authorization::UserEntityRolesQuery.query(user, entity)
+ elsif project
Authorization::UserProjectRolesQuery.query(user, project)
else
Authorization::UserGlobalRolesQuery.query(user)
diff --git a/app/services/authorization/user_allowed_service.rb b/app/services/authorization/user_allowed_service.rb
index ecddf6a6d64..756f2b8ecfa 100644
--- a/app/services/authorization/user_allowed_service.rb
+++ b/app/services/authorization/user_allowed_service.rb
@@ -41,6 +41,9 @@ class Authorization::UserAllowedService
# Context can be:
# * a project : returns true if user is allowed to do the specified action on this project
# * a group of projects : returns true if user is allowed on every project
+ # * an entity that a user can become a member of specifically (listed in Member::ALLOWED_ENTITIES) :
+ # * returns true if user is allowed to do the specified action on the given item or
+ # * returns ture if user is allowed to do the specified action on the project the entity belongs to
# * nil with +global+ set to +true+ : check if user has at least one role allowed for this action,
# or falls back to Non Member / Anonymous permissions depending if the user is logged
def call(action, context, global: false)
@@ -66,6 +69,8 @@ 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
@@ -73,6 +78,11 @@ class Authorization::UserAllowedService
end
end
+ def allowed_to_in_entity?(action, entity)
+ # TODO: Check in entity
+ entity.respond_to?(:project) && allowed_to_in_project?(action, entity.project)
+ end
+
def allowed_to_in_project?(action, project)
if project_authorization_cache.cached?(action)
return project_authorization_cache.allowed?(action, project)
@@ -146,6 +156,11 @@ 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
+
+ def supported_entity?(entity)
+ Member::ALLOWED_ENTITIES.include?(entity.class.to_s)
+ end
end