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 20KB

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