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