@@ -1,580 +1,590 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2012 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2012 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 | class_attribute :accept_api_auth_actions |
|
26 | class_attribute :accept_api_auth_actions | |
27 | class_attribute :accept_rss_auth_actions |
|
27 | class_attribute :accept_rss_auth_actions | |
28 | class_attribute :model_object |
|
28 | class_attribute :model_object | |
29 |
|
29 | |||
30 | layout 'base' |
|
30 | layout 'base' | |
31 |
|
31 | |||
32 | protect_from_forgery |
|
32 | protect_from_forgery | |
33 | def handle_unverified_request |
|
33 | def handle_unverified_request | |
34 | super |
|
34 | super | |
35 | cookies.delete(:autologin) |
|
35 | cookies.delete(:autologin) | |
36 | end |
|
36 | end | |
37 |
|
37 | |||
38 | before_filter :session_expiration, :user_setup, :check_if_login_required, :set_localization |
|
38 | before_filter :session_expiration, :user_setup, :check_if_login_required, :set_localization | |
39 |
|
39 | |||
40 | rescue_from ActionController::InvalidAuthenticityToken, :with => :invalid_authenticity_token |
|
40 | rescue_from ActionController::InvalidAuthenticityToken, :with => :invalid_authenticity_token | |
41 | rescue_from ::Unauthorized, :with => :deny_access |
|
41 | rescue_from ::Unauthorized, :with => :deny_access | |
42 | rescue_from ::ActionView::MissingTemplate, :with => :missing_template |
|
42 | rescue_from ::ActionView::MissingTemplate, :with => :missing_template | |
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 | def session_expiration |
|
48 | def session_expiration | |
49 | if session[:user_id] |
|
49 | if session[:user_id] | |
50 | if session_expired? && !try_to_autologin |
|
50 | if session_expired? && !try_to_autologin | |
51 | reset_session |
|
51 | reset_session | |
52 | flash[:error] = l(:error_session_expired) |
|
52 | flash[:error] = l(:error_session_expired) | |
53 | redirect_to signin_url |
|
53 | redirect_to signin_url | |
54 | else |
|
54 | else | |
55 | session[:atime] = Time.now.utc.to_i |
|
55 | session[:atime] = Time.now.utc.to_i | |
56 | end |
|
56 | end | |
57 | end |
|
57 | end | |
58 | end |
|
58 | end | |
59 |
|
59 | |||
60 | def session_expired? |
|
60 | def session_expired? | |
61 | if Setting.session_lifetime? |
|
61 | if Setting.session_lifetime? | |
62 | unless session[:ctime] && (Time.now.utc.to_i - session[:ctime].to_i <= Setting.session_lifetime.to_i * 60) |
|
62 | unless session[:ctime] && (Time.now.utc.to_i - session[:ctime].to_i <= Setting.session_lifetime.to_i * 60) | |
63 | return true |
|
63 | return true | |
64 | end |
|
64 | end | |
65 | end |
|
65 | end | |
66 | if Setting.session_timeout? |
|
66 | if Setting.session_timeout? | |
67 | unless session[:atime] && (Time.now.utc.to_i - session[:atime].to_i <= Setting.session_timeout.to_i * 60) |
|
67 | unless session[:atime] && (Time.now.utc.to_i - session[:atime].to_i <= Setting.session_timeout.to_i * 60) | |
68 | return true |
|
68 | return true | |
69 | end |
|
69 | end | |
70 | end |
|
70 | end | |
71 | false |
|
71 | false | |
72 | end |
|
72 | end | |
73 |
|
73 | |||
74 | def start_user_session(user) |
|
74 | def start_user_session(user) | |
75 | session[:user_id] = user.id |
|
75 | session[:user_id] = user.id | |
76 | session[:ctime] = Time.now.utc.to_i |
|
76 | session[:ctime] = Time.now.utc.to_i | |
77 | session[:atime] = Time.now.utc.to_i |
|
77 | session[:atime] = Time.now.utc.to_i | |
78 | end |
|
78 | end | |
79 |
|
79 | |||
80 | def user_setup |
|
80 | def user_setup | |
81 | # Check the settings cache for each request |
|
81 | # Check the settings cache for each request | |
82 | Setting.check_cache |
|
82 | Setting.check_cache | |
83 | # Find the current user |
|
83 | # Find the current user | |
84 | User.current = find_current_user |
|
84 | User.current = find_current_user | |
85 | logger.info(" Current user: " + (User.current.logged? ? "#{User.current.login} (id=#{User.current.id})" : "anonymous")) if logger |
|
85 | logger.info(" Current user: " + (User.current.logged? ? "#{User.current.login} (id=#{User.current.id})" : "anonymous")) if logger | |
86 | end |
|
86 | end | |
87 |
|
87 | |||
88 | # Returns the current user or nil if no user is logged in |
|
88 | # Returns the current user or nil if no user is logged in | |
89 | # and starts a session if needed |
|
89 | # and starts a session if needed | |
90 | def find_current_user |
|
90 | def find_current_user | |
91 | user = nil |
|
91 | user = nil | |
92 | unless api_request? |
|
92 | unless api_request? | |
93 | if session[:user_id] |
|
93 | if session[:user_id] | |
94 | # existing session |
|
94 | # existing session | |
95 | user = (User.active.find(session[:user_id]) rescue nil) |
|
95 | user = (User.active.find(session[:user_id]) rescue nil) | |
96 | elsif autologin_user = try_to_autologin |
|
96 | elsif autologin_user = try_to_autologin | |
97 | user = autologin_user |
|
97 | user = autologin_user | |
98 | elsif params[:format] == 'atom' && params[:key] && request.get? && accept_rss_auth? |
|
98 | elsif params[:format] == 'atom' && params[:key] && request.get? && accept_rss_auth? | |
99 | # RSS key authentication does not start a session |
|
99 | # RSS key authentication does not start a session | |
100 | user = User.find_by_rss_key(params[:key]) |
|
100 | user = User.find_by_rss_key(params[:key]) | |
101 | end |
|
101 | end | |
102 | end |
|
102 | end | |
103 | if user.nil? && Setting.rest_api_enabled? && accept_api_auth? |
|
103 | if user.nil? && Setting.rest_api_enabled? && accept_api_auth? | |
104 | if (key = api_key_from_request) |
|
104 | if (key = api_key_from_request) | |
105 | # Use API key |
|
105 | # Use API key | |
106 | user = User.find_by_api_key(key) |
|
106 | user = User.find_by_api_key(key) | |
107 | else |
|
107 | else | |
108 | # HTTP Basic, either username/password or API key/random |
|
108 | # HTTP Basic, either username/password or API key/random | |
109 | authenticate_with_http_basic do |username, password| |
|
109 | authenticate_with_http_basic do |username, password| | |
110 | user = User.try_to_login(username, password) || User.find_by_api_key(username) |
|
110 | user = User.try_to_login(username, password) || User.find_by_api_key(username) | |
111 | end |
|
111 | end | |
112 | end |
|
112 | end | |
113 | # Switch user if requested by an admin user |
|
113 | # Switch user if requested by an admin user | |
114 | if user && user.admin? && (username = api_switch_user_from_request) |
|
114 | if user && user.admin? && (username = api_switch_user_from_request) | |
115 | su = User.find_by_login(username) |
|
115 | su = User.find_by_login(username) | |
116 | if su && su.active? |
|
116 | if su && su.active? | |
117 | logger.info(" User switched by: #{user.login} (id=#{user.id})") if logger |
|
117 | logger.info(" User switched by: #{user.login} (id=#{user.id})") if logger | |
118 | user = su |
|
118 | user = su | |
119 | else |
|
119 | else | |
120 | render_error :message => 'Invalid X-Redmine-Switch-User header', :status => 412 |
|
120 | render_error :message => 'Invalid X-Redmine-Switch-User header', :status => 412 | |
121 | end |
|
121 | end | |
122 | end |
|
122 | end | |
123 | end |
|
123 | end | |
124 | user |
|
124 | user | |
125 | end |
|
125 | end | |
126 |
|
126 | |||
127 | def try_to_autologin |
|
127 | def try_to_autologin | |
128 | if cookies[:autologin] && Setting.autologin? |
|
128 | if cookies[:autologin] && Setting.autologin? | |
129 | # auto-login feature starts a new session |
|
129 | # auto-login feature starts a new session | |
130 | user = User.try_to_autologin(cookies[:autologin]) |
|
130 | user = User.try_to_autologin(cookies[:autologin]) | |
131 | if user |
|
131 | if user | |
132 | reset_session |
|
132 | reset_session | |
133 | start_user_session(user) |
|
133 | start_user_session(user) | |
134 | end |
|
134 | end | |
135 | user |
|
135 | user | |
136 | end |
|
136 | end | |
137 | end |
|
137 | end | |
138 |
|
138 | |||
139 | # Sets the logged in user |
|
139 | # Sets the logged in user | |
140 | def logged_user=(user) |
|
140 | def logged_user=(user) | |
141 | reset_session |
|
141 | reset_session | |
142 | if user && user.is_a?(User) |
|
142 | if user && user.is_a?(User) | |
143 | User.current = user |
|
143 | User.current = user | |
144 | start_user_session(user) |
|
144 | start_user_session(user) | |
145 | else |
|
145 | else | |
146 | User.current = User.anonymous |
|
146 | User.current = User.anonymous | |
147 | end |
|
147 | end | |
148 | end |
|
148 | end | |
149 |
|
149 | |||
150 | # Logs out current user |
|
150 | # Logs out current user | |
151 | def logout_user |
|
151 | def logout_user | |
152 | if User.current.logged? |
|
152 | if User.current.logged? | |
153 | cookies.delete :autologin |
|
153 | cookies.delete :autologin | |
154 | Token.delete_all(["user_id = ? AND action = ?", User.current.id, 'autologin']) |
|
154 | Token.delete_all(["user_id = ? AND action = ?", User.current.id, 'autologin']) | |
155 | self.logged_user = nil |
|
155 | self.logged_user = nil | |
156 | end |
|
156 | end | |
157 | end |
|
157 | end | |
158 |
|
158 | |||
159 | # check if login is globally required to access the application |
|
159 | # check if login is globally required to access the application | |
160 | def check_if_login_required |
|
160 | def check_if_login_required | |
161 | # no check needed if user is already logged in |
|
161 | # no check needed if user is already logged in | |
162 | return true if User.current.logged? |
|
162 | return true if User.current.logged? | |
163 | require_login if Setting.login_required? |
|
163 | require_login if Setting.login_required? | |
164 | end |
|
164 | end | |
165 |
|
165 | |||
166 | def set_localization |
|
166 | def set_localization | |
167 | lang = nil |
|
167 | lang = nil | |
168 | if User.current.logged? |
|
168 | if User.current.logged? | |
169 | lang = find_language(User.current.language) |
|
169 | lang = find_language(User.current.language) | |
170 | end |
|
170 | end | |
171 | if lang.nil? && request.env['HTTP_ACCEPT_LANGUAGE'] |
|
171 | if lang.nil? && request.env['HTTP_ACCEPT_LANGUAGE'] | |
172 | accept_lang = parse_qvalues(request.env['HTTP_ACCEPT_LANGUAGE']).first |
|
172 | accept_lang = parse_qvalues(request.env['HTTP_ACCEPT_LANGUAGE']).first | |
173 | if !accept_lang.blank? |
|
173 | if !accept_lang.blank? | |
174 | accept_lang = accept_lang.downcase |
|
174 | accept_lang = accept_lang.downcase | |
175 | lang = find_language(accept_lang) || find_language(accept_lang.split('-').first) |
|
175 | lang = find_language(accept_lang) || find_language(accept_lang.split('-').first) | |
176 | end |
|
176 | end | |
177 | end |
|
177 | end | |
178 | lang ||= Setting.default_language |
|
178 | lang ||= Setting.default_language | |
179 | set_language_if_valid(lang) |
|
179 | set_language_if_valid(lang) | |
180 | end |
|
180 | end | |
181 |
|
181 | |||
182 | def require_login |
|
182 | def require_login | |
183 | if !User.current.logged? |
|
183 | if !User.current.logged? | |
184 | # Extract only the basic url parameters on non-GET requests |
|
184 | # Extract only the basic url parameters on non-GET requests | |
185 | if request.get? |
|
185 | if request.get? | |
186 | url = url_for(params) |
|
186 | url = url_for(params) | |
187 | else |
|
187 | else | |
188 | url = url_for(:controller => params[:controller], :action => params[:action], :id => params[:id], :project_id => params[:project_id]) |
|
188 | url = url_for(:controller => params[:controller], :action => params[:action], :id => params[:id], :project_id => params[:project_id]) | |
189 | end |
|
189 | end | |
190 | respond_to do |format| |
|
190 | respond_to do |format| | |
191 | format.html { redirect_to :controller => "account", :action => "login", :back_url => url } |
|
191 | format.html { redirect_to :controller => "account", :action => "login", :back_url => url } | |
192 | format.atom { redirect_to :controller => "account", :action => "login", :back_url => url } |
|
192 | format.atom { redirect_to :controller => "account", :action => "login", :back_url => url } | |
193 | format.xml { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' } |
|
193 | format.xml { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' } | |
194 | format.js { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' } |
|
194 | format.js { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' } | |
195 | format.json { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' } |
|
195 | format.json { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' } | |
196 | end |
|
196 | end | |
197 | return false |
|
197 | return false | |
198 | end |
|
198 | end | |
199 | true |
|
199 | true | |
200 | end |
|
200 | end | |
201 |
|
201 | |||
202 | def require_admin |
|
202 | def require_admin | |
203 | return unless require_login |
|
203 | return unless require_login | |
204 | if !User.current.admin? |
|
204 | if !User.current.admin? | |
205 | render_403 |
|
205 | render_403 | |
206 | return false |
|
206 | return false | |
207 | end |
|
207 | end | |
208 | true |
|
208 | true | |
209 | end |
|
209 | end | |
210 |
|
210 | |||
211 | def deny_access |
|
211 | def deny_access | |
212 | User.current.logged? ? render_403 : require_login |
|
212 | User.current.logged? ? render_403 : require_login | |
213 | end |
|
213 | end | |
214 |
|
214 | |||
215 | # Authorize the user for the requested action |
|
215 | # Authorize the user for the requested action | |
216 | def authorize(ctrl = params[:controller], action = params[:action], global = false) |
|
216 | def authorize(ctrl = params[:controller], action = params[:action], global = false) | |
217 | allowed = User.current.allowed_to?({:controller => ctrl, :action => action}, @project || @projects, :global => global) |
|
217 | allowed = User.current.allowed_to?({:controller => ctrl, :action => action}, @project || @projects, :global => global) | |
218 | if allowed |
|
218 | if allowed | |
219 | true |
|
219 | true | |
220 | else |
|
220 | else | |
221 | if @project && @project.archived? |
|
221 | if @project && @project.archived? | |
222 | render_403 :message => :notice_not_authorized_archived_project |
|
222 | render_403 :message => :notice_not_authorized_archived_project | |
223 | else |
|
223 | else | |
224 | deny_access |
|
224 | deny_access | |
225 | end |
|
225 | end | |
226 | end |
|
226 | end | |
227 | end |
|
227 | end | |
228 |
|
228 | |||
229 | # Authorize the user for the requested action outside a project |
|
229 | # Authorize the user for the requested action outside a project | |
230 | def authorize_global(ctrl = params[:controller], action = params[:action], global = true) |
|
230 | def authorize_global(ctrl = params[:controller], action = params[:action], global = true) | |
231 | authorize(ctrl, action, global) |
|
231 | authorize(ctrl, action, global) | |
232 | end |
|
232 | end | |
233 |
|
233 | |||
234 | # Find project of id params[:id] |
|
234 | # Find project of id params[:id] | |
235 | def find_project |
|
235 | def find_project | |
236 | @project = Project.find(params[:id]) |
|
236 | @project = Project.find(params[:id]) | |
237 | rescue ActiveRecord::RecordNotFound |
|
237 | rescue ActiveRecord::RecordNotFound | |
238 | render_404 |
|
238 | render_404 | |
239 | end |
|
239 | end | |
240 |
|
240 | |||
241 | # Find project of id params[:project_id] |
|
241 | # Find project of id params[:project_id] | |
242 | def find_project_by_project_id |
|
242 | def find_project_by_project_id | |
243 | @project = Project.find(params[:project_id]) |
|
243 | @project = Project.find(params[:project_id]) | |
244 | rescue ActiveRecord::RecordNotFound |
|
244 | rescue ActiveRecord::RecordNotFound | |
245 | render_404 |
|
245 | render_404 | |
246 | end |
|
246 | end | |
247 |
|
247 | |||
248 | # Find a project based on params[:project_id] |
|
248 | # Find a project based on params[:project_id] | |
249 | # TODO: some subclasses override this, see about merging their logic |
|
249 | # TODO: some subclasses override this, see about merging their logic | |
250 | def find_optional_project |
|
250 | def find_optional_project | |
251 | @project = Project.find(params[:project_id]) unless params[:project_id].blank? |
|
251 | @project = Project.find(params[:project_id]) unless params[:project_id].blank? | |
252 | allowed = User.current.allowed_to?({:controller => params[:controller], :action => params[:action]}, @project, :global => true) |
|
252 | allowed = User.current.allowed_to?({:controller => params[:controller], :action => params[:action]}, @project, :global => true) | |
253 | allowed ? true : deny_access |
|
253 | allowed ? true : deny_access | |
254 | rescue ActiveRecord::RecordNotFound |
|
254 | rescue ActiveRecord::RecordNotFound | |
255 | render_404 |
|
255 | render_404 | |
256 | end |
|
256 | end | |
257 |
|
257 | |||
258 | # Finds and sets @project based on @object.project |
|
258 | # Finds and sets @project based on @object.project | |
259 | def find_project_from_association |
|
259 | def find_project_from_association | |
260 | render_404 unless @object.present? |
|
260 | render_404 unless @object.present? | |
261 |
|
261 | |||
262 | @project = @object.project |
|
262 | @project = @object.project | |
263 | end |
|
263 | end | |
264 |
|
264 | |||
265 | def find_model_object |
|
265 | def find_model_object | |
266 | model = self.class.model_object |
|
266 | model = self.class.model_object | |
267 | if model |
|
267 | if model | |
268 | @object = model.find(params[:id]) |
|
268 | @object = model.find(params[:id]) | |
269 | self.instance_variable_set('@' + controller_name.singularize, @object) if @object |
|
269 | self.instance_variable_set('@' + controller_name.singularize, @object) if @object | |
270 | end |
|
270 | end | |
271 | rescue ActiveRecord::RecordNotFound |
|
271 | rescue ActiveRecord::RecordNotFound | |
272 | render_404 |
|
272 | render_404 | |
273 | end |
|
273 | end | |
274 |
|
274 | |||
275 | def self.model_object(model) |
|
275 | def self.model_object(model) | |
276 | self.model_object = model |
|
276 | self.model_object = model | |
277 | end |
|
277 | end | |
278 |
|
278 | |||
279 | # Filter for bulk issue operations |
|
279 | # Find the issue whose id is the :id parameter | |
|
280 | # Raises a Unauthorized exception if the issue is not visible | |||
|
281 | def find_issue | |||
|
282 | # Issue.visible.find(...) can not be used to redirect user to the login form | |||
|
283 | # if the issue actually exists but requires authentication | |||
|
284 | @issue = Issue.find(params[:id]) | |||
|
285 | raise Unauthorized unless @issue.visible? | |||
|
286 | @project = @issue.project | |||
|
287 | rescue ActiveRecord::RecordNotFound | |||
|
288 | render_404 | |||
|
289 | end | |||
|
290 | ||||
|
291 | # Find issues with a single :id param or :ids array param | |||
|
292 | # Raises a Unauthorized exception if one of the issues is not visible | |||
280 | def find_issues |
|
293 | def find_issues | |
281 | @issues = Issue.find_all_by_id(params[:id] || params[:ids]) |
|
294 | @issues = Issue.find_all_by_id(params[:id] || params[:ids]) | |
282 | raise ActiveRecord::RecordNotFound if @issues.empty? |
|
295 | raise ActiveRecord::RecordNotFound if @issues.empty? | |
283 | if @issues.detect {|issue| !issue.visible?} |
|
296 | raise Unauthorized if @issues.all?(&:visible?) | |
284 | deny_access |
|
|||
285 | return |
|
|||
286 | end |
|
|||
287 | @projects = @issues.collect(&:project).compact.uniq |
|
297 | @projects = @issues.collect(&:project).compact.uniq | |
288 | @project = @projects.first if @projects.size == 1 |
|
298 | @project = @projects.first if @projects.size == 1 | |
289 | rescue ActiveRecord::RecordNotFound |
|
299 | rescue ActiveRecord::RecordNotFound | |
290 | render_404 |
|
300 | render_404 | |
291 | end |
|
301 | end | |
292 |
|
302 | |||
293 | # make sure that the user is a member of the project (or admin) if project is private |
|
303 | # make sure that the user is a member of the project (or admin) if project is private | |
294 | # used as a before_filter for actions that do not require any particular permission on the project |
|
304 | # used as a before_filter for actions that do not require any particular permission on the project | |
295 | def check_project_privacy |
|
305 | def check_project_privacy | |
296 | if @project && !@project.archived? |
|
306 | if @project && !@project.archived? | |
297 | if @project.visible? |
|
307 | if @project.visible? | |
298 | true |
|
308 | true | |
299 | else |
|
309 | else | |
300 | deny_access |
|
310 | deny_access | |
301 | end |
|
311 | end | |
302 | else |
|
312 | else | |
303 | @project = nil |
|
313 | @project = nil | |
304 | render_404 |
|
314 | render_404 | |
305 | false |
|
315 | false | |
306 | end |
|
316 | end | |
307 | end |
|
317 | end | |
308 |
|
318 | |||
309 | def back_url |
|
319 | def back_url | |
310 | url = params[:back_url] |
|
320 | url = params[:back_url] | |
311 | if url.nil? && referer = request.env['HTTP_REFERER'] |
|
321 | if url.nil? && referer = request.env['HTTP_REFERER'] | |
312 | url = CGI.unescape(referer.to_s) |
|
322 | url = CGI.unescape(referer.to_s) | |
313 | end |
|
323 | end | |
314 | url |
|
324 | url | |
315 | end |
|
325 | end | |
316 |
|
326 | |||
317 | def redirect_back_or_default(default) |
|
327 | def redirect_back_or_default(default) | |
318 | back_url = params[:back_url].to_s |
|
328 | back_url = params[:back_url].to_s | |
319 | if back_url.present? |
|
329 | if back_url.present? | |
320 | begin |
|
330 | begin | |
321 | uri = URI.parse(back_url) |
|
331 | uri = URI.parse(back_url) | |
322 | # do not redirect user to another host or to the login or register page |
|
332 | # do not redirect user to another host or to the login or register page | |
323 | if (uri.relative? || (uri.host == request.host)) && !uri.path.match(%r{/(login|account/register)}) |
|
333 | if (uri.relative? || (uri.host == request.host)) && !uri.path.match(%r{/(login|account/register)}) | |
324 | redirect_to(back_url) |
|
334 | redirect_to(back_url) | |
325 | return |
|
335 | return | |
326 | end |
|
336 | end | |
327 | rescue URI::InvalidURIError |
|
337 | rescue URI::InvalidURIError | |
328 | logger.warn("Could not redirect to invalid URL #{back_url}") |
|
338 | logger.warn("Could not redirect to invalid URL #{back_url}") | |
329 | # redirect to default |
|
339 | # redirect to default | |
330 | end |
|
340 | end | |
331 | end |
|
341 | end | |
332 | redirect_to default |
|
342 | redirect_to default | |
333 | false |
|
343 | false | |
334 | end |
|
344 | end | |
335 |
|
345 | |||
336 | # Redirects to the request referer if present, redirects to args or call block otherwise. |
|
346 | # Redirects to the request referer if present, redirects to args or call block otherwise. | |
337 | def redirect_to_referer_or(*args, &block) |
|
347 | def redirect_to_referer_or(*args, &block) | |
338 | redirect_to :back |
|
348 | redirect_to :back | |
339 | rescue ::ActionController::RedirectBackError |
|
349 | rescue ::ActionController::RedirectBackError | |
340 | if args.any? |
|
350 | if args.any? | |
341 | redirect_to *args |
|
351 | redirect_to *args | |
342 | elsif block_given? |
|
352 | elsif block_given? | |
343 | block.call |
|
353 | block.call | |
344 | else |
|
354 | else | |
345 | raise "#redirect_to_referer_or takes arguments or a block" |
|
355 | raise "#redirect_to_referer_or takes arguments or a block" | |
346 | end |
|
356 | end | |
347 | end |
|
357 | end | |
348 |
|
358 | |||
349 | def render_403(options={}) |
|
359 | def render_403(options={}) | |
350 | @project = nil |
|
360 | @project = nil | |
351 | render_error({:message => :notice_not_authorized, :status => 403}.merge(options)) |
|
361 | render_error({:message => :notice_not_authorized, :status => 403}.merge(options)) | |
352 | return false |
|
362 | return false | |
353 | end |
|
363 | end | |
354 |
|
364 | |||
355 | def render_404(options={}) |
|
365 | def render_404(options={}) | |
356 | render_error({:message => :notice_file_not_found, :status => 404}.merge(options)) |
|
366 | render_error({:message => :notice_file_not_found, :status => 404}.merge(options)) | |
357 | return false |
|
367 | return false | |
358 | end |
|
368 | end | |
359 |
|
369 | |||
360 | # Renders an error response |
|
370 | # Renders an error response | |
361 | def render_error(arg) |
|
371 | def render_error(arg) | |
362 | arg = {:message => arg} unless arg.is_a?(Hash) |
|
372 | arg = {:message => arg} unless arg.is_a?(Hash) | |
363 |
|
373 | |||
364 | @message = arg[:message] |
|
374 | @message = arg[:message] | |
365 | @message = l(@message) if @message.is_a?(Symbol) |
|
375 | @message = l(@message) if @message.is_a?(Symbol) | |
366 | @status = arg[:status] || 500 |
|
376 | @status = arg[:status] || 500 | |
367 |
|
377 | |||
368 | respond_to do |format| |
|
378 | respond_to do |format| | |
369 | format.html { |
|
379 | format.html { | |
370 | render :template => 'common/error', :layout => use_layout, :status => @status |
|
380 | render :template => 'common/error', :layout => use_layout, :status => @status | |
371 | } |
|
381 | } | |
372 | format.any { head @status } |
|
382 | format.any { head @status } | |
373 | end |
|
383 | end | |
374 | end |
|
384 | end | |
375 |
|
385 | |||
376 | # Handler for ActionView::MissingTemplate exception |
|
386 | # Handler for ActionView::MissingTemplate exception | |
377 | def missing_template |
|
387 | def missing_template | |
378 | logger.warn "Missing template, responding with 404" |
|
388 | logger.warn "Missing template, responding with 404" | |
379 | @project = nil |
|
389 | @project = nil | |
380 | render_404 |
|
390 | render_404 | |
381 | end |
|
391 | end | |
382 |
|
392 | |||
383 | # Filter for actions that provide an API response |
|
393 | # Filter for actions that provide an API response | |
384 | # but have no HTML representation for non admin users |
|
394 | # but have no HTML representation for non admin users | |
385 | def require_admin_or_api_request |
|
395 | def require_admin_or_api_request | |
386 | return true if api_request? |
|
396 | return true if api_request? | |
387 | if User.current.admin? |
|
397 | if User.current.admin? | |
388 | true |
|
398 | true | |
389 | elsif User.current.logged? |
|
399 | elsif User.current.logged? | |
390 | render_error(:status => 406) |
|
400 | render_error(:status => 406) | |
391 | else |
|
401 | else | |
392 | deny_access |
|
402 | deny_access | |
393 | end |
|
403 | end | |
394 | end |
|
404 | end | |
395 |
|
405 | |||
396 | # Picks which layout to use based on the request |
|
406 | # Picks which layout to use based on the request | |
397 | # |
|
407 | # | |
398 | # @return [boolean, string] name of the layout to use or false for no layout |
|
408 | # @return [boolean, string] name of the layout to use or false for no layout | |
399 | def use_layout |
|
409 | def use_layout | |
400 | request.xhr? ? false : 'base' |
|
410 | request.xhr? ? false : 'base' | |
401 | end |
|
411 | end | |
402 |
|
412 | |||
403 | def invalid_authenticity_token |
|
413 | def invalid_authenticity_token | |
404 | if api_request? |
|
414 | if api_request? | |
405 | logger.error "Form authenticity token is missing or is invalid. API calls must include a proper Content-type header (text/xml or text/json)." |
|
415 | logger.error "Form authenticity token is missing or is invalid. API calls must include a proper Content-type header (text/xml or text/json)." | |
406 | end |
|
416 | end | |
407 | render_error "Invalid form authenticity token." |
|
417 | render_error "Invalid form authenticity token." | |
408 | end |
|
418 | end | |
409 |
|
419 | |||
410 | def render_feed(items, options={}) |
|
420 | def render_feed(items, options={}) | |
411 | @items = items || [] |
|
421 | @items = items || [] | |
412 | @items.sort! {|x,y| y.event_datetime <=> x.event_datetime } |
|
422 | @items.sort! {|x,y| y.event_datetime <=> x.event_datetime } | |
413 | @items = @items.slice(0, Setting.feeds_limit.to_i) |
|
423 | @items = @items.slice(0, Setting.feeds_limit.to_i) | |
414 | @title = options[:title] || Setting.app_title |
|
424 | @title = options[:title] || Setting.app_title | |
415 | render :template => "common/feed", :formats => [:atom], :layout => false, |
|
425 | render :template => "common/feed", :formats => [:atom], :layout => false, | |
416 | :content_type => 'application/atom+xml' |
|
426 | :content_type => 'application/atom+xml' | |
417 | end |
|
427 | end | |
418 |
|
428 | |||
419 | def self.accept_rss_auth(*actions) |
|
429 | def self.accept_rss_auth(*actions) | |
420 | if actions.any? |
|
430 | if actions.any? | |
421 | self.accept_rss_auth_actions = actions |
|
431 | self.accept_rss_auth_actions = actions | |
422 | else |
|
432 | else | |
423 | self.accept_rss_auth_actions || [] |
|
433 | self.accept_rss_auth_actions || [] | |
424 | end |
|
434 | end | |
425 | end |
|
435 | end | |
426 |
|
436 | |||
427 | def accept_rss_auth?(action=action_name) |
|
437 | def accept_rss_auth?(action=action_name) | |
428 | self.class.accept_rss_auth.include?(action.to_sym) |
|
438 | self.class.accept_rss_auth.include?(action.to_sym) | |
429 | end |
|
439 | end | |
430 |
|
440 | |||
431 | def self.accept_api_auth(*actions) |
|
441 | def self.accept_api_auth(*actions) | |
432 | if actions.any? |
|
442 | if actions.any? | |
433 | self.accept_api_auth_actions = actions |
|
443 | self.accept_api_auth_actions = actions | |
434 | else |
|
444 | else | |
435 | self.accept_api_auth_actions || [] |
|
445 | self.accept_api_auth_actions || [] | |
436 | end |
|
446 | end | |
437 | end |
|
447 | end | |
438 |
|
448 | |||
439 | def accept_api_auth?(action=action_name) |
|
449 | def accept_api_auth?(action=action_name) | |
440 | self.class.accept_api_auth.include?(action.to_sym) |
|
450 | self.class.accept_api_auth.include?(action.to_sym) | |
441 | end |
|
451 | end | |
442 |
|
452 | |||
443 | # Returns the number of objects that should be displayed |
|
453 | # Returns the number of objects that should be displayed | |
444 | # on the paginated list |
|
454 | # on the paginated list | |
445 | def per_page_option |
|
455 | def per_page_option | |
446 | per_page = nil |
|
456 | per_page = nil | |
447 | if params[:per_page] && Setting.per_page_options_array.include?(params[:per_page].to_s.to_i) |
|
457 | if params[:per_page] && Setting.per_page_options_array.include?(params[:per_page].to_s.to_i) | |
448 | per_page = params[:per_page].to_s.to_i |
|
458 | per_page = params[:per_page].to_s.to_i | |
449 | session[:per_page] = per_page |
|
459 | session[:per_page] = per_page | |
450 | elsif session[:per_page] |
|
460 | elsif session[:per_page] | |
451 | per_page = session[:per_page] |
|
461 | per_page = session[:per_page] | |
452 | else |
|
462 | else | |
453 | per_page = Setting.per_page_options_array.first || 25 |
|
463 | per_page = Setting.per_page_options_array.first || 25 | |
454 | end |
|
464 | end | |
455 | per_page |
|
465 | per_page | |
456 | end |
|
466 | end | |
457 |
|
467 | |||
458 | # Returns offset and limit used to retrieve objects |
|
468 | # Returns offset and limit used to retrieve objects | |
459 | # for an API response based on offset, limit and page parameters |
|
469 | # for an API response based on offset, limit and page parameters | |
460 | def api_offset_and_limit(options=params) |
|
470 | def api_offset_and_limit(options=params) | |
461 | if options[:offset].present? |
|
471 | if options[:offset].present? | |
462 | offset = options[:offset].to_i |
|
472 | offset = options[:offset].to_i | |
463 | if offset < 0 |
|
473 | if offset < 0 | |
464 | offset = 0 |
|
474 | offset = 0 | |
465 | end |
|
475 | end | |
466 | end |
|
476 | end | |
467 | limit = options[:limit].to_i |
|
477 | limit = options[:limit].to_i | |
468 | if limit < 1 |
|
478 | if limit < 1 | |
469 | limit = 25 |
|
479 | limit = 25 | |
470 | elsif limit > 100 |
|
480 | elsif limit > 100 | |
471 | limit = 100 |
|
481 | limit = 100 | |
472 | end |
|
482 | end | |
473 | if offset.nil? && options[:page].present? |
|
483 | if offset.nil? && options[:page].present? | |
474 | offset = (options[:page].to_i - 1) * limit |
|
484 | offset = (options[:page].to_i - 1) * limit | |
475 | offset = 0 if offset < 0 |
|
485 | offset = 0 if offset < 0 | |
476 | end |
|
486 | end | |
477 | offset ||= 0 |
|
487 | offset ||= 0 | |
478 |
|
488 | |||
479 | [offset, limit] |
|
489 | [offset, limit] | |
480 | end |
|
490 | end | |
481 |
|
491 | |||
482 | # qvalues http header parser |
|
492 | # qvalues http header parser | |
483 | # code taken from webrick |
|
493 | # code taken from webrick | |
484 | def parse_qvalues(value) |
|
494 | def parse_qvalues(value) | |
485 | tmp = [] |
|
495 | tmp = [] | |
486 | if value |
|
496 | if value | |
487 | parts = value.split(/,\s*/) |
|
497 | parts = value.split(/,\s*/) | |
488 | parts.each {|part| |
|
498 | parts.each {|part| | |
489 | if m = %r{^([^\s,]+?)(?:;\s*q=(\d+(?:\.\d+)?))?$}.match(part) |
|
499 | if m = %r{^([^\s,]+?)(?:;\s*q=(\d+(?:\.\d+)?))?$}.match(part) | |
490 | val = m[1] |
|
500 | val = m[1] | |
491 | q = (m[2] or 1).to_f |
|
501 | q = (m[2] or 1).to_f | |
492 | tmp.push([val, q]) |
|
502 | tmp.push([val, q]) | |
493 | end |
|
503 | end | |
494 | } |
|
504 | } | |
495 | tmp = tmp.sort_by{|val, q| -q} |
|
505 | tmp = tmp.sort_by{|val, q| -q} | |
496 | tmp.collect!{|val, q| val} |
|
506 | tmp.collect!{|val, q| val} | |
497 | end |
|
507 | end | |
498 | return tmp |
|
508 | return tmp | |
499 | rescue |
|
509 | rescue | |
500 | nil |
|
510 | nil | |
501 | end |
|
511 | end | |
502 |
|
512 | |||
503 | # Returns a string that can be used as filename value in Content-Disposition header |
|
513 | # Returns a string that can be used as filename value in Content-Disposition header | |
504 | def filename_for_content_disposition(name) |
|
514 | def filename_for_content_disposition(name) | |
505 | request.env['HTTP_USER_AGENT'] =~ %r{MSIE} ? ERB::Util.url_encode(name) : name |
|
515 | request.env['HTTP_USER_AGENT'] =~ %r{MSIE} ? ERB::Util.url_encode(name) : name | |
506 | end |
|
516 | end | |
507 |
|
517 | |||
508 | def api_request? |
|
518 | def api_request? | |
509 | %w(xml json).include? params[:format] |
|
519 | %w(xml json).include? params[:format] | |
510 | end |
|
520 | end | |
511 |
|
521 | |||
512 | # Returns the API key present in the request |
|
522 | # Returns the API key present in the request | |
513 | def api_key_from_request |
|
523 | def api_key_from_request | |
514 | if params[:key].present? |
|
524 | if params[:key].present? | |
515 | params[:key].to_s |
|
525 | params[:key].to_s | |
516 | elsif request.headers["X-Redmine-API-Key"].present? |
|
526 | elsif request.headers["X-Redmine-API-Key"].present? | |
517 | request.headers["X-Redmine-API-Key"].to_s |
|
527 | request.headers["X-Redmine-API-Key"].to_s | |
518 | end |
|
528 | end | |
519 | end |
|
529 | end | |
520 |
|
530 | |||
521 | # Returns the API 'switch user' value if present |
|
531 | # Returns the API 'switch user' value if present | |
522 | def api_switch_user_from_request |
|
532 | def api_switch_user_from_request | |
523 | request.headers["X-Redmine-Switch-User"].to_s.presence |
|
533 | request.headers["X-Redmine-Switch-User"].to_s.presence | |
524 | end |
|
534 | end | |
525 |
|
535 | |||
526 | # Renders a warning flash if obj has unsaved attachments |
|
536 | # Renders a warning flash if obj has unsaved attachments | |
527 | def render_attachment_warning_if_needed(obj) |
|
537 | def render_attachment_warning_if_needed(obj) | |
528 | flash[:warning] = l(:warning_attachments_not_saved, obj.unsaved_attachments.size) if obj.unsaved_attachments.present? |
|
538 | flash[:warning] = l(:warning_attachments_not_saved, obj.unsaved_attachments.size) if obj.unsaved_attachments.present? | |
529 | end |
|
539 | end | |
530 |
|
540 | |||
531 | # Sets the `flash` notice or error based the number of issues that did not save |
|
541 | # Sets the `flash` notice or error based the number of issues that did not save | |
532 | # |
|
542 | # | |
533 | # @param [Array, Issue] issues all of the saved and unsaved Issues |
|
543 | # @param [Array, Issue] issues all of the saved and unsaved Issues | |
534 | # @param [Array, Integer] unsaved_issue_ids the issue ids that were not saved |
|
544 | # @param [Array, Integer] unsaved_issue_ids the issue ids that were not saved | |
535 | def set_flash_from_bulk_issue_save(issues, unsaved_issue_ids) |
|
545 | def set_flash_from_bulk_issue_save(issues, unsaved_issue_ids) | |
536 | if unsaved_issue_ids.empty? |
|
546 | if unsaved_issue_ids.empty? | |
537 | flash[:notice] = l(:notice_successful_update) unless issues.empty? |
|
547 | flash[:notice] = l(:notice_successful_update) unless issues.empty? | |
538 | else |
|
548 | else | |
539 | flash[:error] = l(:notice_failed_to_save_issues, |
|
549 | flash[:error] = l(:notice_failed_to_save_issues, | |
540 | :count => unsaved_issue_ids.size, |
|
550 | :count => unsaved_issue_ids.size, | |
541 | :total => issues.size, |
|
551 | :total => issues.size, | |
542 | :ids => '#' + unsaved_issue_ids.join(', #')) |
|
552 | :ids => '#' + unsaved_issue_ids.join(', #')) | |
543 | end |
|
553 | end | |
544 | end |
|
554 | end | |
545 |
|
555 | |||
546 | # Rescues an invalid query statement. Just in case... |
|
556 | # Rescues an invalid query statement. Just in case... | |
547 | def query_statement_invalid(exception) |
|
557 | def query_statement_invalid(exception) | |
548 | logger.error "Query::StatementInvalid: #{exception.message}" if logger |
|
558 | logger.error "Query::StatementInvalid: #{exception.message}" if logger | |
549 | session.delete(:query) |
|
559 | session.delete(:query) | |
550 | sort_clear if respond_to?(:sort_clear) |
|
560 | sort_clear if respond_to?(:sort_clear) | |
551 | render_error "An error occurred while executing the query and has been logged. Please report this error to your Redmine administrator." |
|
561 | render_error "An error occurred while executing the query and has been logged. Please report this error to your Redmine administrator." | |
552 | end |
|
562 | end | |
553 |
|
563 | |||
554 | # Renders a 200 response for successfull updates or deletions via the API |
|
564 | # Renders a 200 response for successfull updates or deletions via the API | |
555 | def render_api_ok |
|
565 | def render_api_ok | |
556 | render_api_head :ok |
|
566 | render_api_head :ok | |
557 | end |
|
567 | end | |
558 |
|
568 | |||
559 | # Renders a head API response |
|
569 | # Renders a head API response | |
560 | def render_api_head(status) |
|
570 | def render_api_head(status) | |
561 | # #head would return a response body with one space |
|
571 | # #head would return a response body with one space | |
562 | render :text => '', :status => status, :layout => nil |
|
572 | render :text => '', :status => status, :layout => nil | |
563 | end |
|
573 | end | |
564 |
|
574 | |||
565 | # Renders API response on validation failure |
|
575 | # Renders API response on validation failure | |
566 | def render_validation_errors(objects) |
|
576 | def render_validation_errors(objects) | |
567 | if objects.is_a?(Array) |
|
577 | if objects.is_a?(Array) | |
568 | @error_messages = objects.map {|object| object.errors.full_messages}.flatten |
|
578 | @error_messages = objects.map {|object| object.errors.full_messages}.flatten | |
569 | else |
|
579 | else | |
570 | @error_messages = objects.errors.full_messages |
|
580 | @error_messages = objects.errors.full_messages | |
571 | end |
|
581 | end | |
572 | render :template => 'common/error_messages.api', :status => :unprocessable_entity, :layout => nil |
|
582 | render :template => 'common/error_messages.api', :status => :unprocessable_entity, :layout => nil | |
573 | end |
|
583 | end | |
574 |
|
584 | |||
575 | # Overrides #_include_layout? so that #render with no arguments |
|
585 | # Overrides #_include_layout? so that #render with no arguments | |
576 | # doesn't use the layout for api requests |
|
586 | # doesn't use the layout for api requests | |
577 | def _include_layout?(*args) |
|
587 | def _include_layout?(*args) | |
578 | api_request? ? false : super |
|
588 | api_request? ? false : super | |
579 | end |
|
589 | end | |
580 | end |
|
590 | end |
@@ -1,443 +1,431 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2012 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2012 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 | class IssuesController < ApplicationController |
|
18 | class IssuesController < ApplicationController | |
19 | menu_item :new_issue, :only => [:new, :create] |
|
19 | menu_item :new_issue, :only => [:new, :create] | |
20 | default_search_scope :issues |
|
20 | default_search_scope :issues | |
21 |
|
21 | |||
22 | before_filter :find_issue, :only => [:show, :edit, :update] |
|
22 | before_filter :find_issue, :only => [:show, :edit, :update] | |
23 | before_filter :find_issues, :only => [:bulk_edit, :bulk_update, :destroy] |
|
23 | before_filter :find_issues, :only => [:bulk_edit, :bulk_update, :destroy] | |
24 | before_filter :find_project, :only => [:new, :create] |
|
24 | before_filter :find_project, :only => [:new, :create] | |
25 | before_filter :authorize, :except => [:index] |
|
25 | before_filter :authorize, :except => [:index] | |
26 | before_filter :find_optional_project, :only => [:index] |
|
26 | before_filter :find_optional_project, :only => [:index] | |
27 | before_filter :check_for_default_issue_status, :only => [:new, :create] |
|
27 | before_filter :check_for_default_issue_status, :only => [:new, :create] | |
28 | before_filter :build_new_issue_from_params, :only => [:new, :create] |
|
28 | before_filter :build_new_issue_from_params, :only => [:new, :create] | |
29 | accept_rss_auth :index, :show |
|
29 | accept_rss_auth :index, :show | |
30 | accept_api_auth :index, :show, :create, :update, :destroy |
|
30 | accept_api_auth :index, :show, :create, :update, :destroy | |
31 |
|
31 | |||
32 | rescue_from Query::StatementInvalid, :with => :query_statement_invalid |
|
32 | rescue_from Query::StatementInvalid, :with => :query_statement_invalid | |
33 |
|
33 | |||
34 | helper :journals |
|
34 | helper :journals | |
35 | helper :projects |
|
35 | helper :projects | |
36 | include ProjectsHelper |
|
36 | include ProjectsHelper | |
37 | helper :custom_fields |
|
37 | helper :custom_fields | |
38 | include CustomFieldsHelper |
|
38 | include CustomFieldsHelper | |
39 | helper :issue_relations |
|
39 | helper :issue_relations | |
40 | include IssueRelationsHelper |
|
40 | include IssueRelationsHelper | |
41 | helper :watchers |
|
41 | helper :watchers | |
42 | include WatchersHelper |
|
42 | include WatchersHelper | |
43 | helper :attachments |
|
43 | helper :attachments | |
44 | include AttachmentsHelper |
|
44 | include AttachmentsHelper | |
45 | helper :queries |
|
45 | helper :queries | |
46 | include QueriesHelper |
|
46 | include QueriesHelper | |
47 | helper :repositories |
|
47 | helper :repositories | |
48 | include RepositoriesHelper |
|
48 | include RepositoriesHelper | |
49 | helper :sort |
|
49 | helper :sort | |
50 | include SortHelper |
|
50 | include SortHelper | |
51 | include IssuesHelper |
|
51 | include IssuesHelper | |
52 | helper :timelog |
|
52 | helper :timelog | |
53 | include Redmine::Export::PDF |
|
53 | include Redmine::Export::PDF | |
54 |
|
54 | |||
55 | def index |
|
55 | def index | |
56 | retrieve_query |
|
56 | retrieve_query | |
57 | sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria) |
|
57 | sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria) | |
58 | sort_update(@query.sortable_columns) |
|
58 | sort_update(@query.sortable_columns) | |
59 | @query.sort_criteria = sort_criteria.to_a |
|
59 | @query.sort_criteria = sort_criteria.to_a | |
60 |
|
60 | |||
61 | if @query.valid? |
|
61 | if @query.valid? | |
62 | case params[:format] |
|
62 | case params[:format] | |
63 | when 'csv', 'pdf' |
|
63 | when 'csv', 'pdf' | |
64 | @limit = Setting.issues_export_limit.to_i |
|
64 | @limit = Setting.issues_export_limit.to_i | |
65 | when 'atom' |
|
65 | when 'atom' | |
66 | @limit = Setting.feeds_limit.to_i |
|
66 | @limit = Setting.feeds_limit.to_i | |
67 | when 'xml', 'json' |
|
67 | when 'xml', 'json' | |
68 | @offset, @limit = api_offset_and_limit |
|
68 | @offset, @limit = api_offset_and_limit | |
69 | else |
|
69 | else | |
70 | @limit = per_page_option |
|
70 | @limit = per_page_option | |
71 | end |
|
71 | end | |
72 |
|
72 | |||
73 | @issue_count = @query.issue_count |
|
73 | @issue_count = @query.issue_count | |
74 | @issue_pages = Paginator.new self, @issue_count, @limit, params['page'] |
|
74 | @issue_pages = Paginator.new self, @issue_count, @limit, params['page'] | |
75 | @offset ||= @issue_pages.current.offset |
|
75 | @offset ||= @issue_pages.current.offset | |
76 | @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version], |
|
76 | @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version], | |
77 | :order => sort_clause, |
|
77 | :order => sort_clause, | |
78 | :offset => @offset, |
|
78 | :offset => @offset, | |
79 | :limit => @limit) |
|
79 | :limit => @limit) | |
80 | @issue_count_by_group = @query.issue_count_by_group |
|
80 | @issue_count_by_group = @query.issue_count_by_group | |
81 |
|
81 | |||
82 | respond_to do |format| |
|
82 | respond_to do |format| | |
83 | format.html { render :template => 'issues/index', :layout => !request.xhr? } |
|
83 | format.html { render :template => 'issues/index', :layout => !request.xhr? } | |
84 | format.api { |
|
84 | format.api { | |
85 | Issue.load_visible_relations(@issues) if include_in_api_response?('relations') |
|
85 | Issue.load_visible_relations(@issues) if include_in_api_response?('relations') | |
86 | } |
|
86 | } | |
87 | format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") } |
|
87 | format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") } | |
88 | format.csv { send_data(issues_to_csv(@issues, @project, @query, params), :type => 'text/csv; header=present', :filename => 'export.csv') } |
|
88 | format.csv { send_data(issues_to_csv(@issues, @project, @query, params), :type => 'text/csv; header=present', :filename => 'export.csv') } | |
89 | format.pdf { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'export.pdf') } |
|
89 | format.pdf { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'export.pdf') } | |
90 | end |
|
90 | end | |
91 | else |
|
91 | else | |
92 | respond_to do |format| |
|
92 | respond_to do |format| | |
93 | format.html { render(:template => 'issues/index', :layout => !request.xhr?) } |
|
93 | format.html { render(:template => 'issues/index', :layout => !request.xhr?) } | |
94 | format.any(:atom, :csv, :pdf) { render(:nothing => true) } |
|
94 | format.any(:atom, :csv, :pdf) { render(:nothing => true) } | |
95 | format.api { render_validation_errors(@query) } |
|
95 | format.api { render_validation_errors(@query) } | |
96 | end |
|
96 | end | |
97 | end |
|
97 | end | |
98 | rescue ActiveRecord::RecordNotFound |
|
98 | rescue ActiveRecord::RecordNotFound | |
99 | render_404 |
|
99 | render_404 | |
100 | end |
|
100 | end | |
101 |
|
101 | |||
102 | def show |
|
102 | def show | |
103 | @journals = @issue.journals.includes(:user, :details).reorder("#{Journal.table_name}.id ASC").all |
|
103 | @journals = @issue.journals.includes(:user, :details).reorder("#{Journal.table_name}.id ASC").all | |
104 | @journals.each_with_index {|j,i| j.indice = i+1} |
|
104 | @journals.each_with_index {|j,i| j.indice = i+1} | |
105 | @journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project) |
|
105 | @journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project) | |
106 | @journals.reverse! if User.current.wants_comments_in_reverse_order? |
|
106 | @journals.reverse! if User.current.wants_comments_in_reverse_order? | |
107 |
|
107 | |||
108 | @changesets = @issue.changesets.visible.all |
|
108 | @changesets = @issue.changesets.visible.all | |
109 | @changesets.reverse! if User.current.wants_comments_in_reverse_order? |
|
109 | @changesets.reverse! if User.current.wants_comments_in_reverse_order? | |
110 |
|
110 | |||
111 | @relations = @issue.relations.select {|r| r.other_issue(@issue) && r.other_issue(@issue).visible? } |
|
111 | @relations = @issue.relations.select {|r| r.other_issue(@issue) && r.other_issue(@issue).visible? } | |
112 | @allowed_statuses = @issue.new_statuses_allowed_to(User.current) |
|
112 | @allowed_statuses = @issue.new_statuses_allowed_to(User.current) | |
113 | @edit_allowed = User.current.allowed_to?(:edit_issues, @project) |
|
113 | @edit_allowed = User.current.allowed_to?(:edit_issues, @project) | |
114 | @priorities = IssuePriority.active |
|
114 | @priorities = IssuePriority.active | |
115 | @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project) |
|
115 | @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project) | |
116 | respond_to do |format| |
|
116 | respond_to do |format| | |
117 | format.html { |
|
117 | format.html { | |
118 | retrieve_previous_and_next_issue_ids |
|
118 | retrieve_previous_and_next_issue_ids | |
119 | render :template => 'issues/show' |
|
119 | render :template => 'issues/show' | |
120 | } |
|
120 | } | |
121 | format.api |
|
121 | format.api | |
122 | format.atom { render :template => 'journals/index', :layout => false, :content_type => 'application/atom+xml' } |
|
122 | format.atom { render :template => 'journals/index', :layout => false, :content_type => 'application/atom+xml' } | |
123 | format.pdf { |
|
123 | format.pdf { | |
124 | pdf = issue_to_pdf(@issue, :journals => @journals) |
|
124 | pdf = issue_to_pdf(@issue, :journals => @journals) | |
125 | send_data(pdf, :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf") |
|
125 | send_data(pdf, :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf") | |
126 | } |
|
126 | } | |
127 | end |
|
127 | end | |
128 | end |
|
128 | end | |
129 |
|
129 | |||
130 | # Add a new issue |
|
130 | # Add a new issue | |
131 | # The new issue will be created from an existing one if copy_from parameter is given |
|
131 | # The new issue will be created from an existing one if copy_from parameter is given | |
132 | def new |
|
132 | def new | |
133 | respond_to do |format| |
|
133 | respond_to do |format| | |
134 | format.html { render :action => 'new', :layout => !request.xhr? } |
|
134 | format.html { render :action => 'new', :layout => !request.xhr? } | |
135 | format.js { render :partial => 'update_form' } |
|
135 | format.js { render :partial => 'update_form' } | |
136 | end |
|
136 | end | |
137 | end |
|
137 | end | |
138 |
|
138 | |||
139 | def create |
|
139 | def create | |
140 | call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue }) |
|
140 | call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue }) | |
141 | @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads])) |
|
141 | @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads])) | |
142 | if @issue.save |
|
142 | if @issue.save | |
143 | call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue}) |
|
143 | call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue}) | |
144 | respond_to do |format| |
|
144 | respond_to do |format| | |
145 | format.html { |
|
145 | format.html { | |
146 | render_attachment_warning_if_needed(@issue) |
|
146 | render_attachment_warning_if_needed(@issue) | |
147 | flash[:notice] = l(:notice_issue_successful_create, :id => view_context.link_to("##{@issue.id}", issue_path(@issue), :title => @issue.subject)) |
|
147 | flash[:notice] = l(:notice_issue_successful_create, :id => view_context.link_to("##{@issue.id}", issue_path(@issue), :title => @issue.subject)) | |
148 | redirect_to(params[:continue] ? { :action => 'new', :project_id => @issue.project, :issue => {:tracker_id => @issue.tracker, :parent_issue_id => @issue.parent_issue_id}.reject {|k,v| v.nil?} } : |
|
148 | redirect_to(params[:continue] ? { :action => 'new', :project_id => @issue.project, :issue => {:tracker_id => @issue.tracker, :parent_issue_id => @issue.parent_issue_id}.reject {|k,v| v.nil?} } : | |
149 | { :action => 'show', :id => @issue }) |
|
149 | { :action => 'show', :id => @issue }) | |
150 | } |
|
150 | } | |
151 | format.api { render :action => 'show', :status => :created, :location => issue_url(@issue) } |
|
151 | format.api { render :action => 'show', :status => :created, :location => issue_url(@issue) } | |
152 | end |
|
152 | end | |
153 | return |
|
153 | return | |
154 | else |
|
154 | else | |
155 | respond_to do |format| |
|
155 | respond_to do |format| | |
156 | format.html { render :action => 'new' } |
|
156 | format.html { render :action => 'new' } | |
157 | format.api { render_validation_errors(@issue) } |
|
157 | format.api { render_validation_errors(@issue) } | |
158 | end |
|
158 | end | |
159 | end |
|
159 | end | |
160 | end |
|
160 | end | |
161 |
|
161 | |||
162 | def edit |
|
162 | def edit | |
163 | return unless update_issue_from_params |
|
163 | return unless update_issue_from_params | |
164 |
|
164 | |||
165 | respond_to do |format| |
|
165 | respond_to do |format| | |
166 | format.html { } |
|
166 | format.html { } | |
167 | format.xml { } |
|
167 | format.xml { } | |
168 | end |
|
168 | end | |
169 | end |
|
169 | end | |
170 |
|
170 | |||
171 | def update |
|
171 | def update | |
172 | return unless update_issue_from_params |
|
172 | return unless update_issue_from_params | |
173 | @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads])) |
|
173 | @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads])) | |
174 | saved = false |
|
174 | saved = false | |
175 | begin |
|
175 | begin | |
176 | saved = @issue.save_issue_with_child_records(params, @time_entry) |
|
176 | saved = @issue.save_issue_with_child_records(params, @time_entry) | |
177 | rescue ActiveRecord::StaleObjectError |
|
177 | rescue ActiveRecord::StaleObjectError | |
178 | @conflict = true |
|
178 | @conflict = true | |
179 | if params[:last_journal_id] |
|
179 | if params[:last_journal_id] | |
180 | @conflict_journals = @issue.journals_after(params[:last_journal_id]).all |
|
180 | @conflict_journals = @issue.journals_after(params[:last_journal_id]).all | |
181 | @conflict_journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project) |
|
181 | @conflict_journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project) | |
182 | end |
|
182 | end | |
183 | end |
|
183 | end | |
184 |
|
184 | |||
185 | if saved |
|
185 | if saved | |
186 | render_attachment_warning_if_needed(@issue) |
|
186 | render_attachment_warning_if_needed(@issue) | |
187 | flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record? |
|
187 | flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record? | |
188 |
|
188 | |||
189 | respond_to do |format| |
|
189 | respond_to do |format| | |
190 | format.html { redirect_back_or_default({:action => 'show', :id => @issue}) } |
|
190 | format.html { redirect_back_or_default({:action => 'show', :id => @issue}) } | |
191 | format.api { render_api_ok } |
|
191 | format.api { render_api_ok } | |
192 | end |
|
192 | end | |
193 | else |
|
193 | else | |
194 | respond_to do |format| |
|
194 | respond_to do |format| | |
195 | format.html { render :action => 'edit' } |
|
195 | format.html { render :action => 'edit' } | |
196 | format.api { render_validation_errors(@issue) } |
|
196 | format.api { render_validation_errors(@issue) } | |
197 | end |
|
197 | end | |
198 | end |
|
198 | end | |
199 | end |
|
199 | end | |
200 |
|
200 | |||
201 | # Bulk edit/copy a set of issues |
|
201 | # Bulk edit/copy a set of issues | |
202 | def bulk_edit |
|
202 | def bulk_edit | |
203 | @issues.sort! |
|
203 | @issues.sort! | |
204 | @copy = params[:copy].present? |
|
204 | @copy = params[:copy].present? | |
205 | @notes = params[:notes] |
|
205 | @notes = params[:notes] | |
206 |
|
206 | |||
207 | if User.current.allowed_to?(:move_issues, @projects) |
|
207 | if User.current.allowed_to?(:move_issues, @projects) | |
208 | @allowed_projects = Issue.allowed_target_projects_on_move |
|
208 | @allowed_projects = Issue.allowed_target_projects_on_move | |
209 | if params[:issue] |
|
209 | if params[:issue] | |
210 | @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:issue][:project_id].to_s} |
|
210 | @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:issue][:project_id].to_s} | |
211 | if @target_project |
|
211 | if @target_project | |
212 | target_projects = [@target_project] |
|
212 | target_projects = [@target_project] | |
213 | end |
|
213 | end | |
214 | end |
|
214 | end | |
215 | end |
|
215 | end | |
216 | target_projects ||= @projects |
|
216 | target_projects ||= @projects | |
217 |
|
217 | |||
218 | if @copy |
|
218 | if @copy | |
219 | @available_statuses = [IssueStatus.default] |
|
219 | @available_statuses = [IssueStatus.default] | |
220 | else |
|
220 | else | |
221 | @available_statuses = @issues.map(&:new_statuses_allowed_to).reduce(:&) |
|
221 | @available_statuses = @issues.map(&:new_statuses_allowed_to).reduce(:&) | |
222 | end |
|
222 | end | |
223 | @custom_fields = target_projects.map{|p|p.all_issue_custom_fields}.reduce(:&) |
|
223 | @custom_fields = target_projects.map{|p|p.all_issue_custom_fields}.reduce(:&) | |
224 | @assignables = target_projects.map(&:assignable_users).reduce(:&) |
|
224 | @assignables = target_projects.map(&:assignable_users).reduce(:&) | |
225 | @trackers = target_projects.map(&:trackers).reduce(:&) |
|
225 | @trackers = target_projects.map(&:trackers).reduce(:&) | |
226 | @versions = target_projects.map {|p| p.shared_versions.open}.reduce(:&) |
|
226 | @versions = target_projects.map {|p| p.shared_versions.open}.reduce(:&) | |
227 | @categories = target_projects.map {|p| p.issue_categories}.reduce(:&) |
|
227 | @categories = target_projects.map {|p| p.issue_categories}.reduce(:&) | |
228 | if @copy |
|
228 | if @copy | |
229 | @attachments_present = @issues.detect {|i| i.attachments.any?}.present? |
|
229 | @attachments_present = @issues.detect {|i| i.attachments.any?}.present? | |
230 | @subtasks_present = @issues.detect {|i| !i.leaf?}.present? |
|
230 | @subtasks_present = @issues.detect {|i| !i.leaf?}.present? | |
231 | end |
|
231 | end | |
232 |
|
232 | |||
233 | @safe_attributes = @issues.map(&:safe_attribute_names).reduce(:&) |
|
233 | @safe_attributes = @issues.map(&:safe_attribute_names).reduce(:&) | |
234 | render :layout => false if request.xhr? |
|
234 | render :layout => false if request.xhr? | |
235 | end |
|
235 | end | |
236 |
|
236 | |||
237 | def bulk_update |
|
237 | def bulk_update | |
238 | @issues.sort! |
|
238 | @issues.sort! | |
239 | @copy = params[:copy].present? |
|
239 | @copy = params[:copy].present? | |
240 | attributes = parse_params_for_bulk_issue_attributes(params) |
|
240 | attributes = parse_params_for_bulk_issue_attributes(params) | |
241 |
|
241 | |||
242 | unsaved_issue_ids = [] |
|
242 | unsaved_issue_ids = [] | |
243 | moved_issues = [] |
|
243 | moved_issues = [] | |
244 |
|
244 | |||
245 | if @copy && params[:copy_subtasks].present? |
|
245 | if @copy && params[:copy_subtasks].present? | |
246 | # Descendant issues will be copied with the parent task |
|
246 | # Descendant issues will be copied with the parent task | |
247 | # Don't copy them twice |
|
247 | # Don't copy them twice | |
248 | @issues.reject! {|issue| @issues.detect {|other| issue.is_descendant_of?(other)}} |
|
248 | @issues.reject! {|issue| @issues.detect {|other| issue.is_descendant_of?(other)}} | |
249 | end |
|
249 | end | |
250 |
|
250 | |||
251 | @issues.each do |issue| |
|
251 | @issues.each do |issue| | |
252 | issue.reload |
|
252 | issue.reload | |
253 | if @copy |
|
253 | if @copy | |
254 | issue = issue.copy({}, |
|
254 | issue = issue.copy({}, | |
255 | :attachments => params[:copy_attachments].present?, |
|
255 | :attachments => params[:copy_attachments].present?, | |
256 | :subtasks => params[:copy_subtasks].present? |
|
256 | :subtasks => params[:copy_subtasks].present? | |
257 | ) |
|
257 | ) | |
258 | end |
|
258 | end | |
259 | journal = issue.init_journal(User.current, params[:notes]) |
|
259 | journal = issue.init_journal(User.current, params[:notes]) | |
260 | issue.safe_attributes = attributes |
|
260 | issue.safe_attributes = attributes | |
261 | call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue }) |
|
261 | call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue }) | |
262 | if issue.save |
|
262 | if issue.save | |
263 | moved_issues << issue |
|
263 | moved_issues << issue | |
264 | else |
|
264 | else | |
265 | # Keep unsaved issue ids to display them in flash error |
|
265 | # Keep unsaved issue ids to display them in flash error | |
266 | unsaved_issue_ids << issue.id |
|
266 | unsaved_issue_ids << issue.id | |
267 | end |
|
267 | end | |
268 | end |
|
268 | end | |
269 | set_flash_from_bulk_issue_save(@issues, unsaved_issue_ids) |
|
269 | set_flash_from_bulk_issue_save(@issues, unsaved_issue_ids) | |
270 |
|
270 | |||
271 | if params[:follow] |
|
271 | if params[:follow] | |
272 | if @issues.size == 1 && moved_issues.size == 1 |
|
272 | if @issues.size == 1 && moved_issues.size == 1 | |
273 | redirect_to :controller => 'issues', :action => 'show', :id => moved_issues.first |
|
273 | redirect_to :controller => 'issues', :action => 'show', :id => moved_issues.first | |
274 | elsif moved_issues.map(&:project).uniq.size == 1 |
|
274 | elsif moved_issues.map(&:project).uniq.size == 1 | |
275 | redirect_to :controller => 'issues', :action => 'index', :project_id => moved_issues.map(&:project).first |
|
275 | redirect_to :controller => 'issues', :action => 'index', :project_id => moved_issues.map(&:project).first | |
276 | end |
|
276 | end | |
277 | else |
|
277 | else | |
278 | redirect_back_or_default({:controller => 'issues', :action => 'index', :project_id => @project}) |
|
278 | redirect_back_or_default({:controller => 'issues', :action => 'index', :project_id => @project}) | |
279 | end |
|
279 | end | |
280 | end |
|
280 | end | |
281 |
|
281 | |||
282 | def destroy |
|
282 | def destroy | |
283 | @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f |
|
283 | @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f | |
284 | if @hours > 0 |
|
284 | if @hours > 0 | |
285 | case params[:todo] |
|
285 | case params[:todo] | |
286 | when 'destroy' |
|
286 | when 'destroy' | |
287 | # nothing to do |
|
287 | # nothing to do | |
288 | when 'nullify' |
|
288 | when 'nullify' | |
289 | TimeEntry.update_all('issue_id = NULL', ['issue_id IN (?)', @issues]) |
|
289 | TimeEntry.update_all('issue_id = NULL', ['issue_id IN (?)', @issues]) | |
290 | when 'reassign' |
|
290 | when 'reassign' | |
291 | reassign_to = @project.issues.find_by_id(params[:reassign_to_id]) |
|
291 | reassign_to = @project.issues.find_by_id(params[:reassign_to_id]) | |
292 | if reassign_to.nil? |
|
292 | if reassign_to.nil? | |
293 | flash.now[:error] = l(:error_issue_not_found_in_project) |
|
293 | flash.now[:error] = l(:error_issue_not_found_in_project) | |
294 | return |
|
294 | return | |
295 | else |
|
295 | else | |
296 | TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues]) |
|
296 | TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues]) | |
297 | end |
|
297 | end | |
298 | else |
|
298 | else | |
299 | # display the destroy form if it's a user request |
|
299 | # display the destroy form if it's a user request | |
300 | return unless api_request? |
|
300 | return unless api_request? | |
301 | end |
|
301 | end | |
302 | end |
|
302 | end | |
303 | @issues.each do |issue| |
|
303 | @issues.each do |issue| | |
304 | begin |
|
304 | begin | |
305 | issue.reload.destroy |
|
305 | issue.reload.destroy | |
306 | rescue ::ActiveRecord::RecordNotFound # raised by #reload if issue no longer exists |
|
306 | rescue ::ActiveRecord::RecordNotFound # raised by #reload if issue no longer exists | |
307 | # nothing to do, issue was already deleted (eg. by a parent) |
|
307 | # nothing to do, issue was already deleted (eg. by a parent) | |
308 | end |
|
308 | end | |
309 | end |
|
309 | end | |
310 | respond_to do |format| |
|
310 | respond_to do |format| | |
311 | format.html { redirect_back_or_default(:action => 'index', :project_id => @project) } |
|
311 | format.html { redirect_back_or_default(:action => 'index', :project_id => @project) } | |
312 | format.api { render_api_ok } |
|
312 | format.api { render_api_ok } | |
313 | end |
|
313 | end | |
314 | end |
|
314 | end | |
315 |
|
315 | |||
316 | private |
|
316 | private | |
317 | def find_issue |
|
|||
318 | # Issue.visible.find(...) can not be used to redirect user to the login form |
|
|||
319 | # if the issue actually exists but requires authentication |
|
|||
320 | @issue = Issue.find(params[:id], :include => [:project, :tracker, :status, :author, :priority, :category]) |
|
|||
321 | unless @issue.visible? |
|
|||
322 | deny_access |
|
|||
323 | return |
|
|||
324 | end |
|
|||
325 | @project = @issue.project |
|
|||
326 | rescue ActiveRecord::RecordNotFound |
|
|||
327 | render_404 |
|
|||
328 | end |
|
|||
329 |
|
317 | |||
330 | def find_project |
|
318 | def find_project | |
331 | project_id = params[:project_id] || (params[:issue] && params[:issue][:project_id]) |
|
319 | project_id = params[:project_id] || (params[:issue] && params[:issue][:project_id]) | |
332 | @project = Project.find(project_id) |
|
320 | @project = Project.find(project_id) | |
333 | rescue ActiveRecord::RecordNotFound |
|
321 | rescue ActiveRecord::RecordNotFound | |
334 | render_404 |
|
322 | render_404 | |
335 | end |
|
323 | end | |
336 |
|
324 | |||
337 | def retrieve_previous_and_next_issue_ids |
|
325 | def retrieve_previous_and_next_issue_ids | |
338 | retrieve_query_from_session |
|
326 | retrieve_query_from_session | |
339 | if @query |
|
327 | if @query | |
340 | sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria) |
|
328 | sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria) | |
341 | sort_update(@query.sortable_columns, 'issues_index_sort') |
|
329 | sort_update(@query.sortable_columns, 'issues_index_sort') | |
342 | limit = 500 |
|
330 | limit = 500 | |
343 | issue_ids = @query.issue_ids(:order => sort_clause, :limit => (limit + 1), :include => [:assigned_to, :tracker, :priority, :category, :fixed_version]) |
|
331 | issue_ids = @query.issue_ids(:order => sort_clause, :limit => (limit + 1), :include => [:assigned_to, :tracker, :priority, :category, :fixed_version]) | |
344 | if (idx = issue_ids.index(@issue.id)) && idx < limit |
|
332 | if (idx = issue_ids.index(@issue.id)) && idx < limit | |
345 | if issue_ids.size < 500 |
|
333 | if issue_ids.size < 500 | |
346 | @issue_position = idx + 1 |
|
334 | @issue_position = idx + 1 | |
347 | @issue_count = issue_ids.size |
|
335 | @issue_count = issue_ids.size | |
348 | end |
|
336 | end | |
349 | @prev_issue_id = issue_ids[idx - 1] if idx > 0 |
|
337 | @prev_issue_id = issue_ids[idx - 1] if idx > 0 | |
350 | @next_issue_id = issue_ids[idx + 1] if idx < (issue_ids.size - 1) |
|
338 | @next_issue_id = issue_ids[idx + 1] if idx < (issue_ids.size - 1) | |
351 | end |
|
339 | end | |
352 | end |
|
340 | end | |
353 | end |
|
341 | end | |
354 |
|
342 | |||
355 | # Used by #edit and #update to set some common instance variables |
|
343 | # Used by #edit and #update to set some common instance variables | |
356 | # from the params |
|
344 | # from the params | |
357 | # TODO: Refactor, not everything in here is needed by #edit |
|
345 | # TODO: Refactor, not everything in here is needed by #edit | |
358 | def update_issue_from_params |
|
346 | def update_issue_from_params | |
359 | @edit_allowed = User.current.allowed_to?(:edit_issues, @project) |
|
347 | @edit_allowed = User.current.allowed_to?(:edit_issues, @project) | |
360 | @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project) |
|
348 | @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project) | |
361 | @time_entry.attributes = params[:time_entry] |
|
349 | @time_entry.attributes = params[:time_entry] | |
362 |
|
350 | |||
363 | @issue.init_journal(User.current) |
|
351 | @issue.init_journal(User.current) | |
364 |
|
352 | |||
365 | issue_attributes = params[:issue] |
|
353 | issue_attributes = params[:issue] | |
366 | if issue_attributes && params[:conflict_resolution] |
|
354 | if issue_attributes && params[:conflict_resolution] | |
367 | case params[:conflict_resolution] |
|
355 | case params[:conflict_resolution] | |
368 | when 'overwrite' |
|
356 | when 'overwrite' | |
369 | issue_attributes = issue_attributes.dup |
|
357 | issue_attributes = issue_attributes.dup | |
370 | issue_attributes.delete(:lock_version) |
|
358 | issue_attributes.delete(:lock_version) | |
371 | when 'add_notes' |
|
359 | when 'add_notes' | |
372 | issue_attributes = issue_attributes.slice(:notes) |
|
360 | issue_attributes = issue_attributes.slice(:notes) | |
373 | when 'cancel' |
|
361 | when 'cancel' | |
374 | redirect_to issue_path(@issue) |
|
362 | redirect_to issue_path(@issue) | |
375 | return false |
|
363 | return false | |
376 | end |
|
364 | end | |
377 | end |
|
365 | end | |
378 | @issue.safe_attributes = issue_attributes |
|
366 | @issue.safe_attributes = issue_attributes | |
379 | @priorities = IssuePriority.active |
|
367 | @priorities = IssuePriority.active | |
380 | @allowed_statuses = @issue.new_statuses_allowed_to(User.current) |
|
368 | @allowed_statuses = @issue.new_statuses_allowed_to(User.current) | |
381 | true |
|
369 | true | |
382 | end |
|
370 | end | |
383 |
|
371 | |||
384 | # TODO: Refactor, lots of extra code in here |
|
372 | # TODO: Refactor, lots of extra code in here | |
385 | # TODO: Changing tracker on an existing issue should not trigger this |
|
373 | # TODO: Changing tracker on an existing issue should not trigger this | |
386 | def build_new_issue_from_params |
|
374 | def build_new_issue_from_params | |
387 | if params[:id].blank? |
|
375 | if params[:id].blank? | |
388 | @issue = Issue.new |
|
376 | @issue = Issue.new | |
389 | if params[:copy_from] |
|
377 | if params[:copy_from] | |
390 | begin |
|
378 | begin | |
391 | @copy_from = Issue.visible.find(params[:copy_from]) |
|
379 | @copy_from = Issue.visible.find(params[:copy_from]) | |
392 | @copy_attachments = params[:copy_attachments].present? || request.get? |
|
380 | @copy_attachments = params[:copy_attachments].present? || request.get? | |
393 | @copy_subtasks = params[:copy_subtasks].present? || request.get? |
|
381 | @copy_subtasks = params[:copy_subtasks].present? || request.get? | |
394 | @issue.copy_from(@copy_from, :attachments => @copy_attachments, :subtasks => @copy_subtasks) |
|
382 | @issue.copy_from(@copy_from, :attachments => @copy_attachments, :subtasks => @copy_subtasks) | |
395 | rescue ActiveRecord::RecordNotFound |
|
383 | rescue ActiveRecord::RecordNotFound | |
396 | render_404 |
|
384 | render_404 | |
397 | return |
|
385 | return | |
398 | end |
|
386 | end | |
399 | end |
|
387 | end | |
400 | @issue.project = @project |
|
388 | @issue.project = @project | |
401 | else |
|
389 | else | |
402 | @issue = @project.issues.visible.find(params[:id]) |
|
390 | @issue = @project.issues.visible.find(params[:id]) | |
403 | end |
|
391 | end | |
404 |
|
392 | |||
405 | @issue.project = @project |
|
393 | @issue.project = @project | |
406 | @issue.author ||= User.current |
|
394 | @issue.author ||= User.current | |
407 | # Tracker must be set before custom field values |
|
395 | # Tracker must be set before custom field values | |
408 | @issue.tracker ||= @project.trackers.find((params[:issue] && params[:issue][:tracker_id]) || params[:tracker_id] || :first) |
|
396 | @issue.tracker ||= @project.trackers.find((params[:issue] && params[:issue][:tracker_id]) || params[:tracker_id] || :first) | |
409 | if @issue.tracker.nil? |
|
397 | if @issue.tracker.nil? | |
410 | render_error l(:error_no_tracker_in_project) |
|
398 | render_error l(:error_no_tracker_in_project) | |
411 | return false |
|
399 | return false | |
412 | end |
|
400 | end | |
413 | @issue.start_date ||= Date.today if Setting.default_issue_start_date_to_creation_date? |
|
401 | @issue.start_date ||= Date.today if Setting.default_issue_start_date_to_creation_date? | |
414 | @issue.safe_attributes = params[:issue] |
|
402 | @issue.safe_attributes = params[:issue] | |
415 |
|
403 | |||
416 | @priorities = IssuePriority.active |
|
404 | @priorities = IssuePriority.active | |
417 | @allowed_statuses = @issue.new_statuses_allowed_to(User.current, true) |
|
405 | @allowed_statuses = @issue.new_statuses_allowed_to(User.current, true) | |
418 | @available_watchers = (@issue.project.users.sort + @issue.watcher_users).uniq |
|
406 | @available_watchers = (@issue.project.users.sort + @issue.watcher_users).uniq | |
419 | end |
|
407 | end | |
420 |
|
408 | |||
421 | def check_for_default_issue_status |
|
409 | def check_for_default_issue_status | |
422 | if IssueStatus.default.nil? |
|
410 | if IssueStatus.default.nil? | |
423 | render_error l(:error_no_default_issue_status) |
|
411 | render_error l(:error_no_default_issue_status) | |
424 | return false |
|
412 | return false | |
425 | end |
|
413 | end | |
426 | end |
|
414 | end | |
427 |
|
415 | |||
428 | def parse_params_for_bulk_issue_attributes(params) |
|
416 | def parse_params_for_bulk_issue_attributes(params) | |
429 | attributes = (params[:issue] || {}).reject {|k,v| v.blank?} |
|
417 | attributes = (params[:issue] || {}).reject {|k,v| v.blank?} | |
430 | attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'} |
|
418 | attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'} | |
431 | if custom = attributes[:custom_field_values] |
|
419 | if custom = attributes[:custom_field_values] | |
432 | custom.reject! {|k,v| v.blank?} |
|
420 | custom.reject! {|k,v| v.blank?} | |
433 | custom.keys.each do |k| |
|
421 | custom.keys.each do |k| | |
434 | if custom[k].is_a?(Array) |
|
422 | if custom[k].is_a?(Array) | |
435 | custom[k] << '' if custom[k].delete('__none__') |
|
423 | custom[k] << '' if custom[k].delete('__none__') | |
436 | else |
|
424 | else | |
437 | custom[k] = '' if custom[k] == '__none__' |
|
425 | custom[k] = '' if custom[k] == '__none__' | |
438 | end |
|
426 | end | |
439 | end |
|
427 | end | |
440 | end |
|
428 | end | |
441 | attributes |
|
429 | attributes | |
442 | end |
|
430 | end | |
443 | end |
|
431 | end |
@@ -1,113 +1,105 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2012 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2012 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 | class JournalsController < ApplicationController |
|
18 | class JournalsController < ApplicationController | |
19 | before_filter :find_journal, :only => [:edit, :diff] |
|
19 | before_filter :find_journal, :only => [:edit, :diff] | |
20 | before_filter :find_issue, :only => [:new] |
|
20 | before_filter :find_issue, :only => [:new] | |
21 | before_filter :find_optional_project, :only => [:index] |
|
21 | before_filter :find_optional_project, :only => [:index] | |
22 | before_filter :authorize, :only => [:new, :edit, :diff] |
|
22 | before_filter :authorize, :only => [:new, :edit, :diff] | |
23 | accept_rss_auth :index |
|
23 | accept_rss_auth :index | |
24 | menu_item :issues |
|
24 | menu_item :issues | |
25 |
|
25 | |||
26 | helper :issues |
|
26 | helper :issues | |
27 | helper :custom_fields |
|
27 | helper :custom_fields | |
28 | helper :queries |
|
28 | helper :queries | |
29 | include QueriesHelper |
|
29 | include QueriesHelper | |
30 | helper :sort |
|
30 | helper :sort | |
31 | include SortHelper |
|
31 | include SortHelper | |
32 |
|
32 | |||
33 | def index |
|
33 | def index | |
34 | retrieve_query |
|
34 | retrieve_query | |
35 | sort_init 'id', 'desc' |
|
35 | sort_init 'id', 'desc' | |
36 | sort_update(@query.sortable_columns) |
|
36 | sort_update(@query.sortable_columns) | |
37 |
|
37 | |||
38 | if @query.valid? |
|
38 | if @query.valid? | |
39 | @journals = @query.journals(:order => "#{Journal.table_name}.created_on DESC", |
|
39 | @journals = @query.journals(:order => "#{Journal.table_name}.created_on DESC", | |
40 | :limit => 25) |
|
40 | :limit => 25) | |
41 | end |
|
41 | end | |
42 | @title = (@project ? @project.name : Setting.app_title) + ": " + (@query.new_record? ? l(:label_changes_details) : @query.name) |
|
42 | @title = (@project ? @project.name : Setting.app_title) + ": " + (@query.new_record? ? l(:label_changes_details) : @query.name) | |
43 | render :layout => false, :content_type => 'application/atom+xml' |
|
43 | render :layout => false, :content_type => 'application/atom+xml' | |
44 | rescue ActiveRecord::RecordNotFound |
|
44 | rescue ActiveRecord::RecordNotFound | |
45 | render_404 |
|
45 | render_404 | |
46 | end |
|
46 | end | |
47 |
|
47 | |||
48 | def diff |
|
48 | def diff | |
49 | @issue = @journal.issue |
|
49 | @issue = @journal.issue | |
50 | if params[:detail_id].present? |
|
50 | if params[:detail_id].present? | |
51 | @detail = @journal.details.find_by_id(params[:detail_id]) |
|
51 | @detail = @journal.details.find_by_id(params[:detail_id]) | |
52 | else |
|
52 | else | |
53 | @detail = @journal.details.detect {|d| d.prop_key == 'description'} |
|
53 | @detail = @journal.details.detect {|d| d.prop_key == 'description'} | |
54 | end |
|
54 | end | |
55 | (render_404; return false) unless @issue && @detail |
|
55 | (render_404; return false) unless @issue && @detail | |
56 | @diff = Redmine::Helpers::Diff.new(@detail.value, @detail.old_value) |
|
56 | @diff = Redmine::Helpers::Diff.new(@detail.value, @detail.old_value) | |
57 | end |
|
57 | end | |
58 |
|
58 | |||
59 | def new |
|
59 | def new | |
60 | @journal = Journal.visible.find(params[:journal_id]) if params[:journal_id] |
|
60 | @journal = Journal.visible.find(params[:journal_id]) if params[:journal_id] | |
61 | if @journal |
|
61 | if @journal | |
62 | user = @journal.user |
|
62 | user = @journal.user | |
63 | text = @journal.notes |
|
63 | text = @journal.notes | |
64 | else |
|
64 | else | |
65 | user = @issue.author |
|
65 | user = @issue.author | |
66 | text = @issue.description |
|
66 | text = @issue.description | |
67 | end |
|
67 | end | |
68 | # Replaces pre blocks with [...] |
|
68 | # Replaces pre blocks with [...] | |
69 | text = text.to_s.strip.gsub(%r{<pre>((.|\s)*?)</pre>}m, '[...]') |
|
69 | text = text.to_s.strip.gsub(%r{<pre>((.|\s)*?)</pre>}m, '[...]') | |
70 | @content = "#{ll(Setting.default_language, :text_user_wrote, user)}\n> " |
|
70 | @content = "#{ll(Setting.default_language, :text_user_wrote, user)}\n> " | |
71 | @content << text.gsub(/(\r?\n|\r\n?)/, "\n> ") + "\n\n" |
|
71 | @content << text.gsub(/(\r?\n|\r\n?)/, "\n> ") + "\n\n" | |
72 | rescue ActiveRecord::RecordNotFound |
|
72 | rescue ActiveRecord::RecordNotFound | |
73 | render_404 |
|
73 | render_404 | |
74 | end |
|
74 | end | |
75 |
|
75 | |||
76 | def edit |
|
76 | def edit | |
77 | (render_403; return false) unless @journal.editable_by?(User.current) |
|
77 | (render_403; return false) unless @journal.editable_by?(User.current) | |
78 | if request.post? |
|
78 | if request.post? | |
79 | @journal.update_attributes(:notes => params[:notes]) if params[:notes] |
|
79 | @journal.update_attributes(:notes => params[:notes]) if params[:notes] | |
80 | @journal.destroy if @journal.details.empty? && @journal.notes.blank? |
|
80 | @journal.destroy if @journal.details.empty? && @journal.notes.blank? | |
81 | call_hook(:controller_journals_edit_post, { :journal => @journal, :params => params}) |
|
81 | call_hook(:controller_journals_edit_post, { :journal => @journal, :params => params}) | |
82 | respond_to do |format| |
|
82 | respond_to do |format| | |
83 | format.html { redirect_to :controller => 'issues', :action => 'show', :id => @journal.journalized_id } |
|
83 | format.html { redirect_to :controller => 'issues', :action => 'show', :id => @journal.journalized_id } | |
84 | format.js { render :action => 'update' } |
|
84 | format.js { render :action => 'update' } | |
85 | end |
|
85 | end | |
86 | else |
|
86 | else | |
87 | respond_to do |format| |
|
87 | respond_to do |format| | |
88 | format.html { |
|
88 | format.html { | |
89 | # TODO: implement non-JS journal update |
|
89 | # TODO: implement non-JS journal update | |
90 | render :nothing => true |
|
90 | render :nothing => true | |
91 | } |
|
91 | } | |
92 | format.js |
|
92 | format.js | |
93 | end |
|
93 | end | |
94 | end |
|
94 | end | |
95 | end |
|
95 | end | |
96 |
|
96 | |||
97 | private |
|
97 | private | |
98 |
|
98 | |||
99 | def find_journal |
|
99 | def find_journal | |
100 | @journal = Journal.visible.find(params[:id]) |
|
100 | @journal = Journal.visible.find(params[:id]) | |
101 | @project = @journal.journalized.project |
|
101 | @project = @journal.journalized.project | |
102 | rescue ActiveRecord::RecordNotFound |
|
102 | rescue ActiveRecord::RecordNotFound | |
103 | render_404 |
|
103 | render_404 | |
104 | end |
|
104 | end | |
105 |
|
||||
106 | # TODO: duplicated in IssuesController |
|
|||
107 | def find_issue |
|
|||
108 | @issue = Issue.find(params[:id], :include => [:project, :tracker, :status, :author, :priority, :category]) |
|
|||
109 | @project = @issue.project |
|
|||
110 | rescue ActiveRecord::RecordNotFound |
|
|||
111 | render_404 |
|
|||
112 | end |
|
|||
113 | end |
|
105 | end |
General Comments 0
You need to be logged in to leave comments.
Login now