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