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