]> source.dussan.org Git - redmine.git/commitdiff
Merged r15540 (#285).
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Sat, 18 Jun 2016 06:46:55 +0000 (06:46 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Sat, 18 Jun 2016 06:46:55 +0000 (06:46 +0000)
git-svn-id: http://svn.redmine.org/redmine/branches/3.3-stable@15542 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/controllers/projects_controller.rb
app/controllers/reports_controller.rb
app/models/project.rb
app/models/query.rb
app/models/tracker.rb
test/unit/tracker_test.rb

index 7e1798a060488ae9e53bc37befa44c22c2dc1107..8753badde9396faaf4d5238542efbbbec12cd73f 100644 (file)
@@ -137,7 +137,7 @@ class ProjectsController < ApplicationController
     @users_by_role = @project.users_by_role
     @subprojects = @project.children.visible.to_a
     @news = @project.news.limit(5).includes(:author, :project).reorder("#{News.table_name}.created_on DESC").to_a
-    @trackers = @project.rolled_up_trackers
+    @trackers = @project.rolled_up_trackers.visible
 
     cond = @project.project_condition(Setting.display_subprojects_issues?)
 
index b7bf920fdb7576f4284435212ed4f1b56813c5ba..20279871e5acd2c5c1eb2f452eaf4b0337f77b8e 100644 (file)
@@ -20,7 +20,7 @@ class ReportsController < ApplicationController
   before_filter :find_project, :authorize, :find_issue_statuses
 
   def issue_report
-    @trackers = @project.trackers
+    @trackers = @project.rolled_up_trackers(false).visible
     @versions = @project.shared_versions.sort
     @priorities = IssuePriority.all.reverse
     @categories = @project.issue_categories
@@ -43,7 +43,7 @@ class ReportsController < ApplicationController
     case params[:detail]
     when "tracker"
       @field = "tracker_id"
-      @rows = @project.trackers
+      @rows = @project.rolled_up_trackers(false).visible
       @data = Issue.by_tracker(@project)
       @report_title = l(:field_tracker)
     when "version"
index e9b2647d4b9a934d83e5f4cc8cdcc6509dc47cad..b6bc13dde19d0ad14c59773795dab02d696456ed 100644 (file)
@@ -421,16 +421,24 @@ class Project < ActiveRecord::Base
     save
   end
 
-  # Returns an array of the trackers used by the project and its active sub projects
-  def rolled_up_trackers
-    @rolled_up_trackers ||=
-      Tracker.
-        joins(projects: :enabled_modules).
-        where("#{Project.table_name}.lft >= ? AND #{Project.table_name}.rgt <= ? AND #{Project.table_name}.status <> ?", lft, rgt, STATUS_ARCHIVED).
-        where("#{EnabledModule.table_name}.name = ?", 'issue_tracking').
-        uniq.
-        sorted.
-        to_a
+  # Returns a scope of the trackers used by the project and its active sub projects
+  def rolled_up_trackers(include_subprojects=true)
+    if include_subprojects
+      @rolled_up_trackers ||= rolled_up_trackers_base_scope.
+          where("#{Project.table_name}.lft >= ? AND #{Project.table_name}.rgt <= ?", lft, rgt)
+    else
+      rolled_up_trackers_base_scope.
+        where(:projects => {:id => id})
+    end
+  end
+
+  def rolled_up_trackers_base_scope
+    Tracker.
+      joins(projects: :enabled_modules).
+      where("#{Project.table_name}.status <> ?", STATUS_ARCHIVED).
+      where(:enabled_modules => {:name => 'issue_tracking'}).
+      uniq.
+      sorted
   end
 
   # Closes open and locked project versions that are completed
index bb1807f6eae0ef0d8e17b9cee599c7a0a1559ab7..8fab46669c2373b1c80989c17f486a1f8cb9e674 100644 (file)
@@ -301,7 +301,7 @@ class Query < ActiveRecord::Base
   end
 
   def trackers
-    @trackers ||= project.nil? ? Tracker.sorted.to_a : project.rolled_up_trackers
+    @trackers ||= (project.nil? ? Tracker.all : project.rolled_up_trackers).visible.sorted
   end
 
   # Returns a hash of localized labels for all filter operators
index 73cf569fceb7b752f6a9b2deeec48c6cfc2fb63b..5e4a24b5146a31d9770d5c18151d72d4301b3b1a 100644 (file)
@@ -46,6 +46,29 @@ class Tracker < ActiveRecord::Base
   scope :sorted, lambda { order(:position) }
   scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)}
 
+  # Returns the trackers that are visible by the user.
+  #
+  # Examples:
+  #   project.trackers.visible(user)
+  #   => returns the trackers that are visible by the user in project
+  #
+  #   Tracker.visible(user)
+  #   => returns the trackers that are visible by the user in at least on project
+  scope :visible, lambda {|*args|
+    user = args.shift || User.current
+    condition = Project.allowed_to_condition(user, :view_issues) do |role, user|
+      unless role.permissions_all_trackers?(:view_issues)
+        tracker_ids = role.permissions_tracker_ids(:view_issues)
+        if tracker_ids.any?
+          "#{Tracker.table_name}.id IN (#{tracker_ids.join(',')})"
+        else
+          '1=0'
+        end
+      end
+    end
+    joins(:projects).where(condition).uniq
+  }
+
   def to_s; name end
 
   def <=>(tracker)
index 658dbb7f66d65c7aaa512e5be9f469a7dfb6d588..d45291fd75e5d1ab89a70b7575ac7399065a8ddd 100644 (file)
@@ -18,7 +18,7 @@
 require File.expand_path('../../test_helper', __FILE__)
 
 class TrackerTest < ActiveSupport::TestCase
-  fixtures :trackers, :workflows, :issue_statuses, :roles, :issues
+  fixtures :trackers, :workflows, :issue_statuses, :roles, :issues, :projects, :projects_trackers
 
   def test_sorted_scope
     assert_equal Tracker.all.sort, Tracker.sorted.to_a
@@ -28,6 +28,18 @@ class TrackerTest < ActiveSupport::TestCase
     assert_equal Tracker.find_by_name('Feature'), Tracker.named('feature').first
   end
 
+  def test_visible_scope_chained_with_project_rolled_up_trackers
+    project = Project.find(1)
+    role = Role.generate!
+    role.add_permission! :view_issues
+    role.set_permission_trackers :view_issues, [2]
+    role.save!
+    user = User.generate!
+    User.add_to_project user, project, role
+
+    assert_equal [2], project.rolled_up_trackers(false).visible(user).map(&:id)
+  end
+
   def test_copy_workflows
     source = Tracker.find(1)
     rules_count = source.workflow_rules.count