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