@@ -1,548 +1,550 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2011 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2011 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 Unauthorized < Exception; end |
|
21 | class Unauthorized < Exception; end | |
22 |
|
22 | |||
23 | class ApplicationController < ActionController::Base |
|
23 | class ApplicationController < ActionController::Base | |
24 | include Redmine::I18n |
|
24 | include Redmine::I18n | |
25 |
|
25 | |||
26 | layout 'base' |
|
26 | layout 'base' | |
27 | exempt_from_layout 'builder', 'rsb' |
|
27 | exempt_from_layout 'builder', 'rsb' | |
28 |
|
28 | |||
29 | protect_from_forgery |
|
29 | protect_from_forgery | |
30 | def handle_unverified_request |
|
30 | def handle_unverified_request | |
31 | super |
|
31 | super | |
32 | cookies.delete(:autologin) |
|
32 | cookies.delete(:autologin) | |
33 | end |
|
33 | end | |
34 | # Remove broken cookie after upgrade from 0.8.x (#4292) |
|
34 | # Remove broken cookie after upgrade from 0.8.x (#4292) | |
35 | # See https://rails.lighthouseapp.com/projects/8994/tickets/3360 |
|
35 | # See https://rails.lighthouseapp.com/projects/8994/tickets/3360 | |
36 | # TODO: remove it when Rails is fixed |
|
36 | # TODO: remove it when Rails is fixed | |
37 | before_filter :delete_broken_cookies |
|
37 | before_filter :delete_broken_cookies | |
38 | def delete_broken_cookies |
|
38 | def delete_broken_cookies | |
39 | if cookies['_redmine_session'] && cookies['_redmine_session'] !~ /--/ |
|
39 | if cookies['_redmine_session'] && cookies['_redmine_session'] !~ /--/ | |
40 | cookies.delete '_redmine_session' |
|
40 | cookies.delete '_redmine_session' | |
41 | redirect_to home_path |
|
41 | redirect_to home_path | |
42 | return false |
|
42 | return false | |
43 | end |
|
43 | end | |
44 | end |
|
44 | end | |
45 |
|
45 | |||
46 | # FIXME: Remove this when all of Rack and Rails have learned how to |
|
46 | # FIXME: Remove this when all of Rack and Rails have learned how to | |
47 | # properly use encodings |
|
47 | # properly use encodings | |
48 | before_filter :params_filter |
|
48 | before_filter :params_filter | |
49 |
|
49 | |||
50 | def params_filter |
|
50 | def params_filter | |
51 | if RUBY_VERSION >= '1.9' && defined?(Rails) && Rails::VERSION::MAJOR < 3 |
|
51 | if RUBY_VERSION >= '1.9' && defined?(Rails) && Rails::VERSION::MAJOR < 3 | |
52 | self.utf8nize!(params) |
|
52 | self.utf8nize!(params) | |
53 | end |
|
53 | end | |
54 | end |
|
54 | end | |
55 |
|
55 | |||
56 | def utf8nize!(obj) |
|
56 | def utf8nize!(obj) | |
57 | if obj.is_a? String |
|
57 | if obj.frozen? | |
|
58 | obj | |||
|
59 | elsif obj.is_a? String | |||
58 | obj.respond_to?(:force_encoding) ? obj.force_encoding("UTF-8") : obj |
|
60 | obj.respond_to?(:force_encoding) ? obj.force_encoding("UTF-8") : obj | |
59 | elsif obj.is_a? Hash |
|
61 | elsif obj.is_a? Hash | |
60 | obj.each {|k, v| obj[k] = self.utf8nize!(v)} |
|
62 | obj.each {|k, v| obj[k] = self.utf8nize!(v)} | |
61 | elsif obj.is_a? Array |
|
63 | elsif obj.is_a? Array | |
62 | obj.each {|v| self.utf8nize!(v)} |
|
64 | obj.each {|v| self.utf8nize!(v)} | |
63 | else |
|
65 | else | |
64 | obj |
|
66 | obj | |
65 | end |
|
67 | end | |
66 | end |
|
68 | end | |
67 |
|
69 | |||
68 | before_filter :user_setup, :check_if_login_required, :set_localization |
|
70 | before_filter :user_setup, :check_if_login_required, :set_localization | |
69 | filter_parameter_logging :password |
|
71 | filter_parameter_logging :password | |
70 |
|
72 | |||
71 | rescue_from ActionController::InvalidAuthenticityToken, :with => :invalid_authenticity_token |
|
73 | rescue_from ActionController::InvalidAuthenticityToken, :with => :invalid_authenticity_token | |
72 | rescue_from ::Unauthorized, :with => :deny_access |
|
74 | rescue_from ::Unauthorized, :with => :deny_access | |
73 |
|
75 | |||
74 | include Redmine::Search::Controller |
|
76 | include Redmine::Search::Controller | |
75 | include Redmine::MenuManager::MenuController |
|
77 | include Redmine::MenuManager::MenuController | |
76 | helper Redmine::MenuManager::MenuHelper |
|
78 | helper Redmine::MenuManager::MenuHelper | |
77 |
|
79 | |||
78 | Redmine::Scm::Base.all.each do |scm| |
|
80 | Redmine::Scm::Base.all.each do |scm| | |
79 | require_dependency "repository/#{scm.underscore}" |
|
81 | require_dependency "repository/#{scm.underscore}" | |
80 | end |
|
82 | end | |
81 |
|
83 | |||
82 | def user_setup |
|
84 | def user_setup | |
83 | # Check the settings cache for each request |
|
85 | # Check the settings cache for each request | |
84 | Setting.check_cache |
|
86 | Setting.check_cache | |
85 | # Find the current user |
|
87 | # Find the current user | |
86 | User.current = find_current_user |
|
88 | User.current = find_current_user | |
87 | end |
|
89 | end | |
88 |
|
90 | |||
89 | # Returns the current user or nil if no user is logged in |
|
91 | # Returns the current user or nil if no user is logged in | |
90 | # and starts a session if needed |
|
92 | # and starts a session if needed | |
91 | def find_current_user |
|
93 | def find_current_user | |
92 | if session[:user_id] |
|
94 | if session[:user_id] | |
93 | # existing session |
|
95 | # existing session | |
94 | (User.active.find(session[:user_id]) rescue nil) |
|
96 | (User.active.find(session[:user_id]) rescue nil) | |
95 | elsif cookies[:autologin] && Setting.autologin? |
|
97 | elsif cookies[:autologin] && Setting.autologin? | |
96 | # auto-login feature starts a new session |
|
98 | # auto-login feature starts a new session | |
97 | user = User.try_to_autologin(cookies[:autologin]) |
|
99 | user = User.try_to_autologin(cookies[:autologin]) | |
98 | session[:user_id] = user.id if user |
|
100 | session[:user_id] = user.id if user | |
99 | user |
|
101 | user | |
100 | elsif params[:format] == 'atom' && params[:key] && request.get? && accept_rss_auth? |
|
102 | elsif params[:format] == 'atom' && params[:key] && request.get? && accept_rss_auth? | |
101 | # RSS key authentication does not start a session |
|
103 | # RSS key authentication does not start a session | |
102 | User.find_by_rss_key(params[:key]) |
|
104 | User.find_by_rss_key(params[:key]) | |
103 | elsif Setting.rest_api_enabled? && accept_api_auth? |
|
105 | elsif Setting.rest_api_enabled? && accept_api_auth? | |
104 | if (key = api_key_from_request) |
|
106 | if (key = api_key_from_request) | |
105 | # Use API key |
|
107 | # Use API key | |
106 | User.find_by_api_key(key) |
|
108 | User.find_by_api_key(key) | |
107 | else |
|
109 | else | |
108 | # HTTP Basic, either username/password or API key/random |
|
110 | # HTTP Basic, either username/password or API key/random | |
109 | authenticate_with_http_basic do |username, password| |
|
111 | authenticate_with_http_basic do |username, password| | |
110 | User.try_to_login(username, password) || User.find_by_api_key(username) |
|
112 | User.try_to_login(username, password) || User.find_by_api_key(username) | |
111 | end |
|
113 | end | |
112 | end |
|
114 | end | |
113 | end |
|
115 | end | |
114 | end |
|
116 | end | |
115 |
|
117 | |||
116 | # Sets the logged in user |
|
118 | # Sets the logged in user | |
117 | def logged_user=(user) |
|
119 | def logged_user=(user) | |
118 | reset_session |
|
120 | reset_session | |
119 | if user && user.is_a?(User) |
|
121 | if user && user.is_a?(User) | |
120 | User.current = user |
|
122 | User.current = user | |
121 | session[:user_id] = user.id |
|
123 | session[:user_id] = user.id | |
122 | else |
|
124 | else | |
123 | User.current = User.anonymous |
|
125 | User.current = User.anonymous | |
124 | end |
|
126 | end | |
125 | end |
|
127 | end | |
126 |
|
128 | |||
127 | # check if login is globally required to access the application |
|
129 | # check if login is globally required to access the application | |
128 | def check_if_login_required |
|
130 | def check_if_login_required | |
129 | # no check needed if user is already logged in |
|
131 | # no check needed if user is already logged in | |
130 | return true if User.current.logged? |
|
132 | return true if User.current.logged? | |
131 | require_login if Setting.login_required? |
|
133 | require_login if Setting.login_required? | |
132 | end |
|
134 | end | |
133 |
|
135 | |||
134 | def set_localization |
|
136 | def set_localization | |
135 | lang = nil |
|
137 | lang = nil | |
136 | if User.current.logged? |
|
138 | if User.current.logged? | |
137 | lang = find_language(User.current.language) |
|
139 | lang = find_language(User.current.language) | |
138 | end |
|
140 | end | |
139 | if lang.nil? && request.env['HTTP_ACCEPT_LANGUAGE'] |
|
141 | if lang.nil? && request.env['HTTP_ACCEPT_LANGUAGE'] | |
140 | accept_lang = parse_qvalues(request.env['HTTP_ACCEPT_LANGUAGE']).first |
|
142 | accept_lang = parse_qvalues(request.env['HTTP_ACCEPT_LANGUAGE']).first | |
141 | if !accept_lang.blank? |
|
143 | if !accept_lang.blank? | |
142 | accept_lang = accept_lang.downcase |
|
144 | accept_lang = accept_lang.downcase | |
143 | lang = find_language(accept_lang) || find_language(accept_lang.split('-').first) |
|
145 | lang = find_language(accept_lang) || find_language(accept_lang.split('-').first) | |
144 | end |
|
146 | end | |
145 | end |
|
147 | end | |
146 | lang ||= Setting.default_language |
|
148 | lang ||= Setting.default_language | |
147 | set_language_if_valid(lang) |
|
149 | set_language_if_valid(lang) | |
148 | end |
|
150 | end | |
149 |
|
151 | |||
150 | def require_login |
|
152 | def require_login | |
151 | if !User.current.logged? |
|
153 | if !User.current.logged? | |
152 | # Extract only the basic url parameters on non-GET requests |
|
154 | # Extract only the basic url parameters on non-GET requests | |
153 | if request.get? |
|
155 | if request.get? | |
154 | url = url_for(params) |
|
156 | url = url_for(params) | |
155 | else |
|
157 | else | |
156 | url = url_for(:controller => params[:controller], :action => params[:action], :id => params[:id], :project_id => params[:project_id]) |
|
158 | url = url_for(:controller => params[:controller], :action => params[:action], :id => params[:id], :project_id => params[:project_id]) | |
157 | end |
|
159 | end | |
158 | respond_to do |format| |
|
160 | respond_to do |format| | |
159 | format.html { redirect_to :controller => "account", :action => "login", :back_url => url } |
|
161 | format.html { redirect_to :controller => "account", :action => "login", :back_url => url } | |
160 | format.atom { redirect_to :controller => "account", :action => "login", :back_url => url } |
|
162 | format.atom { redirect_to :controller => "account", :action => "login", :back_url => url } | |
161 | format.xml { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' } |
|
163 | format.xml { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' } | |
162 | format.js { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' } |
|
164 | format.js { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' } | |
163 | format.json { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' } |
|
165 | format.json { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' } | |
164 | end |
|
166 | end | |
165 | return false |
|
167 | return false | |
166 | end |
|
168 | end | |
167 | true |
|
169 | true | |
168 | end |
|
170 | end | |
169 |
|
171 | |||
170 | def require_admin |
|
172 | def require_admin | |
171 | return unless require_login |
|
173 | return unless require_login | |
172 | if !User.current.admin? |
|
174 | if !User.current.admin? | |
173 | render_403 |
|
175 | render_403 | |
174 | return false |
|
176 | return false | |
175 | end |
|
177 | end | |
176 | true |
|
178 | true | |
177 | end |
|
179 | end | |
178 |
|
180 | |||
179 | def deny_access |
|
181 | def deny_access | |
180 | User.current.logged? ? render_403 : require_login |
|
182 | User.current.logged? ? render_403 : require_login | |
181 | end |
|
183 | end | |
182 |
|
184 | |||
183 | # Authorize the user for the requested action |
|
185 | # Authorize the user for the requested action | |
184 | def authorize(ctrl = params[:controller], action = params[:action], global = false) |
|
186 | def authorize(ctrl = params[:controller], action = params[:action], global = false) | |
185 | allowed = User.current.allowed_to?({:controller => ctrl, :action => action}, @project || @projects, :global => global) |
|
187 | allowed = User.current.allowed_to?({:controller => ctrl, :action => action}, @project || @projects, :global => global) | |
186 | if allowed |
|
188 | if allowed | |
187 | true |
|
189 | true | |
188 | else |
|
190 | else | |
189 | if @project && @project.archived? |
|
191 | if @project && @project.archived? | |
190 | render_403 :message => :notice_not_authorized_archived_project |
|
192 | render_403 :message => :notice_not_authorized_archived_project | |
191 | else |
|
193 | else | |
192 | deny_access |
|
194 | deny_access | |
193 | end |
|
195 | end | |
194 | end |
|
196 | end | |
195 | end |
|
197 | end | |
196 |
|
198 | |||
197 | # Authorize the user for the requested action outside a project |
|
199 | # Authorize the user for the requested action outside a project | |
198 | def authorize_global(ctrl = params[:controller], action = params[:action], global = true) |
|
200 | def authorize_global(ctrl = params[:controller], action = params[:action], global = true) | |
199 | authorize(ctrl, action, global) |
|
201 | authorize(ctrl, action, global) | |
200 | end |
|
202 | end | |
201 |
|
203 | |||
202 | # Find project of id params[:id] |
|
204 | # Find project of id params[:id] | |
203 | def find_project |
|
205 | def find_project | |
204 | @project = Project.find(params[:id]) |
|
206 | @project = Project.find(params[:id]) | |
205 | rescue ActiveRecord::RecordNotFound |
|
207 | rescue ActiveRecord::RecordNotFound | |
206 | render_404 |
|
208 | render_404 | |
207 | end |
|
209 | end | |
208 |
|
210 | |||
209 | # Find project of id params[:project_id] |
|
211 | # Find project of id params[:project_id] | |
210 | def find_project_by_project_id |
|
212 | def find_project_by_project_id | |
211 | @project = Project.find(params[:project_id]) |
|
213 | @project = Project.find(params[:project_id]) | |
212 | rescue ActiveRecord::RecordNotFound |
|
214 | rescue ActiveRecord::RecordNotFound | |
213 | render_404 |
|
215 | render_404 | |
214 | end |
|
216 | end | |
215 |
|
217 | |||
216 | # Find a project based on params[:project_id] |
|
218 | # Find a project based on params[:project_id] | |
217 | # TODO: some subclasses override this, see about merging their logic |
|
219 | # TODO: some subclasses override this, see about merging their logic | |
218 | def find_optional_project |
|
220 | def find_optional_project | |
219 | @project = Project.find(params[:project_id]) unless params[:project_id].blank? |
|
221 | @project = Project.find(params[:project_id]) unless params[:project_id].blank? | |
220 | allowed = User.current.allowed_to?({:controller => params[:controller], :action => params[:action]}, @project, :global => true) |
|
222 | allowed = User.current.allowed_to?({:controller => params[:controller], :action => params[:action]}, @project, :global => true) | |
221 | allowed ? true : deny_access |
|
223 | allowed ? true : deny_access | |
222 | rescue ActiveRecord::RecordNotFound |
|
224 | rescue ActiveRecord::RecordNotFound | |
223 | render_404 |
|
225 | render_404 | |
224 | end |
|
226 | end | |
225 |
|
227 | |||
226 | # Finds and sets @project based on @object.project |
|
228 | # Finds and sets @project based on @object.project | |
227 | def find_project_from_association |
|
229 | def find_project_from_association | |
228 | render_404 unless @object.present? |
|
230 | render_404 unless @object.present? | |
229 |
|
231 | |||
230 | @project = @object.project |
|
232 | @project = @object.project | |
231 | end |
|
233 | end | |
232 |
|
234 | |||
233 | def find_model_object |
|
235 | def find_model_object | |
234 | model = self.class.read_inheritable_attribute('model_object') |
|
236 | model = self.class.read_inheritable_attribute('model_object') | |
235 | if model |
|
237 | if model | |
236 | @object = model.find(params[:id]) |
|
238 | @object = model.find(params[:id]) | |
237 | self.instance_variable_set('@' + controller_name.singularize, @object) if @object |
|
239 | self.instance_variable_set('@' + controller_name.singularize, @object) if @object | |
238 | end |
|
240 | end | |
239 | rescue ActiveRecord::RecordNotFound |
|
241 | rescue ActiveRecord::RecordNotFound | |
240 | render_404 |
|
242 | render_404 | |
241 | end |
|
243 | end | |
242 |
|
244 | |||
243 | def self.model_object(model) |
|
245 | def self.model_object(model) | |
244 | write_inheritable_attribute('model_object', model) |
|
246 | write_inheritable_attribute('model_object', model) | |
245 | end |
|
247 | end | |
246 |
|
248 | |||
247 | # Filter for bulk issue operations |
|
249 | # Filter for bulk issue operations | |
248 | def find_issues |
|
250 | def find_issues | |
249 | @issues = Issue.find_all_by_id(params[:id] || params[:ids]) |
|
251 | @issues = Issue.find_all_by_id(params[:id] || params[:ids]) | |
250 | raise ActiveRecord::RecordNotFound if @issues.empty? |
|
252 | raise ActiveRecord::RecordNotFound if @issues.empty? | |
251 | if @issues.detect {|issue| !issue.visible?} |
|
253 | if @issues.detect {|issue| !issue.visible?} | |
252 | deny_access |
|
254 | deny_access | |
253 | return |
|
255 | return | |
254 | end |
|
256 | end | |
255 | @projects = @issues.collect(&:project).compact.uniq |
|
257 | @projects = @issues.collect(&:project).compact.uniq | |
256 | @project = @projects.first if @projects.size == 1 |
|
258 | @project = @projects.first if @projects.size == 1 | |
257 | rescue ActiveRecord::RecordNotFound |
|
259 | rescue ActiveRecord::RecordNotFound | |
258 | render_404 |
|
260 | render_404 | |
259 | end |
|
261 | end | |
260 |
|
262 | |||
261 | # Check if project is unique before bulk operations |
|
263 | # Check if project is unique before bulk operations | |
262 | def check_project_uniqueness |
|
264 | def check_project_uniqueness | |
263 | unless @project |
|
265 | unless @project | |
264 | # TODO: let users bulk edit/move/destroy issues from different projects |
|
266 | # TODO: let users bulk edit/move/destroy issues from different projects | |
265 | render_error 'Can not bulk edit/move/destroy issues from different projects' |
|
267 | render_error 'Can not bulk edit/move/destroy issues from different projects' | |
266 | return false |
|
268 | return false | |
267 | end |
|
269 | end | |
268 | end |
|
270 | end | |
269 |
|
271 | |||
270 | # make sure that the user is a member of the project (or admin) if project is private |
|
272 | # make sure that the user is a member of the project (or admin) if project is private | |
271 | # used as a before_filter for actions that do not require any particular permission on the project |
|
273 | # used as a before_filter for actions that do not require any particular permission on the project | |
272 | def check_project_privacy |
|
274 | def check_project_privacy | |
273 | if @project && @project.active? |
|
275 | if @project && @project.active? | |
274 | if @project.is_public? || User.current.member_of?(@project) || User.current.admin? |
|
276 | if @project.is_public? || User.current.member_of?(@project) || User.current.admin? | |
275 | true |
|
277 | true | |
276 | else |
|
278 | else | |
277 | deny_access |
|
279 | deny_access | |
278 | end |
|
280 | end | |
279 | else |
|
281 | else | |
280 | @project = nil |
|
282 | @project = nil | |
281 | render_404 |
|
283 | render_404 | |
282 | false |
|
284 | false | |
283 | end |
|
285 | end | |
284 | end |
|
286 | end | |
285 |
|
287 | |||
286 | def back_url |
|
288 | def back_url | |
287 | params[:back_url] || request.env['HTTP_REFERER'] |
|
289 | params[:back_url] || request.env['HTTP_REFERER'] | |
288 | end |
|
290 | end | |
289 |
|
291 | |||
290 | def redirect_back_or_default(default) |
|
292 | def redirect_back_or_default(default) | |
291 | back_url = CGI.unescape(params[:back_url].to_s) |
|
293 | back_url = CGI.unescape(params[:back_url].to_s) | |
292 | if !back_url.blank? |
|
294 | if !back_url.blank? | |
293 | begin |
|
295 | begin | |
294 | uri = URI.parse(back_url) |
|
296 | uri = URI.parse(back_url) | |
295 | # do not redirect user to another host or to the login or register page |
|
297 | # do not redirect user to another host or to the login or register page | |
296 | if (uri.relative? || (uri.host == request.host)) && !uri.path.match(%r{/(login|account/register)}) |
|
298 | if (uri.relative? || (uri.host == request.host)) && !uri.path.match(%r{/(login|account/register)}) | |
297 | redirect_to(back_url) |
|
299 | redirect_to(back_url) | |
298 | return |
|
300 | return | |
299 | end |
|
301 | end | |
300 | rescue URI::InvalidURIError |
|
302 | rescue URI::InvalidURIError | |
301 | # redirect to default |
|
303 | # redirect to default | |
302 | end |
|
304 | end | |
303 | end |
|
305 | end | |
304 | redirect_to default |
|
306 | redirect_to default | |
305 | false |
|
307 | false | |
306 | end |
|
308 | end | |
307 |
|
309 | |||
308 | def render_403(options={}) |
|
310 | def render_403(options={}) | |
309 | @project = nil |
|
311 | @project = nil | |
310 | render_error({:message => :notice_not_authorized, :status => 403}.merge(options)) |
|
312 | render_error({:message => :notice_not_authorized, :status => 403}.merge(options)) | |
311 | return false |
|
313 | return false | |
312 | end |
|
314 | end | |
313 |
|
315 | |||
314 | def render_404(options={}) |
|
316 | def render_404(options={}) | |
315 | render_error({:message => :notice_file_not_found, :status => 404}.merge(options)) |
|
317 | render_error({:message => :notice_file_not_found, :status => 404}.merge(options)) | |
316 | return false |
|
318 | return false | |
317 | end |
|
319 | end | |
318 |
|
320 | |||
319 | # Renders an error response |
|
321 | # Renders an error response | |
320 | def render_error(arg) |
|
322 | def render_error(arg) | |
321 | arg = {:message => arg} unless arg.is_a?(Hash) |
|
323 | arg = {:message => arg} unless arg.is_a?(Hash) | |
322 |
|
324 | |||
323 | @message = arg[:message] |
|
325 | @message = arg[:message] | |
324 | @message = l(@message) if @message.is_a?(Symbol) |
|
326 | @message = l(@message) if @message.is_a?(Symbol) | |
325 | @status = arg[:status] || 500 |
|
327 | @status = arg[:status] || 500 | |
326 |
|
328 | |||
327 | respond_to do |format| |
|
329 | respond_to do |format| | |
328 | format.html { |
|
330 | format.html { | |
329 | render :template => 'common/error', :layout => use_layout, :status => @status |
|
331 | render :template => 'common/error', :layout => use_layout, :status => @status | |
330 | } |
|
332 | } | |
331 | format.atom { head @status } |
|
333 | format.atom { head @status } | |
332 | format.xml { head @status } |
|
334 | format.xml { head @status } | |
333 | format.js { head @status } |
|
335 | format.js { head @status } | |
334 | format.json { head @status } |
|
336 | format.json { head @status } | |
335 | end |
|
337 | end | |
336 | end |
|
338 | end | |
337 |
|
339 | |||
338 | # Filter for actions that provide an API response |
|
340 | # Filter for actions that provide an API response | |
339 | # but have no HTML representation for non admin users |
|
341 | # but have no HTML representation for non admin users | |
340 | def require_admin_or_api_request |
|
342 | def require_admin_or_api_request | |
341 | return true if api_request? |
|
343 | return true if api_request? | |
342 | if User.current.admin? |
|
344 | if User.current.admin? | |
343 | true |
|
345 | true | |
344 | elsif User.current.logged? |
|
346 | elsif User.current.logged? | |
345 | render_error(:status => 406) |
|
347 | render_error(:status => 406) | |
346 | else |
|
348 | else | |
347 | deny_access |
|
349 | deny_access | |
348 | end |
|
350 | end | |
349 | end |
|
351 | end | |
350 |
|
352 | |||
351 | # Picks which layout to use based on the request |
|
353 | # Picks which layout to use based on the request | |
352 | # |
|
354 | # | |
353 | # @return [boolean, string] name of the layout to use or false for no layout |
|
355 | # @return [boolean, string] name of the layout to use or false for no layout | |
354 | def use_layout |
|
356 | def use_layout | |
355 | request.xhr? ? false : 'base' |
|
357 | request.xhr? ? false : 'base' | |
356 | end |
|
358 | end | |
357 |
|
359 | |||
358 | def invalid_authenticity_token |
|
360 | def invalid_authenticity_token | |
359 | if api_request? |
|
361 | if api_request? | |
360 | logger.error "Form authenticity token is missing or is invalid. API calls must include a proper Content-type header (text/xml or text/json)." |
|
362 | logger.error "Form authenticity token is missing or is invalid. API calls must include a proper Content-type header (text/xml or text/json)." | |
361 | end |
|
363 | end | |
362 | render_error "Invalid form authenticity token." |
|
364 | render_error "Invalid form authenticity token." | |
363 | end |
|
365 | end | |
364 |
|
366 | |||
365 | def render_feed(items, options={}) |
|
367 | def render_feed(items, options={}) | |
366 | @items = items || [] |
|
368 | @items = items || [] | |
367 | @items.sort! {|x,y| y.event_datetime <=> x.event_datetime } |
|
369 | @items.sort! {|x,y| y.event_datetime <=> x.event_datetime } | |
368 | @items = @items.slice(0, Setting.feeds_limit.to_i) |
|
370 | @items = @items.slice(0, Setting.feeds_limit.to_i) | |
369 | @title = options[:title] || Setting.app_title |
|
371 | @title = options[:title] || Setting.app_title | |
370 | render :template => "common/feed.atom", :layout => false, |
|
372 | render :template => "common/feed.atom", :layout => false, | |
371 | :content_type => 'application/atom+xml' |
|
373 | :content_type => 'application/atom+xml' | |
372 | end |
|
374 | end | |
373 |
|
375 | |||
374 | # TODO: remove in Redmine 1.4 |
|
376 | # TODO: remove in Redmine 1.4 | |
375 | def self.accept_key_auth(*actions) |
|
377 | def self.accept_key_auth(*actions) | |
376 | ActiveSupport::Deprecation.warn "ApplicationController.accept_key_auth is deprecated and will be removed in Redmine 1.4. Use accept_rss_auth (or accept_api_auth) instead." |
|
378 | ActiveSupport::Deprecation.warn "ApplicationController.accept_key_auth is deprecated and will be removed in Redmine 1.4. Use accept_rss_auth (or accept_api_auth) instead." | |
377 | accept_rss_auth(*actions) |
|
379 | accept_rss_auth(*actions) | |
378 | end |
|
380 | end | |
379 |
|
381 | |||
380 | # TODO: remove in Redmine 1.4 |
|
382 | # TODO: remove in Redmine 1.4 | |
381 | def accept_key_auth_actions |
|
383 | def accept_key_auth_actions | |
382 | ActiveSupport::Deprecation.warn "ApplicationController.accept_key_auth_actions is deprecated and will be removed in Redmine 1.4. Use accept_rss_auth (or accept_api_auth) instead." |
|
384 | ActiveSupport::Deprecation.warn "ApplicationController.accept_key_auth_actions is deprecated and will be removed in Redmine 1.4. Use accept_rss_auth (or accept_api_auth) instead." | |
383 | self.class.accept_rss_auth |
|
385 | self.class.accept_rss_auth | |
384 | end |
|
386 | end | |
385 |
|
387 | |||
386 | def self.accept_rss_auth(*actions) |
|
388 | def self.accept_rss_auth(*actions) | |
387 | if actions.any? |
|
389 | if actions.any? | |
388 | write_inheritable_attribute('accept_rss_auth_actions', actions) |
|
390 | write_inheritable_attribute('accept_rss_auth_actions', actions) | |
389 | else |
|
391 | else | |
390 | read_inheritable_attribute('accept_rss_auth_actions') || [] |
|
392 | read_inheritable_attribute('accept_rss_auth_actions') || [] | |
391 | end |
|
393 | end | |
392 | end |
|
394 | end | |
393 |
|
395 | |||
394 | def accept_rss_auth?(action=action_name) |
|
396 | def accept_rss_auth?(action=action_name) | |
395 | self.class.accept_rss_auth.include?(action.to_sym) |
|
397 | self.class.accept_rss_auth.include?(action.to_sym) | |
396 | end |
|
398 | end | |
397 |
|
399 | |||
398 | def self.accept_api_auth(*actions) |
|
400 | def self.accept_api_auth(*actions) | |
399 | if actions.any? |
|
401 | if actions.any? | |
400 | write_inheritable_attribute('accept_api_auth_actions', actions) |
|
402 | write_inheritable_attribute('accept_api_auth_actions', actions) | |
401 | else |
|
403 | else | |
402 | read_inheritable_attribute('accept_api_auth_actions') || [] |
|
404 | read_inheritable_attribute('accept_api_auth_actions') || [] | |
403 | end |
|
405 | end | |
404 | end |
|
406 | end | |
405 |
|
407 | |||
406 | def accept_api_auth?(action=action_name) |
|
408 | def accept_api_auth?(action=action_name) | |
407 | self.class.accept_api_auth.include?(action.to_sym) |
|
409 | self.class.accept_api_auth.include?(action.to_sym) | |
408 | end |
|
410 | end | |
409 |
|
411 | |||
410 | # Returns the number of objects that should be displayed |
|
412 | # Returns the number of objects that should be displayed | |
411 | # on the paginated list |
|
413 | # on the paginated list | |
412 | def per_page_option |
|
414 | def per_page_option | |
413 | per_page = nil |
|
415 | per_page = nil | |
414 | if params[:per_page] && Setting.per_page_options_array.include?(params[:per_page].to_s.to_i) |
|
416 | if params[:per_page] && Setting.per_page_options_array.include?(params[:per_page].to_s.to_i) | |
415 | per_page = params[:per_page].to_s.to_i |
|
417 | per_page = params[:per_page].to_s.to_i | |
416 | session[:per_page] = per_page |
|
418 | session[:per_page] = per_page | |
417 | elsif session[:per_page] |
|
419 | elsif session[:per_page] | |
418 | per_page = session[:per_page] |
|
420 | per_page = session[:per_page] | |
419 | else |
|
421 | else | |
420 | per_page = Setting.per_page_options_array.first || 25 |
|
422 | per_page = Setting.per_page_options_array.first || 25 | |
421 | end |
|
423 | end | |
422 | per_page |
|
424 | per_page | |
423 | end |
|
425 | end | |
424 |
|
426 | |||
425 | # Returns offset and limit used to retrieve objects |
|
427 | # Returns offset and limit used to retrieve objects | |
426 | # for an API response based on offset, limit and page parameters |
|
428 | # for an API response based on offset, limit and page parameters | |
427 | def api_offset_and_limit(options=params) |
|
429 | def api_offset_and_limit(options=params) | |
428 | if options[:offset].present? |
|
430 | if options[:offset].present? | |
429 | offset = options[:offset].to_i |
|
431 | offset = options[:offset].to_i | |
430 | if offset < 0 |
|
432 | if offset < 0 | |
431 | offset = 0 |
|
433 | offset = 0 | |
432 | end |
|
434 | end | |
433 | end |
|
435 | end | |
434 | limit = options[:limit].to_i |
|
436 | limit = options[:limit].to_i | |
435 | if limit < 1 |
|
437 | if limit < 1 | |
436 | limit = 25 |
|
438 | limit = 25 | |
437 | elsif limit > 100 |
|
439 | elsif limit > 100 | |
438 | limit = 100 |
|
440 | limit = 100 | |
439 | end |
|
441 | end | |
440 | if offset.nil? && options[:page].present? |
|
442 | if offset.nil? && options[:page].present? | |
441 | offset = (options[:page].to_i - 1) * limit |
|
443 | offset = (options[:page].to_i - 1) * limit | |
442 | offset = 0 if offset < 0 |
|
444 | offset = 0 if offset < 0 | |
443 | end |
|
445 | end | |
444 | offset ||= 0 |
|
446 | offset ||= 0 | |
445 |
|
447 | |||
446 | [offset, limit] |
|
448 | [offset, limit] | |
447 | end |
|
449 | end | |
448 |
|
450 | |||
449 | # qvalues http header parser |
|
451 | # qvalues http header parser | |
450 | # code taken from webrick |
|
452 | # code taken from webrick | |
451 | def parse_qvalues(value) |
|
453 | def parse_qvalues(value) | |
452 | tmp = [] |
|
454 | tmp = [] | |
453 | if value |
|
455 | if value | |
454 | parts = value.split(/,\s*/) |
|
456 | parts = value.split(/,\s*/) | |
455 | parts.each {|part| |
|
457 | parts.each {|part| | |
456 | if m = %r{^([^\s,]+?)(?:;\s*q=(\d+(?:\.\d+)?))?$}.match(part) |
|
458 | if m = %r{^([^\s,]+?)(?:;\s*q=(\d+(?:\.\d+)?))?$}.match(part) | |
457 | val = m[1] |
|
459 | val = m[1] | |
458 | q = (m[2] or 1).to_f |
|
460 | q = (m[2] or 1).to_f | |
459 | tmp.push([val, q]) |
|
461 | tmp.push([val, q]) | |
460 | end |
|
462 | end | |
461 | } |
|
463 | } | |
462 | tmp = tmp.sort_by{|val, q| -q} |
|
464 | tmp = tmp.sort_by{|val, q| -q} | |
463 | tmp.collect!{|val, q| val} |
|
465 | tmp.collect!{|val, q| val} | |
464 | end |
|
466 | end | |
465 | return tmp |
|
467 | return tmp | |
466 | rescue |
|
468 | rescue | |
467 | nil |
|
469 | nil | |
468 | end |
|
470 | end | |
469 |
|
471 | |||
470 | # Returns a string that can be used as filename value in Content-Disposition header |
|
472 | # Returns a string that can be used as filename value in Content-Disposition header | |
471 | def filename_for_content_disposition(name) |
|
473 | def filename_for_content_disposition(name) | |
472 | request.env['HTTP_USER_AGENT'] =~ %r{MSIE} ? ERB::Util.url_encode(name) : name |
|
474 | request.env['HTTP_USER_AGENT'] =~ %r{MSIE} ? ERB::Util.url_encode(name) : name | |
473 | end |
|
475 | end | |
474 |
|
476 | |||
475 | def api_request? |
|
477 | def api_request? | |
476 | %w(xml json).include? params[:format] |
|
478 | %w(xml json).include? params[:format] | |
477 | end |
|
479 | end | |
478 |
|
480 | |||
479 | # Returns the API key present in the request |
|
481 | # Returns the API key present in the request | |
480 | def api_key_from_request |
|
482 | def api_key_from_request | |
481 | if params[:key].present? |
|
483 | if params[:key].present? | |
482 | params[:key] |
|
484 | params[:key] | |
483 | elsif request.headers["X-Redmine-API-Key"].present? |
|
485 | elsif request.headers["X-Redmine-API-Key"].present? | |
484 | request.headers["X-Redmine-API-Key"] |
|
486 | request.headers["X-Redmine-API-Key"] | |
485 | end |
|
487 | end | |
486 | end |
|
488 | end | |
487 |
|
489 | |||
488 | # Renders a warning flash if obj has unsaved attachments |
|
490 | # Renders a warning flash if obj has unsaved attachments | |
489 | def render_attachment_warning_if_needed(obj) |
|
491 | def render_attachment_warning_if_needed(obj) | |
490 | flash[:warning] = l(:warning_attachments_not_saved, obj.unsaved_attachments.size) if obj.unsaved_attachments.present? |
|
492 | flash[:warning] = l(:warning_attachments_not_saved, obj.unsaved_attachments.size) if obj.unsaved_attachments.present? | |
491 | end |
|
493 | end | |
492 |
|
494 | |||
493 | # Sets the `flash` notice or error based the number of issues that did not save |
|
495 | # Sets the `flash` notice or error based the number of issues that did not save | |
494 | # |
|
496 | # | |
495 | # @param [Array, Issue] issues all of the saved and unsaved Issues |
|
497 | # @param [Array, Issue] issues all of the saved and unsaved Issues | |
496 | # @param [Array, Integer] unsaved_issue_ids the issue ids that were not saved |
|
498 | # @param [Array, Integer] unsaved_issue_ids the issue ids that were not saved | |
497 | def set_flash_from_bulk_issue_save(issues, unsaved_issue_ids) |
|
499 | def set_flash_from_bulk_issue_save(issues, unsaved_issue_ids) | |
498 | if unsaved_issue_ids.empty? |
|
500 | if unsaved_issue_ids.empty? | |
499 | flash[:notice] = l(:notice_successful_update) unless issues.empty? |
|
501 | flash[:notice] = l(:notice_successful_update) unless issues.empty? | |
500 | else |
|
502 | else | |
501 | flash[:error] = l(:notice_failed_to_save_issues, |
|
503 | flash[:error] = l(:notice_failed_to_save_issues, | |
502 | :count => unsaved_issue_ids.size, |
|
504 | :count => unsaved_issue_ids.size, | |
503 | :total => issues.size, |
|
505 | :total => issues.size, | |
504 | :ids => '#' + unsaved_issue_ids.join(', #')) |
|
506 | :ids => '#' + unsaved_issue_ids.join(', #')) | |
505 | end |
|
507 | end | |
506 | end |
|
508 | end | |
507 |
|
509 | |||
508 | # Rescues an invalid query statement. Just in case... |
|
510 | # Rescues an invalid query statement. Just in case... | |
509 | def query_statement_invalid(exception) |
|
511 | def query_statement_invalid(exception) | |
510 | logger.error "Query::StatementInvalid: #{exception.message}" if logger |
|
512 | logger.error "Query::StatementInvalid: #{exception.message}" if logger | |
511 | session.delete(:query) |
|
513 | session.delete(:query) | |
512 | sort_clear if respond_to?(:sort_clear) |
|
514 | sort_clear if respond_to?(:sort_clear) | |
513 | render_error "An error occurred while executing the query and has been logged. Please report this error to your Redmine administrator." |
|
515 | render_error "An error occurred while executing the query and has been logged. Please report this error to your Redmine administrator." | |
514 | end |
|
516 | end | |
515 |
|
517 | |||
516 | # Renders API response on validation failure |
|
518 | # Renders API response on validation failure | |
517 | def render_validation_errors(object) |
|
519 | def render_validation_errors(object) | |
518 | options = { :status => :unprocessable_entity, :layout => false } |
|
520 | options = { :status => :unprocessable_entity, :layout => false } | |
519 | options.merge!(case params[:format] |
|
521 | options.merge!(case params[:format] | |
520 | when 'xml'; { :xml => object.errors } |
|
522 | when 'xml'; { :xml => object.errors } | |
521 | when 'json'; { :json => {'errors' => object.errors} } # ActiveResource client compliance |
|
523 | when 'json'; { :json => {'errors' => object.errors} } # ActiveResource client compliance | |
522 | else |
|
524 | else | |
523 | raise "Unknown format #{params[:format]} in #render_validation_errors" |
|
525 | raise "Unknown format #{params[:format]} in #render_validation_errors" | |
524 | end |
|
526 | end | |
525 | ) |
|
527 | ) | |
526 | render options |
|
528 | render options | |
527 | end |
|
529 | end | |
528 |
|
530 | |||
529 | # Overrides #default_template so that the api template |
|
531 | # Overrides #default_template so that the api template | |
530 | # is used automatically if it exists |
|
532 | # is used automatically if it exists | |
531 | def default_template(action_name = self.action_name) |
|
533 | def default_template(action_name = self.action_name) | |
532 | if api_request? |
|
534 | if api_request? | |
533 | begin |
|
535 | begin | |
534 | return self.view_paths.find_template(default_template_name(action_name), 'api') |
|
536 | return self.view_paths.find_template(default_template_name(action_name), 'api') | |
535 | rescue ::ActionView::MissingTemplate |
|
537 | rescue ::ActionView::MissingTemplate | |
536 | # the api template was not found |
|
538 | # the api template was not found | |
537 | # fallback to the default behaviour |
|
539 | # fallback to the default behaviour | |
538 | end |
|
540 | end | |
539 | end |
|
541 | end | |
540 | super |
|
542 | super | |
541 | end |
|
543 | end | |
542 |
|
544 | |||
543 | # Overrides #pick_layout so that #render with no arguments |
|
545 | # Overrides #pick_layout so that #render with no arguments | |
544 | # doesn't use the layout for api requests |
|
546 | # doesn't use the layout for api requests | |
545 | def pick_layout(*args) |
|
547 | def pick_layout(*args) | |
546 | api_request? ? nil : super |
|
548 | api_request? ? nil : super | |
547 | end |
|
549 | end | |
548 | end |
|
550 | end |
General Comments 0
You need to be logged in to leave comments.
Login now