From 60e95f7541d1117cf9ad38f26be3a856bae06b71 Mon Sep 17 00:00:00 2001 From: Jens Ulferts Date: Tue, 9 Aug 2016 18:05:09 +0200 Subject: [PATCH] limit enabled modules subquery to projects of interest --- app/models/project.rb | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/app/models/project.rb b/app/models/project.rb index 32834706e1..4b0e4eb358 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -354,7 +354,21 @@ class Project < ActiveRecord::Base if perm = Redmine::AccessControl.permission(permission) unless perm.project_module.nil? # If the permission belongs to a project module, make sure the module is enabled - base_statement << " AND #{table_alias}.id IN (SELECT em.project_id FROM #{EnabledModule.table_name} em WHERE em.name='#{perm.project_module}')" + enabled_module_statement = "SELECT em.project_id FROM #{EnabledModule.table_name} em" + + if options[:project] && options[:with_subprojects] + enabled_module_statement << " JOIN #{table_alias} ON #{table_alias}.id = em.project_id" + end + + enabled_module_statement << " WHERE em.name='#{perm.project_module}'" + + if options[:project] + project_statement = "em.project_id = #{options[:project].id}" + project_statement << " OR (#{table_alias}.lft > #{options[:project].lft} AND #{table_alias}.rgt < #{options[:project].rgt})" if options[:with_subprojects] + + enabled_module_statement << " AND (#{project_statement})" + end + base_statement << " AND #{table_alias}.id IN (#{enabled_module_statement})" end end if options[:project]