Mercurial > hg > soundsoftware-site
comparison app/controllers/.svn/text-base/application_controller.rb.svn-base @ 119:8661b858af72
* Update to Redmine trunk rev 4705
author | Chris Cannam |
---|---|
date | Thu, 13 Jan 2011 14:12:06 +0000 |
parents | 94944d00e43c |
children | cbce1fd3b1b7 |
comparison
equal
deleted
inserted
replaced
39:150ceac17a8d | 119:8661b858af72 |
---|---|
20 | 20 |
21 class ApplicationController < ActionController::Base | 21 class ApplicationController < ActionController::Base |
22 include Redmine::I18n | 22 include Redmine::I18n |
23 | 23 |
24 layout 'base' | 24 layout 'base' |
25 exempt_from_layout 'builder' | 25 exempt_from_layout 'builder', 'rsb' |
26 | 26 |
27 # Remove broken cookie after upgrade from 0.8.x (#4292) | 27 # Remove broken cookie after upgrade from 0.8.x (#4292) |
28 # See https://rails.lighthouseapp.com/projects/8994/tickets/3360 | 28 # See https://rails.lighthouseapp.com/projects/8994/tickets/3360 |
29 # TODO: remove it when Rails is fixed | 29 # TODO: remove it when Rails is fixed |
30 before_filter :delete_broken_cookies | 30 before_filter :delete_broken_cookies |
69 session[:user_id] = user.id if user | 69 session[:user_id] = user.id if user |
70 user | 70 user |
71 elsif params[:format] == 'atom' && params[:key] && accept_key_auth_actions.include?(params[:action]) | 71 elsif params[:format] == 'atom' && params[:key] && accept_key_auth_actions.include?(params[:action]) |
72 # RSS key authentication does not start a session | 72 # RSS key authentication does not start a session |
73 User.find_by_rss_key(params[:key]) | 73 User.find_by_rss_key(params[:key]) |
74 elsif Setting.rest_api_enabled? && ['xml', 'json'].include?(params[:format]) | 74 elsif Setting.rest_api_enabled? && api_request? |
75 if params[:key].present? && accept_key_auth_actions.include?(params[:action]) | 75 if (key = api_key_from_request) && accept_key_auth_actions.include?(params[:action]) |
76 # Use API key | 76 # Use API key |
77 User.find_by_api_key(params[:key]) | 77 User.find_by_api_key(key) |
78 else | 78 else |
79 # HTTP Basic, either username/password or API key/random | 79 # HTTP Basic, either username/password or API key/random |
80 authenticate_with_http_basic do |username, password| | 80 authenticate_with_http_basic do |username, password| |
81 User.try_to_login(username, password) || User.find_by_api_key(username) | 81 User.try_to_login(username, password) || User.find_by_api_key(username) |
82 end | 82 end |
347 per_page = Setting.per_page_options_array.first || 25 | 347 per_page = Setting.per_page_options_array.first || 25 |
348 end | 348 end |
349 per_page | 349 per_page |
350 end | 350 end |
351 | 351 |
352 # Returns offset and limit used to retrieve objects | |
353 # for an API response based on offset, limit and page parameters | |
354 def api_offset_and_limit(options=params) | |
355 if options[:offset].present? | |
356 offset = options[:offset].to_i | |
357 if offset < 0 | |
358 offset = 0 | |
359 end | |
360 end | |
361 limit = options[:limit].to_i | |
362 if limit < 1 | |
363 limit = 25 | |
364 elsif limit > 100 | |
365 limit = 100 | |
366 end | |
367 if offset.nil? && options[:page].present? | |
368 offset = (options[:page].to_i - 1) * limit | |
369 offset = 0 if offset < 0 | |
370 end | |
371 offset ||= 0 | |
372 | |
373 [offset, limit] | |
374 end | |
375 | |
352 # qvalues http header parser | 376 # qvalues http header parser |
353 # code taken from webrick | 377 # code taken from webrick |
354 def parse_qvalues(value) | 378 def parse_qvalues(value) |
355 tmp = [] | 379 tmp = [] |
356 if value | 380 if value |
376 end | 400 end |
377 | 401 |
378 def api_request? | 402 def api_request? |
379 %w(xml json).include? params[:format] | 403 %w(xml json).include? params[:format] |
380 end | 404 end |
405 | |
406 # Returns the API key present in the request | |
407 def api_key_from_request | |
408 if params[:key].present? | |
409 params[:key] | |
410 elsif request.headers["X-Redmine-API-Key"].present? | |
411 request.headers["X-Redmine-API-Key"] | |
412 end | |
413 end | |
381 | 414 |
382 # Renders a warning flash if obj has unsaved attachments | 415 # Renders a warning flash if obj has unsaved attachments |
383 def render_attachment_warning_if_needed(obj) | 416 def render_attachment_warning_if_needed(obj) |
384 flash[:warning] = l(:warning_attachments_not_saved, obj.unsaved_attachments.size) if obj.unsaved_attachments.present? | 417 flash[:warning] = l(:warning_attachments_not_saved, obj.unsaved_attachments.size) if obj.unsaved_attachments.present? |
385 end | 418 end |
411 def object_errors_to_json(object) | 444 def object_errors_to_json(object) |
412 object.errors.collect do |attribute, error| | 445 object.errors.collect do |attribute, error| |
413 { attribute => error } | 446 { attribute => error } |
414 end.to_json | 447 end.to_json |
415 end | 448 end |
416 | 449 |
450 # Renders API response on validation failure | |
451 def render_validation_errors(object) | |
452 options = { :status => :unprocessable_entity, :layout => false } | |
453 options.merge!(case params[:format] | |
454 when 'xml'; { :xml => object.errors } | |
455 when 'json'; { :json => {'errors' => object.errors} } # ActiveResource client compliance | |
456 else | |
457 raise "Unknown format #{params[:format]} in #render_validation_errors" | |
458 end | |
459 ) | |
460 render options | |
461 end | |
462 | |
463 # Overrides #default_template so that the api template | |
464 # is used automatically if it exists | |
465 def default_template(action_name = self.action_name) | |
466 if api_request? | |
467 begin | |
468 return self.view_paths.find_template(default_template_name(action_name), 'api') | |
469 rescue ::ActionView::MissingTemplate | |
470 # the api template was not found | |
471 # fallback to the default behaviour | |
472 end | |
473 end | |
474 super | |
475 end | |
476 | |
477 # Overrides #pick_layout so that #render with no arguments | |
478 # doesn't use the layout for api requests | |
479 def pick_layout(*args) | |
480 api_request? ? nil : super | |
481 end | |
417 end | 482 end |