]> source.dussan.org Git - redmine.git/commitdiff
Adds Issue#allowed_target_trackers (#7839).
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Mon, 30 May 2016 18:20:13 +0000 (18:20 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Mon, 30 May 2016 18:20:13 +0000 (18:20 +0000)
git-svn-id: http://svn.redmine.org/redmine/trunk@15430 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/controllers/context_menus_controller.rb
app/controllers/issues_controller.rb
app/helpers/issues_helper.rb
app/models/issue.rb
app/models/mail_handler.rb
app/views/issues/_form.html.erb
app/views/issues/index.html.erb
lib/redmine.rb
test/unit/issue_test.rb

index 1e5652d09a770e8319244b2dd1a510ee63a0e3e2..59ee3a77a9543b18b9cea7a6811bb3a3674241b7 100644 (file)
@@ -41,12 +41,11 @@ class ContextMenusController < ApplicationController
       else
         @assignables = @project.assignable_users
       end
-      @trackers = @project.trackers
     else
       #when multiple projects, we only keep the intersection of each set
       @assignables = @projects.map(&:assignable_users).reduce(:&)
-      @trackers = @projects.map(&:trackers).reduce(:&)
     end
+    @trackers = @projects.map {|p| Issue.allowed_target_trackers(p) }.reduce(:&)
     @versions = @projects.map {|p| p.shared_versions.open}.reduce(:&)
 
     @priorities = IssuePriority.active.reverse
index cf402de226824182a0d7bb47baba7998b6e83eb2..1db5e6ff8168372ee1496b2d064e57bf79acd780 100644 (file)
@@ -230,7 +230,7 @@ class IssuesController < ApplicationController
     end
     @custom_fields = @issues.map{|i|i.editable_custom_fields}.reduce(:&)
     @assignables = target_projects.map(&:assignable_users).reduce(:&)
-    @trackers = target_projects.map(&:trackers).reduce(:&)
+    @trackers = target_projects.map {|p| Issue.allowed_target_trackers(p) }.reduce(:&)
     @versions = target_projects.map {|p| p.shared_versions.open}.reduce(:&)
     @categories = target_projects.map {|p| p.issue_categories}.reduce(:&)
     if @copy
@@ -465,7 +465,7 @@ class IssuesController < ApplicationController
     @issue.safe_attributes = attrs
 
     if @issue.project
-      @issue.tracker ||= @issue.project.trackers.first
+      @issue.tracker ||= @issue.allowed_target_trackers.first
       if @issue.tracker.nil?
         render_error l(:error_no_tracker_in_project)
         return false
index b505765d8e1cce363f215e1905a96d92cad02f15..ce6d9f8f22f230ea8e35aad9ff5bbf91013e716a 100644 (file)
@@ -168,6 +168,16 @@ module IssuesHelper
     link_to(l(:button_add), new_project_issue_path(issue.project, :issue => attrs))
   end
 
+  def trackers_options_for_select(issue)
+    trackers = issue.allowed_target_trackers
+    if issue.new_record? && issue.parent_issue_id.present?
+      trackers = trackers.reject do |tracker|
+        issue.tracker_id != tracker.id && tracker.disabled_core_fields.include?('parent_issue_id')
+      end
+    end
+    trackers.collect {|t| [t.name, t.id]}
+  end
+
   class IssueFieldsRows
     include ActionView::Helpers::TagHelper
 
index 0ee8ff3d31c4bd48a51c1603efb50c4d4a0c4420..d6133d3a93dd3cf0e62181c24c208fd12d3ccdf5 100644 (file)
@@ -479,12 +479,14 @@ class Issue < ActiveRecord::Base
     end
 
     if (t = attrs.delete('tracker_id')) && safe_attribute?('tracker_id')
-      self.tracker_id = t
+      if allowed_target_trackers(user).where(:id => t.to_i).exists?
+        self.tracker_id = t
+      end
     end
     if project
-      # Set the default tracker to accept custom field values
+      # Set a default tracker to accept custom field values
       # even if tracker is not specified
-      self.tracker ||= project.trackers.first
+      self.tracker ||= allowed_target_trackers(user).first
     end
 
     statuses_allowed = new_statuses_allowed_to(user)
@@ -822,16 +824,6 @@ class Issue < ActiveRecord::Base
     !leaf?
   end
 
-  def assignable_trackers
-    trackers = project.trackers
-    if new_record? && parent_issue_id.present?
-      trackers = trackers.reject do |tracker|
-        tracker_id != tracker.id && tracker.disabled_core_fields.include?('parent_issue_id')
-      end
-    end
-    trackers
-  end
-
   # Users the issue can be assigned to
   def assignable_users
     users = project.assignable_users.to_a
@@ -1373,6 +1365,20 @@ class Issue < ActiveRecord::Base
     end
     Project.where(condition).having_trackers
   end
+  # Returns a scope of trackers that user can assign the issue to
+  def allowed_target_trackers(user=User.current)
+    if project
+      self.class.allowed_target_trackers(project, user, tracker_id_was)
+    else
+      Tracker.none
+    end
+  end
+
+  # Returns a scope of trackers that user can assign project issues to
+  def self.allowed_target_trackers(project, user=User.current, current_tracker=nil)
+    project.trackers.sorted
+  end
 
   private
 
index 40bd02a0789bc26d18a87a850d6c8cd0f066fca6..a619f115dd2d5211f380ad95dad7dde7abe8b7c9 100644 (file)
@@ -199,7 +199,14 @@ class MailHandler < ActionMailer::Base
     end
 
     issue = Issue.new(:author => user, :project => project)
-    issue.safe_attributes = issue_attributes_from_keywords(issue)
+    attributes = issue_attributes_from_keywords(issue)
+    if handler_options[:no_permission_check]
+      issue.tracker_id = attributes['tracker_id']
+      if project
+        issue.tracker_id ||= project.trackers.first.try(:id)
+      end
+    end
+    issue.safe_attributes = attributes
     issue.safe_attributes = {'custom_field_values' => custom_field_values_from_keywords(issue)}
     issue.subject = cleaned_up_subject
     if issue.subject.blank?
@@ -420,10 +427,6 @@ class MailHandler < ActionMailer::Base
       'done_ratio' => get_keyword(:done_ratio, :format => '(\d|10)?0')
     }.delete_if {|k, v| v.blank? }
 
