@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?)
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
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"
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
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
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)
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
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