##// END OF EJS Templates
Merged r3702 from trunk....
Jean-Philippe Lang -
r3600:98f3e98d823e
parent child
Show More
@@ -1,293 +1,294
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
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
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.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require 'uri'
18 require 'uri'
19 require 'cgi'
19 require 'cgi'
20
20
21 class ApplicationController < ActionController::Base
21 class ApplicationController < ActionController::Base
22 include Redmine::I18n
22 include Redmine::I18n
23
23
24 layout 'base'
24 layout 'base'
25
25
26 # Remove broken cookie after upgrade from 0.8.x (#4292)
26 # Remove broken cookie after upgrade from 0.8.x (#4292)
27 # See https://rails.lighthouseapp.com/projects/8994/tickets/3360
27 # See https://rails.lighthouseapp.com/projects/8994/tickets/3360
28 # TODO: remove it when Rails is fixed
28 # TODO: remove it when Rails is fixed
29 before_filter :delete_broken_cookies
29 before_filter :delete_broken_cookies
30 def delete_broken_cookies
30 def delete_broken_cookies
31 if cookies['_redmine_session'] && cookies['_redmine_session'] !~ /--/
31 if cookies['_redmine_session'] && cookies['_redmine_session'] !~ /--/
32 cookies.delete '_redmine_session'
32 cookies.delete '_redmine_session'
33 redirect_to home_path
33 redirect_to home_path
34 return false
34 return false
35 end
35 end
36 end
36 end
37
37
38 before_filter :user_setup, :check_if_login_required, :set_localization
38 before_filter :user_setup, :check_if_login_required, :set_localization
39 filter_parameter_logging :password
39 filter_parameter_logging :password
40 protect_from_forgery
40 protect_from_forgery
41
41
42 rescue_from ActionController::InvalidAuthenticityToken, :with => :invalid_authenticity_token
42 rescue_from ActionController::InvalidAuthenticityToken, :with => :invalid_authenticity_token
43
43
44 include Redmine::Search::Controller
44 include Redmine::Search::Controller
45 include Redmine::MenuManager::MenuController
45 include Redmine::MenuManager::MenuController
46 helper Redmine::MenuManager::MenuHelper
46 helper Redmine::MenuManager::MenuHelper
47
47
48 REDMINE_SUPPORTED_SCM.each do |scm|
48 REDMINE_SUPPORTED_SCM.each do |scm|
49 require_dependency "repository/#{scm.underscore}"
49 require_dependency "repository/#{scm.underscore}"
50 end
50 end
51
51
52 def user_setup
52 def user_setup
53 # Check the settings cache for each request
53 # Check the settings cache for each request
54 Setting.check_cache
54 Setting.check_cache
55 # Find the current user
55 # Find the current user
56 User.current = find_current_user
56 User.current = find_current_user
57 end
57 end
58
58
59 # Returns the current user or nil if no user is logged in
59 # Returns the current user or nil if no user is logged in
60 # and starts a session if needed
60 # and starts a session if needed
61 def find_current_user
61 def find_current_user
62 if session[:user_id]
62 if session[:user_id]
63 # existing session
63 # existing session
64 (User.active.find(session[:user_id]) rescue nil)
64 (User.active.find(session[:user_id]) rescue nil)
65 elsif cookies[:autologin] && Setting.autologin?
65 elsif cookies[:autologin] && Setting.autologin?
66 # auto-login feature starts a new session
66 # auto-login feature starts a new session
67 user = User.try_to_autologin(cookies[:autologin])
67 user = User.try_to_autologin(cookies[:autologin])
68 session[:user_id] = user.id if user
68 session[:user_id] = user.id if user
69 user
69 user
70 elsif params[:format] == 'atom' && params[:key] && accept_key_auth_actions.include?(params[:action])
70 elsif params[:format] == 'atom' && params[:key] && accept_key_auth_actions.include?(params[:action])
71 # RSS key authentication does not start a session
71 # RSS key authentication does not start a session
72 User.find_by_rss_key(params[:key])
72 User.find_by_rss_key(params[:key])
73 elsif Setting.rest_api_enabled? && ['xml', 'json'].include?(params[:format]) && accept_key_auth_actions.include?(params[:action])
73 elsif Setting.rest_api_enabled? && ['xml', 'json'].include?(params[:format]) && accept_key_auth_actions.include?(params[:action])
74 if params[:key].present?
74 if params[:key].present?
75 # Use API key
75 # Use API key
76 User.find_by_api_key(params[:key])
76 User.find_by_api_key(params[:key])
77 else
77 else
78 # HTTP Basic, either username/password or API key/random
78 # HTTP Basic, either username/password or API key/random
79 authenticate_with_http_basic do |username, password|
79 authenticate_with_http_basic do |username, password|
80 User.try_to_login(username, password) || User.find_by_api_key(username)
80 User.try_to_login(username, password) || User.find_by_api_key(username)
81 end
81 end
82 end
82 end
83 end
83 end
84 end
84 end
85
85
86 # Sets the logged in user
86 # Sets the logged in user
87 def logged_user=(user)
87 def logged_user=(user)
88 reset_session
88 reset_session
89 if user && user.is_a?(User)
89 if user && user.is_a?(User)
90 User.current = user
90 User.current = user
91 session[:user_id] = user.id
91 session[:user_id] = user.id
92 else
92 else
93 User.current = User.anonymous
93 User.current = User.anonymous
94 end
94 end
95 end
95 end
96
96
97 # check if login is globally required to access the application
97 # check if login is globally required to access the application
98 def check_if_login_required
98 def check_if_login_required
99 # no check needed if user is already logged in
99 # no check needed if user is already logged in
100 return true if User.current.logged?
100 return true if User.current.logged?
101 require_login if Setting.login_required?
101 require_login if Setting.login_required?
102 end
102 end
103
103
104 def set_localization
104 def set_localization
105 lang = nil
105 lang = nil
106 if User.current.logged?
106 if User.current.logged?
107 lang = find_language(User.current.language)
107 lang = find_language(User.current.language)
108 end
108 end
109 if lang.nil? && request.env['HTTP_ACCEPT_LANGUAGE']
109 if lang.nil? && request.env['HTTP_ACCEPT_LANGUAGE']
110 accept_lang = parse_qvalues(request.env['HTTP_ACCEPT_LANGUAGE']).first.downcase
110 accept_lang = parse_qvalues(request.env['HTTP_ACCEPT_LANGUAGE']).first
111 if !accept_lang.blank?
111 if !accept_lang.blank?
112 accept_lang = accept_lang.downcase
112 lang = find_language(accept_lang) || find_language(accept_lang.split('-').first)
113 lang = find_language(accept_lang) || find_language(accept_lang.split('-').first)
113 end
114 end
114 end
115 end
115 lang ||= Setting.default_language
116 lang ||= Setting.default_language
116 set_language_if_valid(lang)
117 set_language_if_valid(lang)
117 end
118 end
118
119
119 def require_login
120 def require_login
120 if !User.current.logged?
121 if !User.current.logged?
121 # Extract only the basic url parameters on non-GET requests
122 # Extract only the basic url parameters on non-GET requests
122 if request.get?
123 if request.get?
123 url = url_for(params)
124 url = url_for(params)
124 else
125 else
125 url = url_for(:controller => params[:controller], :action => params[:action], :id => params[:id], :project_id => params[:project_id])
126 url = url_for(:controller => params[:controller], :action => params[:action], :id => params[:id], :project_id => params[:project_id])
126 end
127 end
127 respond_to do |format|
128 respond_to do |format|
128 format.html { redirect_to :controller => "account", :action => "login", :back_url => url }
129 format.html { redirect_to :controller => "account", :action => "login", :back_url => url }
129 format.atom { redirect_to :controller => "account", :action => "login", :back_url => url }
130 format.atom { redirect_to :controller => "account", :action => "login", :back_url => url }
130 format.xml { head :unauthorized }
131 format.xml { head :unauthorized }
131 format.json { head :unauthorized }
132 format.json { head :unauthorized }
132 end
133 end
133 return false
134 return false
134 end
135 end
135 true
136 true
136 end
137 end
137
138
138 def require_admin
139 def require_admin
139 return unless require_login
140 return unless require_login
140 if !User.current.admin?
141 if !User.current.admin?
141 render_403
142 render_403
142 return false
143 return false
143 end
144 end
144 true
145 true
145 end
146 end
146
147
147 def deny_access
148 def deny_access
148 User.current.logged? ? render_403 : require_login
149 User.current.logged? ? render_403 : require_login
149 end
150 end
150
151
151 # Authorize the user for the requested action
152 # Authorize the user for the requested action
152 def authorize(ctrl = params[:controller], action = params[:action], global = false)
153 def authorize(ctrl = params[:controller], action = params[:action], global = false)
153 allowed = User.current.allowed_to?({:controller => ctrl, :action => action}, @project, :global => global)
154 allowed = User.current.allowed_to?({:controller => ctrl, :action => action}, @project, :global => global)
154 allowed ? true : deny_access
155 allowed ? true : deny_access
155 end
156 end
156
157
157 # Authorize the user for the requested action outside a project
158 # Authorize the user for the requested action outside a project
158 def authorize_global(ctrl = params[:controller], action = params[:action], global = true)
159 def authorize_global(ctrl = params[:controller], action = params[:action], global = true)
159 authorize(ctrl, action, global)
160 authorize(ctrl, action, global)
160 end
161 end
161
162
162 # make sure that the user is a member of the project (or admin) if project is private
163 # make sure that the user is a member of the project (or admin) if project is private
163 # used as a before_filter for actions that do not require any particular permission on the project
164 # used as a before_filter for actions that do not require any particular permission on the project
164 def check_project_privacy
165 def check_project_privacy
165 if @project && @project.active?
166 if @project && @project.active?
166 if @project.is_public? || User.current.member_of?(@project) || User.current.admin?
167 if @project.is_public? || User.current.member_of?(@project) || User.current.admin?
167 true
168 true
168 else
169 else
169 User.current.logged? ? render_403 : require_login
170 User.current.logged? ? render_403 : require_login
170 end
171 end
171 else
172 else
172 @project = nil
173 @project = nil
173 render_404
174 render_404
174 false
175 false
175 end
176 end
176 end
177 end
177
178
178 def redirect_back_or_default(default)
179 def redirect_back_or_default(default)
179 back_url = CGI.unescape(params[:back_url].to_s)
180 back_url = CGI.unescape(params[:back_url].to_s)
180 if !back_url.blank?
181 if !back_url.blank?
181 begin
182 begin
182 uri = URI.parse(back_url)
183 uri = URI.parse(back_url)
183 # do not redirect user to another host or to the login or register page
184 # do not redirect user to another host or to the login or register page
184 if (uri.relative? || (uri.host == request.host)) && !uri.path.match(%r{/(login|account/register)})
185 if (uri.relative? || (uri.host == request.host)) && !uri.path.match(%r{/(login|account/register)})
185 redirect_to(back_url)
186 redirect_to(back_url)
186 return
187 return
187 end
188 end
188 rescue URI::InvalidURIError
189 rescue URI::InvalidURIError
189 # redirect to default
190 # redirect to default
190 end
191 end
191 end
192 end
192 redirect_to default
193 redirect_to default
193 end
194 end
194
195
195 def render_403
196 def render_403
196 @project = nil
197 @project = nil
197 render :template => "common/403", :layout => (request.xhr? ? false : 'base'), :status => 403
198 render :template => "common/403", :layout => (request.xhr? ? false : 'base'), :status => 403
198 return false
199 return false
199 end
200 end
200
201
201 def render_404
202 def render_404
202 render :template => "common/404", :layout => !request.xhr?, :status => 404
203 render :template => "common/404", :layout => !request.xhr?, :status => 404
203 return false
204 return false
204 end
205 end
205
206
206 def render_error(msg)
207 def render_error(msg)
207 flash.now[:error] = msg
208 flash.now[:error] = msg
208 render :text => '', :layout => !request.xhr?, :status => 500
209 render :text => '', :layout => !request.xhr?, :status => 500
209 end
210 end
210
211
211 def invalid_authenticity_token
212 def invalid_authenticity_token
212 render_error "Invalid form authenticity token."
213 render_error "Invalid form authenticity token."
213 end
214 end
214
215
215 def render_feed(items, options={})
216 def render_feed(items, options={})
216 @items = items || []
217 @items = items || []
217 @items.sort! {|x,y| y.event_datetime <=> x.event_datetime }
218 @items.sort! {|x,y| y.event_datetime <=> x.event_datetime }
218 @items = @items.slice(0, Setting.feeds_limit.to_i)
219 @items = @items.slice(0, Setting.feeds_limit.to_i)
219 @title = options[:title] || Setting.app_title
220 @title = options[:title] || Setting.app_title
220 render :template => "common/feed.atom.rxml", :layout => false, :content_type => 'application/atom+xml'
221 render :template => "common/feed.atom.rxml", :layout => false, :content_type => 'application/atom+xml'
221 end
222 end
222
223
223 def self.accept_key_auth(*actions)
224 def self.accept_key_auth(*actions)
224 actions = actions.flatten.map(&:to_s)
225 actions = actions.flatten.map(&:to_s)
225 write_inheritable_attribute('accept_key_auth_actions', actions)
226 write_inheritable_attribute('accept_key_auth_actions', actions)
226 end
227 end
227
228
228 def accept_key_auth_actions
229 def accept_key_auth_actions
229 self.class.read_inheritable_attribute('accept_key_auth_actions') || []
230 self.class.read_inheritable_attribute('accept_key_auth_actions') || []
230 end
231 end
231
232
232 # TODO: move to model
233 # TODO: move to model
233 def attach_files(obj, attachments)
234 def attach_files(obj, attachments)
234 attached = []
235 attached = []
235 unsaved = []
236 unsaved = []
236 if attachments && attachments.is_a?(Hash)
237 if attachments && attachments.is_a?(Hash)
237 attachments.each_value do |attachment|
238 attachments.each_value do |attachment|
238 file = attachment['file']
239 file = attachment['file']
239 next unless file && file.size > 0
240 next unless file && file.size > 0
240 a = Attachment.create(:container => obj,
241 a = Attachment.create(:container => obj,
241 :file => file,
242 :file => file,
242 :description => attachment['description'].to_s.strip,
243 :description => attachment['description'].to_s.strip,
243 :author => User.current)
244 :author => User.current)
244 a.new_record? ? (unsaved << a) : (attached << a)
245 a.new_record? ? (unsaved << a) : (attached << a)
245 end
246 end
246 if unsaved.any?
247 if unsaved.any?
247 flash[:warning] = l(:warning_attachments_not_saved, unsaved.size)
248 flash[:warning] = l(:warning_attachments_not_saved, unsaved.size)
248 end
249 end
249 end
250 end
250 attached
251 attached
251 end
252 end
252
253
253 # Returns the number of objects that should be displayed
254 # Returns the number of objects that should be displayed
254 # on the paginated list
255 # on the paginated list
255 def per_page_option
256 def per_page_option
256 per_page = nil
257 per_page = nil
257 if params[:per_page] && Setting.per_page_options_array.include?(params[:per_page].to_s.to_i)
258 if params[:per_page] && Setting.per_page_options_array.include?(params[:per_page].to_s.to_i)
258 per_page = params[:per_page].to_s.to_i
259 per_page = params[:per_page].to_s.to_i
259 session[:per_page] = per_page
260 session[:per_page] = per_page
260 elsif session[:per_page]
261 elsif session[:per_page]
261 per_page = session[:per_page]
262 per_page = session[:per_page]
262 else
263 else
263 per_page = Setting.per_page_options_array.first || 25
264 per_page = Setting.per_page_options_array.first || 25
264 end
265 end
265 per_page
266 per_page
266 end
267 end
267
268
268 # qvalues http header parser
269 # qvalues http header parser
269 # code taken from webrick
270 # code taken from webrick
270 def parse_qvalues(value)
271 def parse_qvalues(value)
271 tmp = []
272 tmp = []
272 if value
273 if value
273 parts = value.split(/,\s*/)
274 parts = value.split(/,\s*/)
274 parts.each {|part|
275 parts.each {|part|
275 if m = %r{^([^\s,]+?)(?:;\s*q=(\d+(?:\.\d+)?))?$}.match(part)
276 if m = %r{^([^\s,]+?)(?:;\s*q=(\d+(?:\.\d+)?))?$}.match(part)
276 val = m[1]
277 val = m[1]
277 q = (m[2] or 1).to_f
278 q = (m[2] or 1).to_f
278 tmp.push([val, q])
279 tmp.push([val, q])
279 end
280 end
280 }
281 }
281 tmp = tmp.sort_by{|val, q| -q}
282 tmp = tmp.sort_by{|val, q| -q}
282 tmp.collect!{|val, q| val}
283 tmp.collect!{|val, q| val}
283 end
284 end
284 return tmp
285 return tmp
285 rescue
286 rescue
286 nil
287 nil
287 end
288 end
288
289
289 # Returns a string that can be used as filename value in Content-Disposition header
290 # Returns a string that can be used as filename value in Content-Disposition header
290 def filename_for_content_disposition(name)
291 def filename_for_content_disposition(name)
291 request.env['HTTP_USER_AGENT'] =~ %r{MSIE} ? ERB::Util.url_encode(name) : name
292 request.env['HTTP_USER_AGENT'] =~ %r{MSIE} ? ERB::Util.url_encode(name) : name
292 end
293 end
293 end
294 end
General Comments 0
You need to be logged in to leave comments. Login now