annotate .svn/pristine/e7/e7ec720b55ff6ce9b841d98e9e7841c76ca516a6.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

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