joins_enabled_modules.patch

Pavel Rosick√Ĺ, 2017-06-07 22:51

Download (11 KB)

View differences:

controllers/versions_controller.rb (working copy)
47 47
        @issues_by_version = {}
48 48
        if @selected_tracker_ids.any? && @versions.any?
49 49
          issues = Issue.visible.
50
            includes(:project, :tracker).
50
            eager_load(:project, :tracker).
51 51
            preload(:status, :priority, :fixed_version).
52 52
            where(:tracker_id => @selected_tracker_ids, :project_id => project_ids, :fixed_version_id => @versions.map(&:id)).
53 53
            order("#{Project.table_name}.lft, #{Tracker.table_name}.position, #{Issue.table_name}.id")
models/board.rb (working copy)
31 31
  attr_protected :id
32 32

  
33 33
  scope :visible, lambda {|*args|
34
    joins(:project).
35
    where(Project.allowed_to_condition(args.shift || User.current, :view_messages, *args))
34
    user = args.shift || User.current
35
    options = args.shift || {}
36
    options[:skip_pre_condition] = true
37
    permission = :view_messages
38
    Project.enabled_modules_scope(all, permission).where(Project.allowed_to_condition(user, permission, options))
36 39
  }
37 40

  
38 41
  safe_attributes 'name', 'description', 'parent_id', 'position'
models/changeset.rb (working copy)
49 49
  attr_protected :id
50 50

  
51 51
  scope :visible, lambda {|*args|
52
    joins(:repository => :project).
53
    where(Project.allowed_to_condition(args.shift || User.current, :view_changesets, *args))
52
    user = args.shift || User.current
53
    options = args.shift || {}
54
    options[:skip_pre_condition] = true
55
    scope = if project_module = Redmine::AccessControl.permission(:view_changesets).try(:project_module)
56
      joins(:repository => {:project => :enabled_modules}).where(:enabled_modules => {:name => project_module})
57
    else
58
      joins(:repository => :project)
59
    end
60

  
61
    scope.where(Project.allowed_to_condition(user, :view_changesets, options))
54 62
  }
55 63

  
56 64
  after_create :scan_for_issues
models/document.rb (working copy)
36 36
  after_create :send_notification
37 37

  
38 38
  scope :visible, lambda {|*args|
39
    joins(:project).
40
    where(Project.allowed_to_condition(args.shift || User.current, :view_documents, *args))
39
    user = args.shift || User.current
40
    options = args.shift || {}
41
    options[:skip_pre_condition] = true
42
    permission = :view_documents
43
    Project.enabled_modules_scope(all, permission).where(Project.allowed_to_condition(user, permission, options))
41 44
  }
42 45

  
43 46
  safe_attributes 'category_id', 'title', 'description', 'custom_fields', 'custom_field_values'
models/issue.rb (working copy)
72 72
  attr_protected :id
73 73

  
74 74
  scope :visible, lambda {|*args|
75
    joins(:project).
76
    where(Issue.visible_condition(args.shift || User.current, *args))
75
    user = args.shift || User.current
76
    options = args.shift || {}
77
    options[:skip_pre_condition] = true
78
    Project.enabled_modules_scope(all, :view_issues).where(Issue.visible_condition(user, options))
77 79
  }
78 80

  
79 81
  scope :open, lambda {|*args|
......
1527 1529
  # Returns a scope of projects that user can assign issues to
1528 1530
  # If current_project is given, it will be included in the scope
1529 1531
  def self.allowed_target_projects(user=User.current, current_project=nil)
1530
    condition = Project.allowed_to_condition(user, :add_issues)
1532
    condition = Project.allowed_to_condition(user, :add_issues, :skip_pre_condition => true)
1531 1533
    if current_project
1532 1534
      condition = ["(#{condition}) OR #{Project.table_name}.id = ?", current_project.id]
1533 1535
    end
1534
    Project.where(condition).having_trackers
1536

  
1537
    scope = Project
1538
    if project_module = Redmine::AccessControl.permission(:add_issues).try(:project_module)
1539
      scope = scope.joins(:enabled_modules).where(:enabled_modules => {:name => project_module})
1540
    end
1541

  
1542
    scope.where(condition).having_trackers
1535 1543
  end
1536 1544

  
1537 1545
  # Returns a scope of trackers that user can assign the issue to
models/journal.rb (working copy)
49 49
    user = args.shift || User.current
50 50
    options = args.shift || {}
51 51

  
52
    joins(:issue => :project).
52
    options[:skip_pre_condition] = true
53
    scope = if project_module = Redmine::AccessControl.permission(:view_issues).try(:project_module)
54
      joins(:issue => {:project => :enabled_modules}).where(:enabled_modules => {:name => project_module})
55
    else
56
      joins(:issue => :project)
57
    end
58

  
59
    scope.
53 60
      where(Issue.visible_condition(user, options)).
54 61
      where(Journal.visible_notes_condition(user, :skip_pre_condition => true))
55 62
  }
models/message.rb (working copy)
49 49
  after_create :send_notification
