##// END OF EJS Templates
Display an error when authenticity token is invalid....
Jean-Philippe Lang -
r2980:f3bcb705f746
parent child
Show More
@@ -1,270 +1,276
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 and return false
33 redirect_to home_path and return false
34 end
34 end
35 end
35 end
36
36
37 before_filter :user_setup, :check_if_login_required, :set_localization
37 before_filter :user_setup, :check_if_login_required, :set_localization
38 filter_parameter_logging :password
38 filter_parameter_logging :password
39 protect_from_forgery
39 protect_from_forgery
40
40
41 rescue_from ActionController::InvalidAuthenticityToken, :with => :invalid_authenticity_token
42
41 include Redmine::Search::Controller
43 include Redmine::Search::Controller
42 include Redmine::MenuManager::MenuController
44 include Redmine::MenuManager::MenuController
43 helper Redmine::MenuManager::MenuHelper
45 helper Redmine::MenuManager::MenuHelper
44
46
45 REDMINE_SUPPORTED_SCM.each do |scm|
47 REDMINE_SUPPORTED_SCM.each do |scm|
46 require_dependency "repository/#{scm.underscore}"
48 require_dependency "repository/#{scm.underscore}"
47 end
49 end
48
50
49 def user_setup
51 def user_setup
50 # Check the settings cache for each request
52 # Check the settings cache for each request
51 Setting.check_cache
53 Setting.check_cache
52 # Find the current user
54 # Find the current user
53 User.current = find_current_user
55 User.current = find_current_user
54 end
56 end
55
57
56 # Returns the current user or nil if no user is logged in
58 # Returns the current user or nil if no user is logged in
57 # and starts a session if needed
59 # and starts a session if needed
58 def find_current_user
60 def find_current_user
59 if session[:user_id]
61 if session[:user_id]
60 # existing session
62 # existing session
61 (User.active.find(session[:user_id]) rescue nil)
63 (User.active.find(session[:user_id]) rescue nil)
62 elsif cookies[:autologin] && Setting.autologin?
64 elsif cookies[:autologin] && Setting.autologin?
63 # auto-login feature starts a new session
65 # auto-login feature starts a new session
64 user = User.try_to_autologin(cookies[:autologin])
66 user = User.try_to_autologin(cookies[:autologin])
65 session[:user_id] = user.id if user
67 session[:user_id] = user.id if user
66 user
68 user
67 elsif params[:format] == 'atom' && params[:key] && accept_key_auth_actions.include?(params[:action])
69 elsif params[:format] == 'atom' && params[:key] && accept_key_auth_actions.include?(params[:action])
68 # RSS key authentication does not start a session
70 # RSS key authentication does not start a session
69 User.find_by_rss_key(params[:key])
71 User.find_by_rss_key(params[:key])
70 end
72 end
71 end
73 end
72
74
73 # Sets the logged in user
75 # Sets the logged in user
74 def logged_user=(user)
76 def logged_user=(user)
75 reset_session
77 reset_session
76 if user && user.is_a?(User)
78 if user && user.is_a?(User)
77 User.current = user
79 User.current = user
78 session[:user_id] = user.id
80 session[:user_id] = user.id
79 else
81 else
80 User.current = User.anonymous
82 User.current = User.anonymous
81 end
83 end
82 end
84 end
83
85
84 # check if login is globally required to access the application
86 # check if login is globally required to access the application
85 def check_if_login_required
87 def check_if_login_required
86 # no check needed if user is already logged in
88 # no check needed if user is already logged in
87 return true if User.current.logged?
89 return true if User.current.logged?
88 require_login if Setting.login_required?
90 require_login if Setting.login_required?
89 end
91 end
90
92
91 def set_localization
93 def set_localization
92 lang = nil
94 lang = nil
93 if User.current.logged?
95 if User.current.logged?
94 lang = find_language(User.current.language)
96 lang = find_language(User.current.language)
95 end
97 end
96 if lang.nil? && request.env['HTTP_ACCEPT_LANGUAGE']
98 if lang.nil? && request.env['HTTP_ACCEPT_LANGUAGE']
97 accept_lang = parse_qvalues(request.env['HTTP_ACCEPT_LANGUAGE']).first.downcase
99 accept_lang = parse_qvalues(request.env['HTTP_ACCEPT_LANGUAGE']).first.downcase
98 if !accept_lang.blank?
100 if !accept_lang.blank?
99 lang = find_language(accept_lang) || find_language(accept_lang.split('-').first)
101 lang = find_language(accept_lang) || find_language(accept_lang.split('-').first)
100 end
102 end
101 end
103 end
102 lang ||= Setting.default_language
104 lang ||= Setting.default_language
103 set_language_if_valid(lang)
105 set_language_if_valid(lang)
104 end
106 end
105
107
106 def require_login
108 def require_login
107 if !User.current.logged?
109 if !User.current.logged?
108 # Extract only the basic url parameters on non-GET requests
110 # Extract only the basic url parameters on non-GET requests
109 if request.get?
111 if request.get?
110 url = url_for(params)
112 url = url_for(params)
111 else
113 else
112 url = url_for(:controller => params[:controller], :action => params[:action], :id => params[:id], :project_id => params[:project_id])
114 url = url_for(:controller => params[:controller], :action => params[:action], :id => params[:id], :project_id => params[:project_id])
113 end
115 end
114 redirect_to :controller => "account", :action => "login", :back_url => url
116 redirect_to :controller => "account", :action => "login", :back_url => url
115 return false
117 return false
116 end
118 end
117 true
119 true
118 end
120 end
119
121
120 def require_admin
122 def require_admin
121 return unless require_login
123 return unless require_login
122 if !User.current.admin?
124 if !User.current.admin?
123 render_403
125 render_403
124 return false
126 return false
125 end
127 end
126 true
128 true
127 end
129 end
128
130
129 def deny_access
131 def deny_access
130 User.current.logged? ? render_403 : require_login
132 User.current.logged? ? render_403 : require_login
131 end
133 end
132
134
133 # Authorize the user for the requested action
135 # Authorize the user for the requested action
134 def authorize(ctrl = params[:controller], action = params[:action], global = false)
136 def authorize(ctrl = params[:controller], action = params[:action], global = false)
135 allowed = User.current.allowed_to?({:controller => ctrl, :action => action}, @project, :global => global)
137 allowed = User.current.allowed_to?({:controller => ctrl, :action => action}, @project, :global => global)
136 allowed ? true : deny_access
138 allowed ? true : deny_access
137 end
139 end
138
140
139 # Authorize the user for the requested action outside a project
141 # Authorize the user for the requested action outside a project
140 def authorize_global(ctrl = params[:controller], action = params[:action], global = true)
142 def authorize_global(ctrl = params[:controller], action = params[:action], global = true)
141 authorize(ctrl, action, global)
143 authorize(ctrl, action, global)
142 end
144 end
143
145
144 # make sure that the user is a member of the project (or admin) if project is private
146 # make sure that the user is a member of the project (or admin) if project is private
145 # used as a before_filter for actions that do not require any particular permission on the project
147 # used as a before_filter for actions that do not require any particular permission on the project
146 def check_project_privacy
148 def check_project_privacy
147 if @project && @project.active?
149 if @project && @project.active?
148 if @project.is_public? || User.current.member_of?(@project) || User.current.admin?
150 if @project.is_public? || User.current.member_of?(@project) || User.current.admin?
149 true
151 true
150 else
152 else
151 User.current.logged? ? render_403 : require_login
153 User.current.logged? ? render_403 : require_login
152 end
154 end
153 else
155 else
154 @project = nil
156 @project = nil
155 render_404
157 render_404
156 false
158 false
157 end
159 end
158 end
160 end
159
161
160 def redirect_back_or_default(default)
162 def redirect_back_or_default(default)
161 back_url = CGI.unescape(params[:back_url].to_s)
163 back_url = CGI.unescape(params[:back_url].to_s)
162 if !back_url.blank?
164 if !back_url.blank?
163 begin
165 begin
164 uri = URI.parse(back_url)
166 uri = URI.parse(back_url)
165 # do not redirect user to another host or to the login or register page
167 # do not redirect user to another host or to the login or register page
166 if (uri.relative? || (uri.host == request.host)) && !uri.path.match(%r{/(login|account/register)})
168 if (uri.relative? || (uri.host == request.host)) && !uri.path.match(%r{/(login|account/register)})
167 redirect_to(back_url) and return
169 redirect_to(back_url) and return
168 end
170 end
169 rescue URI::InvalidURIError
171 rescue URI::InvalidURIError
170 # redirect to default
172 # redirect to default
171 end
173 end
172 end
174 end
173 redirect_to default
175 redirect_to default
174 end
176 end
175
177
176 def render_403
178 def render_403
177 @project = nil
179 @project = nil
178 render :template => "common/403", :layout => !request.xhr?, :status => 403
180 render :template => "common/403", :layout => !request.xhr?, :status => 403
179 return false
181 return false
180 end
182 end
181
183
182 def render_404
184 def render_404
183 render :template => "common/404", :layout => !request.xhr?, :status => 404
185 render :template => "common/404", :layout => !request.xhr?, :status => 404
184 return false
186 return false
185 end
187 end
186
188
187 def render_error(msg)
189 def render_error(msg)
188 flash.now[:error] = msg
190 flash.now[:error] = msg
189 render :text => '', :layout => !request.xhr?, :status => 500
191 render :text => '', :layout => !request.xhr?, :status => 500
190 end
192 end
191
193
194 def invalid_authenticity_token
195 render_error "Invalid form authenticity token."
196 end
197
192 def render_feed(items, options={})
198 def render_feed(items, options={})
193 @items = items || []
199 @items = items || []
194 @items.sort! {|x,y| y.event_datetime <=> x.event_datetime }
200 @items.sort! {|x,y| y.event_datetime <=> x.event_datetime }
195 @items = @items.slice(0, Setting.feeds_limit.to_i)
201 @items = @items.slice(0, Setting.feeds_limit.to_i)
196 @title = options[:title] || Setting.app_title
202 @title = options[:title] || Setting.app_title
197 render :template => "common/feed.atom.rxml", :layout => false, :content_type => 'application/atom+xml'
203 render :template => "common/feed.atom.rxml", :layout => false, :content_type => 'application/atom+xml'
198 end
204 end
199
205
200 def self.accept_key_auth(*actions)
206 def self.accept_key_auth(*actions)
201 actions = actions.flatten.map(&:to_s)
207 actions = actions.flatten.map(&:to_s)
202 write_inheritable_attribute('accept_key_auth_actions', actions)
208 write_inheritable_attribute('accept_key_auth_actions', actions)
203 end
209 end
204
210
205 def accept_key_auth_actions
211 def accept_key_auth_actions
206 self.class.read_inheritable_attribute('accept_key_auth_actions') || []
212 self.class.read_inheritable_attribute('accept_key_auth_actions') || []
207 end
213 end
208
214
209 # TODO: move to model
215 # TODO: move to model
210 def attach_files(obj, attachments)
216 def attach_files(obj, attachments)
211 attached = []
217 attached = []
212 unsaved = []
218 unsaved = []
213 if attachments && attachments.is_a?(Hash)
219 if attachments && attachments.is_a?(Hash)
214 attachments.each_value do |attachment|
220 attachments.each_value do |attachment|
215 file = attachment['file']
221 file = attachment['file']
216 next unless file && file.size > 0
222 next unless file && file.size > 0
217 a = Attachment.create(:container => obj,
223 a = Attachment.create(:container => obj,
218 :file => file,
224 :file => file,
219 :description => attachment['description'].to_s.strip,
225 :description => attachment['description'].to_s.strip,
220 :author => User.current)
226 :author => User.current)
221 a.new_record? ? (unsaved << a) : (attached << a)
227 a.new_record? ? (unsaved << a) : (attached << a)
222 end
228 end
223 if unsaved.any?
229 if unsaved.any?
224 flash[:warning] = l(:warning_attachments_not_saved, unsaved.size)
230 flash[:warning] = l(:warning_attachments_not_saved, unsaved.size)
225 end
231 end
226 end
232 end
227 attached
233 attached
228 end
234 end
229
235
230 # Returns the number of objects that should be displayed
236 # Returns the number of objects that should be displayed
231 # on the paginated list
237 # on the paginated list
232 def per_page_option
238 def per_page_option
233 per_page = nil
239 per_page = nil
234 if params[:per_page] && Setting.per_page_options_array.include?(params[:per_page].to_s.to_i)
240 if params[:per_page] && Setting.per_page_options_array.include?(params[:per_page].to_s.to_i)
235 per_page = params[:per_page].to_s.to_i
241 per_page = params[:per_page].to_s.to_i
236 session[:per_page] = per_page
242 session[:per_page] = per_page
237 elsif session[:per_page]
243 elsif session[:per_page]
238 per_page = session[:per_page]
244 per_page = session[:per_page]
239 else
245 else
240 per_page = Setting.per_page_options_array.first || 25
246 per_page = Setting.per_page_options_array.first || 25
241 end
247 end
242 per_page
248 per_page
243 end
249 end
244
250
245 # qvalues http header parser
251 # qvalues http header parser
246 # code taken from webrick
252 # code taken from webrick
247 def parse_qvalues(value)
253 def parse_qvalues(value)
248 tmp = []
254 tmp = []
249 if value
255 if value
250 parts = value.split(/,\s*/)
256 parts = value.split(/,\s*/)
251 parts.each {|part|
257 parts.each {|part|
252 if m = %r{^([^\s,]+?)(?:;\s*q=(\d+(?:\.\d+)?))?$}.match(part)
258 if m = %r{^([^\s,]+?)(?:;\s*q=(\d+(?:\.\d+)?))?$}.match(part)
253 val = m[1]
259 val = m[1]
254 q = (m[2] or 1).to_f
260 q = (m[2] or 1).to_f
255 tmp.push([val, q])
261 tmp.push([val, q])
256 end
262 end
257 }
263 }
258 tmp = tmp.sort_by{|val, q| -q}
264 tmp = tmp.sort_by{|val, q| -q}
259 tmp.collect!{|val, q| val}
265 tmp.collect!{|val, q| val}
260 end
266 end
261 return tmp
267 return tmp
262 rescue
268 rescue
263 nil
269 nil
264 end
270 end
265
271
266 # Returns a string that can be used as filename value in Content-Disposition header
272 # Returns a string that can be used as filename value in Content-Disposition header
267 def filename_for_content_disposition(name)
273 def filename_for_content_disposition(name)
268 request.env['HTTP_USER_AGENT'] =~ %r{MSIE} ? ERB::Util.url_encode(name) : name
274 request.env['HTTP_USER_AGENT'] =~ %r{MSIE} ? ERB::Util.url_encode(name) : name
269 end
275 end
270 end
276 end
General Comments 0
You need to be logged in to leave comments. Login now