##// END OF EJS Templates
Removed dead code....
Jean-Philippe Lang -
r8831:3be511cdab00
parent child
Show More
@@ -1,550 +1,541
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require 'uri'
19 19 require 'cgi'
20 20
21 21 class Unauthorized < Exception; end
22 22
23 23 class ApplicationController < ActionController::Base
24 24 include Redmine::I18n
25 25
26 26 layout 'base'
27 27 exempt_from_layout 'builder', 'rsb'
28 28
29 29 protect_from_forgery
30 30 def handle_unverified_request
31 31 super
32 32 cookies.delete(:autologin)
33 33 end
34 34 # Remove broken cookie after upgrade from 0.8.x (#4292)
35 35 # See https://rails.lighthouseapp.com/projects/8994/tickets/3360
36 36 # TODO: remove it when Rails is fixed
37 37 before_filter :delete_broken_cookies
38 38 def delete_broken_cookies
39 39 if cookies['_redmine_session'] && cookies['_redmine_session'] !~ /--/
40 40 cookies.delete '_redmine_session'
41 41 redirect_to home_path
42 42 return false
43 43 end
44 44 end
45 45
46 46 # FIXME: Remove this when all of Rack and Rails have learned how to
47 47 # properly use encodings
48 48 before_filter :params_filter
49 49
50 50 def params_filter
51 51 if RUBY_VERSION >= '1.9' && defined?(Rails) && Rails::VERSION::MAJOR < 3
52 52 self.utf8nize!(params)
53 53 end
54 54 end
55 55
56 56 def utf8nize!(obj)
57 57 if obj.frozen?
58 58 obj
59 59 elsif obj.is_a? String
60 60 obj.respond_to?(:force_encoding) ? obj.force_encoding("UTF-8") : obj
61 61 elsif obj.is_a? Hash
62 62 obj.each {|k, v| obj[k] = self.utf8nize!(v)}
63 63 elsif obj.is_a? Array
64 64 obj.each {|v| self.utf8nize!(v)}
65 65 else
66 66 obj
67 67 end
68 68 end
69 69
70 70 before_filter :user_setup, :check_if_login_required, :set_localization
71 71 filter_parameter_logging :password
72 72
73 73 rescue_from ActionController::InvalidAuthenticityToken, :with => :invalid_authenticity_token
74 74 rescue_from ::Unauthorized, :with => :deny_access
75 75
76 76 include Redmine::Search::Controller
77 77 include Redmine::MenuManager::MenuController
78 78 helper Redmine::MenuManager::MenuHelper
79 79
80 80 Redmine::Scm::Base.all.each do |scm|
81 81 require_dependency "repository/#{scm.underscore}"
82 82 end
83 83
84 84 def user_setup
85 85 # Check the settings cache for each request
86 86 Setting.check_cache
87 87 # Find the current user
88 88 User.current = find_current_user
89 89 end
90 90
91 91 # Returns the current user or nil if no user is logged in
92 92 # and starts a session if needed
93 93 def find_current_user
94 94 if session[:user_id]
95 95 # existing session
96 96 (User.active.find(session[:user_id]) rescue nil)
97 97 elsif cookies[:autologin] && Setting.autologin?
98 98 # auto-login feature starts a new session
99 99 user = User.try_to_autologin(cookies[:autologin])
100 100 session[:user_id] = user.id if user
101 101 user
102 102 elsif params[:format] == 'atom' && params[:key] && request.get? && accept_rss_auth?
103 103 # RSS key authentication does not start a session
104 104 User.find_by_rss_key(params[:key])
105 105 elsif Setting.rest_api_enabled? && accept_api_auth?
106 106 if (key = api_key_from_request)
107 107 # Use API key
108 108 User.find_by_api_key(key)
109 109 else
110 110 # HTTP Basic, either username/password or API key/random
111 111 authenticate_with_http_basic do |username, password|
112 112 User.try_to_login(username, password) || User.find_by_api_key(username)
113 113 end
114 114 end
115 115 end
116 116 end
117 117
118 118 # Sets the logged in user
119 119 def logged_user=(user)
120 120 reset_session
121 121 if user && user.is_a?(User)
122 122 User.current = user
123 123 session[:user_id] = user.id
124 124 else
125 125 User.current = User.anonymous
126 126 end
127 127 end
128 128
129 129 # check if login is globally required to access the application
130 130 def check_if_login_required
131 131 # no check needed if user is already logged in
132 132 return true if User.current.logged?
133 133 require_login if Setting.login_required?
134 134 end
135 135
136 136 def set_localization
137 137 lang = nil
138 138 if User.current.logged?
139 139 lang = find_language(User.current.language)
140 140 end
141 141 if lang.nil? && request.env['HTTP_ACCEPT_LANGUAGE']
142 142 accept_lang = parse_qvalues(request.env['HTTP_ACCEPT_LANGUAGE']).first
143 143 if !accept_lang.blank?
144 144 accept_lang = accept_lang.downcase
145 145 lang = find_language(accept_lang) || find_language(accept_lang.split('-').first)
146 146 end
147 147 end
148 148 lang ||= Setting.default_language
149 149 set_language_if_valid(lang)
150 150 end
151 151
152 152 def require_login
153 153 if !User.current.logged?
154 154 # Extract only the basic url parameters on non-GET requests
155 155 if request.get?
156 156 url = url_for(params)
157 157 else
158 158 url = url_for(:controller => params[:controller], :action => params[:action], :id => params[:id], :project_id => params[:project_id])
159 159 end
160 160 respond_to do |format|
161 161 format.html { redirect_to :controller => "account", :action => "login", :back_url => url }
162 162 format.atom { redirect_to :controller => "account", :action => "login", :back_url => url }
163 163 format.xml { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' }
164 164 format.js { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' }
165 165 format.json { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' }
166 166 end
167 167 return false
168 168 end
169 169 true
170 170 end
171 171
172 172 def require_admin
173 173 return unless require_login
174 174 if !User.current.admin?
175 175 render_403
176 176 return false
177 177 end
178 178 true
179 179 end
180 180
181 181 def deny_access
182 182 User.current.logged? ? render_403 : require_login
183 183 end
184 184
185 185 # Authorize the user for the requested action
186 186 def authorize(ctrl = params[:controller], action = params[:action], global = false)
187 187 allowed = User.current.allowed_to?({:controller => ctrl, :action => action}, @project || @projects, :global => global)
188 188 if allowed
189 189 true
190 190 else
191 191 if @project && @project.archived?
192 192 render_403 :message => :notice_not_authorized_archived_project
193 193 else
194 194 deny_access
195 195 end
196 196 end
197 197 end
198 198
199 199 # Authorize the user for the requested action outside a project
200 200 def authorize_global(ctrl = params[:controller], action = params[:action], global = true)
201 201 authorize(ctrl, action, global)
202 202 end
203 203
204 204 # Find project of id params[:id]
205 205 def find_project
206 206 @project = Project.find(params[:id])
207 207 rescue ActiveRecord::RecordNotFound
208 208 render_404
209 209 end
210 210
211 211 # Find project of id params[:project_id]
212 212 def find_project_by_project_id
213 213 @project = Project.find(params[:project_id])
214 214 rescue ActiveRecord::RecordNotFound
215 215 render_404
216 216 end
217 217
218 218 # Find a project based on params[:project_id]
219 219 # TODO: some subclasses override this, see about merging their logic
220 220 def find_optional_project
221 221 @project = Project.find(params[:project_id]) unless params[:project_id].blank?
222 222 allowed = User.current.allowed_to?({:controller => params[:controller], :action => params[:action]}, @project, :global => true)
223 223 allowed ? true : deny_access
224 224 rescue ActiveRecord::RecordNotFound
225 225 render_404
226 226 end
227 227
228 228 # Finds and sets @project based on @object.project
229 229 def find_project_from_association
230 230 render_404 unless @object.present?
231 231
232 232 @project = @object.project
233 233 end
234 234
235 235 def find_model_object
236 236 model = self.class.read_inheritable_attribute('model_object')
237 237 if model
238 238 @object = model.find(params[:id])
239 239 self.instance_variable_set('@' + controller_name.singularize, @object) if @object
240 240 end
241 241 rescue ActiveRecord::RecordNotFound
242 242 render_404
243 243 end
244 244
245 245 def self.model_object(model)
246 246 write_inheritable_attribute('model_object', model)
247 247 end
248 248
249 249 # Filter for bulk issue operations
250 250 def find_issues
251 251 @issues = Issue.find_all_by_id(params[:id] || params[:ids])
252 252 raise ActiveRecord::RecordNotFound if @issues.empty?
253 253 if @issues.detect {|issue| !issue.visible?}
254 254 deny_access
255 255 return
256 256 end
257 257 @projects = @issues.collect(&:project).compact.uniq
258 258 @project = @projects.first if @projects.size == 1
259 259 rescue ActiveRecord::RecordNotFound
260 260 render_404
261 261 end
262 262
263 # Check if project is unique before bulk operations
264 def check_project_uniqueness
265 unless @project
266 # TODO: let users bulk edit/move/destroy issues from different projects
267 render_error 'Can not bulk edit/move/destroy issues from different projects'
268 return false
269 end
270 end
271
272 263 # make sure that the user is a member of the project (or admin) if project is private
273 264 # used as a before_filter for actions that do not require any particular permission on the project
274 265 def check_project_privacy
275 266 if @project && @project.active?
276 267 if @project.is_public? || User.current.member_of?(@project) || User.current.admin?
277 268 true
278 269 else
279 270 deny_access
280 271 end
281 272 else
282 273 @project = nil
283 274 render_404
284 275 false
285 276 end
286 277 end
287 278
288 279 def back_url
289 280 params[:back_url] || request.env['HTTP_REFERER']
290 281 end
291 282
292 283 def redirect_back_or_default(default)
293 284 back_url = CGI.unescape(params[:back_url].to_s)
294 285 if !back_url.blank?
295 286 begin
296 287 uri = URI.parse(back_url)
297 288 # do not redirect user to another host or to the login or register page
298 289 if (uri.relative? || (uri.host == request.host)) && !uri.path.match(%r{/(login|account/register)})
299 290 redirect_to(back_url)
300 291 return
301 292 end
302 293 rescue URI::InvalidURIError
303 294 # redirect to default
304 295 end
305 296 end
306 297 redirect_to default
307 298 false
308 299 end
309 300
310 301 def render_403(options={})
311 302 @project = nil
312 303 render_error({:message => :notice_not_authorized, :status => 403}.merge(options))
313 304 return false
314 305 end
315 306
316 307 def render_404(options={})
317 308 render_error({:message => :notice_file_not_found, :status => 404}.merge(options))
318 309 return false
319 310 end
320 311
321 312 # Renders an error response
322 313 def render_error(arg)
323 314 arg = {:message => arg} unless arg.is_a?(Hash)
324 315
325 316 @message = arg[:message]
326 317 @message = l(@message) if @message.is_a?(Symbol)
327 318 @status = arg[:status] || 500
328 319
329 320 respond_to do |format|
330 321 format.html {
331 322 render :template => 'common/error', :layout => use_layout, :status => @status
332 323 }
333 324 format.atom { head @status }
334 325 format.xml { head @status }
335 326 format.js { head @status }
336 327 format.json { head @status }
337 328 end
338 329 end
339 330
340 331 # Filter for actions that provide an API response
341 332 # but have no HTML representation for non admin users
342 333 def require_admin_or_api_request
343 334 return true if api_request?
344 335 if User.current.admin?
345 336 true
346 337 elsif User.current.logged?
347 338 render_error(:status => 406)
348 339 else
349 340 deny_access
350 341 end
351 342 end
352 343
353 344 # Picks which layout to use based on the request
354 345 #
355 346 # @return [boolean, string] name of the layout to use or false for no layout
356 347 def use_layout
357 348 request.xhr? ? false : 'base'
358 349 end
359 350
360 351 def invalid_authenticity_token
361 352 if api_request?
362 353 logger.error "Form authenticity token is missing or is invalid. API calls must include a proper Content-type header (text/xml or text/json)."
363 354 end
364 355 render_error "Invalid form authenticity token."
365 356 end
366 357
367 358 def render_feed(items, options={})
368 359 @items = items || []
369 360 @items.sort! {|x,y| y.event_datetime <=> x.event_datetime }
370 361 @items = @items.slice(0, Setting.feeds_limit.to_i)
371 362 @title = options[:title] || Setting.app_title
372 363 render :template => "common/feed.atom", :layout => false,
373 364 :content_type => 'application/atom+xml'
374 365 end
375 366
376 367 # TODO: remove in Redmine 1.4
377 368 def self.accept_key_auth(*actions)
378 369 ActiveSupport::Deprecation.warn "ApplicationController.accept_key_auth is deprecated and will be removed in Redmine 1.4. Use accept_rss_auth (or accept_api_auth) instead."
379 370 accept_rss_auth(*actions)
380 371 end
381 372
382 373 # TODO: remove in Redmine 1.4
383 374 def accept_key_auth_actions
384 375 ActiveSupport::Deprecation.warn "ApplicationController.accept_key_auth_actions is deprecated and will be removed in Redmine 1.4. Use accept_rss_auth (or accept_api_auth) instead."
385 376 self.class.accept_rss_auth
386 377 end
387 378
388 379 def self.accept_rss_auth(*actions)
389 380 if actions.any?
390 381 write_inheritable_attribute('accept_rss_auth_actions', actions)
391 382 else
392 383 read_inheritable_attribute('accept_rss_auth_actions') || []
393 384 end
394 385 end
395 386
396 387 def accept_rss_auth?(action=action_name)
397 388 self.class.accept_rss_auth.include?(action.to_sym)
398 389 end
399 390
400 391 def self.accept_api_auth(*actions)
401 392 if actions.any?
402 393 write_inheritable_attribute('accept_api_auth_actions', actions)
403 394 else
404 395 read_inheritable_attribute('accept_api_auth_actions') || []
405 396 end
406 397 end
407 398
408 399 def accept_api_auth?(action=action_name)
409 400 self.class.accept_api_auth.include?(action.to_sym)
410 401 end
411 402
412 403 # Returns the number of objects that should be displayed
413 404 # on the paginated list
414 405 def per_page_option
415 406 per_page = nil
416 407 if params[:per_page] && Setting.per_page_options_array.include?(params[:per_page].to_s.to_i)
417 408 per_page = params[:per_page].to_s.to_i
418 409 session[:per_page] = per_page
419 410 elsif session[:per_page]
420 411 per_page = session[:per_page]
421 412 else
422 413 per_page = Setting.per_page_options_array.first || 25
423 414 end
424 415 per_page
425 416 end
426 417
427 418 # Returns offset and limit used to retrieve objects
428 419 # for an API response based on offset, limit and page parameters
429 420 def api_offset_and_limit(options=params)
430 421 if options[:offset].present?
431 422 offset = options[:offset].to_i
432 423 if offset < 0
433 424 offset = 0
434 425 end
435 426 end
436 427 limit = options[:limit].to_i
437 428 if limit < 1
438 429 limit = 25
439 430 elsif limit > 100
440 431 limit = 100
441 432 end
442 433 if offset.nil? && options[:page].present?
443 434 offset = (options[:page].to_i - 1) * limit
444 435 offset = 0 if offset < 0
445 436 end
446 437 offset ||= 0
447 438
448 439 [offset, limit]
449 440 end
450 441
451 442 # qvalues http header parser
452 443 # code taken from webrick
453 444 def parse_qvalues(value)
454 445 tmp = []
455 446 if value
456 447 parts = value.split(/,\s*/)
457 448 parts.each {|part|
458 449 if m = %r{^([^\s,]+?)(?:;\s*q=(\d+(?:\.\d+)?))?$}.match(part)
459 450 val = m[1]
460 451 q = (m[2] or 1).to_f
461 452 tmp.push([val, q])
462 453 end
463 454 }
464 455 tmp = tmp.sort_by{|val, q| -q}
465 456 tmp.collect!{|val, q| val}
466 457 end
467 458 return tmp
468 459 rescue
469 460 nil
470 461 end
471 462
472 463 # Returns a string that can be used as filename value in Content-Disposition header
473 464 def filename_for_content_disposition(name)
474 465 request.env['HTTP_USER_AGENT'] =~ %r{MSIE} ? ERB::Util.url_encode(name) : name
475 466 end
476 467
477 468 def api_request?
478 469 %w(xml json).include? params[:format]
479 470 end
480 471
481 472 # Returns the API key present in the request
482 473 def api_key_from_request
483 474 if params[:key].present?
484 475 params[:key]
485 476 elsif request.headers["X-Redmine-API-Key"].present?
486 477 request.headers["X-Redmine-API-Key"]
487 478 end
488 479 end
489 480
490 481 # Renders a warning flash if obj has unsaved attachments
491 482 def render_attachment_warning_if_needed(obj)
492 483 flash[:warning] = l(:warning_attachments_not_saved, obj.unsaved_attachments.size) if obj.unsaved_attachments.present?
493 484 end
494 485
495 486 # Sets the `flash` notice or error based the number of issues that did not save
496 487 #
497 488 # @param [Array, Issue] issues all of the saved and unsaved Issues
498 489 # @param [Array, Integer] unsaved_issue_ids the issue ids that were not saved
499 490 def set_flash_from_bulk_issue_save(issues, unsaved_issue_ids)
500 491 if unsaved_issue_ids.empty?
501 492 flash[:notice] = l(:notice_successful_update) unless issues.empty?
502 493 else
503 494 flash[:error] = l(:notice_failed_to_save_issues,
504 495 :count => unsaved_issue_ids.size,
505 496 :total => issues.size,
506 497 :ids => '#' + unsaved_issue_ids.join(', #'))
507 498 end
508 499 end
509 500
510 501 # Rescues an invalid query statement. Just in case...
511 502 def query_statement_invalid(exception)
512 503 logger.error "Query::StatementInvalid: #{exception.message}" if logger
513 504 session.delete(:query)
514 505 sort_clear if respond_to?(:sort_clear)
515 506 render_error "An error occurred while executing the query and has been logged. Please report this error to your Redmine administrator."
516 507 end
517 508
518 509 # Renders API response on validation failure
519 510 def render_validation_errors(object)
520 511 options = { :status => :unprocessable_entity, :layout => false }
521 512 options.merge!(case params[:format]
522 513 when 'xml'; { :xml => object.errors }
523 514 when 'json'; { :json => {'errors' => object.errors} } # ActiveResource client compliance
524 515 else
525 516 raise "Unknown format #{params[:format]} in #render_validation_errors"
526 517 end
527 518 )
528 519 render options
529 520 end
530 521
531 522 # Overrides #default_template so that the api template
532 523 # is used automatically if it exists
533 524 def default_template(action_name = self.action_name)
534 525 if api_request?
535 526 begin
536 527 return self.view_paths.find_template(default_template_name(action_name), 'api')
537 528 rescue ::ActionView::MissingTemplate
538 529 # the api template was not found
539 530 # fallback to the default behaviour
540 531 end
541 532 end
542 533 super
543 534 end
544 535
545 536 # Overrides #pick_layout so that #render with no arguments
546 537 # doesn't use the layout for api requests
547 538 def pick_layout(*args)
548 539 api_request? ? nil : super
549 540 end
550 541 end
@@ -1,429 +1,428
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 class IssuesController < ApplicationController
19 19 menu_item :new_issue, :only => [:new, :create]
20 20 default_search_scope :issues
21 21
22 22 before_filter :find_issue, :only => [:show, :edit, :update]
23 before_filter :find_issues, :only => [:bulk_edit, :bulk_update, :move, :perform_move, :destroy]
24 before_filter :check_project_uniqueness, :only => [:move, :perform_move]
23 before_filter :find_issues, :only => [:bulk_edit, :bulk_update, :destroy]
25 24 before_filter :find_project, :only => [:new, :create]
26 25 before_filter :authorize, :except => [:index]
27 26 before_filter :find_optional_project, :only => [:index]
28 27 before_filter :check_for_default_issue_status, :only => [:new, :create]
29 28 before_filter :build_new_issue_from_params, :only => [:new, :create]
30 29 accept_rss_auth :index, :show
31 30 accept_api_auth :index, :show, :create, :update, :destroy
32 31
33 32 rescue_from Query::StatementInvalid, :with => :query_statement_invalid
34 33
35 34 helper :journals
36 35 helper :projects
37 36 include ProjectsHelper
38 37 helper :custom_fields
39 38 include CustomFieldsHelper
40 39 helper :issue_relations
41 40 include IssueRelationsHelper
42 41 helper :watchers
43 42 include WatchersHelper
44 43 helper :attachments
45 44 include AttachmentsHelper
46 45 helper :queries
47 46 include QueriesHelper
48 47 helper :repositories
49 48 include RepositoriesHelper
50 49 helper :sort
51 50 include SortHelper
52 51 include IssuesHelper
53 52 helper :timelog
54 53 helper :gantt
55 54 include Redmine::Export::PDF
56 55
57 56 verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed }
58 57 verify :method => :post, :only => :bulk_update, :render => {:nothing => true, :status => :method_not_allowed }
59 58 verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
60 59
61 60 def index
62 61 retrieve_query
63 62 sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
64 63 sort_update(@query.sortable_columns)
65 64
66 65 if @query.valid?
67 66 case params[:format]
68 67 when 'csv', 'pdf'
69 68 @limit = Setting.issues_export_limit.to_i
70 69 when 'atom'
71 70 @limit = Setting.feeds_limit.to_i
72 71 when 'xml', 'json'
73 72 @offset, @limit = api_offset_and_limit
74 73 else
75 74 @limit = per_page_option
76 75 end
77 76
78 77 @issue_count = @query.issue_count
79 78 @issue_pages = Paginator.new self, @issue_count, @limit, params['page']
80 79 @offset ||= @issue_pages.current.offset
81 80 @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version],
82 81 :order => sort_clause,
83 82 :offset => @offset,
84 83 :limit => @limit)
85 84 @issue_count_by_group = @query.issue_count_by_group
86 85
87 86 respond_to do |format|
88 87 format.html { render :template => 'issues/index', :layout => !request.xhr? }
89 88 format.api {
90 89 Issue.load_relations(@issues) if include_in_api_response?('relations')
91 90 }
92 91 format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") }
93 92 format.csv { send_data(issues_to_csv(@issues, @project, @query, params), :type => 'text/csv; header=present', :filename => 'export.csv') }
94 93 format.pdf { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'export.pdf') }
95 94 end
96 95 else
97 96 respond_to do |format|
98 97 format.html { render(:template => 'issues/index', :layout => !request.xhr?) }
99 98 format.any(:atom, :csv, :pdf) { render(:nothing => true) }
100 99 format.api { render_validation_errors(@query) }
101 100 end
102 101 end
103 102 rescue ActiveRecord::RecordNotFound
104 103 render_404
105 104 end
106 105
107 106 def show
108 107 @journals = @issue.journals.find(:all, :include => [:user, :details], :order => "#{Journal.table_name}.created_on ASC")
109 108 @journals.each_with_index {|j,i| j.indice = i+1}
110 109 @journals.reverse! if User.current.wants_comments_in_reverse_order?
111 110
112 111 @changesets = @issue.changesets.visible.all
113 112 @changesets.reverse! if User.current.wants_comments_in_reverse_order?
114 113
115 114 @relations = @issue.relations.select {|r| r.other_issue(@issue) && r.other_issue(@issue).visible? }
116 115 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
117 116 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
118 117 @priorities = IssuePriority.active
119 118 @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
120 119 respond_to do |format|
121 120 format.html {
122 121 retrieve_previous_and_next_issue_ids
123 122 render :template => 'issues/show'
124 123 }
125 124 format.api
126 125 format.atom { render :template => 'journals/index', :layout => false, :content_type => 'application/atom+xml' }
127 126 format.pdf { send_data(issue_to_pdf(@issue), :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf") }
128 127 end
129 128 end
130 129
131 130 # Add a new issue
132 131 # The new issue will be created from an existing one if copy_from parameter is given
133 132 def new
134 133 respond_to do |format|
135 134 format.html { render :action => 'new', :layout => !request.xhr? }
136 135 format.js {
137 136 render(:update) { |page|
138 137 if params[:project_change]
139 138 page.replace_html 'all_attributes', :partial => 'form'
140 139 else
141 140 page.replace_html 'attributes', :partial => 'attributes'
142 141 end
143 142 m = User.current.allowed_to?(:log_time, @issue.project) ? 'show' : 'hide'
144 143 page << "if ($('log_time')) {Element.#{m}('log_time');}"
145 144 }
146 145 }
147 146 end
148 147 end
149 148
150 149 def create
151 150 call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue })
152 151 @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads]))
153 152 if @issue.save
154 153 call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue})
155 154 respond_to do |format|
156 155 format.html {
157 156 render_attachment_warning_if_needed(@issue)
158 157 flash[:notice] = l(:notice_issue_successful_create, :id => "<a href='#{issue_path(@issue)}'>##{@issue.id}</a>")
159 158 redirect_to(params[:continue] ? { :action => 'new', :project_id => @issue.project, :issue => {:tracker_id => @issue.tracker, :parent_issue_id => @issue.parent_issue_id}.reject {|k,v| v.nil?} } :
160 159 { :action => 'show', :id => @issue })
161 160 }
162 161 format.api { render :action => 'show', :status => :created, :location => issue_url(@issue) }
163 162 end
164 163 return
165 164 else
166 165 respond_to do |format|
167 166 format.html { render :action => 'new' }
168 167 format.api { render_validation_errors(@issue) }
169 168 end
170 169 end
171 170 end
172 171
173 172 def edit
174 173 return unless update_issue_from_params
175 174
176 175 respond_to do |format|
177 176 format.html { }
178 177 format.xml { }
179 178 end
180 179 end
181 180
182 181 def update
183 182 return unless update_issue_from_params
184 183 @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads]))
185 184 saved = false
186 185 begin
187 186 saved = @issue.save_issue_with_child_records(params, @time_entry)
188 187 rescue ActiveRecord::StaleObjectError
189 188 @conflict = true
190 189 if params[:last_journal_id]
191 190 if params[:last_journal_id].present?
192 191 last_journal_id = params[:last_journal_id].to_i
193 192 @conflict_journals = @issue.journals.all(:conditions => ["#{Journal.table_name}.id > ?", last_journal_id])
194 193 else
195 194 @conflict_journals = @issue.journals.all
196 195 end
197 196 end
198 197 end
199 198
200 199 if saved
201 200 render_attachment_warning_if_needed(@issue)
202 201 flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
203 202
204 203 respond_to do |format|
205 204 format.html { redirect_back_or_default({:action => 'show', :id => @issue}) }
206 205 format.api { head :ok }
207 206 end
208 207 else
209 208 respond_to do |format|
210 209 format.html { render :action => 'edit' }
211 210 format.api { render_validation_errors(@issue) }
212 211 end
213 212 end
214 213 end
215 214
216 215 # Bulk edit/copy a set of issues
217 216 def bulk_edit
218 217 @issues.sort!
219 218 @copy = params[:copy].present?
220 219 @notes = params[:notes]
221 220
222 221 if User.current.allowed_to?(:move_issues, @projects)
223 222 @allowed_projects = Issue.allowed_target_projects_on_move
224 223 if params[:issue]
225 224 @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:issue][:project_id]}
226 225 if @target_project
227 226 target_projects = [@target_project]
228 227 end
229 228 end
230 229 end
231 230 target_projects ||= @projects
232 231
233 232 @available_statuses = @issues.map(&:new_statuses_allowed_to).reduce(:&)
234 233 @custom_fields = target_projects.map{|p|p.all_issue_custom_fields}.reduce(:&)
235 234 @assignables = target_projects.map(&:assignable_users).reduce(:&)
236 235 @trackers = target_projects.map(&:trackers).reduce(:&)
237 236
238 237 @safe_attributes = @issues.map(&:safe_attribute_names).reduce(:&)
239 238 render :layout => false if request.xhr?
240 239 end
241 240
242 241 def bulk_update
243 242 @issues.sort!
244 243 @copy = params[:copy].present?
245 244 attributes = parse_params_for_bulk_issue_attributes(params)
246 245
247 246 unsaved_issue_ids = []
248 247 moved_issues = []
249 248 @issues.each do |issue|
250 249 issue.reload
251 250 if @copy
252 251 issue = issue.copy
253 252 end
254 253 journal = issue.init_journal(User.current, params[:notes])
255 254 issue.safe_attributes = attributes
256 255 call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue })
257 256 if issue.save
258 257 moved_issues << issue
259 258 else
260 259 # Keep unsaved issue ids to display them in flash error
261 260 unsaved_issue_ids << issue.id
262 261 end
263 262 end
264 263 set_flash_from_bulk_issue_save(@issues, unsaved_issue_ids)
265 264
266 265 if params[:follow]
267 266 if @issues.size == 1 && moved_issues.size == 1
268 267 redirect_to :controller => 'issues', :action => 'show', :id => moved_issues.first
269 268 elsif moved_issues.map(&:project).uniq.size == 1
270 269 redirect_to :controller => 'issues', :action => 'index', :project_id => moved_issues.map(&:project).first
271 270 end
272 271 else
273 272 redirect_back_or_default({:controller => 'issues', :action => 'index', :project_id => @project})
274 273 end
275 274 end
276 275
277 276 verify :method => :delete, :only => :destroy, :render => { :nothing => true, :status => :method_not_allowed }
278 277 def destroy
279 278 @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f
280 279 if @hours > 0
281 280 case params[:todo]
282 281 when 'destroy'
283 282 # nothing to do
284 283 when 'nullify'
285 284 TimeEntry.update_all('issue_id = NULL', ['issue_id IN (?)', @issues])
286 285 when 'reassign'
287 286 reassign_to = @project.issues.find_by_id(params[:reassign_to_id])
288 287 if reassign_to.nil?
289 288 flash.now[:error] = l(:error_issue_not_found_in_project)
290 289 return
291 290 else
292 291 TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues])
293 292 end
294 293 else
295 294 # display the destroy form if it's a user request
296 295 return unless api_request?
297 296 end
298 297 end
299 298 @issues.each do |issue|
300 299 begin
301 300 issue.reload.destroy
302 301 rescue ::ActiveRecord::RecordNotFound # raised by #reload if issue no longer exists
303 302 # nothing to do, issue was already deleted (eg. by a parent)
304 303 end
305 304 end
306 305 respond_to do |format|
307 306 format.html { redirect_back_or_default(:action => 'index', :project_id => @project) }
308 307 format.api { head :ok }
309 308 end
310 309 end
311 310
312 311 private
313 312 def find_issue
314 313 # Issue.visible.find(...) can not be used to redirect user to the login form
315 314 # if the issue actually exists but requires authentication
316 315 @issue = Issue.find(params[:id], :include => [:project, :tracker, :status, :author, :priority, :category])
317 316 unless @issue.visible?
318 317 deny_access
319 318 return
320 319 end
321 320 @project = @issue.project
322 321 rescue ActiveRecord::RecordNotFound
323 322 render_404
324 323 end
325 324
326 325 def find_project
327 326 project_id = params[:project_id] || (params[:issue] && params[:issue][:project_id])
328 327 @project = Project.find(project_id)
329 328 rescue ActiveRecord::RecordNotFound
330 329 render_404
331 330 end
332 331
333 332 def retrieve_previous_and_next_issue_ids
334 333 retrieve_query_from_session
335 334 if @query
336 335 sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
337 336 sort_update(@query.sortable_columns, 'issues_index_sort')
338 337 limit = 500
339 338 issue_ids = @query.issue_ids(:order => sort_clause, :limit => (limit + 1), :include => [:assigned_to, :tracker, :priority, :category, :fixed_version])
340 339 if (idx = issue_ids.index(@issue.id)) && idx < limit
341 340 if issue_ids.size < 500
342 341 @issue_position = idx + 1
343 342 @issue_count = issue_ids.size
344 343 end
345 344 @prev_issue_id = issue_ids[idx - 1] if idx > 0
346 345 @next_issue_id = issue_ids[idx + 1] if idx < (issue_ids.size - 1)
347 346 end
348 347 end
349 348 end
350 349
351 350 # Used by #edit and #update to set some common instance variables
352 351 # from the params
353 352 # TODO: Refactor, not everything in here is needed by #edit
354 353 def update_issue_from_params
355 354 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
356 355 @priorities = IssuePriority.active
357 356 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
358 357 @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
359 358 @time_entry.attributes = params[:time_entry]
360 359
361 360 @notes = params[:notes] || (params[:issue].present? ? params[:issue][:notes] : nil)
362 361 @issue.init_journal(User.current, @notes)
363 362
364 363 issue_attributes = params[:issue]
365 364 if issue_attributes && params[:conflict_resolution]
366 365 case params[:conflict_resolution]
367 366 when 'overwrite'
368 367 issue_attributes = issue_attributes.dup
369 368 issue_attributes.delete(:lock_version)
370 369 when 'add_notes'
371 370 issue_attributes = {}
372 371 when 'cancel'
373 372 redirect_to issue_path(@issue)
374 373 return false
375 374 end
376 375 end
377 376 @issue.safe_attributes = issue_attributes
378 377 true
379 378 end
380 379
381 380 # TODO: Refactor, lots of extra code in here
382 381 # TODO: Changing tracker on an existing issue should not trigger this
383 382 def build_new_issue_from_params
384 383 if params[:id].blank?
385 384 @issue = Issue.new
386 385 if params[:copy_from]
387 386 begin
388 387 @copy_from = Issue.visible.find(params[:copy_from])
389 388 @copy_attachments = params[:copy_attachments].present? || request.get?
390 389 @issue.copy_from(@copy_from, :attachments => @copy_attachments)
391 390 rescue ActiveRecord::RecordNotFound
392 391 render_404
393 392 return
394 393 end
395 394 end
396 395 @issue.project = @project
397 396 else
398 397 @issue = @project.issues.visible.find(params[:id])
399 398 end
400 399
401 400 @issue.project = @project
402 401 @issue.author = User.current
403 402 # Tracker must be set before custom field values
404 403 @issue.tracker ||= @project.trackers.find((params[:issue] && params[:issue][:tracker_id]) || params[:tracker_id] || :first)
405 404 if @issue.tracker.nil?
406 405 render_error l(:error_no_tracker_in_project)
407 406 return false
408 407 end
409 408 @issue.start_date ||= Date.today if Setting.default_issue_start_date_to_creation_date?
410 409 @issue.safe_attributes = params[:issue]
411 410
412 411 @priorities = IssuePriority.active
413 412 @allowed_statuses = @issue.new_statuses_allowed_to(User.current, true)
414 413 end
415 414
416 415 def check_for_default_issue_status
417 416 if IssueStatus.default.nil?
418 417 render_error l(:error_no_default_issue_status)
419 418 return false
420 419 end
421 420 end
422 421
423 422 def parse_params_for_bulk_issue_attributes(params)
424 423 attributes = (params[:issue] || {}).reject {|k,v| v.blank?}
425 424 attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'}
426 425 attributes[:custom_field_values].reject! {|k,v| v.blank?} if attributes[:custom_field_values]
427 426 attributes
428 427 end
429 428 end
General Comments 0
You need to be logged in to leave comments. Login now