-    if issue.new_record? && attrs['tracker_id'].nil?
-      attrs['tracker_id'] = issue.project.trackers.first.try(:id)
-    end
-
     attrs
   end
 
index 788a2f38e15d3d3534787be99d0da454a2059999..1e82b6f8432ac55f40281ca32fdb7c884a172b5f 100644 (file)
@@ -14,7 +14,7 @@
 <% end %>
 
 <% if @issue.safe_attribute? 'tracker_id' %>
-<p><%= f.select :tracker_id, @issue.assignable_trackers.collect {|t| [t.name, t.id]}, {:required => true},
+<p><%= f.select :tracker_id, trackers_options_for_select(@issue), {:required => true},
                 :onchange => "updateIssueFrom('#{escape_javascript update_issue_form_path(@project, @issue)}', this)" %></p>
 <% end %>
 
index 7dcba2629b3affc843bc24d1c2abf5fc1e3f1bcf..fd762023fbf4362f31d110e60194a752f5bd9cf7 100644 (file)
@@ -1,5 +1,5 @@
 <div class="contextual">
-  <% if User.current.allowed_to?(:add_issues, @project, :global => true) && (@project.nil? || @project.trackers.any?) %>
+  <% if User.current.allowed_to?(:add_issues, @project, :global => true) && (@project.nil? || Issue.allowed_target_trackers(@project).any?) %>
     <%= link_to l(:label_issue_new), _new_project_issue_path(@project), :class => 'icon icon-add new-issue' %>
   <% end %>
 </div>
index 1d37a523b5196ff6a5d80ed9261500d489440cb9..0c670d1b5f2ef8caa224b95bc7983f27fd6b58cf 100644 (file)
@@ -233,7 +233,7 @@ Redmine::MenuManager.map :project_menu do |menu|
   menu.push :issues, { :controller => 'issues', :action => 'index' }, :param => :project_id, :caption => :label_issue_plural
   menu.push :new_issue, { :controller => 'issues', :action => 'new', :copy_from => nil }, :param => :project_id, :caption => :label_issue_new,
               :html => { :accesskey => Redmine::AccessKeys.key_for(:new_issue) },
-              :if => Proc.new { |p| Setting.new_project_issue_tab_enabled? && p.trackers.any? },
+              :if => Proc.new { |p| Setting.new_project_issue_tab_enabled? && Issue.allowed_target_trackers(p).any? },
               :permission => :add_issues
   menu.push :gantt, { :controller => 'gantts', :action => 'show' }, :param => :project_id, :caption => :label_gantt
   menu.push :calendar, { :controller => 'calendars', :action => 'show' }, :param => :project_id, :caption => :label_calendar
index d7c7d231bfe524224e046a5ea7f4db760161170e..363cdd07105e669b50ce16d31208e6ec840b3589 100644 (file)
@@ -737,9 +737,10 @@ class IssueTest < ActiveSupport::TestCase
     target = Tracker.find(2)
     target.core_fields = %w(assigned_to_id due_date)
     target.save!
+    user = User.find(2)
 
-    issue = Issue.new(:tracker => source)
-    issue.safe_attributes = {'tracker_id' => 2, 'due_date' => '2012-07-14'}
+    issue = Issue.new(:project => Project.find(1), :tracker => source)
+    issue.send :safe_attributes=, {'tracker_id' => 2, 'due_date' => '2012-07-14'}, user
     assert_equal target, issue.tracker
     assert_equal Date.parse('2012-07-14'), issue.due_date
   end