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