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 @ 912:5e80956cc792
History | View | Annotate | Download (17 KB)
| 1 | 441:cbce1fd3b1b7 | Chris | # Redmine - project management software
|
|---|---|---|---|
| 2 | # Copyright (C) 2006-2011 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 | |||
| 26 | layout 'base'
|
||
| 27 | 117:af80e5618e9b | Chris | exempt_from_layout 'builder', 'rsb' |
| 28 | 441:cbce1fd3b1b7 | Chris | |
| 29 | 909:cbb26bc654de | Chris | protect_from_forgery |
| 30 | def handle_unverified_request |
||
| 31 | super
|
||
| 32 | cookies.delete(:autologin)
|
||
| 33 | end
|
||
| 34 | 0:513646585e45 | Chris | # Remove broken cookie after upgrade from 0.8.x (#4292)
|
| 35 | # See https://rails.lighthouseapp.com/projects/8994/tickets/3360
|
||
| 36 | # TODO: remove it when Rails is fixed
|
||
| 37 | before_filter :delete_broken_cookies
|
||
| 38 | def delete_broken_cookies |
||
| 39 | if cookies['_redmine_session'] && cookies['_redmine_session'] !~ /--/ |
||
| 40 | 441:cbce1fd3b1b7 | Chris | cookies.delete '_redmine_session'
|
| 41 | 0:513646585e45 | Chris | redirect_to home_path |
| 42 | return false |
||
| 43 | end
|
||
| 44 | end
|
||
| 45 | 441:cbce1fd3b1b7 | Chris | |
| 46 | 0:513646585e45 | Chris | before_filter :user_setup, :check_if_login_required, :set_localization |
| 47 | filter_parameter_logging :password
|
||
| 48 | 441:cbce1fd3b1b7 | Chris | |
| 49 | 0:513646585e45 | Chris | rescue_from ActionController::InvalidAuthenticityToken, :with => :invalid_authenticity_token |
| 50 | 507:0c939c159af4 | Chris | rescue_from ::Unauthorized, :with => :deny_access |
| 51 | 441:cbce1fd3b1b7 | Chris | |
| 52 | 0:513646585e45 | Chris | include Redmine::Search::Controller |
| 53 | include Redmine::MenuManager::MenuController |
||
| 54 | helper Redmine::MenuManager::MenuHelper |
||
| 55 | 441:cbce1fd3b1b7 | Chris | |
| 56 | 0:513646585e45 | Chris | Redmine::Scm::Base.all.each do |scm| |
| 57 | require_dependency "repository/#{scm.underscore}"
|
||
| 58 | end
|
||
| 59 | |||
| 60 | def user_setup |
||
| 61 | # Check the settings cache for each request
|
||
| 62 | Setting.check_cache
|
||
| 63 | # Find the current user
|
||
| 64 | User.current = find_current_user
|
||
| 65 | end
|
||
| 66 | 441:cbce1fd3b1b7 | Chris | |
| 67 | 0:513646585e45 | Chris | # Returns the current user or nil if no user is logged in
|
| 68 | # and starts a session if needed
|
||
| 69 | def find_current_user |
||
| 70 | if session[:user_id] |
||
| 71 | # existing session
|
||
| 72 | (User.active.find(session[:user_id]) rescue nil) |
||
| 73 | elsif cookies[:autologin] && Setting.autologin? |
||
| 74 | # auto-login feature starts a new session
|
||
| 75 | user = User.try_to_autologin(cookies[:autologin]) |
||
| 76 | session[:user_id] = user.id if user |
||
| 77 | user |
||
| 78 | 507:0c939c159af4 | Chris | elsif params[:format] == 'atom' && params[:key] && request.get? && accept_rss_auth? |
| 79 | 0:513646585e45 | Chris | # RSS key authentication does not start a session
|
| 80 | User.find_by_rss_key(params[:key]) |
||
| 81 | 507:0c939c159af4 | Chris | elsif Setting.rest_api_enabled? && accept_api_auth? |
| 82 | if (key = api_key_from_request)
|
||
| 83 | 0:513646585e45 | Chris | # Use API key
|
| 84 | 117:af80e5618e9b | Chris | User.find_by_api_key(key)
|
| 85 | 0:513646585e45 | Chris | else
|
| 86 | # HTTP Basic, either username/password or API key/random
|
||
| 87 | authenticate_with_http_basic do |username, password|
|
||
| 88 | User.try_to_login(username, password) || User.find_by_api_key(username) |
||
| 89 | end
|
||
| 90 | end
|
||
| 91 | end
|
||
| 92 | end
|
||
| 93 | |||
| 94 | # Sets the logged in user
|
||
| 95 | def logged_user=(user) |
||
| 96 | reset_session |
||
| 97 | if user && user.is_a?(User) |
||
| 98 | User.current = user
|
||
| 99 | session[:user_id] = user.id
|
||
| 100 | else
|
||
| 101 | User.current = User.anonymous |
||
| 102 | end
|
||
| 103 | end
|
||
| 104 | 441:cbce1fd3b1b7 | Chris | |
| 105 | 0:513646585e45 | Chris | # check if login is globally required to access the application
|
| 106 | def check_if_login_required |
||
| 107 | # no check needed if user is already logged in
|
||
| 108 | return true if User.current.logged? |
||
| 109 | require_login if Setting.login_required? |
||
| 110 | 441:cbce1fd3b1b7 | Chris | end
|
| 111 | |||
| 112 | 0:513646585e45 | Chris | def set_localization |
| 113 | lang = nil
|
||
| 114 | if User.current.logged? |
||
| 115 | lang = find_language(User.current.language)
|
||
| 116 | end
|
||
| 117 | if lang.nil? && request.env['HTTP_ACCEPT_LANGUAGE'] |
||
| 118 | accept_lang = parse_qvalues(request.env['HTTP_ACCEPT_LANGUAGE']).first
|
||
| 119 | if !accept_lang.blank?
|
||
| 120 | accept_lang = accept_lang.downcase |
||
| 121 | lang = find_language(accept_lang) || find_language(accept_lang.split('-').first)
|
||
| 122 | end
|
||
| 123 | end
|
||
| 124 | lang ||= Setting.default_language
|
||
| 125 | set_language_if_valid(lang) |
||
| 126 | end
|
||
| 127 | 441:cbce1fd3b1b7 | Chris | |
| 128 | 0:513646585e45 | Chris | def require_login |
| 129 | if !User.current.logged? |
||
| 130 | # Extract only the basic url parameters on non-GET requests
|
||
| 131 | if request.get?
|
||
| 132 | url = url_for(params) |
||
| 133 | else
|
||
| 134 | url = url_for(:controller => params[:controller], :action => params[:action], :id => params[:id], :project_id => params[:project_id]) |
||
| 135 | end
|
||
| 136 | respond_to do |format|
|
||
| 137 | format.html { redirect_to :controller => "account", :action => "login", :back_url => url }
|
||
| 138 | format.atom { redirect_to :controller => "account", :action => "login", :back_url => url }
|
||
| 139 | format.xml { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' }
|
||
| 140 | format.js { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' }
|
||
| 141 | format.json { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' }
|
||
| 142 | end
|
||
| 143 | return false |
||
| 144 | end
|
||
| 145 | true
|
||
| 146 | end
|
||
| 147 | |||
| 148 | def require_admin |
||
| 149 | return unless require_login |
||
| 150 | if !User.current.admin? |
||
| 151 | render_403 |
||
| 152 | return false |
||
| 153 | end
|
||
| 154 | true
|
||
| 155 | end
|
||
| 156 | 441:cbce1fd3b1b7 | Chris | |
| 157 | 0:513646585e45 | Chris | def deny_access |
| 158 | User.current.logged? ? render_403 : require_login
|
||
| 159 | end
|
||
| 160 | |||
| 161 | # Authorize the user for the requested action
|
||
| 162 | def authorize(ctrl = params[:controller], action = params[:action], global = false) |
||
| 163 | 37:94944d00e43c | chris | allowed = User.current.allowed_to?({:controller => ctrl, :action => action}, @project || @projects, :global => global) |
| 164 | if allowed
|
||
| 165 | true
|
||
| 166 | else
|
||
| 167 | if @project && @project.archived? |
||
| 168 | render_403 :message => :notice_not_authorized_archived_project |
||
| 169 | else
|
||
| 170 | deny_access |
||
| 171 | end
|
||
| 172 | end
|
||
| 173 | 0:513646585e45 | Chris | end
|
| 174 | |||
| 175 | # Authorize the user for the requested action outside a project
|
||
| 176 | def authorize_global(ctrl = params[:controller], action = params[:action], global = true) |
||
| 177 | authorize(ctrl, action, global) |
||
| 178 | end
|
||
| 179 | |||
| 180 | # Find project of id params[:id]
|
||
| 181 | def find_project |
||
| 182 | @project = Project.find(params[:id]) |
||
| 183 | rescue ActiveRecord::RecordNotFound |
||
| 184 | 743:7ded87cc4b80 | chris | User.current.logged? ? render_404 : require_login
|
| 185 | 0:513646585e45 | Chris | end
|
| 186 | |||
| 187 | 22:40f7cfd4df19 | chris | # Find project of id params[:project_id]
|
| 188 | def find_project_by_project_id |
||
| 189 | @project = Project.find(params[:project_id]) |
||
| 190 | rescue ActiveRecord::RecordNotFound |
||
| 191 | 743:7ded87cc4b80 | chris | User.current.logged? ? render_404 : require_login
|
| 192 | 22:40f7cfd4df19 | chris | end
|
| 193 | |||
| 194 | 0:513646585e45 | Chris | # Find a project based on params[:project_id]
|
| 195 | # TODO: some subclasses override this, see about merging their logic
|
||
| 196 | def find_optional_project |
||
| 197 | @project = Project.find(params[:project_id]) unless params[:project_id].blank? |
||
| 198 | allowed = User.current.allowed_to?({:controller => params[:controller], :action => params[:action]}, @project, :global => true) |
||
| 199 | allowed ? true : deny_access
|
||
| 200 | rescue ActiveRecord::RecordNotFound |
||
| 201 | render_404 |
||
| 202 | end
|
||
| 203 | |||
| 204 | # Finds and sets @project based on @object.project
|
||
| 205 | def find_project_from_association |
||
| 206 | render_404 unless @object.present? |
||
| 207 | 441:cbce1fd3b1b7 | Chris | |
| 208 | 0:513646585e45 | Chris | @project = @object.project |
| 209 | end
|
||
| 210 | |||
| 211 | def find_model_object |
||
| 212 | model = self.class.read_inheritable_attribute('model_object') |
||
| 213 | if model
|
||
| 214 | @object = model.find(params[:id]) |
||
| 215 | self.instance_variable_set('@' + controller_name.singularize, @object) if @object |
||
| 216 | end
|
||
| 217 | rescue ActiveRecord::RecordNotFound |
||
| 218 | render_404 |
||
| 219 | end
|
||
| 220 | |||
| 221 | def self.model_object(model) |
||
| 222 | write_inheritable_attribute('model_object', model)
|
||
| 223 | end
|
||
| 224 | 14:1d32c0a0efbf | Chris | |
| 225 | # Filter for bulk issue operations
|
||
| 226 | def find_issues |
||
| 227 | @issues = Issue.find_all_by_id(params[:id] || params[:ids]) |
||
| 228 | raise ActiveRecord::RecordNotFound if @issues.empty? |
||
| 229 | 441:cbce1fd3b1b7 | Chris | if @issues.detect {|issue| !issue.visible?} |
| 230 | deny_access |
||
| 231 | return
|
||
| 232 | end
|
||
| 233 | 37:94944d00e43c | chris | @projects = @issues.collect(&:project).compact.uniq |
| 234 | @project = @projects.first if @projects.size == 1 |
||
| 235 | rescue ActiveRecord::RecordNotFound |
||
| 236 | render_404 |
||
| 237 | end
|
||
| 238 | 441:cbce1fd3b1b7 | Chris | |
| 239 | 37:94944d00e43c | chris | # Check if project is unique before bulk operations
|
| 240 | def check_project_uniqueness |
||
| 241 | unless @project |
||
| 242 | 14:1d32c0a0efbf | Chris | # TODO: let users bulk edit/move/destroy issues from different projects
|
| 243 | render_error 'Can not bulk edit/move/destroy issues from different projects'
|
||
| 244 | return false |
||
| 245 | end
|
||
| 246 | end
|
||
| 247 | 441:cbce1fd3b1b7 | Chris | |
| 248 | 0:513646585e45 | Chris | # make sure that the user is a member of the project (or admin) if project is private
|
| 249 | # used as a before_filter for actions that do not require any particular permission on the project
|
||
| 250 | def check_project_privacy |
||
| 251 | if @project && @project.active? |
||
| 252 | if @project.is_public? || User.current.member_of?(@project) || User.current.admin? |
||
| 253 | true
|
||
| 254 | else
|
||
| 255 | 909:cbb26bc654de | Chris | deny_access |
| 256 | 0:513646585e45 | Chris | end
|
| 257 | else
|
||
| 258 | @project = nil |
||
| 259 | render_404 |
||
| 260 | false
|
||
| 261 | end
|
||
| 262 | end
|
||
| 263 | |||
| 264 | 14:1d32c0a0efbf | Chris | def back_url |
| 265 | params[:back_url] || request.env['HTTP_REFERER'] |
||
| 266 | end
|
||
| 267 | |||
| 268 | 0:513646585e45 | Chris | def redirect_back_or_default(default) |
| 269 | back_url = CGI.unescape(params[:back_url].to_s) |
||
| 270 | if !back_url.blank?
|
||
| 271 | begin
|
||
| 272 | uri = URI.parse(back_url)
|
||
| 273 | # do not redirect user to another host or to the login or register page
|
||
| 274 | if (uri.relative? || (uri.host == request.host)) && !uri.path.match(%r{/(login|account/register)}) |
||
| 275 | 365:7d8567890fa3 | chris | # soundsoftware: if back_url is the home page,
|
| 276 | # change it to My Page (#125)
|
||
| 277 | if (uri.path == home_path)
|
||
| 278 | 474:dff13616e464 | chris | if (uri.path =~ /\/$/) |
| 279 | uri.path = uri.path + "my"
|
||
| 280 | else
|
||
| 281 | uri.path = uri.path + "/my"
|
||
| 282 | end
|
||
| 283 | 365:7d8567890fa3 | chris | end
|
| 284 | 237:4b1a23d81683 | chris | # soundsoftware: if login page is https but back_url http,
|
| 285 | # switch back_url to https to ensure cookie validity (#83)
|
||
| 286 | if (uri.scheme == "http") && (URI.parse(request.url).scheme == "https") |
||
| 287 | uri.scheme = "https"
|
||
| 288 | end
|
||
| 289 | 365:7d8567890fa3 | chris | back_url = uri.to_s |
| 290 | 0:513646585e45 | Chris | redirect_to(back_url) |
| 291 | return
|
||
| 292 | end
|
||
| 293 | rescue URI::InvalidURIError |
||
| 294 | # redirect to default
|
||
| 295 | end
|
||
| 296 | end
|
||
| 297 | redirect_to default |
||
| 298 | 441:cbce1fd3b1b7 | Chris | false
|
| 299 | 0:513646585e45 | Chris | end
|
| 300 | 441:cbce1fd3b1b7 | Chris | |
| 301 | 37:94944d00e43c | chris | def render_403(options={}) |
| 302 | 0:513646585e45 | Chris | @project = nil |
| 303 | 37:94944d00e43c | chris | render_error({:message => :notice_not_authorized, :status => 403}.merge(options))
|
| 304 | 0:513646585e45 | Chris | return false |
| 305 | end
|
||
| 306 | 441:cbce1fd3b1b7 | Chris | |
| 307 | 37:94944d00e43c | chris | def render_404(options={}) |
| 308 | render_error({:message => :notice_file_not_found, :status => 404}.merge(options))
|
||
| 309 | 0:513646585e45 | Chris | return false |
| 310 | end
|
||
| 311 | 441:cbce1fd3b1b7 | Chris | |
| 312 | 37:94944d00e43c | chris | # Renders an error response
|
| 313 | def render_error(arg) |
||
| 314 | arg = {:message => arg} unless arg.is_a?(Hash)
|
||
| 315 | 441:cbce1fd3b1b7 | Chris | |
| 316 | 37:94944d00e43c | chris | @message = arg[:message] |
| 317 | @message = l(@message) if @message.is_a?(Symbol) |
||
| 318 | @status = arg[:status] || 500 |
||
| 319 | 441:cbce1fd3b1b7 | Chris | |
| 320 | 0:513646585e45 | Chris | respond_to do |format|
|
| 321 | 37:94944d00e43c | chris | format.html {
|
| 322 | render :template => 'common/error', :layout => use_layout, :status => @status |
||
| 323 | 0:513646585e45 | Chris | } |
| 324 | 37:94944d00e43c | chris | format.atom { head @status }
|
| 325 | format.xml { head @status }
|
||
| 326 | format.js { head @status }
|
||
| 327 | format.json { head @status }
|
||
| 328 | 0:513646585e45 | Chris | end
|
| 329 | end
|
||
| 330 | 909:cbb26bc654de | Chris | |
| 331 | # Filter for actions that provide an API response
|
||
| 332 | # but have no HTML representation for non admin users
|
||
| 333 | def require_admin_or_api_request |
||
| 334 | return true if api_request? |
||
| 335 | if User.current.admin? |
||
| 336 | true
|
||
| 337 | elsif User.current.logged? |
||
| 338 | render_error(:status => 406) |
||
| 339 | else
|
||
| 340 | deny_access |
||
| 341 | end
|
||
| 342 | end
|
||
| 343 | 14:1d32c0a0efbf | Chris | |
| 344 | # Picks which layout to use based on the request
|
||
| 345 | #
|
||
| 346 | # @return [boolean, string] name of the layout to use or false for no layout
|
||
| 347 | def use_layout |
||
| 348 | request.xhr? ? false : 'base' |
||
| 349 | end
|
||
| 350 | 441:cbce1fd3b1b7 | Chris | |
| 351 | 0:513646585e45 | Chris | def invalid_authenticity_token |
| 352 | if api_request?
|
||
| 353 | logger.error "Form authenticity token is missing or is invalid. API calls must include a proper Content-type header (text/xml or text/json)."
|
||
| 354 | end
|
||
| 355 | 79:aea1779e6f18 | Chris | render_error "Invalid form authenticity token. Perhaps your session has timed out; try reloading the form and entering your details again."
|
| 356 | 0:513646585e45 | Chris | end
|
| 357 | 441:cbce1fd3b1b7 | Chris | |
| 358 | def render_feed(items, options={}) |
||
| 359 | 0:513646585e45 | Chris | @items = items || []
|
| 360 | @items.sort! {|x,y| y.event_datetime <=> x.event_datetime }
|
||
| 361 | @items = @items.slice(0, Setting.feeds_limit.to_i) |
||
| 362 | @title = options[:title] || Setting.app_title |
||
| 363 | 909:cbb26bc654de | Chris | render :template => "common/feed.atom", :layout => false, |
| 364 | :content_type => 'application/atom+xml' |
||
| 365 | 0:513646585e45 | Chris | end
|
| 366 | 909:cbb26bc654de | Chris | |
| 367 | 507:0c939c159af4 | Chris | # TODO: remove in Redmine 1.4
|
| 368 | 0:513646585e45 | Chris | def self.accept_key_auth(*actions) |
| 369 | 507:0c939c159af4 | Chris | ActiveSupport::Deprecation.warn "ApplicationController.accept_key_auth is deprecated and will be removed in Redmine 1.4. Use accept_rss_auth (or accept_api_auth) instead." |
| 370 | accept_rss_auth(*actions) |
||
| 371 | 0:513646585e45 | Chris | end
|
| 372 | 441:cbce1fd3b1b7 | Chris | |
| 373 | 507:0c939c159af4 | Chris | # TODO: remove in Redmine 1.4
|
| 374 | 0:513646585e45 | Chris | def accept_key_auth_actions |
| 375 | 507:0c939c159af4 | Chris | ActiveSupport::Deprecation.warn "ApplicationController.accept_key_auth_actions is deprecated and will be removed in Redmine 1.4. Use accept_rss_auth (or accept_api_auth) instead." |
| 376 | self.class.accept_rss_auth
|
||
| 377 | end
|
||
| 378 | 909:cbb26bc654de | Chris | |
| 379 | 507:0c939c159af4 | Chris | def self.accept_rss_auth(*actions) |
| 380 | if actions.any?
|
||
| 381 | write_inheritable_attribute('accept_rss_auth_actions', actions)
|
||
| 382 | else
|
||
| 383 | read_inheritable_attribute('accept_rss_auth_actions') || []
|
||
| 384 | end
|
||
| 385 | end
|
||
| 386 | 909:cbb26bc654de | Chris | |
| 387 | 507:0c939c159af4 | Chris | def accept_rss_auth?(action=action_name) |
| 388 | self.class.accept_rss_auth.include?(action.to_sym)
|
||
| 389 | end
|
||
| 390 | 909:cbb26bc654de | Chris | |
| 391 | 507:0c939c159af4 | Chris | def self.accept_api_auth(*actions) |
| 392 | if actions.any?
|
||
| 393 | write_inheritable_attribute('accept_api_auth_actions', actions)
|
||
| 394 | else
|
||
| 395 | read_inheritable_attribute('accept_api_auth_actions') || []
|
||
| 396 | end
|
||
| 397 | end
|
||
| 398 | 909:cbb26bc654de | Chris | |
| 399 | 507:0c939c159af4 | Chris | def accept_api_auth?(action=action_name) |
| 400 | self.class.accept_api_auth.include?(action.to_sym)
|
||
| 401 | 0:513646585e45 | Chris | end
|
| 402 | 441:cbce1fd3b1b7 | Chris | |
| 403 | 0:513646585e45 | Chris | # Returns the number of objects that should be displayed
|
| 404 | # on the paginated list
|
||
| 405 | def per_page_option |
||
| 406 | per_page = nil
|
||
| 407 | if params[:per_page] && Setting.per_page_options_array.include?(params[:per_page].to_s.to_i) |
||
| 408 | per_page = params[:per_page].to_s.to_i
|
||
| 409 | session[:per_page] = per_page
|
||
| 410 | elsif session[:per_page] |
||
| 411 | per_page = session[:per_page]
|
||
| 412 | else
|
||
| 413 | per_page = Setting.per_page_options_array.first || 25 |
||
| 414 | end
|
||
| 415 | per_page |
||
| 416 | end
|
||
| 417 | |||
| 418 | 117:af80e5618e9b | Chris | # Returns offset and limit used to retrieve objects
|
| 419 | # for an API response based on offset, limit and page parameters
|
||
| 420 | def api_offset_and_limit(options=params) |
||
| 421 | if options[:offset].present? |
||
| 422 | offset = options[:offset].to_i
|
||
| 423 | if offset < 0 |
||
| 424 | offset = 0
|
||
| 425 | end
|
||
| 426 | end
|
||
| 427 | limit = options[:limit].to_i
|
||
| 428 | if limit < 1 |
||
| 429 | limit = 25
|
||
| 430 | elsif limit > 100 |
||
| 431 | limit = 100
|
||
| 432 | end
|
||
| 433 | if offset.nil? && options[:page].present? |
||
| 434 | offset = (options[:page].to_i - 1) * limit |
||
| 435 | offset = 0 if offset < 0 |
||
| 436 | end
|
||
| 437 | offset ||= 0
|
||
| 438 | 441:cbce1fd3b1b7 | Chris | |
| 439 | 117:af80e5618e9b | Chris | [offset, limit] |
| 440 | end
|
||
| 441 | 441:cbce1fd3b1b7 | Chris | |
| 442 | 0:513646585e45 | Chris | # qvalues http header parser
|
| 443 | # code taken from webrick
|
||
| 444 | def parse_qvalues(value) |
||
| 445 | tmp = [] |
||
| 446 | if value
|
||
| 447 | parts = value.split(/,\s*/)
|
||
| 448 | parts.each {|part|
|
||
| 449 | if m = %r{^([^\s,]+?)(?:;\s*q=(\d+(?:\.\d+)?))?$}.match(part) |
||
| 450 | val = m[1]
|
||
| 451 | q = (m[2] or 1).to_f |
||
| 452 | tmp.push([val, q]) |
||
| 453 | end
|
||
| 454 | } |
||
| 455 | tmp = tmp.sort_by{|val, q| -q}
|
||
| 456 | tmp.collect!{|val, q| val}
|
||
| 457 | end
|
||
| 458 | return tmp
|
||
| 459 | rescue
|
||
| 460 | nil
|
||
| 461 | end
|
||
| 462 | 441:cbce1fd3b1b7 | Chris | |
| 463 | 0:513646585e45 | Chris | # Returns a string that can be used as filename value in Content-Disposition header
|
| 464 | def filename_for_content_disposition(name) |
||
| 465 | request.env['HTTP_USER_AGENT'] =~ %r{MSIE} ? ERB::Util.url_encode(name) : name |
||
| 466 | end
|
||
| 467 | 441:cbce1fd3b1b7 | Chris | |
| 468 | 0:513646585e45 | Chris | def api_request? |
| 469 | %w(xml json).include? params[:format] |
||
| 470 | end
|
||
| 471 | 441:cbce1fd3b1b7 | Chris | |
| 472 | 117:af80e5618e9b | Chris | # Returns the API key present in the request
|
| 473 | def api_key_from_request |
||
| 474 | if params[:key].present? |
||
| 475 | params[:key]
|
||
| 476 | elsif request.headers["X-Redmine-API-Key"].present? |
||
| 477 | request.headers["X-Redmine-API-Key"]
|
||
| 478 | end
|
||
| 479 | end
|
||
| 480 | 0:513646585e45 | Chris | |
| 481 | # Renders a warning flash if obj has unsaved attachments
|
||
| 482 | def render_attachment_warning_if_needed(obj) |
||
| 483 | flash[:warning] = l(:warning_attachments_not_saved, obj.unsaved_attachments.size) if obj.unsaved_attachments.present? |
||
| 484 | end
|
||
| 485 | |||
| 486 | 14:1d32c0a0efbf | Chris | # Sets the `flash` notice or error based the number of issues that did not save
|
| 487 | #
|
||
| 488 | # @param [Array, Issue] issues all of the saved and unsaved Issues
|
||
| 489 | # @param [Array, Integer] unsaved_issue_ids the issue ids that were not saved
|
||
| 490 | def set_flash_from_bulk_issue_save(issues, unsaved_issue_ids) |
||
| 491 | if unsaved_issue_ids.empty?
|
||
| 492 | flash[:notice] = l(:notice_successful_update) unless issues.empty? |
||
| 493 | else
|
||
| 494 | flash[:error] = l(:notice_failed_to_save_issues, |
||
| 495 | :count => unsaved_issue_ids.size,
|
||
| 496 | :total => issues.size,
|
||
| 497 | :ids => '#' + unsaved_issue_ids.join(', #')) |
||
| 498 | end
|
||
| 499 | end
|
||
| 500 | |||
| 501 | 0:513646585e45 | Chris | # Rescues an invalid query statement. Just in case...
|
| 502 | def query_statement_invalid(exception) |
||
| 503 | logger.error "Query::StatementInvalid: #{exception.message}" if logger |
||
| 504 | session.delete(:query)
|
||
| 505 | sort_clear if respond_to?(:sort_clear) |
||
| 506 | render_error "An error occurred while executing the query and has been logged. Please report this error to your Redmine administrator."
|
||
| 507 | end
|
||
| 508 | |||
| 509 | 117:af80e5618e9b | Chris | # Renders API response on validation failure
|
| 510 | def render_validation_errors(object) |
||
| 511 | options = { :status => :unprocessable_entity, :layout => false }
|
||
| 512 | options.merge!(case params[:format] |
||
| 513 | when 'xml'; { :xml => object.errors } |
||
| 514 | when 'json'; { :json => {'errors' => object.errors} } # ActiveResource client compliance |
||
| 515 | else
|
||
| 516 | raise "Unknown format #{params[:format]} in #render_validation_errors"
|
||
| 517 | end
|
||
| 518 | ) |
||
| 519 | render options |
||
| 520 | end
|
||
| 521 | 441:cbce1fd3b1b7 | Chris | |
| 522 | 117:af80e5618e9b | Chris | # Overrides #default_template so that the api template
|
| 523 | # is used automatically if it exists
|
||
| 524 | def default_template(action_name = self.action_name) |
||
| 525 | if api_request?
|
||
| 526 | begin
|
||
| 527 | return self.view_paths.find_template(default_template_name(action_name), 'api') |
||
| 528 | rescue ::ActionView::MissingTemplate |
||
| 529 | # the api template was not found
|
||
| 530 | # fallback to the default behaviour
|
||
| 531 | end
|
||
| 532 | end
|
||
| 533 | super
|
||
| 534 | end
|
||
| 535 | 441:cbce1fd3b1b7 | Chris | |
| 536 | 117:af80e5618e9b | Chris | # Overrides #pick_layout so that #render with no arguments
|
| 537 | # doesn't use the layout for api requests
|
||
| 538 | def pick_layout(*args) |
||
| 539 | api_request? ? nil : super |
||
| 540 | end
|
||
| 541 | 0:513646585e45 | Chris | end |