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

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