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