50 50

  
51 51
  scope :visible, lambda {|*args|
52
    joins(:board => :project).
53
    where(Project.allowed_to_condition(args.shift || User.current, :view_messages, *args))
52
    user = args.shift || User.current
53
    options = args.shift || {}
54
    options[:skip_pre_condition] = true
55
    scope = if project_module = Redmine::AccessControl.permission(:view_messages).try(:project_module)
56
      joins(:board => {:project => :enabled_modules}).where(:enabled_modules => {:name => project_module})
57
    else
58
      joins(:board => :project)
59
    end
60

  
61
    scope.where(Project.allowed_to_condition(user, :view_messages, options))
54 62
  }
55 63

  
56 64
  safe_attributes 'subject', 'content'
models/news.rb (working copy)
39 39
  after_create :send_notification
40 40

  
41 41
  scope :visible, lambda {|*args|
42
    joins(:project).
43
    where(Project.allowed_to_condition(args.shift || User.current, :view_news, *args))
42
    user = args.shift || User.current
43
    options = args.shift || {}
44
    options[:skip_pre_condition] = true
45
    permission = :view_news
46
    Project.enabled_modules_scope(all, permission).where(Project.allowed_to_condition(user, permission, options))
44 47
  }
45 48

  
46 49
  safe_attributes 'title', 'summary', 'description'
models/project.rb (working copy)
91 91
  scope :active, lambda { where(:status => STATUS_ACTIVE) }
92 92
  scope :status, lambda {|arg| where(arg.blank? ? nil : {:status => arg.to_i}) }
93 93
  scope :all_public, lambda { where(:is_public => true) }
94
  scope :visible, lambda {|*args| where(Project.visible_condition(args.shift || User.current, *args)) }
94
  scope :visible, lambda { |*args|
95
    user = args.shift || User.current
96
    options = args.shift || {}
97
    options[:skip_pre_condition] = true
98
    scope = all
99
    if project_module = Redmine::AccessControl.permission(:view_project).try(:project_module)
100
      scope = scope.joins(:enabled_modules).where(:enabled_modules => {:name => project_module})
101
    end
102

  
103
    scope.where(Project.visible_condition(user, options))
104
  }
95 105
  scope :allowed_to, lambda {|*args|
96 106
    user = User.current
97 107
    permission = nil
......
169 179
    allowed_to_condition(user, :view_project, options)
170 180
  end
171 181

  
182
  def self.enabled_modules_scope(scope, permission)
183
    if project_module = Redmine::AccessControl.permission(permission).try(:project_module)
184
      scope.joins(:project => :enabled_modules).where(:enabled_modules => {:name => project_module})
185
    else
186
      scope.joins(:project)
187
    end
188
  end
189

  
172 190
  # Returns a SQL conditions string used to find all projects for which +user+ has the given +permission+
173 191
  #
174 192
  # Valid options:
models/time_entry.rb (working copy)
51 51
  validate :validate_time_entry
52 52

  
53 53
  scope :visible, lambda {|*args|
54
    joins(:project).
55
    where(TimeEntry.visible_condition(args.shift || User.current, *args))
54
    user = args.shift || User.current
55
    options = args.shift || {}
56
    options[:skip_pre_condition] = true
57
    permission = :view_time_entries
58
    Project.enabled_modules_scope(all, permission).where(TimeEntry.visible_condition(user, options))
56 59
  }
57 60
  scope :left_join_issue, lambda {
58 61
    joins("LEFT OUTER JOIN #{Issue.table_name} ON #{Issue.table_name}.id = #{TimeEntry.table_name}.issue_id")
models/tracker.rb (working copy)
57 57
  #   => returns the trackers that are visible by the user in at least on project
58 58
  scope :visible, lambda {|*args|
59 59
    user = args.shift || User.current
60
    condition = Project.allowed_to_condition(user, :view_issues) do |role, user|
61
      unless role.permissions_all_trackers?(:view_issues)
62
        tracker_ids = role.permissions_tracker_ids(:view_issues)
60
    options = args.shift || {}
61
    options[:skip_pre_condition] = true
62
    permission = :view_issues
63

  
64
    condition = Project.allowed_to_condition(user, permission, options) do |role, user|
65
      unless role.permissions_all_trackers?(permission)
66
        tracker_ids = role.permissions_tracker_ids(permission)
63 67
        if tracker_ids.any?
64 68
          "#{Tracker.table_name}.id IN (#{tracker_ids.join(',')})"
65 69
        else
......
67 71
        end
68 72
      end
69 73
    end
70
    joins(:projects).where(condition).distinct
74

  
75
    scope = if project_module = Redmine::AccessControl.permission(permission).try(:project_module)
76
      joins(:projects => :enabled_modules).where(:enabled_modules => {:name => project_module})
77
    else
78
      joins(:projects)
79
    end
80

  
81
    scope.where(condition).distinct
71 82
  }
72 83

  
73 84
  safe_attributes 'name',
models/version.rb (working copy)
55 55
    end
56 56
  }
57 57
  scope :visible, lambda {|*args|
58
    joins(:project).
59
    where(Project.allowed_to_condition(args.first || User.current, :view_issues))
58
    user = args.shift || User.current
59
    options = args.shift || {}
60
    options[:skip_pre_condition] = true
61
    permission = :view_issues
62
    Project.enabled_modules_scope(all, permission).where(Project.allowed_to_condition(user, permission, options))
60 63
  }
61 64

  
62 65
  safe_attributes 'name',