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

backport Project.allowed_to performance improvements to 13.0

ulferts41f547cd5c89
diff --git a/app/controllers/placeholder_users_controller.rb b/app/controllers/placeholder_users_controller.rb
index 806bf3e95d0..d2936152867 100644
--- a/app/controllers/placeholder_users_controller.rb
+++ b/app/controllers/placeholder_users_controller.rb
@@ -50,9 +50,14 @@ class PlaceholderUsersController < ApplicationController
end
def show
- # show projects based on current user visibility
- @memberships = @placeholder_user.memberships
- .visible(current_user)
+ # show projects based on current user visibility.
+ # But don't simply concatenate the .visible scope to the memberships
+ # as .memberships has an include and an order which for whatever reason
+ # also gets applied to the Project.allowed_to parts concatenated by a UNION
+ # and an order inside a UNION is not allowed in postgres.
+ @memberships = @placeholder_user
+ .memberships
+ .where(id: Member.visible(current_user))
respond_to do |format|
format.html { render layout: 'no_menu' }
diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb
index 64138919770..b3a5c839d72 100644
--- a/app/controllers/users_controller.rb
+++ b/app/controllers/users_controller.rb
@@ -65,10 +65,14 @@ class UsersController < ApplicationController
end
def show
- # show projects based on current user visibility
+ # show projects based on current user visibility.
+ # But don't simply concatenate the .visible scope to the memberships
+ # as .memberships has an include and an order which for whatever reason
+ # also gets applied to the Project.allowed_to parts concatenated by a UNION
+ # and an order inside a UNION is not allowed in postgres.
@memberships = @user.memberships
.where.not(project_id: nil)
- .visible(current_user)
+ .where(id: Member.visible(current_user))
if can_show_user?
@events = events
diff --git a/app/models/members/scopes/visible.rb b/app/models/members/scopes/visible.rb
index 943b69e7d92..27c1aa089c8 100644
--- a/app/models/members/scopes/visible.rb
+++ b/app/models/members/scopes/visible.rb
@@ -43,16 +43,16 @@ module Members::Scopes
private
def visible_for_non_admins(user)
- view_members = Project.where(id: Project.allowed_to(user, :view_members))
- manage_members = Project.where(id: Project.allowed_to(user, :manage_members))
+ view_members = Project.allowed_to(user, :view_members)
+ manage_members = Project.allowed_to(user, :manage_members)
project_scope = view_members.or(manage_members)
- Member.where(project_id: project_scope.select(:id))
+ where(project_id: project_scope.select(:id))
end
def visible_for_admins
- Member.all
+ all
end
end
end
diff --git a/app/models/project.rb b/app/models/project.rb
index bb82a97d931..eeac6404f01 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -150,6 +150,9 @@ class Project < ApplicationRecord
friendly_id :identifier, use: :finders
+ include ::Scopes::Scoped
+ scopes :allowed_to
+
scope :has_module, ->(mod) {
where(["#{Project.table_name}.id IN (SELECT em.project_id FROM #{EnabledModule.table_name} em WHERE em.name=?)", mod.to_s])
}
@@ -208,12 +211,6 @@ class Project < ApplicationRecord
allowed_to(user, :view_project)
end
- # Returns a ActiveRecord::Relation to find all projects for which
- # +user+ has the given +permission+
- def self.allowed_to(user, permission)
- Authorization.projects(permission, user)
- end
-
# Returns a :conditions SQL string that can be used to find the issues associated with this project.
#
# Examples:
diff --git a/app/models/projects/scopes/allowed_to.rb b/app/models/projects/scopes/allowed_to.rb
new file mode 100644
index 00000000000..fca4a7cb610
--- /dev/null
+++ b/app/models/projects/scopes/allowed_to.rb
@@ -0,0 +1,170 @@
+# -- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2010-2023 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+# ++
+
+module Projects::Scopes
+ module AllowedTo
+ extend ActiveSupport::Concern
+
+ class_methods do
+ # Returns an ActiveRecord::Relation to find all projects for which
+ # +user+ has the given +permission+
+ def allowed_to(user, permission)
+ permissions = allowed_to_permissions(permission)
+
+ if user.admin? && permissions.all?(&:grant_to_admin?)
+ where(id: allowed_to_admin_relation(permissions))
+ elsif user.anonymous?
+ where(id: allowed_to_non_member_relation(user, permissions))
+ else
+ where(arel_table[:id].in(allowed_to_member_relation(user, permissions)
+ .arel
+ .union(:all, allowed_to_non_member_relation(user, permissions).arel)))
+ end
+ end
+
+ private
+
+ def allowed_to_admin_relation(permissions)
+ joins(allowed_to_enabled_module_join(permissions))
+ .where(arel_table[:active].eq(true))
+ end
+
+ def allowed_to_non_member_relation(user, permissions)
+ joins(allowed_to_enabled_module_join(permissions))
+ .joins(allowed_to_builtin_roles_in_active_project_join(user))
+ .joins(allowed_to_role_permission_join(permissions))
+ .select(:id)
+ end
+
+ def allowed_to_member_relation(user, permissions)
+ Member
+ .joins(allowed_to_member_in_active_project_join(user))
+ .joins(allowed_to_enabled_module_join(permissions))
+ .joins(:roles, :member_roles)
+ .joins(allowed_to_role_permission_join(permissions))
+ .select('projects.id')
+ end
+
+ def allowed_to_enabled_module_join(permissions)
+ project_module = permissions.filter_map(&:project_module).uniq
+ enabled_module_table = EnabledModule.arel_table
+
+ if project_module.any?
+ arel_table.join(enabled_module_table, Arel::Nodes::InnerJoin)
+ .on(arel_table[:id].eq(enabled_module_table[:project_id])
+ .and(enabled_module_table[:name].in(project_module))
+ .and(arel_table[:active].eq(true)))
+ .join_sources
+ end
+ end
+
+ def allowed_to_role_permission_join(permissions)
+ return if permissions.all?(&:public?)
+
+ role_permissions_table = RolePermission.arel_table
+ enabled_modules_table = EnabledModule.arel_table
+ roles_table = Role.arel_table
+
+ condition = permissions.inject(Arel::Nodes::False.new) do |or_condition, permission|
+ permission_condition = role_permissions_table[:permission].eq(permission.name)
+
+ if permission.project_module.present?
+ permission_condition = permission_condition.and(enabled_modules_table[:name].eq(permission.project_module))
+ end
+
+ or_condition.or(permission_condition)
+ end
+
+ arel_table
+ .join(role_permissions_table, Arel::Nodes::InnerJoin)
+ .on(roles_table[:id].eq(role_permissions_table[:role_id])
+ .and(condition))
+ .join_sources
+ end
+
+ def allowed_to_permissions(permission)
+ if permission.is_a?(Hash)
+ OpenProject::AccessControl.allow_actions(permission)
+ else
+ [OpenProject::AccessControl.permission(permission)].compact
+ end
+ end
+
+ def allowed_to_members_condition(user)
+ members_table = Member.arel_table
+
+ members_table[:project_id].eq(arel_table[:id])
+ .and(members_table[:user_id].eq(user.id))
+ end
+
+ def allowed_to_builtin_roles_in_active_project_join(user)
+ condition = allowed_to_built_roles_in_active_project_condition(user)
+
+ if user.logged?
+ condition = condition.and(allowed_to_no_member_exists_condition(user))
+ end
+
+ roles_table = Role.arel_table
+
+ arel_table.join(roles_table)
+ .on(condition)
+ .join_sources
+ end
+
+ def allowed_to_member_in_active_project_join(user)
+ arel_table.join(arel_table)
+ .on(arel_table[:active].eq(true)
+ .and(allowed_to_members_condition(user)))
+ .join_sources
+ end
+
+ def allowed_to_built_roles_in_active_project_condition(user)
+ builtin = if user.logged?
+ Role::BUILTIN_NON_MEMBER
+ else
+ Role::BUILTIN_ANONYMOUS
+ end
+
+ roles_table = Role.arel_table
+
+ roles_table[:builtin].eq(builtin)
+ .and(arel_table[:active])
+ .and(arel_table[:public])
+ end
+
+ def allowed_to_no_member_exists_condition(user)
+ Member
+ .select(1)
+ .where(allowed_to_members_condition(user))
+ .arel
+ .exists
+ .not
+ end
+ end
+ end
+end
diff --git a/app/services/authorization.rb b/app/services/authorization.rb
index f9fb6610da3..7b75dea0c0a 100644
--- a/app/services/authorization.rb
+++ b/app/services/authorization.rb
@@ -36,7 +36,7 @@ module Authorization
# Returns all projects a user has a certain permission in
def projects(action, user)
- Authorization::ProjectQuery.query(user, action)
+ Project.allowed_to(action, user)
end
# Returns all roles a user has in a certain project or globally
diff --git a/app/views/individual_principals/_memberships.html.erb b/app/views/individual_principals/_memberships.html.erb
index 9f58675be7b..04278116548 100644
--- a/app/views/individual_principals/_memberships.html.erb
+++ b/app/views/individual_principals/_memberships.html.erb
@@ -31,7 +31,7 @@ See COPYRIGHT and LICENSE files for more details.
.new(@individual_principal.memberships.build, current_user)
.assignable_projects
.order(Arel.sql('lft')) %>
-<% memberships = @individual_principal.memberships.visible(current_user) %>
+<% memberships = @individual_principal.memberships.where(id: Member.visible(current_user)) %>
<div class="grid-block">
<div class="grid-content">
diff --git a/spec/models/projects/allowed_to_scope_spec.rb b/spec/models/projects/allowed_to_scope_spec.rb
index 84a1783cc05..7dfbbb51064 100644
--- a/spec/models/projects/allowed_to_scope_spec.rb
+++ b/spec/models/projects/allowed_to_scope_spec.rb
@@ -63,6 +63,7 @@ RSpec.describe Project, 'allowed to' do
let(:action) { :view_work_packages }
let(:public_action) { :view_news }
let(:public_non_module_action) { :view_project }
+ let(:non_module_action) { :edit_project }
let(:member) do
build(:member,
user:,
@@ -72,13 +73,13 @@ RSpec.describe Project, 'allowed to' do
shared_examples_for 'includes the project' do
it 'includes the project' do
- expect(Project.allowed_to(user, action)).to match_array [project]
+ expect(described_class.allowed_to(user, action)).to contain_exactly(project)
end
end
shared_examples_for 'is empty' do
it 'is empty' do
- expect(Project.allowed_to(user, action)).to be_empty
+ expect(described_class.allowed_to(user, action)).to be_empty
end
end
@@ -88,8 +89,8 @@ RSpec.describe Project, 'allowed to' do
user.save!
end
- context 'w/ the user being member
- w/ the role having the permission' do
+ context 'with the user being member
+ with the role having the permission' do
before do
non_member_role.save!
member.save!
@@ -98,9 +99,9 @@ RSpec.describe Project, 'allowed to' do
it_behaves_like 'includes the project'
end
- context 'w/ the user being member
- w/ the role having the permission
- w/o the project being active' do
+ context 'with the user being member
+ with the role having the permission
+ without the project being active' do
let(:project_status) { false }
before do
@@ -110,8 +111,8 @@ RSpec.describe Project, 'allowed to' do
it_behaves_like 'is empty'
end
- context 'w/ the user being member
- w/o the role having the permission' do
+ context 'with the user being member
+ without the role having the permission' do
let(:permissions) { [] }
before do
@@ -121,14 +122,14 @@ RSpec.describe Project, 'allowed to' do
it_behaves_like 'is empty'
end
- context 'w/o the user being member
- w/ the role having the permission' do
+ context 'without the user being member
+ with the role having the permission' do
it_behaves_like 'is empty'
end
- context 'w/ the user being member
- w/ the role having the permission
- w/o the associated module being active' do
+ context 'with the user being member
+ with the role having the permission
+ without the associated module being active' do
before do
member.save!
project.enabled_modules = []
@@ -137,45 +138,75 @@ RSpec.describe Project, 'allowed to' do
it_behaves_like 'is empty'
end
- context 'w/ the user being member
- w/ the permission being public' do
+ context 'with the user being member
+ with the permission being public' do
before do
member.save!
end
it 'includes the project' do
- expect(Project.allowed_to(user, public_action)).to match_array [project]
+ expect(described_class.allowed_to(user, public_action)).to contain_exactly(project)
end
end
- context 'w/ the user being member
- w/ the permission being public
- w/o the associated module being active' do
+ context 'with the user being member
+ with the permission being public
+ without the associated module being active' do
before do
member.save!
project.enabled_modules = []
end
it 'is empty' do
- expect(Project.allowed_to(user, public_action)).to be_empty
+ expect(described_class.allowed_to(user, public_action)).to be_empty
end
end
- context 'w/ the user being member
- w/ the permission being public and not module bound
- w/ no module bing active' do
+ context 'with the user being member
+ with the permission being public and not module bound
+ with no module being active' do
before do
member.save!
project.enabled_modules = []
end
it 'includes the project' do
- expect(Project.allowed_to(user, public_non_module_action)).to match_array [project]
+ expect(described_class.allowed_to(user, public_non_module_action)).to contain_exactly(project)
+ end
+ end
+
+ context 'with the user being member
+ without the permission being module bound
+ with the role having the permission
+ with no module active' do
+ let(:permissions) { [non_module_action] }
+
+ before do
+ member.save!
+ end
+
+ it 'includes the project' do
+ expect(described_class.allowed_to(user, non_module_action)).to contain_exactly(project)
+ end
+ end
+
+ context 'with the user being member
+ without the permission being module bound
+ without the role having the permission
+ with no module active' do
+ let(:permissions) { [] }
+
+ before do
+ member.save!
+ end
+
+ it 'is empty' do
+ expect(described_class.allowed_to(user, non_module_action)).to be_empty
end
end
end
- shared_examples_for 'w/ an admin user' do
+ shared_examples_for 'with an admin user' do
let(:user) { admin }
before do
@@ -183,26 +214,32 @@ RSpec.describe Project, 'allowed to' do
user.save!
end
- context 'w/o the user being a member' do
+ context 'without the user being a member' do
it_behaves_like 'includes the project'
end
- context 'w/o the project being active' do
+ context 'without the project being active' do
let(:project_status) { false }
it_behaves_like 'is empty'
end
- context 'w/o the project being active
- w/ the permission being public' do
+ context 'without the project being active
+ with the permission being public' do
let(:project_status) { false }
it 'is empty' do
- expect(Project.allowed_to(user, public_action)).to be_empty
+ expect(described_class.allowed_to(user, public_action)).to be_empty
+ end
+ end
+
+ context 'with the project being active with the permission being public' do
+ it 'includes the project' do
+ expect(described_class.allowed_to(user, public_action)).to contain_exactly(project)
end
end
- context 'w/o the project module being active' do
+ context 'without the project module being active' do
before do
project.enabled_modules = []
end
@@ -211,99 +248,99 @@ RSpec.describe Project, 'allowed to' do
end
end
- context 'w/ the project being private' do
+ context 'with the project being private' do
let(:project) { private_project }
it_behaves_like 'member based allowed to check'
- it_behaves_like 'w/ an admin user'
+ it_behaves_like 'with an admin user'
- context 'w/ the user not being logged in' do
+ context 'with the user not being logged in' do
before do
project.save!
anonymous.save!
anonymous_role.save!
end
- context 'w/ the anonymous role having the permission' do
+ context 'with the anonymous role having the permission' do
it 'is empty' do
- expect(Project.allowed_to(anonymous, action)).to be_empty
+ expect(described_class.allowed_to(anonymous, action)).to be_empty
end
end
- context 'w/ the permission being public' do
+ context 'with the permission being public' do
it 'is empty' do
- expect(Project.allowed_to(anonymous, public_action)).to be_empty
+ expect(described_class.allowed_to(anonymous, public_action)).to be_empty
end
end
end
end
- context 'w/ the project being public' do
+ context 'with the project being public' do
let(:project) { public_project }
it_behaves_like 'member based allowed to check'
- it_behaves_like 'w/ an admin user'
+ it_behaves_like 'with an admin user'
- context 'w/ the user not being logged in' do
+ context 'with the user not being logged in' do
before do
project.save!
anonymous.save!
anonymous_role.save!
end
- context 'w/ the anonymous role having the permission' do
+ context 'with the anonymous role having the permission' do
it 'includes the project' do
- expect(Project.allowed_to(anonymous, action)).to match_array [project]
+ expect(described_class.allowed_to(anonymous, action)).to contain_exactly(project)
end
end
- context 'w/ the anonymous role having the permission
- w/o the project being active' do
+ context 'with the anonymous role having the permission
+ without the project being active' do
let(:project_status) { false }
it 'is empty' do
- expect(Project.allowed_to(anonymous, action)).to be_empty
+ expect(described_class.allowed_to(anonymous, action)).to be_empty
end
end
- context 'w/o the anonymous role having the permission' do
+ context 'without the anonymous role having the permission' do
let(:anonymous_permissions) { [] }
it 'is empty' do
- expect(Project.allowed_to(anonymous, action)).to be_empty
+ expect(described_class.allowed_to(anonymous, action)).to be_empty
end
end
- context 'w/ the permission being public' do
+ context 'with the permission being public' do
it 'includes the project' do
- expect(Project.allowed_to(anonymous, public_action)).to match_array [project]
+ expect(described_class.allowed_to(anonymous, public_action)).to contain_exactly(project)
end
end
- context 'w/ the permission being public
- w/ the associated module not being active' do
+ context 'with the permission being public
+ with the associated module not being active' do
before do
project.enabled_modules = []
end
it 'is empty' do
- expect(Project.allowed_to(anonymous, public_action)).to be_empty
+ expect(described_class.allowed_to(anonymous, public_action)).to be_empty
end
end
- context 'w/ the permission being public and not module bound
- w/ no module being active' do
+ context 'with the permission being public and not module bound
+ with no module being active' do
before do
project.enabled_modules = []
end
it 'includes the project' do
- expect(Project.allowed_to(anonymous, public_non_module_action)).to match_array [project]
+ expect(described_class.allowed_to(anonymous, public_non_module_action)).to contain_exactly(project)
end
end
end
- context 'w/ the user being member' do
+ context 'with the user being member' do
before do
project.save!
user.save!
@@ -311,8 +348,8 @@ RSpec.describe Project, 'allowed to' do
member.save!
end
- context 'w/ the role not having the permission
- w/ non member having the permission' do
+ context 'with the role not having the permission
+ with non member having the permission' do
let(:permissions) { [] }
let(:non_member_permissions) { [action] }
@@ -320,64 +357,64 @@ RSpec.describe Project, 'allowed to' do
end
end
- context 'w/o the user being member' do
+ context 'without the user being member' do
before do
project.save!
user.save!
non_member_role.save!
end
- context 'w/ the non member role having the permission' do
+ context 'with the non member role having the permission' do
it_behaves_like 'includes the project'
end
- context 'w/ the non member role having the permission
- w/o the project being active' do
+ context 'with the non member role having the permission
+ without the project being active' do
let(:project_status) { false }
it_behaves_like 'is empty'
end
- context 'w/ the permission being public and not module bound
- w/o the project being active' do
+ context 'with the permission being public and not module bound
+ without the project being active' do
let(:project_status) { false }
it 'is empty' do
- expect(Project.allowed_to(user, public_non_module_action)).to be_empty
+ expect(described_class.allowed_to(user, public_non_module_action)).to be_empty
end
end
- context 'w/o the non member role having the permission' do
+ context 'without the non member role having the permission' do
let(:non_member_permissions) { [] }
it_behaves_like 'is empty'
end
- context 'w/ the permission being public' do
+ context 'with the permission being public' do
it 'includes the project' do
- expect(Project.allowed_to(user, public_action)).to match_array [project]
+ expect(described_class.allowed_to(user, public_action)).to contain_exactly(project)
end
end
- context 'w/ the permission being public
- w/ the module not being active' do
+ context 'with the permission being public
+ with the module not being active' do
before do
project.enabled_modules = []
end
it 'is empty' do
- expect(Project.allowed_to(user, public_action)).to be_empty
+ expect(described_class.allowed_to(user, public_action)).to be_empty
end
end
- context 'w/ the permission being public and not module bound
- w/ no module active' do
+ context 'with the permission being public and not module bound
+ with no module active' do
before do
project.enabled_modules = []
end
it 'includes the project' do
- expect(Project.allowed_to(user, public_non_module_action)).to match_array [project]
+ expect(described_class.allowed_to(user, public_non_module_action)).to contain_exactly(project)
end
end
end