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