annotate .svn/pristine/31/3190ee598f70a34c3d3f93dfd554a0ad83b8e462.svn-base @ 1524:82fac3dcf466 redmine-2.5-integration

Fix failure to interpret Javascript when autocompleting members for project
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Thu, 11 Sep 2014 10:24:38 +0100
parents b450a9d58aed
children
rev   line source
Chris@1516 1 # Redmine - project management software
Chris@1516 2 # Copyright (C) 2006-2014 Jean-Philippe Lang
Chris@1516 3 #
Chris@1516 4 # This program is free software; you can redistribute it and/or
Chris@1516 5 # modify it under the terms of the GNU General Public License
Chris@1516 6 # as published by the Free Software Foundation; either version 2
Chris@1516 7 # of the License, or (at your option) any later version.
Chris@1516 8 #
Chris@1516 9 # This program is distributed in the hope that it will be useful,
Chris@1516 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1516 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1516 12 # GNU General Public License for more details.
Chris@1516 13 #
Chris@1516 14 # You should have received a copy of the GNU General Public License
Chris@1516 15 # along with this program; if not, write to the Free Software
Chris@1516 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1516 17
Chris@1516 18 require 'uri'
Chris@1516 19 require 'cgi'
Chris@1516 20
Chris@1516 21 class Unauthorized < Exception; end
Chris@1516 22
Chris@1516 23 class ApplicationController < ActionController::Base
Chris@1516 24 include Redmine::I18n
Chris@1516 25 include Redmine::Pagination
Chris@1516 26 include RoutesHelper
Chris@1516 27 helper :routes
Chris@1516 28
Chris@1516 29 class_attribute :accept_api_auth_actions
Chris@1516 30 class_attribute :accept_rss_auth_actions
Chris@1516 31 class_attribute :model_object
Chris@1516 32
Chris@1516 33 layout 'base'
Chris@1516 34
Chris@1516 35 protect_from_forgery
Chris@1516 36
Chris@1516 37 def verify_authenticity_token
Chris@1516 38 unless api_request?
Chris@1516 39 super
Chris@1516 40 end
Chris@1516 41 end
Chris@1516 42
Chris@1516 43 def handle_unverified_request
Chris@1516 44 unless api_request?
Chris@1516 45 super
Chris@1516 46 cookies.delete(autologin_cookie_name)
Chris@1516 47 self.logged_user = nil
Chris@1516 48 render_error :status => 422, :message => "Invalid form authenticity token."
Chris@1516 49 end
Chris@1516 50 end
Chris@1516 51
Chris@1516 52 before_filter :session_expiration, :user_setup, :check_if_login_required, :check_password_change, :set_localization
Chris@1516 53
Chris@1516 54 rescue_from ::Unauthorized, :with => :deny_access
Chris@1516 55 rescue_from ::ActionView::MissingTemplate, :with => :missing_template
Chris@1516 56
Chris@1516 57 include Redmine::Search::Controller
Chris@1516 58 include Redmine::MenuManager::MenuController
Chris@1516 59 helper Redmine::MenuManager::MenuHelper
Chris@1516 60
Chris@1516 61 def session_expiration
Chris@1516 62 if session[:user_id]
Chris@1516 63 if session_expired? && !try_to_autologin
Chris@1516 64 reset_session
Chris@1516 65 flash[:error] = l(:error_session_expired)
Chris@1516 66 redirect_to signin_url
Chris@1516 67 else
Chris@1516 68 session[:atime] = Time.now.utc.to_i
Chris@1516 69 end
Chris@1516 70 end
Chris@1516 71 end
Chris@1516 72
Chris@1516 73 def session_expired?
Chris@1516 74 if Setting.session_lifetime?
Chris@1516 75 unless session[:ctime] && (Time.now.utc.to_i - session[:ctime].to_i <= Setting.session_lifetime.to_i * 60)
Chris@1516 76 return true
Chris@1516 77 end
Chris@1516 78 end
Chris@1516 79 if Setting.session_timeout?
Chris@1516 80 unless session[:atime] && (Time.now.utc.to_i - session[:atime].to_i <= Setting.session_timeout.to_i * 60)
Chris@1516 81 return true
Chris@1516 82 end
Chris@1516 83 end
Chris@1516 84 false
Chris@1516 85 end
Chris@1516 86
Chris@1516 87 def start_user_session(user)
Chris@1516 88 session[:user_id] = user.id
Chris@1516 89 session[:ctime] = Time.now.utc.to_i
Chris@1516 90 session[:atime] = Time.now.utc.to_i
Chris@1516 91 if user.must_change_password?
Chris@1516 92 session[:pwd] = '1'
Chris@1516 93 end
Chris@1516 94 end
Chris@1516 95
Chris@1516 96 def user_setup
Chris@1516 97 # Check the settings cache for each request
Chris@1516 98 Setting.check_cache
Chris@1516 99 # Find the current user
Chris@1516 100 User.current = find_current_user
Chris@1516 101 logger.info(" Current user: " + (User.current.logged? ? "#{User.current.login} (id=#{User.current.id})" : "anonymous")) if logger
Chris@1516 102 end
Chris@1516 103
Chris@1516 104 # Returns the current user or nil if no user is logged in
Chris@1516 105 # and starts a session if needed
Chris@1516 106 def find_current_user
Chris@1516 107 user = nil
Chris@1516 108 unless api_request?
Chris@1516 109 if session[:user_id]
Chris@1516 110 # existing session
Chris@1516 111 user = (User.active.find(session[:user_id]) rescue nil)
Chris@1516 112 elsif autologin_user = try_to_autologin
Chris@1516 113 user = autologin_user
Chris@1516 114 elsif params[:format] == 'atom' && params[:key] && request.get? && accept_rss_auth?
Chris@1516 115 # RSS key authentication does not start a session
Chris@1516 116 user = User.find_by_rss_key(params[:key])
Chris@1516 117 end
Chris@1516 118 end
Chris@1516 119 if user.nil? && Setting.rest_api_enabled? && accept_api_auth?
Chris@1516 120 if (key = api_key_from_request)
Chris@1516 121 # Use API key
Chris@1516 122 user = User.find_by_api_key(key)
Chris@1516 123 else
Chris@1516 124 # HTTP Basic, either username/password or API key/random
Chris@1516 125 authenticate_with_http_basic do |username, password|
Chris@1516 126 user = User.try_to_login(username, password) || User.find_by_api_key(username)
Chris@1516 127 end
Chris@1516 128 if user && user.must_change_password?
Chris@1516 129 render_error :message => 'You must change your password', :status => 403
Chris@1516 130 return
Chris@1516 131 end
Chris@1516 132 end
Chris@1516 133 # Switch user if requested by an admin user
Chris@1516 134 if user && user.admin? && (username = api_switch_user_from_request)
Chris@1516 135 su = User.find_by_login(username)
Chris@1516 136 if su && su.active?
Chris@1516 137 logger.info(" User switched by: #{user.login} (id=#{user.id})") if logger
Chris@1516 138 user = su
Chris@1516 139 else
Chris@1516 140 render_error :message => 'Invalid X-Redmine-Switch-User header', :status => 412
Chris@1516 141 end
Chris@1516 142 end
Chris@1516 143 end
Chris@1516 144 user
Chris@1516 145 end
Chris@1516 146
Chris@1516 147 def autologin_cookie_name
Chris@1516 148 Redmine::Configuration['autologin_cookie_name'].presence || 'autologin'
Chris@1516 149 end
Chris@1516 150
Chris@1516 151 def try_to_autologin
Chris@1516 152 if cookies[autologin_cookie_name] && Setting.autologin?
Chris@1516 153 # auto-login feature starts a new session
Chris@1516 154 user = User.try_to_autologin(cookies[autologin_cookie_name])
Chris@1516 155 if user
Chris@1516 156 reset_session
Chris@1516 157 start_user_session(user)
Chris@1516 158 end
Chris@1516 159 user
Chris@1516 160 end
Chris@1516 161 end
Chris@1516 162
Chris@1516 163 # Sets the logged in user
Chris@1516 164 def logged_user=(user)
Chris@1516 165 reset_session
Chris@1516 166 if user && user.is_a?(User)
Chris@1516 167 User.current = user
Chris@1516 168 start_user_session(user)
Chris@1516 169 else
Chris@1516 170 User.current = User.anonymous
Chris@1516 171 end
Chris@1516 172 end
Chris@1516 173
Chris@1516 174 # Logs out current user
Chris@1516 175 def logout_user
Chris@1516 176 if User.current.logged?
Chris@1516 177 cookies.delete(autologin_cookie_name)
Chris@1516 178 Token.delete_all(["user_id = ? AND action = ?", User.current.id, 'autologin'])
Chris@1516 179 self.logged_user = nil
Chris@1516 180 end
Chris@1516 181 end
Chris@1516 182
Chris@1516 183 # check if login is globally required to access the application
Chris@1516 184 def check_if_login_required
Chris@1516 185 # no check needed if user is already logged in
Chris@1516 186 return true if User.current.logged?
Chris@1516 187 require_login if Setting.login_required?
Chris@1516 188 end
Chris@1516 189
Chris@1516 190 def check_password_change
Chris@1516 191 if session[:pwd]
Chris@1516 192 if User.current.must_change_password?
Chris@1516 193 redirect_to my_password_path
Chris@1516 194 else
Chris@1516 195 session.delete(:pwd)
Chris@1516 196 end
Chris@1516 197 end
Chris@1516 198 end
Chris@1516 199
Chris@1516 200 def set_localization
Chris@1516 201 lang = nil
Chris@1516 202 if User.current.logged?
Chris@1516 203 lang = find_language(User.current.language)
Chris@1516 204 end
Chris@1516 205 if lang.nil? && request.env['HTTP_ACCEPT_LANGUAGE']
Chris@1516 206 accept_lang = parse_qvalues(request.env['HTTP_ACCEPT_LANGUAGE']).first
Chris@1516 207 if !accept_lang.blank?
Chris@1516 208 accept_lang = accept_lang.downcase
Chris@1516 209 lang = find_language(accept_lang) || find_language(accept_lang.split('-').first)
Chris@1516 210 end
Chris@1516 211 end
Chris@1516 212 lang ||= Setting.default_language
Chris@1516 213 set_language_if_valid(lang)
Chris@1516 214 end
Chris@1516 215
Chris@1516 216 def require_login
Chris@1516 217 if !User.current.logged?
Chris@1516 218 # Extract only the basic url parameters on non-GET requests
Chris@1516 219 if request.get?
Chris@1516 220 url = url_for(params)
Chris@1516 221 else
Chris@1516 222 url = url_for(:controller => params[:controller], :action => params[:action], :id => params[:id], :project_id => params[:project_id])
Chris@1516 223 end
Chris@1516 224 respond_to do |format|
Chris@1516 225 format.html {
Chris@1516 226 if request.xhr?
Chris@1516 227 head :unauthorized
Chris@1516 228 else
Chris@1516 229 redirect_to :controller => "account", :action => "login", :back_url => url
Chris@1516 230 end
Chris@1516 231 }
Chris@1516 232 format.atom { redirect_to :controller => "account", :action => "login", :back_url => url }
Chris@1516 233 format.xml { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' }
Chris@1516 234 format.js { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' }
Chris@1516 235 format.json { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' }
Chris@1516 236 end
Chris@1516 237 return false
Chris@1516 238 end
Chris@1516 239 true
Chris@1516 240 end
Chris@1516 241
Chris@1516 242 def require_admin
Chris@1516 243 return unless require_login
Chris@1516 244 if !User.current.admin?
Chris@1516 245 render_403
Chris@1516 246 return false
Chris@1516 247 end
Chris@1516 248 true
Chris@1516 249 end
Chris@1516 250
Chris@1516 251 def deny_access
Chris@1516 252 User.current.logged? ? render_403 : require_login
Chris@1516 253 end
Chris@1516 254
Chris@1516 255 # Authorize the user for the requested action
Chris@1516 256 def authorize(ctrl = params[:controller], action = params[:action], global = false)
Chris@1516 257 allowed = User.current.allowed_to?({:controller => ctrl, :action => action}, @project || @projects, :global => global)
Chris@1516 258 if allowed
Chris@1516 259 true
Chris@1516 260 else
Chris@1516 261 if @project && @project.archived?
Chris@1516 262 render_403 :message => :notice_not_authorized_archived_project
Chris@1516 263 else
Chris@1516 264 deny_access
Chris@1516 265 end
Chris@1516 266 end
Chris@1516 267 end
Chris@1516 268
Chris@1516 269 # Authorize the user for the requested action outside a project
Chris@1516 270 def authorize_global(ctrl = params[:controller], action = params[:action], global = true)
Chris@1516 271 authorize(ctrl, action, global)
Chris@1516 272 end
Chris@1516 273
Chris@1516 274 # Find project of id params[:id]
Chris@1516 275 def find_project
Chris@1516 276 @project = Project.find(params[:id])
Chris@1516 277 rescue ActiveRecord::RecordNotFound
Chris@1516 278 render_404
Chris@1516 279 end
Chris@1516 280
Chris@1516 281 # Find project of id params[:project_id]
Chris@1516 282 def find_project_by_project_id
Chris@1516 283 @project = Project.find(params[:project_id])
Chris@1516 284 rescue ActiveRecord::RecordNotFound
Chris@1516 285 render_404
Chris@1516 286 end
Chris@1516 287
Chris@1516 288 # Find a project based on params[:project_id]
Chris@1516 289 # TODO: some subclasses override this, see about merging their logic
Chris@1516 290 def find_optional_project
Chris@1516 291 @project = Project.find(params[:project_id]) unless params[:project_id].blank?
Chris@1516 292 allowed = User.current.allowed_to?({:controller => params[:controller], :action => params[:action]}, @project, :global => true)
Chris@1516 293 allowed ? true : deny_access
Chris@1516 294 rescue ActiveRecord::RecordNotFound
Chris@1516 295 render_404
Chris@1516 296 end
Chris@1516 297
Chris@1516 298 # Finds and sets @project based on @object.project
Chris@1516 299 def find_project_from_association
Chris@1516 300 render_404 unless @object.present?
Chris@1516 301
Chris@1516 302 @project = @object.project
Chris@1516 303 end
Chris@1516 304
Chris@1516 305 def find_model_object
Chris@1516 306 model = self.class.model_object
Chris@1516 307 if model
Chris@1516 308 @object = model.find(params[:id])
Chris@1516 309 self.instance_variable_set('@' + controller_name.singularize, @object) if @object
Chris@1516 310 end
Chris@1516 311 rescue ActiveRecord::RecordNotFound
Chris@1516 312 render_404
Chris@1516 313 end
Chris@1516 314
Chris@1516 315 def self.model_object(model)
Chris@1516 316 self.model_object = model
Chris@1516 317 end
Chris@1516 318
Chris@1516 319 # Find the issue whose id is the :id parameter
Chris@1516 320 # Raises a Unauthorized exception if the issue is not visible
Chris@1516 321 def find_issue
Chris@1516 322 # Issue.visible.find(...) can not be used to redirect user to the login form
Chris@1516 323 # if the issue actually exists but requires authentication
Chris@1516 324 @issue = Issue.find(params[:id])
Chris@1516 325 raise Unauthorized unless @issue.visible?
Chris@1516 326 @project = @issue.project
Chris@1516 327 rescue ActiveRecord::RecordNotFound
Chris@1516 328 render_404
Chris@1516 329 end
Chris@1516 330
Chris@1516 331 # Find issues with a single :id param or :ids array param
Chris@1516 332 # Raises a Unauthorized exception if one of the issues is not visible
Chris@1516 333 def find_issues
Chris@1516 334 @issues = Issue.where(:id => (params[:id] || params[:ids])).preload(:project, :status, :tracker, :priority, :author, :assigned_to, :relations_to).to_a
Chris@1516 335 raise ActiveRecord::RecordNotFound if @issues.empty?
Chris@1516 336 raise Unauthorized unless @issues.all?(&:visible?)
Chris@1516 337 @projects = @issues.collect(&:project).compact.uniq
Chris@1516 338 @project = @projects.first if @projects.size == 1
Chris@1516 339 rescue ActiveRecord::RecordNotFound
Chris@1516 340 render_404
Chris@1516 341 end
Chris@1516 342
Chris@1516 343 def find_attachments
Chris@1516 344 if (attachments = params[:attachments]).present?
Chris@1516 345 att = attachments.values.collect do |attachment|
Chris@1516 346 Attachment.find_by_token( attachment[:token] ) if attachment[:token].present?
Chris@1516 347 end
Chris@1516 348 att.compact!
Chris@1516 349 end
Chris@1516 350 @attachments = att || []
Chris@1516 351 end
Chris@1516 352
Chris@1516 353 # make sure that the user is a member of the project (or admin) if project is private
Chris@1516 354 # used as a before_filter for actions that do not require any particular permission on the project
Chris@1516 355 def check_project_privacy
Chris@1516 356 if @project && !@project.archived?
Chris@1516 357 if @project.visible?
Chris@1516 358 true
Chris@1516 359 else
Chris@1516 360 deny_access
Chris@1516 361 end
Chris@1516 362 else
Chris@1516 363 @project = nil
Chris@1516 364 render_404
Chris@1516 365 false
Chris@1516 366 end
Chris@1516 367 end
Chris@1516 368
Chris@1516 369 def back_url
Chris@1516 370 url = params[:back_url]
Chris@1516 371 if url.nil? && referer = request.env['HTTP_REFERER']
Chris@1516 372 url = CGI.unescape(referer.to_s)
Chris@1516 373 end
Chris@1516 374 url
Chris@1516 375 end
Chris@1516 376
Chris@1516 377 def redirect_back_or_default(default)
Chris@1516 378 back_url = params[:back_url].to_s
Chris@1516 379 if back_url.present? && valid_back_url?(back_url)
Chris@1516 380 redirect_to(back_url)
Chris@1516 381 return
Chris@1516 382 end
Chris@1516 383 redirect_to default
Chris@1516 384 false
Chris@1516 385 end
Chris@1516 386
Chris@1516 387 # Returns true if back_url is a valid url for redirection, otherwise false
Chris@1516 388 def valid_back_url?(back_url)
Chris@1516 389 if CGI.unescape(back_url).include?('..')
Chris@1516 390 return false
Chris@1516 391 end
Chris@1516 392
Chris@1516 393 begin
Chris@1516 394 uri = URI.parse(back_url)
Chris@1516 395 rescue URI::InvalidURIError
Chris@1516 396 return false
Chris@1516 397 end
Chris@1516 398
Chris@1516 399 if uri.host.present? && uri.host != request.host
Chris@1516 400 return false
Chris@1516 401 end
Chris@1516 402
Chris@1516 403 if uri.path.match(%r{/(login|account/register)})
Chris@1516 404 return false
Chris@1516 405 end
Chris@1516 406
Chris@1516 407 if relative_url_root.present? && !uri.path.starts_with?(relative_url_root)
Chris@1516 408 return false
Chris@1516 409 end
Chris@1516 410
Chris@1516 411 return true
Chris@1516 412 end
Chris@1516 413 private :valid_back_url?
Chris@1516 414
Chris@1516 415 # Redirects to the request referer if present, redirects to args or call block otherwise.
Chris@1516 416 def redirect_to_referer_or(*args, &block)
Chris@1516 417 redirect_to :back
Chris@1516 418 rescue ::ActionController::RedirectBackError
Chris@1516 419 if args.any?
Chris@1516 420 redirect_to *args
Chris@1516 421 elsif block_given?
Chris@1516 422 block.call
Chris@1516 423 else
Chris@1516 424 raise "#redirect_to_referer_or takes arguments or a block"
Chris@1516 425 end
Chris@1516 426 end
Chris@1516 427
Chris@1516 428 def render_403(options={})
Chris@1516 429 @project = nil
Chris@1516 430 render_error({:message => :notice_not_authorized, :status => 403}.merge(options))
Chris@1516 431 return false
Chris@1516 432 end
Chris@1516 433
Chris@1516 434 def render_404(options={})
Chris@1516 435 render_error({:message => :notice_file_not_found, :status => 404}.merge(options))
Chris@1516 436 return false
Chris@1516 437 end
Chris@1516 438
Chris@1516 439 # Renders an error response
Chris@1516 440 def render_error(arg)
Chris@1516 441 arg = {:message => arg} unless arg.is_a?(Hash)
Chris@1516 442
Chris@1516 443 @message = arg[:message]
Chris@1516 444 @message = l(@message) if @message.is_a?(Symbol)
Chris@1516 445 @status = arg[:status] || 500
Chris@1516 446
Chris@1516 447 respond_to do |format|
Chris@1516 448 format.html {
Chris@1516 449 render :template => 'common/error', :layout => use_layout, :status => @status
Chris@1516 450 }
Chris@1516 451 format.any { head @status }
Chris@1516 452 end
Chris@1516 453 end
Chris@1516 454
Chris@1516 455 # Handler for ActionView::MissingTemplate exception
Chris@1516 456 def missing_template
Chris@1516 457 logger.warn "Missing template, responding with 404"
Chris@1516 458 @project = nil
Chris@1516 459 render_404
Chris@1516 460 end
Chris@1516 461
Chris@1516 462 # Filter for actions that provide an API response
Chris@1516 463 # but have no HTML representation for non admin users
Chris@1516 464 def require_admin_or_api_request
Chris@1516 465 return true if api_request?
Chris@1516 466 if User.current.admin?
Chris@1516 467 true
Chris@1516 468 elsif User.current.logged?
Chris@1516 469 render_error(:status => 406)
Chris@1516 470 else
Chris@1516 471 deny_access
Chris@1516 472 end
Chris@1516 473 end
Chris@1516 474
Chris@1516 475 # Picks which layout to use based on the request
Chris@1516 476 #
Chris@1516 477 # @return [boolean, string] name of the layout to use or false for no layout
Chris@1516 478 def use_layout
Chris@1516 479 request.xhr? ? false : 'base'
Chris@1516 480 end
Chris@1516 481
Chris@1516 482 def render_feed(items, options={})
Chris@1516 483 @items = items || []
Chris@1516 484 @items.sort! {|x,y| y.event_datetime <=> x.event_datetime }
Chris@1516 485 @items = @items.slice(0, Setting.feeds_limit.to_i)
Chris@1516 486 @title = options[:title] || Setting.app_title
Chris@1516 487 render :template => "common/feed", :formats => [:atom], :layout => false,
Chris@1516 488 :content_type => 'application/atom+xml'
Chris@1516 489 end
Chris@1516 490
Chris@1516 491 def self.accept_rss_auth(*actions)
Chris@1516 492 if actions.any?
Chris@1516 493 self.accept_rss_auth_actions = actions
Chris@1516 494 else
Chris@1516 495 self.accept_rss_auth_actions || []
Chris@1516 496 end
Chris@1516 497 end
Chris@1516 498
Chris@1516 499 def accept_rss_auth?(action=action_name)
Chris@1516 500 self.class.accept_rss_auth.include?(action.to_sym)
Chris@1516 501 end
Chris@1516 502
Chris@1516 503 def self.accept_api_auth(*actions)
Chris@1516 504 if actions.any?
Chris@1516 505 self.accept_api_auth_actions = actions
Chris@1516 506 else
Chris@1516 507 self.accept_api_auth_actions || []
Chris@1516 508 end
Chris@1516 509 end
Chris@1516 510
Chris@1516 511 def accept_api_auth?(action=action_name)
Chris@1516 512 self.class.accept_api_auth.include?(action.to_sym)
Chris@1516 513 end
Chris@1516 514
Chris@1516 515 # Returns the number of objects that should be displayed
Chris@1516 516 # on the paginated list
Chris@1516 517 def per_page_option
Chris@1516 518 per_page = nil
Chris@1516 519 if params[:per_page] && Setting.per_page_options_array.include?(params[:per_page].to_s.to_i)
Chris@1516 520 per_page = params[:per_page].to_s.to_i
Chris@1516 521 session[:per_page] = per_page
Chris@1516 522 elsif session[:per_page]
Chris@1516 523 per_page = session[:per_page]
Chris@1516 524 else
Chris@1516 525 per_page = Setting.per_page_options_array.first || 25
Chris@1516 526 end
Chris@1516 527 per_page
Chris@1516 528 end
Chris@1516 529
Chris@1516 530 # Returns offset and limit used to retrieve objects
Chris@1516 531 # for an API response based on offset, limit and page parameters
Chris@1516 532 def api_offset_and_limit(options=params)
Chris@1516 533 if options[:offset].present?
Chris@1516 534 offset = options[:offset].to_i
Chris@1516 535 if offset < 0
Chris@1516 536 offset = 0
Chris@1516 537 end
Chris@1516 538 end
Chris@1516 539 limit = options[:limit].to_i
Chris@1516 540 if limit < 1
Chris@1516 541 limit = 25
Chris@1516 542 elsif limit > 100
Chris@1516 543 limit = 100
Chris@1516 544 end
Chris@1516 545 if offset.nil? && options[:page].present?
Chris@1516 546 offset = (options[:page].to_i - 1) * limit
Chris@1516 547 offset = 0 if offset < 0
Chris@1516 548 end
Chris@1516 549 offset ||= 0
Chris@1516 550
Chris@1516 551 [offset, limit]
Chris@1516 552 end
Chris@1516 553
Chris@1516 554 # qvalues http header parser
Chris@1516 555 # code taken from webrick
Chris@1516 556 def parse_qvalues(value)
Chris@1516 557 tmp = []
Chris@1516 558 if value
Chris@1516 559 parts = value.split(/,\s*/)
Chris@1516 560 parts.each {|part|
Chris@1516 561 if m = %r{^([^\s,]+?)(?:;\s*q=(\d+(?:\.\d+)?))?$}.match(part)
Chris@1516 562 val = m[1]
Chris@1516 563 q = (m[2] or 1).to_f
Chris@1516 564 tmp.push([val, q])
Chris@1516 565 end
Chris@1516 566 }
Chris@1516 567 tmp = tmp.sort_by{|val, q| -q}
Chris@1516 568 tmp.collect!{|val, q| val}
Chris@1516 569 end
Chris@1516 570 return tmp
Chris@1516 571 rescue
Chris@1516 572 nil
Chris@1516 573 end
Chris@1516 574
Chris@1516 575 # Returns a string that can be used as filename value in Content-Disposition header
Chris@1516 576 def filename_for_content_disposition(name)
Chris@1516 577 request.env['HTTP_USER_AGENT'] =~ %r{(MSIE|Trident)} ? ERB::Util.url_encode(name) : name
Chris@1516 578 end
Chris@1516 579
Chris@1516 580 def api_request?
Chris@1516 581 %w(xml json).include? params[:format]
Chris@1516 582 end
Chris@1516 583
Chris@1516 584 # Returns the API key present in the request
Chris@1516 585 def api_key_from_request
Chris@1516 586 if params[:key].present?
Chris@1516 587 params[:key].to_s
Chris@1516 588 elsif request.headers["X-Redmine-API-Key"].present?
Chris@1516 589 request.headers["X-Redmine-API-Key"].to_s
Chris@1516 590 end
Chris@1516 591 end
Chris@1516 592
Chris@1516 593 # Returns the API 'switch user' value if present
Chris@1516 594 def api_switch_user_from_request
Chris@1516 595 request.headers["X-Redmine-Switch-User"].to_s.presence
Chris@1516 596 end
Chris@1516 597
Chris@1516 598 # Renders a warning flash if obj has unsaved attachments
Chris@1516 599 def render_attachment_warning_if_needed(obj)
Chris@1516 600 flash[:warning] = l(:warning_attachments_not_saved, obj.unsaved_attachments.size) if obj.unsaved_attachments.present?
Chris@1516 601 end
Chris@1516 602
Chris@1516 603 # Rescues an invalid query statement. Just in case...
Chris@1516 604 def query_statement_invalid(exception)
Chris@1516 605 logger.error "Query::StatementInvalid: #{exception.message}" if logger
Chris@1516 606 session.delete(:query)
Chris@1516 607 sort_clear if respond_to?(:sort_clear)
Chris@1516 608 render_error "An error occurred while executing the query and has been logged. Please report this error to your Redmine administrator."
Chris@1516 609 end
Chris@1516 610
Chris@1516 611 # Renders a 200 response for successfull updates or deletions via the API
Chris@1516 612 def render_api_ok
Chris@1516 613 render_api_head :ok
Chris@1516 614 end
Chris@1516 615
Chris@1516 616 # Renders a head API response
Chris@1516 617 def render_api_head(status)
Chris@1516 618 # #head would return a response body with one space
Chris@1516 619 render :text => '', :status => status, :layout => nil
Chris@1516 620 end
Chris@1516 621
Chris@1516 622 # Renders API response on validation failure
Chris@1516 623 def render_validation_errors(objects)
Chris@1516 624 if objects.is_a?(Array)
Chris@1516 625 @error_messages = objects.map {|object| object.errors.full_messages}.flatten
Chris@1516 626 else
Chris@1516 627 @error_messages = objects.errors.full_messages
Chris@1516 628 end
Chris@1516 629 render :template => 'common/error_messages.api', :status => :unprocessable_entity, :layout => nil
Chris@1516 630 end
Chris@1516 631
Chris@1516 632 # Overrides #_include_layout? so that #render with no arguments
Chris@1516 633 # doesn't use the layout for api requests
Chris@1516 634 def _include_layout?(*args)
Chris@1516 635 api_request? ? false : super
Chris@1516 636 end
Chris@1516 637 end