@@ -1,341 +1,342 | |||
|
1 | 1 | # redMine - project management software |
|
2 | 2 | # Copyright (C) 2006-2007 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 ApplicationController < ActionController::Base |
|
22 | 22 | include Redmine::I18n |
|
23 | 23 | |
|
24 | 24 | layout 'base' |
|
25 | 25 | exempt_from_layout 'builder' |
|
26 | 26 | |
|
27 | 27 | # Remove broken cookie after upgrade from 0.8.x (#4292) |
|
28 | 28 | # See https://rails.lighthouseapp.com/projects/8994/tickets/3360 |
|
29 | 29 | # TODO: remove it when Rails is fixed |
|
30 | 30 | before_filter :delete_broken_cookies |
|
31 | 31 | def delete_broken_cookies |
|
32 | 32 | if cookies['_redmine_session'] && cookies['_redmine_session'] !~ /--/ |
|
33 | 33 | cookies.delete '_redmine_session' |
|
34 | 34 | redirect_to home_path |
|
35 | 35 | return false |
|
36 | 36 | end |
|
37 | 37 | end |
|
38 | 38 | |
|
39 | 39 | before_filter :user_setup, :check_if_login_required, :set_localization |
|
40 | 40 | filter_parameter_logging :password |
|
41 | 41 | protect_from_forgery |
|
42 | 42 | |
|
43 | 43 | rescue_from ActionController::InvalidAuthenticityToken, :with => :invalid_authenticity_token |
|
44 | 44 | |
|
45 | 45 | include Redmine::Search::Controller |
|
46 | 46 | include Redmine::MenuManager::MenuController |
|
47 | 47 | helper Redmine::MenuManager::MenuHelper |
|
48 | 48 | |
|
49 | 49 | Redmine::Scm::Base.all.each do |scm| |
|
50 | 50 | require_dependency "repository/#{scm.underscore}" |
|
51 | 51 | end |
|
52 | 52 | |
|
53 | 53 | def user_setup |
|
54 | 54 | # Check the settings cache for each request |
|
55 | 55 | Setting.check_cache |
|
56 | 56 | # Find the current user |
|
57 | 57 | User.current = find_current_user |
|
58 | 58 | end |
|
59 | 59 | |
|
60 | 60 | # Returns the current user or nil if no user is logged in |
|
61 | 61 | # and starts a session if needed |
|
62 | 62 | def find_current_user |
|
63 | 63 | if session[:user_id] |
|
64 | 64 | # existing session |
|
65 | 65 | (User.active.find(session[:user_id]) rescue nil) |
|
66 | 66 | elsif cookies[:autologin] && Setting.autologin? |
|
67 | 67 | # auto-login feature starts a new session |
|
68 | 68 | user = User.try_to_autologin(cookies[:autologin]) |
|
69 | 69 | session[:user_id] = user.id if user |
|
70 | 70 | user |
|
71 | 71 | elsif params[:format] == 'atom' && params[:key] && accept_key_auth_actions.include?(params[:action]) |
|
72 | 72 | # RSS key authentication does not start a session |
|
73 | 73 | User.find_by_rss_key(params[:key]) |
|
74 | 74 | elsif Setting.rest_api_enabled? && ['xml', 'json'].include?(params[:format]) |
|
75 | 75 | if params[:key].present? && accept_key_auth_actions.include?(params[:action]) |
|
76 | 76 | # Use API key |
|
77 | 77 | User.find_by_api_key(params[:key]) |
|
78 | 78 | else |
|
79 | 79 | # HTTP Basic, either username/password or API key/random |
|
80 | 80 | authenticate_with_http_basic do |username, password| |
|
81 | 81 | User.try_to_login(username, password) || User.find_by_api_key(username) |
|
82 | 82 | end |
|
83 | 83 | end |
|
84 | 84 | end |
|
85 | 85 | end |
|
86 | 86 | |
|
87 | 87 | # Sets the logged in user |
|
88 | 88 | def logged_user=(user) |
|
89 | 89 | reset_session |
|
90 | 90 | if user && user.is_a?(User) |
|
91 | 91 | User.current = user |
|
92 | 92 | session[:user_id] = user.id |
|
93 | 93 | else |
|
94 | 94 | User.current = User.anonymous |
|
95 | 95 | end |
|
96 | 96 | end |
|
97 | 97 | |
|
98 | 98 | # check if login is globally required to access the application |
|
99 | 99 | def check_if_login_required |
|
100 | 100 | # no check needed if user is already logged in |
|
101 | 101 | return true if User.current.logged? |
|
102 | 102 | require_login if Setting.login_required? |
|
103 | 103 | end |
|
104 | 104 | |
|
105 | 105 | def set_localization |
|
106 | 106 | lang = nil |
|
107 | 107 | if User.current.logged? |
|
108 | 108 | lang = find_language(User.current.language) |
|
109 | 109 | end |
|
110 | 110 | if lang.nil? && request.env['HTTP_ACCEPT_LANGUAGE'] |
|
111 |
accept_lang = parse_qvalues(request.env['HTTP_ACCEPT_LANGUAGE']).first |
|
|
111 | accept_lang = parse_qvalues(request.env['HTTP_ACCEPT_LANGUAGE']).first | |
|
112 | 112 | if !accept_lang.blank? |
|
113 | accept_lang = accept_lang.downcase | |
|
113 | 114 | lang = find_language(accept_lang) || find_language(accept_lang.split('-').first) |
|
114 | 115 | end |
|
115 | 116 | end |
|
116 | 117 | lang ||= Setting.default_language |
|
117 | 118 | set_language_if_valid(lang) |
|
118 | 119 | end |
|
119 | 120 | |
|
120 | 121 | def require_login |
|
121 | 122 | if !User.current.logged? |
|
122 | 123 | # Extract only the basic url parameters on non-GET requests |
|
123 | 124 | if request.get? |
|
124 | 125 | url = url_for(params) |
|
125 | 126 | else |
|
126 | 127 | url = url_for(:controller => params[:controller], :action => params[:action], :id => params[:id], :project_id => params[:project_id]) |
|
127 | 128 | end |
|
128 | 129 | respond_to do |format| |
|
129 | 130 | format.html { redirect_to :controller => "account", :action => "login", :back_url => url } |
|
130 | 131 | format.atom { redirect_to :controller => "account", :action => "login", :back_url => url } |
|
131 | 132 | format.xml { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' } |
|
132 | 133 | format.json { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' } |
|
133 | 134 | end |
|
134 | 135 | return false |
|
135 | 136 | end |
|
136 | 137 | true |
|
137 | 138 | end |
|
138 | 139 | |
|
139 | 140 | def require_admin |
|
140 | 141 | return unless require_login |
|
141 | 142 | if !User.current.admin? |
|
142 | 143 | render_403 |
|
143 | 144 | return false |
|
144 | 145 | end |
|
145 | 146 | true |
|
146 | 147 | end |
|
147 | 148 | |
|
148 | 149 | def deny_access |
|
149 | 150 | User.current.logged? ? render_403 : require_login |
|
150 | 151 | end |
|
151 | 152 | |
|
152 | 153 | # Authorize the user for the requested action |
|
153 | 154 | def authorize(ctrl = params[:controller], action = params[:action], global = false) |
|
154 | 155 | allowed = User.current.allowed_to?({:controller => ctrl, :action => action}, @project, :global => global) |
|
155 | 156 | allowed ? true : deny_access |
|
156 | 157 | end |
|
157 | 158 | |
|
158 | 159 | # Authorize the user for the requested action outside a project |
|
159 | 160 | def authorize_global(ctrl = params[:controller], action = params[:action], global = true) |
|
160 | 161 | authorize(ctrl, action, global) |
|
161 | 162 | end |
|
162 | 163 | |
|
163 | 164 | # Find project of id params[:id] |
|
164 | 165 | def find_project |
|
165 | 166 | @project = Project.find(params[:id]) |
|
166 | 167 | rescue ActiveRecord::RecordNotFound |
|
167 | 168 | render_404 |
|
168 | 169 | end |
|
169 | 170 | |
|
170 | 171 | # Finds and sets @project based on @object.project |
|
171 | 172 | def find_project_from_association |
|
172 | 173 | render_404 unless @object.present? |
|
173 | 174 | |
|
174 | 175 | @project = @object.project |
|
175 | 176 | rescue ActiveRecord::RecordNotFound |
|
176 | 177 | render_404 |
|
177 | 178 | end |
|
178 | 179 | |
|
179 | 180 | def find_model_object |
|
180 | 181 | model = self.class.read_inheritable_attribute('model_object') |
|
181 | 182 | if model |
|
182 | 183 | @object = model.find(params[:id]) |
|
183 | 184 | self.instance_variable_set('@' + controller_name.singularize, @object) if @object |
|
184 | 185 | end |
|
185 | 186 | rescue ActiveRecord::RecordNotFound |
|
186 | 187 | render_404 |
|
187 | 188 | end |
|
188 | 189 | |
|
189 | 190 | def self.model_object(model) |
|
190 | 191 | write_inheritable_attribute('model_object', model) |
|
191 | 192 | end |
|
192 | 193 | |
|
193 | 194 | # make sure that the user is a member of the project (or admin) if project is private |
|
194 | 195 | # used as a before_filter for actions that do not require any particular permission on the project |
|
195 | 196 | def check_project_privacy |
|
196 | 197 | if @project && @project.active? |
|
197 | 198 | if @project.is_public? || User.current.member_of?(@project) || User.current.admin? |
|
198 | 199 | true |
|
199 | 200 | else |
|
200 | 201 | User.current.logged? ? render_403 : require_login |
|
201 | 202 | end |
|
202 | 203 | else |
|
203 | 204 | @project = nil |
|
204 | 205 | render_404 |
|
205 | 206 | false |
|
206 | 207 | end |
|
207 | 208 | end |
|
208 | 209 | |
|
209 | 210 | def redirect_back_or_default(default) |
|
210 | 211 | back_url = CGI.unescape(params[:back_url].to_s) |
|
211 | 212 | if !back_url.blank? |
|
212 | 213 | begin |
|
213 | 214 | uri = URI.parse(back_url) |
|
214 | 215 | # do not redirect user to another host or to the login or register page |
|
215 | 216 | if (uri.relative? || (uri.host == request.host)) && !uri.path.match(%r{/(login|account/register)}) |
|
216 | 217 | redirect_to(back_url) |
|
217 | 218 | return |
|
218 | 219 | end |
|
219 | 220 | rescue URI::InvalidURIError |
|
220 | 221 | # redirect to default |
|
221 | 222 | end |
|
222 | 223 | end |
|
223 | 224 | redirect_to default |
|
224 | 225 | end |
|
225 | 226 | |
|
226 | 227 | def render_403 |
|
227 | 228 | @project = nil |
|
228 | 229 | respond_to do |format| |
|
229 | 230 | format.html { render :template => "common/403", :layout => (request.xhr? ? false : 'base'), :status => 403 } |
|
230 | 231 | format.atom { head 403 } |
|
231 | 232 | format.xml { head 403 } |
|
232 | 233 | format.json { head 403 } |
|
233 | 234 | end |
|
234 | 235 | return false |
|
235 | 236 | end |
|
236 | 237 | |
|
237 | 238 | def render_404 |
|
238 | 239 | respond_to do |format| |
|
239 | 240 | format.html { render :template => "common/404", :layout => !request.xhr?, :status => 404 } |
|
240 | 241 | format.atom { head 404 } |
|
241 | 242 | format.xml { head 404 } |
|
242 | 243 | format.json { head 404 } |
|
243 | 244 | end |
|
244 | 245 | return false |
|
245 | 246 | end |
|
246 | 247 | |
|
247 | 248 | def render_error(msg) |
|
248 | 249 | respond_to do |format| |
|
249 | 250 | format.html { |
|
250 | 251 | flash.now[:error] = msg |
|
251 | 252 | render :text => '', :layout => !request.xhr?, :status => 500 |
|
252 | 253 | } |
|
253 | 254 | format.atom { head 500 } |
|
254 | 255 | format.xml { head 500 } |
|
255 | 256 | format.json { head 500 } |
|
256 | 257 | end |
|
257 | 258 | end |
|
258 | 259 | |
|
259 | 260 | def invalid_authenticity_token |
|
260 | 261 | if api_request? |
|
261 | 262 | logger.error "Form authenticity token is missing or is invalid. API calls must include a proper Content-type header (text/xml or text/json)." |
|
262 | 263 | end |
|
263 | 264 | render_error "Invalid form authenticity token." |
|
264 | 265 | end |
|
265 | 266 | |
|
266 | 267 | def render_feed(items, options={}) |
|
267 | 268 | @items = items || [] |
|
268 | 269 | @items.sort! {|x,y| y.event_datetime <=> x.event_datetime } |
|
269 | 270 | @items = @items.slice(0, Setting.feeds_limit.to_i) |
|
270 | 271 | @title = options[:title] || Setting.app_title |
|
271 | 272 | render :template => "common/feed.atom.rxml", :layout => false, :content_type => 'application/atom+xml' |
|
272 | 273 | end |
|
273 | 274 | |
|
274 | 275 | def self.accept_key_auth(*actions) |
|
275 | 276 | actions = actions.flatten.map(&:to_s) |
|
276 | 277 | write_inheritable_attribute('accept_key_auth_actions', actions) |
|
277 | 278 | end |
|
278 | 279 | |
|
279 | 280 | def accept_key_auth_actions |
|
280 | 281 | self.class.read_inheritable_attribute('accept_key_auth_actions') || [] |
|
281 | 282 | end |
|
282 | 283 | |
|
283 | 284 | # Returns the number of objects that should be displayed |
|
284 | 285 | # on the paginated list |
|
285 | 286 | def per_page_option |
|
286 | 287 | per_page = nil |
|
287 | 288 | if params[:per_page] && Setting.per_page_options_array.include?(params[:per_page].to_s.to_i) |
|
288 | 289 | per_page = params[:per_page].to_s.to_i |
|
289 | 290 | session[:per_page] = per_page |
|
290 | 291 | elsif session[:per_page] |
|
291 | 292 | per_page = session[:per_page] |
|
292 | 293 | else |
|
293 | 294 | per_page = Setting.per_page_options_array.first || 25 |
|
294 | 295 | end |
|
295 | 296 | per_page |
|
296 | 297 | end |
|
297 | 298 | |
|
298 | 299 | # qvalues http header parser |
|
299 | 300 | # code taken from webrick |
|
300 | 301 | def parse_qvalues(value) |
|
301 | 302 | tmp = [] |
|
302 | 303 | if value |
|
303 | 304 | parts = value.split(/,\s*/) |
|
304 | 305 | parts.each {|part| |
|
305 | 306 | if m = %r{^([^\s,]+?)(?:;\s*q=(\d+(?:\.\d+)?))?$}.match(part) |
|
306 | 307 | val = m[1] |
|
307 | 308 | q = (m[2] or 1).to_f |
|
308 | 309 | tmp.push([val, q]) |
|
309 | 310 | end |
|
310 | 311 | } |
|
311 | 312 | tmp = tmp.sort_by{|val, q| -q} |
|
312 | 313 | tmp.collect!{|val, q| val} |
|
313 | 314 | end |
|
314 | 315 | return tmp |
|
315 | 316 | rescue |
|
316 | 317 | nil |
|
317 | 318 | end |
|
318 | 319 | |
|
319 | 320 | # Returns a string that can be used as filename value in Content-Disposition header |
|
320 | 321 | def filename_for_content_disposition(name) |
|
321 | 322 | request.env['HTTP_USER_AGENT'] =~ %r{MSIE} ? ERB::Util.url_encode(name) : name |
|
322 | 323 | end |
|
323 | 324 | |
|
324 | 325 | def api_request? |
|
325 | 326 | %w(xml json).include? params[:format] |
|
326 | 327 | end |
|
327 | 328 | |
|
328 | 329 | # Renders a warning flash if obj has unsaved attachments |
|
329 | 330 | def render_attachment_warning_if_needed(obj) |
|
330 | 331 | flash[:warning] = l(:warning_attachments_not_saved, obj.unsaved_attachments.size) if obj.unsaved_attachments.present? |
|
331 | 332 | end |
|
332 | 333 | |
|
333 | 334 | # Rescues an invalid query statement. Just in case... |
|
334 | 335 | def query_statement_invalid(exception) |
|
335 | 336 | logger.error "Query::StatementInvalid: #{exception.message}" if logger |
|
336 | 337 | session.delete(:query) |
|
337 | 338 | sort_clear if respond_to?(:sort_clear) |
|
338 | 339 | render_error "An error occurred while executing the query and has been logged. Please report this error to your Redmine administrator." |
|
339 | 340 | end |
|
340 | 341 | |
|
341 | 342 | end |
General Comments 0
You need to be logged in to leave comments.
Login now