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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
move to hand written sql
ulfertsab53695e3647
diff --git a/app/models/project.rb b/app/models/project.rbindex bb82a97d931..eeac6404f01 100644--- a/app/models/project.rb+++ b/app/models/project.rb@@ -150,6 +150,9 @@ class Project < ApplicationRecordfriendly_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 < ApplicationRecordallowed_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.rbnew file mode 100644index 00000000000..6d2bd4cbbd5--- /dev/null+++ b/app/models/projects/scopes/allowed_to.rb@@ -0,0 +1,140 @@+# -- 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 a ActiveRecord::Relation to find all projects for which+ # +user+ has the given +permission++ def allowed_to(user, permission)+ permissions = if permission.is_a?(Hash)+ OpenProject::AccessControl.allow_actions(permission)+ else+ [OpenProject::AccessControl.permission(permission)].compact+ end++ if user.admin?+ Project+ .joins(:enabled_modules)+ .where(active: true)+ .where(enabled_modules: { name: permissions.map(&:project_module).compact.uniq })+ elsif user.anonymous?+ Project.find_by_sql(allowed_to_non_member_sql(user, permissions))+ else+ Project.find_by_sql("#{allowed_to_member_sql(user, permissions)} UNION #{allowed_to_non_member_sql(user, permissions)}")+ end+ end++ private++ def allowed_to_non_member_sql(user, permissions)+ sql = <<~SQL.squish+ SELECT "projects".*+ FROM "projects"+ #{allowed_to_enabled_module_join(permissions)}+ INNER JOIN "roles"+ ON "roles"."builtin" IN (#{Role::BUILTIN_ANONYMOUS}, #{Role::BUILTIN_NON_MEMBER})+ AND "projects"."active" = TRUE+ AND "projects"."public" = TRUE+ AND NOT EXISTS (SELECT 1+ FROM members+ WHERE members.project_id = projects.id+ AND members.user_id = :user_id+ AND members.entity_type IS NULL+ AND members.entity_id IS NULL+ LIMIT 1)+ #{allowed_to_role_permission_join(permissions)}+ SQL++ OpenProject::SqlSanitization.sanitize(sql,+ user_id: user.id,+ permission: permissions.map(&:name))+ end++ def allowed_to_member_sql(user, permissions)+ sql = <<~SQL.squish+ SELECT "projects".*+ FROM "projects"+ #{allowed_to_enabled_module_join(permissions)}+ LEFT OUTER JOIN "members"+ ON "projects"."id" = "members"."project_id"+ AND "members"."user_id" = :user_id+ AND "members"."entity_type" IS NULL+ AND "members"."entity_id" IS NULL+ AND "projects"."active" = TRUE+ LEFT OUTER JOIN "member_roles"+ ON "members"."id" = "member_roles"."member_id"+ LEFT OUTER JOIN "roles"+ ON "projects"."active" = TRUE+ AND ("roles"."id" = "member_roles"."role_id" OR+ ("projects"."public" = TRUE AND "roles"."builtin" = #{Role::BUILTIN_NON_MEMBER} AND "roles"."id" IS NULL))+ #{allowed_to_role_permission_join(permissions)}+ WHERE "roles"."id" IS NOT NULL+ SQL++ OpenProject::SqlSanitization.sanitize(sql,+ user_id: user.id,+ permission: permissions.map(&:name))+ end++ def allowed_to_enabled_module_join(permissions)+ project_module = permissions.map(&:project_module).compact.uniq++ if project_module.any?+ sql = <<~SQL.squish+ INNER JOIN "enabled_modules"+ ON "projects"."id" = "enabled_modules"."project_id"+ AND "enabled_modules"."name" IN (:enabled_module_names)+ AND "projects"."active" = TRUE+ SQL++ OpenProject::SqlSanitization.sanitize(sql,+ enabled_module_names: project_module)+ end+ end++ def allowed_to_role_permission_join(permissions)+ return if permissions.all?(&:public?)++ condition = permissions.map do |permission|+ <<~SQL.squish+ (enabled_modules.name = '#{permission.project_module}' AND "role_permissions"."permission" = '#{permission.name}')+ SQL+ end.join(' OR ')++ <<~SQL.squish+ JOIN "role_permissions"+ ON roles.id = role_permissions.role_id+ AND (#{condition})+ SQL+ end+ end+ end+enddiff --git a/app/services/authorization.rb b/app/services/authorization.rbindex c6818741fc2..6405426fdbc 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 indef projects(action, user)- Authorization::ProjectQuery.query(user, action)+ Project.allowed_to(action, user)end# Returns all roles a user has in a certain project, for a specific entity or globally