You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

issues_controller.rb 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2019 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. class IssuesController < ApplicationController
  19. default_search_scope :issues
  20. before_action :find_issue, :only => [:show, :edit, :update]
  21. before_action :find_issues, :only => [:bulk_edit, :bulk_update, :destroy]
  22. before_action :authorize, :except => [:index, :new, :create]
  23. before_action :find_optional_project, :only => [:index, :new, :create]
  24. before_action :build_new_issue_from_params, :only => [:new, :create]
  25. accept_rss_auth :index, :show
  26. accept_api_auth :index, :show, :create, :update, :destroy
  27. rescue_from Query::StatementInvalid, :with => :query_statement_invalid
  28. helper :journals
  29. helper :projects
  30. helper :custom_fields
  31. helper :issue_relations
  32. helper :watchers
  33. helper :attachments
  34. helper :queries
  35. include QueriesHelper
  36. helper :repositories
  37. helper :timelog
  38. def index
  39. use_session = !request.format.csv?
  40. retrieve_query(IssueQuery, use_session)
  41. if @query.valid?
  42. respond_to do |format|
  43. format.html {
  44. @issue_count = @query.issue_count
  45. @issue_pages = Paginator.new @issue_count, per_page_option, params['page']
  46. @issues = @query.issues(:offset => @issue_pages.offset, :limit => @issue_pages.per_page)
  47. render :layout => !request.xhr?
  48. }
  49. format.api {
  50. @offset, @limit = api_offset_and_limit
  51. @query.column_names = %w(author)
  52. @issue_count = @query.issue_count
  53. @issues = @query.issues(:offset => @offset, :limit => @limit)
  54. Issue.load_visible_relations(@issues) if include_in_api_response?('relations')
  55. }
  56. format.atom {
  57. @issues = @query.issues(:limit => Setting.feeds_limit.to_i)
  58. render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}")
  59. }
  60. format.csv {
  61. @issues = @query.issues(:limit => Setting.issues_export_limit.to_i)
  62. send_data(query_to_csv(@issues, @query, params[:csv]), :type => 'text/csv; header=present', :filename => 'issues.csv')
  63. }
  64. format.pdf {
  65. @issues = @query.issues(:limit => Setting.issues_export_limit.to_i)
  66. send_file_headers! :type => 'application/pdf', :filename => 'issues.pdf'
  67. }
  68. end
  69. else
  70. respond_to do |format|
  71. format.html { render :layout => !request.xhr? }
  72. format.any(:atom, :csv, :pdf) { head 422 }
  73. format.api { render_validation_errors(@query) }
  74. end
  75. end
  76. rescue ActiveRecord::RecordNotFound
  77. render_404
  78. end
  79. def show
  80. @journals = @issue.visible_journals_with_index
  81. @changesets = @issue.changesets.visible.preload(:repository, :user).to_a
  82. @relations = @issue.relations.select {|r| r.other_issue(@issue) && r.other_issue(@issue).visible? }
  83. if User.current.wants_comments_in_reverse_order?
  84. @journals.reverse!
  85. @changesets.reverse!
  86. end
  87. if User.current.allowed_to?(:view_time_entries, @project)
  88. Issue.load_visible_spent_hours([@issue])
  89. Issue.load_visible_total_spent_hours([@issue])
  90. end
  91. respond_to do |format|
  92. format.html {
  93. @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
  94. @priorities = IssuePriority.active
  95. @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
  96. @relation = IssueRelation.new
  97. retrieve_previous_and_next_issue_ids
  98. render :template => 'issues/show'
  99. }
  100. format.api
  101. format.atom { render :template => 'journals/index', :layout => false, :content_type => 'application/atom+xml' }
  102. format.pdf {
  103. send_file_headers! :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf"
  104. }
  105. end
  106. end
  107. def new
  108. respond_to do |format|
  109. format.html { render :action => 'new', :layout => !request.xhr? }
  110. format.js
  111. end
  112. end
  113. def create
  114. unless User.current.allowed_to?(:add_issues, @issue.project, :global => true)
  115. raise ::Unauthorized
  116. end
  117. call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue })
  118. @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads]))
  119. if @issue.save
  120. call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue})
  121. respond_to do |format|
  122. format.html {
  123. render_attachment_warning_if_needed(@issue)
  124. flash[:notice] = l(:notice_issue_successful_create, :id => view_context.link_to("##{@issue.id}", issue_path(@issue), :title => @issue.subject))
  125. redirect_after_create
  126. }
  127. format.api { render :action => 'show', :status => :created, :location => issue_url(@issue) }
  128. end
  129. return
  130. else
  131. respond_to do |format|
  132. format.html {
  133. if @issue.project.nil?
  134. render_error :status => 422
  135. else
  136. render :action => 'new'
  137. end
  138. }
  139. format.api { render_validation_errors(@issue) }
  140. end
  141. end
  142. end
  143. def edit
  144. return unless update_issue_from_params
  145. respond_to do |format|
  146. format.html { }
  147. format.js
  148. end
  149. end
  150. def update
  151. return unless update_issue_from_params
  152. @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads]))
  153. saved = false
  154. begin
  155. saved = save_issue_with_child_records
  156. rescue ActiveRecord::StaleObjectError
  157. @conflict = true
  158. if params[:last_journal_id]
  159. @conflict_journals = @issue.journals_after(params[:last_journal_id]).to_a
  160. @conflict_journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project)
  161. end
  162. end
  163. if saved
  164. render_attachment_warning_if_needed(@issue)
  165. flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record? || params[:no_flash]
  166. respond_to do |format|
  167. format.html { redirect_back_or_default issue_path(@issue, previous_and_next_issue_ids_params) }
  168. format.api { render_api_ok }
  169. end
  170. else
  171. respond_to do |format|
  172. format.html { render :action => 'edit' }
  173. format.api { render_validation_errors(@issue) }
  174. end
  175. end
  176. end
  177. # Bulk edit/copy a set of issues
  178. def bulk_edit
  179. @issues.sort!
  180. @copy = params[:copy].present?
  181. @notes = params[:notes]
  182. if @copy
  183. unless User.current.allowed_to?(:copy_issues, @projects)
  184. raise ::Unauthorized
  185. end
  186. else
  187. unless @issues.all?(&:attributes_editable?)
  188. raise ::Unauthorized
  189. end
  190. end
  191. edited_issues = Issue.where(:id => @issues.map(&:id)).to_a
  192. @values_by_custom_field = {}
  193. edited_issues.each do |issue|
  194. issue.custom_field_values.each do |c|
  195. if c.value_present?
  196. @values_by_custom_field[c.custom_field] ||= []
  197. @values_by_custom_field[c.custom_field] << issue.id
  198. end
  199. end
  200. end
  201. @allowed_projects = Issue.allowed_target_projects
  202. if params[:issue]
  203. @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:issue][:project_id].to_s}
  204. if @target_project
  205. target_projects = [@target_project]
  206. edited_issues.each {|issue| issue.project = @target_project}
  207. end
  208. end
  209. target_projects ||= @projects
  210. @trackers = target_projects.map {|p| Issue.allowed_target_trackers(p) }.reduce(:&)
  211. if params[:issue]
  212. @target_tracker = @trackers.detect {|t| t.id.to_s == params[:issue][:tracker_id].to_s}
  213. if @target_tracker
  214. edited_issues.each {|issue| issue.tracker = @target_tracker}
  215. end
  216. end
  217. if @copy
  218. # Copied issues will get their default statuses
  219. @available_statuses = []
  220. else
  221. @available_statuses = edited_issues.map(&:new_statuses_allowed_to).reduce(:&)
  222. end
  223. if params[:issue]
  224. @target_status = @available_statuses.detect {|t| t.id.to_s == params[:issue][:status_id].to_s}
  225. if @target_status
  226. edited_issues.each {|issue| issue.status = @target_status}
  227. end
  228. end
  229. edited_issues.each do |issue|
  230. issue.custom_field_values.each do |c|
  231. if c.value_present? && @values_by_custom_field[c.custom_field]
  232. @values_by_custom_field[c.custom_field].delete(issue.id)
  233. end
  234. end
  235. end
  236. @values_by_custom_field.delete_if {|k,v| v.blank?}
  237. @custom_fields = edited_issues.map{|i|i.editable_custom_fields}.reduce(:&).select {|field| field.format.bulk_edit_supported}
  238. @assignables = target_projects.map(&:assignable_users).reduce(:&)
  239. @versions = target_projects.map {|p| p.shared_versions.open}.reduce(:&)
  240. @categories = target_projects.map {|p| p.issue_categories}.reduce(:&)
  241. if @copy
  242. @attachments_present = @issues.detect {|i| i.attachments.any?}.present?
  243. @subtasks_present = @issues.detect {|i| !i.leaf?}.present?
  244. @watchers_present = User.current.allowed_to?(:add_issue_watchers, @projects) && Watcher.where(:watchable_type => 'Issue', :watchable_id => @issues.map(&:id)).exists?
  245. end
  246. @safe_attributes = edited_issues.map(&:safe_attribute_names).reduce(:&)
  247. @issue_params = params[:issue] || {}
  248. @issue_params[:custom_field_values] ||= {}
  249. end
  250. def bulk_update
  251. @issues.sort!
  252. @copy = params[:copy].present?
  253. attributes = parse_params_for_bulk_update(params[:issue])
  254. copy_subtasks = (params[:copy_subtasks] == '1')
  255. copy_attachments = (params[:copy_attachments] == '1')
  256. copy_watchers = (params[:copy_watchers] == '1')
  257. if @copy
  258. unless User.current.allowed_to?(:copy_issues, @projects)
  259. raise ::Unauthorized
  260. end
  261. target_projects = @projects
  262. if attributes['project_id'].present?
  263. target_projects = Project.where(:id => attributes['project_id']).to_a
  264. end
  265. unless User.current.allowed_to?(:add_issues, target_projects)
  266. raise ::Unauthorized
  267. end
  268. unless User.current.allowed_to?(:add_issue_watchers, @projects)
  269. copy_watchers = false
  270. end
  271. else
  272. unless @issues.all?(&:attributes_editable?)
  273. raise ::Unauthorized
  274. end
  275. end
  276. unsaved_issues = []
  277. saved_issues = []
  278. if @copy && copy_subtasks
  279. # Descendant issues will be copied with the parent task
  280. # Don't copy them twice
  281. @issues.reject! {|issue| @issues.detect {|other| issue.is_descendant_of?(other)}}
  282. end
  283. @issues.each do |orig_issue|
  284. orig_issue.reload
  285. if @copy
  286. issue = orig_issue.copy({},
  287. :attachments => copy_attachments,
  288. :subtasks => copy_subtasks,
  289. :watchers => copy_watchers,
  290. :link => link_copy?(params[:link_copy])
  291. )
  292. else
  293. issue = orig_issue
  294. end
  295. journal = issue.init_journal(User.current, params[:notes])
  296. issue.safe_attributes = attributes
  297. call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue })
  298. if issue.save
  299. saved_issues << issue
  300. else
  301. unsaved_issues << orig_issue
  302. end
  303. end
  304. if unsaved_issues.empty?
  305. flash[:notice] = l(:notice_successful_update) unless saved_issues.empty?
  306. if params[:follow]
  307. if @issues.size == 1 && saved_issues.size == 1
  308. redirect_to issue_path(saved_issues.first)
  309. elsif saved_issues.map(&:project).uniq.size == 1
  310. redirect_to project_issues_path(saved_issues.map(&:project).first)
  311. end
  312. else
  313. redirect_back_or_default _project_issues_path(@project)
  314. end
  315. else
  316. @saved_issues = @issues
  317. @unsaved_issues = unsaved_issues
  318. @issues = Issue.visible.where(:id => @unsaved_issues.map(&:id)).to_a
  319. bulk_edit
  320. render :action => 'bulk_edit'
  321. end
  322. end
  323. def destroy
  324. raise Unauthorized unless @issues.all?(&:deletable?)
  325. # all issues and their descendants are about to be deleted
  326. issues_and_descendants_ids = Issue.self_and_descendants(@issues).pluck(:id)
  327. time_entries = TimeEntry.where(:issue_id => issues_and_descendants_ids)
  328. @hours = time_entries.sum(:hours).to_f
  329. if @hours > 0
  330. case params[:todo]
  331. when 'destroy'
  332. # nothing to do
  333. when 'nullify'
  334. if Setting.timelog_required_fields.include?('issue_id')
  335. flash.now[:error] = l(:field_issue) + " " + ::I18n.t('activerecord.errors.messages.blank')
  336. return
  337. else
  338. time_entries.update_all(:issue_id => nil)
  339. end
  340. when 'reassign'
  341. reassign_to = @project && @project.issues.find_by_id(params[:reassign_to_id])
  342. if reassign_to.nil?
  343. flash.now[:error] = l(:error_issue_not_found_in_project)
  344. return
  345. elsif issues_and_descendants_ids.include?(reassign_to.id)
  346. flash.now[:error] = l(:error_cannot_reassign_time_entries_to_an_issue_about_to_be_deleted)
  347. return
  348. else
  349. time_entries.update_all(:issue_id => reassign_to.id, :project_id => reassign_to.project_id)
  350. end
  351. else
  352. # display the destroy form if it's a user request
  353. return unless api_request?
  354. end
  355. end
  356. @issues.each do |issue|
  357. begin
  358. issue.reload.destroy
  359. rescue ::ActiveRecord::RecordNotFound # raised by #reload if issue no longer exists
  360. # nothing to do, issue was already deleted (eg. by a parent)
  361. end
  362. end
  363. respond_to do |format|
  364. format.html { redirect_back_or_default _project_issues_path(@project) }
  365. format.api { render_api_ok }
  366. end
  367. end
  368. # Overrides Redmine::MenuManager::MenuController::ClassMethods for
  369. # when the "New issue" tab is enabled
  370. def current_menu_item
  371. if Setting.new_item_menu_tab == '1' && [:new, :create].include?(action_name.to_sym)
  372. :new_issue
  373. else
  374. super
  375. end
  376. end
  377. private
  378. def retrieve_previous_and_next_issue_ids
  379. if params[:prev_issue_id].present? || params[:next_issue_id].present?
  380. @prev_issue_id = params[:prev_issue_id].presence.try(:to_i)
  381. @next_issue_id = params[:next_issue_id].presence.try(:to_i)
  382. @issue_position = params[:issue_position].presence.try(:to_i)
  383. @issue_count = params[:issue_count].presence.try(:to_i)
  384. else
  385. retrieve_query_from_session
  386. if @query
  387. @per_page = per_page_option
  388. limit = 500
  389. issue_ids = @query.issue_ids(:limit => (limit + 1))
  390. if (idx = issue_ids.index(@issue.id)) && idx < limit
  391. if issue_ids.size < 500
  392. @issue_position = idx + 1
  393. @issue_count = issue_ids.size
  394. end
  395. @prev_issue_id = issue_ids[idx - 1] if idx > 0
  396. @next_issue_id = issue_ids[idx + 1] if idx < (issue_ids.size - 1)
  397. end
  398. query_params = @query.as_params
  399. if @issue_position
  400. query_params = query_params.merge(:page => (@issue_position / per_page_option) + 1, :per_page => per_page_option)
  401. end
  402. @query_path = _project_issues_path(@query.project, query_params)
  403. end
  404. end
  405. end
  406. def previous_and_next_issue_ids_params
  407. {
  408. :prev_issue_id => params[:prev_issue_id],
  409. :next_issue_id => params[:next_issue_id],
  410. :issue_position => params[:issue_position],
  411. :issue_count => params[:issue_count]
  412. }.reject {|k,v| k.blank?}
  413. end
  414. # Used by #edit and #update to set some common instance variables
  415. # from the params
  416. def update_issue_from_params
  417. @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
  418. if params[:time_entry]
  419. @time_entry.safe_attributes = params[:time_entry]
  420. end
  421. @issue.init_journal(User.current)
  422. issue_attributes = params[:issue]
  423. issue_attributes[:assigned_to_id] = User.current.id if issue_attributes && issue_attributes[:assigned_to_id] == 'me'
  424. if issue_attributes && params[:conflict_resolution]
  425. case params[:conflict_resolution]
  426. when 'overwrite'
  427. issue_attributes = issue_attributes.dup
  428. issue_attributes.delete(:lock_version)
  429. when 'add_notes'
  430. issue_attributes = issue_attributes.slice(:notes, :private_notes)
  431. when 'cancel'
  432. redirect_to issue_path(@issue)
  433. return false
  434. end
  435. end
  436. @issue.safe_attributes = issue_attributes
  437. @priorities = IssuePriority.active
  438. @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
  439. true
  440. end
  441. # Used by #new and #create to build a new issue from the params
  442. # The new issue will be copied from an existing one if copy_from parameter is given
  443. def build_new_issue_from_params
  444. @issue = Issue.new
  445. if params[:copy_from]
  446. begin
  447. @issue.init_journal(User.current)
  448. @copy_from = Issue.visible.find(params[:copy_from])
  449. unless User.current.allowed_to?(:copy_issues, @copy_from.project)
  450. raise ::Unauthorized
  451. end
  452. @link_copy = link_copy?(params[:link_copy]) || request.get?
  453. @copy_attachments = params[:copy_attachments].present? || request.get?
  454. @copy_subtasks = params[:copy_subtasks].present? || request.get?
  455. @copy_watchers = User.current.allowed_to?(:add_issue_watchers, @project)
  456. @issue.copy_from(@copy_from, :attachments => @copy_attachments, :subtasks => @copy_subtasks, :watchers => @copy_watchers, :link => @link_copy)
  457. @issue.parent_issue_id = @copy_from.parent_id
  458. rescue ActiveRecord::RecordNotFound
  459. render_404
  460. return
  461. end
  462. end
  463. @issue.project = @project
  464. if request.get?
  465. @issue.project ||= @issue.allowed_target_projects.first
  466. end
  467. @issue.author ||= User.current
  468. @issue.start_date ||= User.current.today if Setting.default_issue_start_date_to_creation_date?
  469. attrs = (params[:issue] || {}).deep_dup
  470. if action_name == 'new' && params[:was_default_status] == attrs[:status_id]
  471. attrs.delete(:status_id)
  472. end
  473. if action_name == 'new' && params[:form_update_triggered_by] == 'issue_project_id'
  474. # Discard submitted version when changing the project on the issue form
  475. # so we can use the default version for the new project
  476. attrs.delete(:fixed_version_id)
  477. end
  478. attrs[:assigned_to_id] = User.current.id if attrs[:assigned_to_id] == 'me'
  479. @issue.safe_attributes = attrs
  480. if @issue.project
  481. @issue.tracker ||= @issue.allowed_target_trackers.first
  482. if @issue.tracker.nil?
  483. if @issue.project.trackers.any?
  484. # None of the project trackers is allowed to the user
  485. render_error :message => l(:error_no_tracker_allowed_for_new_issue_in_project), :status => 403
  486. else
  487. # Project has no trackers
  488. render_error l(:error_no_tracker_in_project)
  489. end
  490. return false
  491. end
  492. if @issue.status.nil?
  493. render_error l(:error_no_default_issue_status)
  494. return false
  495. end
  496. elsif request.get?
  497. render_error :message => l(:error_no_projects_with_tracker_allowed_for_new_issue), :status => 403
  498. return false
  499. end
  500. @priorities = IssuePriority.active
  501. @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
  502. end
  503. # Saves @issue and a time_entry from the parameters
  504. def save_issue_with_child_records
  505. Issue.transaction do
  506. if params[:time_entry] && (params[:time_entry][:hours].present? || params[:time_entry][:comments].present?) && User.current.allowed_to?(:log_time, @issue.project)
  507. time_entry = @time_entry || TimeEntry.new
  508. time_entry.project = @issue.project
  509. time_entry.issue = @issue
  510. time_entry.user = User.current
  511. time_entry.spent_on = User.current.today
  512. time_entry.safe_attributes = params[:time_entry]
  513. @issue.time_entries << time_entry
  514. end
  515. call_hook(:controller_issues_edit_before_save, { :params => params, :issue => @issue, :time_entry => time_entry, :journal => @issue.current_journal})
  516. if @issue.save
  517. call_hook(:controller_issues_edit_after_save, { :params => params, :issue => @issue, :time_entry => time_entry, :journal => @issue.current_journal})
  518. else
  519. raise ActiveRecord::Rollback
  520. end
  521. end
  522. end
  523. # Returns true if the issue copy should be linked
  524. # to the original issue
  525. def link_copy?(param)
  526. case Setting.link_copied_issue
  527. when 'yes'
  528. true
  529. when 'no'
  530. false
  531. when 'ask'
  532. param == '1'
  533. end
  534. end
  535. # Redirects user after a successful issue creation
  536. def redirect_after_create
  537. if params[:continue]
  538. url_params = {}
  539. url_params[:issue] = {:tracker_id => @issue.tracker, :parent_issue_id => @issue.parent_issue_id}.reject {|k,v| v.nil?}
  540. url_params[:back_url] = params[:back_url].presence
  541. if params[:project_id]
  542. redirect_to new_project_issue_path(@issue.project, url_params)
  543. else
  544. url_params[:issue].merge! :project_id => @issue.project_id
  545. redirect_to new_issue_path(url_params)
  546. end
  547. else
  548. redirect_back_or_default issue_path(@issue)
  549. end
  550. end
  551. end