@@ -1,538 +1,551 | |||||
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.frozen? |
|
57 | if obj.frozen? | |
58 | obj |
|
58 | obj | |
59 | elsif obj.is_a? String |
|
59 | elsif obj.is_a? String | |
60 | obj.respond_to?(:force_encoding) ? obj.force_encoding("UTF-8") : obj |
|
60 | obj.respond_to?(:force_encoding) ? obj.force_encoding("UTF-8") : obj | |
61 | elsif obj.is_a? Hash |
|
61 | elsif obj.is_a? Hash | |
62 | obj.each {|k, v| obj[k] = self.utf8nize!(v)} |
|
62 | obj.each {|k, v| obj[k] = self.utf8nize!(v)} | |
63 | elsif obj.is_a? Array |
|
63 | elsif obj.is_a? Array | |
64 | obj.each {|v| self.utf8nize!(v)} |
|
64 | obj.each {|v| self.utf8nize!(v)} | |
65 | else |
|
65 | else | |
66 | obj |
|
66 | obj | |
67 | end |
|
67 | end | |
68 | end |
|
68 | end | |
69 |
|
69 | |||
70 | before_filter :user_setup, :check_if_login_required, :set_localization |
|
70 | before_filter :user_setup, :check_if_login_required, :set_localization | |
71 | filter_parameter_logging :password |
|
71 | filter_parameter_logging :password | |
72 |
|
72 | |||
73 | rescue_from ActionController::InvalidAuthenticityToken, :with => :invalid_authenticity_token |
|
73 | rescue_from ActionController::InvalidAuthenticityToken, :with => :invalid_authenticity_token | |
74 | rescue_from ::Unauthorized, :with => :deny_access |
|
74 | rescue_from ::Unauthorized, :with => :deny_access | |
75 |
|
75 | |||
76 | include Redmine::Search::Controller |
|
76 | include Redmine::Search::Controller | |
77 | include Redmine::MenuManager::MenuController |
|
77 | include Redmine::MenuManager::MenuController | |
78 | helper Redmine::MenuManager::MenuHelper |
|
78 | helper Redmine::MenuManager::MenuHelper | |
79 |
|
79 | |||
80 | Redmine::Scm::Base.all.each do |scm| |
|
80 | Redmine::Scm::Base.all.each do |scm| | |
81 | require_dependency "repository/#{scm.underscore}" |
|
81 | require_dependency "repository/#{scm.underscore}" | |
82 | end |
|
82 | end | |
83 |
|
83 | |||
84 | def user_setup |
|
84 | def user_setup | |
85 | # Check the settings cache for each request |
|
85 | # Check the settings cache for each request | |
86 | Setting.check_cache |
|
86 | Setting.check_cache | |
87 | # Find the current user |
|
87 | # Find the current user | |
88 | User.current = find_current_user |
|
88 | User.current = find_current_user | |
89 | end |
|
89 | end | |
90 |
|
90 | |||
91 | # 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 | |
92 | # and starts a session if needed |
|
92 | # and starts a session if needed | |
93 | def find_current_user |
|
93 | def find_current_user | |
94 | if session[:user_id] |
|
94 | if session[:user_id] | |
95 | # existing session |
|
95 | # existing session | |
96 | (User.active.find(session[:user_id]) rescue nil) |
|
96 | (User.active.find(session[:user_id]) rescue nil) | |
97 | elsif cookies[:autologin] && Setting.autologin? |
|
97 | elsif cookies[:autologin] && Setting.autologin? | |
98 | # auto-login feature starts a new session |
|
98 | # auto-login feature starts a new session | |
99 | user = User.try_to_autologin(cookies[:autologin]) |
|
99 | user = User.try_to_autologin(cookies[:autologin]) | |
100 | session[:user_id] = user.id if user |
|
100 | session[:user_id] = user.id if user | |
101 | user |
|
101 | user | |
102 | elsif params[:format] == 'atom' && params[:key] && request.get? && accept_rss_auth? |
|
102 | elsif params[:format] == 'atom' && params[:key] && request.get? && accept_rss_auth? | |
103 | # RSS key authentication does not start a session |
|
103 | # RSS key authentication does not start a session | |
104 | User.find_by_rss_key(params[:key]) |
|
104 | User.find_by_rss_key(params[:key]) | |
105 | elsif Setting.rest_api_enabled? && accept_api_auth? |
|
105 | elsif Setting.rest_api_enabled? && accept_api_auth? | |
106 | if (key = api_key_from_request) |
|
106 | if (key = api_key_from_request) | |
107 | # Use API key |
|
107 | # Use API key | |
108 | User.find_by_api_key(key) |
|
108 | User.find_by_api_key(key) | |
109 | else |
|
109 | else | |
110 | # HTTP Basic, either username/password or API key/random |
|
110 | # HTTP Basic, either username/password or API key/random | |
111 | authenticate_with_http_basic do |username, password| |
|
111 | authenticate_with_http_basic do |username, password| | |
112 | 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) | |
113 | end |
|
113 | end | |
114 | end |
|
114 | end | |
115 | end |
|
115 | end | |
116 | end |
|
116 | end | |
117 |
|
117 | |||
118 | # Sets the logged in user |
|
118 | # Sets the logged in user | |
119 | def logged_user=(user) |
|
119 | def logged_user=(user) | |
120 | reset_session |
|
120 | reset_session | |
121 | if user && user.is_a?(User) |
|
121 | if user && user.is_a?(User) | |
122 | User.current = user |
|
122 | User.current = user | |
123 | session[:user_id] = user.id |
|
123 | session[:user_id] = user.id | |
124 | else |
|
124 | else | |
125 | User.current = User.anonymous |
|
125 | User.current = User.anonymous | |
126 | end |
|
126 | end | |
127 | end |
|
127 | end | |
128 |
|
128 | |||
129 | # check if login is globally required to access the application |
|
129 | # check if login is globally required to access the application | |
130 | def check_if_login_required |
|
130 | def check_if_login_required | |
131 | # no check needed if user is already logged in |
|
131 | # no check needed if user is already logged in | |
132 | return true if User.current.logged? |
|
132 | return true if User.current.logged? | |
133 | require_login if Setting.login_required? |
|
133 | require_login if Setting.login_required? | |
134 | end |
|
134 | end | |
135 |
|
135 | |||
136 | def set_localization |
|
136 | def set_localization | |
137 | lang = nil |
|
137 | lang = nil | |
138 | if User.current.logged? |
|
138 | if User.current.logged? | |
139 | lang = find_language(User.current.language) |
|
139 | lang = find_language(User.current.language) | |
140 | end |
|
140 | end | |
141 | if lang.nil? && request.env['HTTP_ACCEPT_LANGUAGE'] |
|
141 | if lang.nil? && request.env['HTTP_ACCEPT_LANGUAGE'] | |
142 | accept_lang = parse_qvalues(request.env['HTTP_ACCEPT_LANGUAGE']).first |
|
142 | accept_lang = parse_qvalues(request.env['HTTP_ACCEPT_LANGUAGE']).first | |
143 | if !accept_lang.blank? |
|
143 | if !accept_lang.blank? | |
144 | accept_lang = accept_lang.downcase |
|
144 | accept_lang = accept_lang.downcase | |
145 | lang = find_language(accept_lang) || find_language(accept_lang.split('-').first) |
|
145 | lang = find_language(accept_lang) || find_language(accept_lang.split('-').first) | |
146 | end |
|
146 | end | |
147 | end |
|
147 | end | |
148 | lang ||= Setting.default_language |
|
148 | lang ||= Setting.default_language | |
149 | set_language_if_valid(lang) |
|
149 | set_language_if_valid(lang) | |
150 | end |
|
150 | end | |
151 |
|
151 | |||
152 | def require_login |
|
152 | def require_login | |
153 | if !User.current.logged? |
|
153 | if !User.current.logged? | |
154 | # Extract only the basic url parameters on non-GET requests |
|
154 | # Extract only the basic url parameters on non-GET requests | |
155 | if request.get? |
|
155 | if request.get? | |
156 | url = url_for(params) |
|
156 | url = url_for(params) | |
157 | else |
|
157 | else | |
158 | 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]) | |
159 | end |
|
159 | end | |
160 | respond_to do |format| |
|
160 | respond_to do |format| | |
161 | format.html { redirect_to :controller => "account", :action => "login", :back_url => url } |
|
161 | format.html { redirect_to :controller => "account", :action => "login", :back_url => url } | |
162 | format.atom { redirect_to :controller => "account", :action => "login", :back_url => url } |
|
162 | format.atom { redirect_to :controller => "account", :action => "login", :back_url => url } | |
163 | format.xml { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' } |
|
163 | format.xml { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' } | |
164 | format.js { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' } |
|
164 | format.js { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' } | |
165 | format.json { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' } |
|
165 | format.json { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' } | |
166 | end |
|
166 | end | |
167 | return false |
|
167 | return false | |
168 | end |
|
168 | end | |
169 | true |
|
169 | true | |
170 | end |
|
170 | end | |
171 |
|
171 | |||
172 | def require_admin |
|
172 | def require_admin | |
173 | return unless require_login |
|
173 | return unless require_login | |
174 | if !User.current.admin? |
|
174 | if !User.current.admin? | |
175 | render_403 |
|
175 | render_403 | |
176 | return false |
|
176 | return false | |
177 | end |
|
177 | end | |
178 | true |
|
178 | true | |
179 | end |
|
179 | end | |
180 |
|
180 | |||
181 | def deny_access |
|
181 | def deny_access | |
182 | User.current.logged? ? render_403 : require_login |
|
182 | User.current.logged? ? render_403 : require_login | |
183 | end |
|
183 | end | |
184 |
|
184 | |||
185 | # Authorize the user for the requested action |
|
185 | # Authorize the user for the requested action | |
186 | def authorize(ctrl = params[:controller], action = params[:action], global = false) |
|
186 | def authorize(ctrl = params[:controller], action = params[:action], global = false) | |
187 | 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) | |
188 | if allowed |
|
188 | if allowed | |
189 | true |
|
189 | true | |
190 | else |
|
190 | else | |
191 | if @project && @project.archived? |
|
191 | if @project && @project.archived? | |
192 | render_403 :message => :notice_not_authorized_archived_project |
|
192 | render_403 :message => :notice_not_authorized_archived_project | |
193 | else |
|
193 | else | |
194 | deny_access |
|
194 | deny_access | |
195 | end |
|
195 | end | |
196 | end |
|
196 | end | |
197 | end |
|
197 | end | |
198 |
|
198 | |||
199 | # Authorize the user for the requested action outside a project |
|
199 | # Authorize the user for the requested action outside a project | |
200 | def authorize_global(ctrl = params[:controller], action = params[:action], global = true) |
|
200 | def authorize_global(ctrl = params[:controller], action = params[:action], global = true) | |
201 | authorize(ctrl, action, global) |
|
201 | authorize(ctrl, action, global) | |
202 | end |
|
202 | end | |
203 |
|
203 | |||
204 | # Find project of id params[:id] |
|
204 | # Find project of id params[:id] | |
205 | def find_project |
|
205 | def find_project | |
206 | @project = Project.find(params[:id]) |
|
206 | @project = Project.find(params[:id]) | |
207 | rescue ActiveRecord::RecordNotFound |
|
207 | rescue ActiveRecord::RecordNotFound | |
208 | render_404 |
|
208 | render_404 | |
209 | end |
|
209 | end | |
210 |
|
210 | |||
211 | # Find project of id params[:project_id] |
|
211 | # Find project of id params[:project_id] | |
212 | def find_project_by_project_id |
|
212 | def find_project_by_project_id | |
213 | @project = Project.find(params[:project_id]) |
|
213 | @project = Project.find(params[:project_id]) | |
214 | rescue ActiveRecord::RecordNotFound |
|
214 | rescue ActiveRecord::RecordNotFound | |
215 | render_404 |
|
215 | render_404 | |
216 | end |
|
216 | end | |
217 |
|
217 | |||
218 | # Find a project based on params[:project_id] |
|
218 | # Find a project based on params[:project_id] | |
219 | # TODO: some subclasses override this, see about merging their logic |
|
219 | # TODO: some subclasses override this, see about merging their logic | |
220 | def find_optional_project |
|
220 | def find_optional_project | |
221 | @project = Project.find(params[:project_id]) unless params[:project_id].blank? |
|
221 | @project = Project.find(params[:project_id]) unless params[:project_id].blank? | |
222 | 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) | |
223 | allowed ? true : deny_access |
|
223 | allowed ? true : deny_access | |
224 | rescue ActiveRecord::RecordNotFound |
|
224 | rescue ActiveRecord::RecordNotFound | |
225 | render_404 |
|
225 | render_404 | |
226 | end |
|
226 | end | |
227 |
|
227 | |||
228 | # Finds and sets @project based on @object.project |
|
228 | # Finds and sets @project based on @object.project | |
229 | def find_project_from_association |
|
229 | def find_project_from_association | |
230 | render_404 unless @object.present? |
|
230 | render_404 unless @object.present? | |
231 |
|
231 | |||
232 | @project = @object.project |
|
232 | @project = @object.project | |
233 | end |
|
233 | end | |
234 |
|
234 | |||
235 | def find_model_object |
|
235 | def find_model_object | |
236 | model = self.class.read_inheritable_attribute('model_object') |
|
236 | model = self.class.read_inheritable_attribute('model_object') | |
237 | if model |
|
237 | if model | |
238 | @object = model.find(params[:id]) |
|
238 | @object = model.find(params[:id]) | |
239 | self.instance_variable_set('@' + controller_name.singularize, @object) if @object |
|
239 | self.instance_variable_set('@' + controller_name.singularize, @object) if @object | |
240 | end |
|
240 | end | |
241 | rescue ActiveRecord::RecordNotFound |
|
241 | rescue ActiveRecord::RecordNotFound | |
242 | render_404 |
|
242 | render_404 | |
243 | end |
|
243 | end | |
244 |
|
244 | |||
245 | def self.model_object(model) |
|
245 | def self.model_object(model) | |
246 | write_inheritable_attribute('model_object', model) |
|
246 | write_inheritable_attribute('model_object', model) | |
247 | end |
|
247 | end | |
248 |
|
248 | |||
249 | # Filter for bulk issue operations |
|
249 | # Filter for bulk issue operations | |
250 | def find_issues |
|
250 | def find_issues | |
251 | @issues = Issue.find_all_by_id(params[:id] || params[:ids]) |
|
251 | @issues = Issue.find_all_by_id(params[:id] || params[:ids]) | |
252 | raise ActiveRecord::RecordNotFound if @issues.empty? |
|
252 | raise ActiveRecord::RecordNotFound if @issues.empty? | |
253 | if @issues.detect {|issue| !issue.visible?} |
|
253 | if @issues.detect {|issue| !issue.visible?} | |
254 | deny_access |
|
254 | deny_access | |
255 | return |
|
255 | return | |
256 | end |
|
256 | end | |
257 | @projects = @issues.collect(&:project).compact.uniq |
|
257 | @projects = @issues.collect(&:project).compact.uniq | |
258 | @project = @projects.first if @projects.size == 1 |
|
258 | @project = @projects.first if @projects.size == 1 | |
259 | rescue ActiveRecord::RecordNotFound |
|
259 | rescue ActiveRecord::RecordNotFound | |
260 | render_404 |
|
260 | render_404 | |
261 | end |
|
261 | end | |
262 |
|
262 | |||
263 | # make sure that the user is a member of the project (or admin) if project is private |
|
263 | # make sure that the user is a member of the project (or admin) if project is private | |
264 | # used as a before_filter for actions that do not require any particular permission on the project |
|
264 | # used as a before_filter for actions that do not require any particular permission on the project | |
265 | def check_project_privacy |
|
265 | def check_project_privacy | |
266 | if @project && @project.active? |
|
266 | if @project && @project.active? | |
267 | if @project.visible? |
|
267 | if @project.visible? | |
268 | true |
|
268 | true | |
269 | else |
|
269 | else | |
270 | deny_access |
|
270 | deny_access | |
271 | end |
|
271 | end | |
272 | else |
|
272 | else | |
273 | @project = nil |
|
273 | @project = nil | |
274 | render_404 |
|
274 | render_404 | |
275 | false |
|
275 | false | |
276 | end |
|
276 | end | |
277 | end |
|
277 | end | |
278 |
|
278 | |||
279 | def back_url |
|
279 | def back_url | |
280 | params[:back_url] || request.env['HTTP_REFERER'] |
|
280 | params[:back_url] || request.env['HTTP_REFERER'] | |
281 | end |
|
281 | end | |
282 |
|
282 | |||
283 | def redirect_back_or_default(default) |
|
283 | def redirect_back_or_default(default) | |
284 | back_url = CGI.unescape(params[:back_url].to_s) |
|
284 | back_url = CGI.unescape(params[:back_url].to_s) | |
285 | if !back_url.blank? |
|
285 | if !back_url.blank? | |
286 | begin |
|
286 | begin | |
287 | uri = URI.parse(back_url) |
|
287 | uri = URI.parse(back_url) | |
288 | # do not redirect user to another host or to the login or register page |
|
288 | # do not redirect user to another host or to the login or register page | |
289 | if (uri.relative? || (uri.host == request.host)) && !uri.path.match(%r{/(login|account/register)}) |
|
289 | if (uri.relative? || (uri.host == request.host)) && !uri.path.match(%r{/(login|account/register)}) | |
290 | redirect_to(back_url) |
|
290 | redirect_to(back_url) | |
291 | return |
|
291 | return | |
292 | end |
|
292 | end | |
293 | rescue URI::InvalidURIError |
|
293 | rescue URI::InvalidURIError | |
294 | # redirect to default |
|
294 | # redirect to default | |
295 | end |
|
295 | end | |
296 | end |
|
296 | end | |
297 | redirect_to default |
|
297 | redirect_to default | |
298 | false |
|
298 | false | |
299 | end |
|
299 | end | |
300 |
|
300 | |||
|
301 | # Redirects to the request referer if present, redirects to args or call block otherwise. | |||
|
302 | def redirect_to_referer_or(*args, &block) | |||
|
303 | redirect_to :back | |||
|
304 | rescue ::ActionController::RedirectBackError | |||
|
305 | if args.any? | |||
|
306 | redirect_to *args | |||
|
307 | elsif block_given? | |||
|
308 | block.call | |||
|
309 | else | |||
|
310 | raise "#redirect_to_referer_or takes arguments or a block" | |||
|
311 | end | |||
|
312 | end | |||
|
313 | ||||
301 | def render_403(options={}) |
|
314 | def render_403(options={}) | |
302 | @project = nil |
|
315 | @project = nil | |
303 | render_error({:message => :notice_not_authorized, :status => 403}.merge(options)) |
|
316 | render_error({:message => :notice_not_authorized, :status => 403}.merge(options)) | |
304 | return false |
|
317 | return false | |
305 | end |
|
318 | end | |
306 |
|
319 | |||
307 | def render_404(options={}) |
|
320 | def render_404(options={}) | |
308 | render_error({:message => :notice_file_not_found, :status => 404}.merge(options)) |
|
321 | render_error({:message => :notice_file_not_found, :status => 404}.merge(options)) | |
309 | return false |
|
322 | return false | |
310 | end |
|
323 | end | |
311 |
|
324 | |||
312 | # Renders an error response |
|
325 | # Renders an error response | |
313 | def render_error(arg) |
|
326 | def render_error(arg) | |
314 | arg = {:message => arg} unless arg.is_a?(Hash) |
|
327 | arg = {:message => arg} unless arg.is_a?(Hash) | |
315 |
|
328 | |||
316 | @message = arg[:message] |
|
329 | @message = arg[:message] | |
317 | @message = l(@message) if @message.is_a?(Symbol) |
|
330 | @message = l(@message) if @message.is_a?(Symbol) | |
318 | @status = arg[:status] || 500 |
|
331 | @status = arg[:status] || 500 | |
319 |
|
332 | |||
320 | respond_to do |format| |
|
333 | respond_to do |format| | |
321 | format.html { |
|
334 | format.html { | |
322 | render :template => 'common/error', :layout => use_layout, :status => @status |
|
335 | render :template => 'common/error', :layout => use_layout, :status => @status | |
323 | } |
|
336 | } | |
324 | format.atom { head @status } |
|
337 | format.atom { head @status } | |
325 | format.xml { head @status } |
|
338 | format.xml { head @status } | |
326 | format.js { head @status } |
|
339 | format.js { head @status } | |
327 | format.json { head @status } |
|
340 | format.json { head @status } | |
328 | end |
|
341 | end | |
329 | end |
|
342 | end | |
330 |
|
343 | |||
331 | # Filter for actions that provide an API response |
|
344 | # Filter for actions that provide an API response | |
332 | # but have no HTML representation for non admin users |
|
345 | # but have no HTML representation for non admin users | |
333 | def require_admin_or_api_request |
|
346 | def require_admin_or_api_request | |
334 | return true if api_request? |
|
347 | return true if api_request? | |
335 | if User.current.admin? |
|
348 | if User.current.admin? | |
336 | true |
|
349 | true | |
337 | elsif User.current.logged? |
|
350 | elsif User.current.logged? | |
338 | render_error(:status => 406) |
|
351 | render_error(:status => 406) | |
339 | else |
|
352 | else | |
340 | deny_access |
|
353 | deny_access | |
341 | end |
|
354 | end | |
342 | end |
|
355 | end | |
343 |
|
356 | |||
344 | # Picks which layout to use based on the request |
|
357 | # Picks which layout to use based on the request | |
345 | # |
|
358 | # | |
346 | # @return [boolean, string] name of the layout to use or false for no layout |
|
359 | # @return [boolean, string] name of the layout to use or false for no layout | |
347 | def use_layout |
|
360 | def use_layout | |
348 | request.xhr? ? false : 'base' |
|
361 | request.xhr? ? false : 'base' | |
349 | end |
|
362 | end | |
350 |
|
363 | |||
351 | def invalid_authenticity_token |
|
364 | def invalid_authenticity_token | |
352 | if api_request? |
|
365 | if api_request? | |
353 | logger.error "Form authenticity token is missing or is invalid. API calls must include a proper Content-type header (text/xml or text/json)." |
|
366 | logger.error "Form authenticity token is missing or is invalid. API calls must include a proper Content-type header (text/xml or text/json)." | |
354 | end |
|
367 | end | |
355 | render_error "Invalid form authenticity token." |
|
368 | render_error "Invalid form authenticity token." | |
356 | end |
|
369 | end | |
357 |
|
370 | |||
358 | def render_feed(items, options={}) |
|
371 | def render_feed(items, options={}) | |
359 | @items = items || [] |
|
372 | @items = items || [] | |
360 | @items.sort! {|x,y| y.event_datetime <=> x.event_datetime } |
|
373 | @items.sort! {|x,y| y.event_datetime <=> x.event_datetime } | |
361 | @items = @items.slice(0, Setting.feeds_limit.to_i) |
|
374 | @items = @items.slice(0, Setting.feeds_limit.to_i) | |
362 | @title = options[:title] || Setting.app_title |
|
375 | @title = options[:title] || Setting.app_title | |
363 | render :template => "common/feed.atom", :layout => false, |
|
376 | render :template => "common/feed.atom", :layout => false, | |
364 | :content_type => 'application/atom+xml' |
|
377 | :content_type => 'application/atom+xml' | |
365 | end |
|
378 | end | |
366 |
|
379 | |||
367 | # TODO: remove in Redmine 1.4 |
|
380 | # TODO: remove in Redmine 1.4 | |
368 | def self.accept_key_auth(*actions) |
|
381 | def self.accept_key_auth(*actions) | |
369 | 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." |
|
382 | 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." | |
370 | accept_rss_auth(*actions) |
|
383 | accept_rss_auth(*actions) | |
371 | end |
|
384 | end | |
372 |
|
385 | |||
373 | # TODO: remove in Redmine 1.4 |
|
386 | # TODO: remove in Redmine 1.4 | |
374 | def accept_key_auth_actions |
|
387 | def accept_key_auth_actions | |
375 | 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." |
|
388 | 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." | |
376 | self.class.accept_rss_auth |
|
389 | self.class.accept_rss_auth | |
377 | end |
|
390 | end | |
378 |
|
391 | |||
379 | def self.accept_rss_auth(*actions) |
|
392 | def self.accept_rss_auth(*actions) | |
380 | if actions.any? |
|
393 | if actions.any? | |
381 | write_inheritable_attribute('accept_rss_auth_actions', actions) |
|
394 | write_inheritable_attribute('accept_rss_auth_actions', actions) | |
382 | else |
|
395 | else | |
383 | read_inheritable_attribute('accept_rss_auth_actions') || [] |
|
396 | read_inheritable_attribute('accept_rss_auth_actions') || [] | |
384 | end |
|
397 | end | |
385 | end |
|
398 | end | |
386 |
|
399 | |||
387 | def accept_rss_auth?(action=action_name) |
|
400 | def accept_rss_auth?(action=action_name) | |
388 | self.class.accept_rss_auth.include?(action.to_sym) |
|
401 | self.class.accept_rss_auth.include?(action.to_sym) | |
389 | end |
|
402 | end | |
390 |
|
403 | |||
391 | def self.accept_api_auth(*actions) |
|
404 | def self.accept_api_auth(*actions) | |
392 | if actions.any? |
|
405 | if actions.any? | |
393 | write_inheritable_attribute('accept_api_auth_actions', actions) |
|
406 | write_inheritable_attribute('accept_api_auth_actions', actions) | |
394 | else |
|
407 | else | |
395 | read_inheritable_attribute('accept_api_auth_actions') || [] |
|
408 | read_inheritable_attribute('accept_api_auth_actions') || [] | |
396 | end |
|
409 | end | |
397 | end |
|
410 | end | |
398 |
|
411 | |||
399 | def accept_api_auth?(action=action_name) |
|
412 | def accept_api_auth?(action=action_name) | |
400 | self.class.accept_api_auth.include?(action.to_sym) |
|
413 | self.class.accept_api_auth.include?(action.to_sym) | |
401 | end |
|
414 | end | |
402 |
|
415 | |||
403 | # Returns the number of objects that should be displayed |
|
416 | # Returns the number of objects that should be displayed | |
404 | # on the paginated list |
|
417 | # on the paginated list | |
405 | def per_page_option |
|
418 | def per_page_option | |
406 | per_page = nil |
|
419 | per_page = nil | |
407 | if params[:per_page] && Setting.per_page_options_array.include?(params[:per_page].to_s.to_i) |
|
420 | if params[:per_page] && Setting.per_page_options_array.include?(params[:per_page].to_s.to_i) | |
408 | per_page = params[:per_page].to_s.to_i |
|
421 | per_page = params[:per_page].to_s.to_i | |
409 | session[:per_page] = per_page |
|
422 | session[:per_page] = per_page | |
410 | elsif session[:per_page] |
|
423 | elsif session[:per_page] | |
411 | per_page = session[:per_page] |
|
424 | per_page = session[:per_page] | |
412 | else |
|
425 | else | |
413 | per_page = Setting.per_page_options_array.first || 25 |
|
426 | per_page = Setting.per_page_options_array.first || 25 | |
414 | end |
|
427 | end | |
415 | per_page |
|
428 | per_page | |
416 | end |
|
429 | end | |
417 |
|
430 | |||
418 | # Returns offset and limit used to retrieve objects |
|
431 | # Returns offset and limit used to retrieve objects | |
419 | # for an API response based on offset, limit and page parameters |
|
432 | # for an API response based on offset, limit and page parameters | |
420 | def api_offset_and_limit(options=params) |
|
433 | def api_offset_and_limit(options=params) | |
421 | if options[:offset].present? |
|
434 | if options[:offset].present? | |
422 | offset = options[:offset].to_i |
|
435 | offset = options[:offset].to_i | |
423 | if offset < 0 |
|
436 | if offset < 0 | |
424 | offset = 0 |
|
437 | offset = 0 | |
425 | end |
|
438 | end | |
426 | end |
|
439 | end | |
427 | limit = options[:limit].to_i |
|
440 | limit = options[:limit].to_i | |
428 | if limit < 1 |
|
441 | if limit < 1 | |
429 | limit = 25 |
|
442 | limit = 25 | |
430 | elsif limit > 100 |
|
443 | elsif limit > 100 | |
431 | limit = 100 |
|
444 | limit = 100 | |
432 | end |
|
445 | end | |
433 | if offset.nil? && options[:page].present? |
|
446 | if offset.nil? && options[:page].present? | |
434 | offset = (options[:page].to_i - 1) * limit |
|
447 | offset = (options[:page].to_i - 1) * limit | |
435 | offset = 0 if offset < 0 |
|
448 | offset = 0 if offset < 0 | |
436 | end |
|
449 | end | |
437 | offset ||= 0 |
|
450 | offset ||= 0 | |
438 |
|
451 | |||
439 | [offset, limit] |
|
452 | [offset, limit] | |
440 | end |
|
453 | end | |
441 |
|
454 | |||
442 | # qvalues http header parser |
|
455 | # qvalues http header parser | |
443 | # code taken from webrick |
|
456 | # code taken from webrick | |
444 | def parse_qvalues(value) |
|
457 | def parse_qvalues(value) | |
445 | tmp = [] |
|
458 | tmp = [] | |
446 | if value |
|
459 | if value | |
447 | parts = value.split(/,\s*/) |
|
460 | parts = value.split(/,\s*/) | |
448 | parts.each {|part| |
|
461 | parts.each {|part| | |
449 | if m = %r{^([^\s,]+?)(?:;\s*q=(\d+(?:\.\d+)?))?$}.match(part) |
|
462 | if m = %r{^([^\s,]+?)(?:;\s*q=(\d+(?:\.\d+)?))?$}.match(part) | |
450 | val = m[1] |
|
463 | val = m[1] | |
451 | q = (m[2] or 1).to_f |
|
464 | q = (m[2] or 1).to_f | |
452 | tmp.push([val, q]) |
|
465 | tmp.push([val, q]) | |
453 | end |
|
466 | end | |
454 | } |
|
467 | } | |
455 | tmp = tmp.sort_by{|val, q| -q} |
|
468 | tmp = tmp.sort_by{|val, q| -q} | |
456 | tmp.collect!{|val, q| val} |
|
469 | tmp.collect!{|val, q| val} | |
457 | end |
|
470 | end | |
458 | return tmp |
|
471 | return tmp | |
459 | rescue |
|
472 | rescue | |
460 | nil |
|
473 | nil | |
461 | end |
|
474 | end | |
462 |
|
475 | |||
463 | # Returns a string that can be used as filename value in Content-Disposition header |
|
476 | # Returns a string that can be used as filename value in Content-Disposition header | |
464 | def filename_for_content_disposition(name) |
|
477 | def filename_for_content_disposition(name) | |
465 | request.env['HTTP_USER_AGENT'] =~ %r{MSIE} ? ERB::Util.url_encode(name) : name |
|
478 | request.env['HTTP_USER_AGENT'] =~ %r{MSIE} ? ERB::Util.url_encode(name) : name | |
466 | end |
|
479 | end | |
467 |
|
480 | |||
468 | def api_request? |
|
481 | def api_request? | |
469 | %w(xml json).include? params[:format] |
|
482 | %w(xml json).include? params[:format] | |
470 | end |
|
483 | end | |
471 |
|
484 | |||
472 | # Returns the API key present in the request |
|
485 | # Returns the API key present in the request | |
473 | def api_key_from_request |
|
486 | def api_key_from_request | |
474 | if params[:key].present? |
|
487 | if params[:key].present? | |
475 | params[:key] |
|
488 | params[:key] | |
476 | elsif request.headers["X-Redmine-API-Key"].present? |
|
489 | elsif request.headers["X-Redmine-API-Key"].present? | |
477 | request.headers["X-Redmine-API-Key"] |
|
490 | request.headers["X-Redmine-API-Key"] | |
478 | end |
|
491 | end | |
479 | end |
|
492 | end | |
480 |
|
493 | |||
481 | # Renders a warning flash if obj has unsaved attachments |
|
494 | # Renders a warning flash if obj has unsaved attachments | |
482 | def render_attachment_warning_if_needed(obj) |
|
495 | def render_attachment_warning_if_needed(obj) | |
483 | flash[:warning] = l(:warning_attachments_not_saved, obj.unsaved_attachments.size) if obj.unsaved_attachments.present? |
|
496 | flash[:warning] = l(:warning_attachments_not_saved, obj.unsaved_attachments.size) if obj.unsaved_attachments.present? | |
484 | end |
|
497 | end | |
485 |
|
498 | |||
486 | # Sets the `flash` notice or error based the number of issues that did not save |
|
499 | # Sets the `flash` notice or error based the number of issues that did not save | |
487 | # |
|
500 | # | |
488 | # @param [Array, Issue] issues all of the saved and unsaved Issues |
|
501 | # @param [Array, Issue] issues all of the saved and unsaved Issues | |
489 | # @param [Array, Integer] unsaved_issue_ids the issue ids that were not saved |
|
502 | # @param [Array, Integer] unsaved_issue_ids the issue ids that were not saved | |
490 | def set_flash_from_bulk_issue_save(issues, unsaved_issue_ids) |
|
503 | def set_flash_from_bulk_issue_save(issues, unsaved_issue_ids) | |
491 | if unsaved_issue_ids.empty? |
|
504 | if unsaved_issue_ids.empty? | |
492 | flash[:notice] = l(:notice_successful_update) unless issues.empty? |
|
505 | flash[:notice] = l(:notice_successful_update) unless issues.empty? | |
493 | else |
|
506 | else | |
494 | flash[:error] = l(:notice_failed_to_save_issues, |
|
507 | flash[:error] = l(:notice_failed_to_save_issues, | |
495 | :count => unsaved_issue_ids.size, |
|
508 | :count => unsaved_issue_ids.size, | |
496 | :total => issues.size, |
|
509 | :total => issues.size, | |
497 | :ids => '#' + unsaved_issue_ids.join(', #')) |
|
510 | :ids => '#' + unsaved_issue_ids.join(', #')) | |
498 | end |
|
511 | end | |
499 | end |
|
512 | end | |
500 |
|
513 | |||
501 | # Rescues an invalid query statement. Just in case... |
|
514 | # Rescues an invalid query statement. Just in case... | |
502 | def query_statement_invalid(exception) |
|
515 | def query_statement_invalid(exception) | |
503 | logger.error "Query::StatementInvalid: #{exception.message}" if logger |
|
516 | logger.error "Query::StatementInvalid: #{exception.message}" if logger | |
504 | session.delete(:query) |
|
517 | session.delete(:query) | |
505 | sort_clear if respond_to?(:sort_clear) |
|
518 | sort_clear if respond_to?(:sort_clear) | |
506 | render_error "An error occurred while executing the query and has been logged. Please report this error to your Redmine administrator." |
|
519 | render_error "An error occurred while executing the query and has been logged. Please report this error to your Redmine administrator." | |
507 | end |
|
520 | end | |
508 |
|
521 | |||
509 | # Renders API response on validation failure |
|
522 | # Renders API response on validation failure | |
510 | def render_validation_errors(objects) |
|
523 | def render_validation_errors(objects) | |
511 | if objects.is_a?(Array) |
|
524 | if objects.is_a?(Array) | |
512 | @error_messages = objects.map {|object| object.errors.full_messages}.flatten |
|
525 | @error_messages = objects.map {|object| object.errors.full_messages}.flatten | |
513 | else |
|
526 | else | |
514 | @error_messages = objects.errors.full_messages |
|
527 | @error_messages = objects.errors.full_messages | |
515 | end |
|
528 | end | |
516 | render :template => 'common/error_messages.api', :status => :unprocessable_entity, :layout => false |
|
529 | render :template => 'common/error_messages.api', :status => :unprocessable_entity, :layout => false | |
517 | end |
|
530 | end | |
518 |
|
531 | |||
519 | # Overrides #default_template so that the api template |
|
532 | # Overrides #default_template so that the api template | |
520 | # is used automatically if it exists |
|
533 | # is used automatically if it exists | |
521 | def default_template(action_name = self.action_name) |
|
534 | def default_template(action_name = self.action_name) | |
522 | if api_request? |
|
535 | if api_request? | |
523 | begin |
|
536 | begin | |
524 | return self.view_paths.find_template(default_template_name(action_name), 'api') |
|
537 | return self.view_paths.find_template(default_template_name(action_name), 'api') | |
525 | rescue ::ActionView::MissingTemplate |
|
538 | rescue ::ActionView::MissingTemplate | |
526 | # the api template was not found |
|
539 | # the api template was not found | |
527 | # fallback to the default behaviour |
|
540 | # fallback to the default behaviour | |
528 | end |
|
541 | end | |
529 | end |
|
542 | end | |
530 | super |
|
543 | super | |
531 | end |
|
544 | end | |
532 |
|
545 | |||
533 | # Overrides #pick_layout so that #render with no arguments |
|
546 | # Overrides #pick_layout so that #render with no arguments | |
534 | # doesn't use the layout for api requests |
|
547 | # doesn't use the layout for api requests | |
535 | def pick_layout(*args) |
|
548 | def pick_layout(*args) | |
536 | api_request? ? nil : super |
|
549 | api_request? ? nil : super | |
537 | end |
|
550 | end | |
538 | end |
|
551 | end |
@@ -1,126 +1,124 | |||||
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 | class AttachmentsController < ApplicationController |
|
18 | class AttachmentsController < ApplicationController | |
19 | before_filter :find_project, :except => :upload |
|
19 | before_filter :find_project, :except => :upload | |
20 | before_filter :file_readable, :read_authorize, :only => [:show, :download] |
|
20 | before_filter :file_readable, :read_authorize, :only => [:show, :download] | |
21 | before_filter :delete_authorize, :only => :destroy |
|
21 | before_filter :delete_authorize, :only => :destroy | |
22 | before_filter :authorize_global, :only => :upload |
|
22 | before_filter :authorize_global, :only => :upload | |
23 |
|
23 | |||
24 | accept_api_auth :show, :download, :upload |
|
24 | accept_api_auth :show, :download, :upload | |
25 |
|
25 | |||
26 | def show |
|
26 | def show | |
27 | respond_to do |format| |
|
27 | respond_to do |format| | |
28 | format.html { |
|
28 | format.html { | |
29 | if @attachment.is_diff? |
|
29 | if @attachment.is_diff? | |
30 | @diff = File.new(@attachment.diskfile, "rb").read |
|
30 | @diff = File.new(@attachment.diskfile, "rb").read | |
31 | @diff_type = params[:type] || User.current.pref[:diff_type] || 'inline' |
|
31 | @diff_type = params[:type] || User.current.pref[:diff_type] || 'inline' | |
32 | @diff_type = 'inline' unless %w(inline sbs).include?(@diff_type) |
|
32 | @diff_type = 'inline' unless %w(inline sbs).include?(@diff_type) | |
33 | # Save diff type as user preference |
|
33 | # Save diff type as user preference | |
34 | if User.current.logged? && @diff_type != User.current.pref[:diff_type] |
|
34 | if User.current.logged? && @diff_type != User.current.pref[:diff_type] | |
35 | User.current.pref[:diff_type] = @diff_type |
|
35 | User.current.pref[:diff_type] = @diff_type | |
36 | User.current.preference.save |
|
36 | User.current.preference.save | |
37 | end |
|
37 | end | |
38 | render :action => 'diff' |
|
38 | render :action => 'diff' | |
39 | elsif @attachment.is_text? && @attachment.filesize <= Setting.file_max_size_displayed.to_i.kilobyte |
|
39 | elsif @attachment.is_text? && @attachment.filesize <= Setting.file_max_size_displayed.to_i.kilobyte | |
40 | @content = File.new(@attachment.diskfile, "rb").read |
|
40 | @content = File.new(@attachment.diskfile, "rb").read | |
41 | render :action => 'file' |
|
41 | render :action => 'file' | |
42 | else |
|
42 | else | |
43 | download |
|
43 | download | |
44 | end |
|
44 | end | |
45 | } |
|
45 | } | |
46 | format.api |
|
46 | format.api | |
47 | end |
|
47 | end | |
48 | end |
|
48 | end | |
49 |
|
49 | |||
50 | def download |
|
50 | def download | |
51 | if @attachment.container.is_a?(Version) || @attachment.container.is_a?(Project) |
|
51 | if @attachment.container.is_a?(Version) || @attachment.container.is_a?(Project) | |
52 | @attachment.increment_download |
|
52 | @attachment.increment_download | |
53 | end |
|
53 | end | |
54 |
|
54 | |||
55 | # images are sent inline |
|
55 | # images are sent inline | |
56 | send_file @attachment.diskfile, :filename => filename_for_content_disposition(@attachment.filename), |
|
56 | send_file @attachment.diskfile, :filename => filename_for_content_disposition(@attachment.filename), | |
57 | :type => detect_content_type(@attachment), |
|
57 | :type => detect_content_type(@attachment), | |
58 | :disposition => (@attachment.image? ? 'inline' : 'attachment') |
|
58 | :disposition => (@attachment.image? ? 'inline' : 'attachment') | |
59 |
|
59 | |||
60 | end |
|
60 | end | |
61 |
|
61 | |||
62 | def upload |
|
62 | def upload | |
63 | # Make sure that API users get used to set this content type |
|
63 | # Make sure that API users get used to set this content type | |
64 | # as it won't trigger Rails' automatic parsing of the request body for parameters |
|
64 | # as it won't trigger Rails' automatic parsing of the request body for parameters | |
65 | unless request.content_type == 'application/octet-stream' |
|
65 | unless request.content_type == 'application/octet-stream' | |
66 | render :nothing => true, :status => 406 |
|
66 | render :nothing => true, :status => 406 | |
67 | return |
|
67 | return | |
68 | end |
|
68 | end | |
69 |
|
69 | |||
70 | @attachment = Attachment.new(:file => request.body) |
|
70 | @attachment = Attachment.new(:file => request.body) | |
71 | @attachment.author = User.current |
|
71 | @attachment.author = User.current | |
72 | @attachment.filename = Redmine::Utils.random_hex(16) |
|
72 | @attachment.filename = Redmine::Utils.random_hex(16) | |
73 |
|
73 | |||
74 | if @attachment.save |
|
74 | if @attachment.save | |
75 | respond_to do |format| |
|
75 | respond_to do |format| | |
76 | format.api { render :action => 'upload', :status => :created } |
|
76 | format.api { render :action => 'upload', :status => :created } | |
77 | end |
|
77 | end | |
78 | else |
|
78 | else | |
79 | respond_to do |format| |
|
79 | respond_to do |format| | |
80 | format.api { render_validation_errors(@attachment) } |
|
80 | format.api { render_validation_errors(@attachment) } | |
81 | end |
|
81 | end | |
82 | end |
|
82 | end | |
83 | end |
|
83 | end | |
84 |
|
84 | |||
85 | def destroy |
|
85 | def destroy | |
86 | if @attachment.container.respond_to?(:init_journal) |
|
86 | if @attachment.container.respond_to?(:init_journal) | |
87 | @attachment.container.init_journal(User.current) |
|
87 | @attachment.container.init_journal(User.current) | |
88 | end |
|
88 | end | |
89 | # Make sure association callbacks are called |
|
89 | # Make sure association callbacks are called | |
90 | @attachment.container.attachments.delete(@attachment) |
|
90 | @attachment.container.attachments.delete(@attachment) | |
91 | redirect_to :back |
|
91 | redirect_back_or_default project_path(@project) | |
92 | rescue ::ActionController::RedirectBackError |
|
|||
93 | redirect_to :controller => 'projects', :action => 'show', :id => @project |
|
|||
94 | end |
|
92 | end | |
95 |
|
93 | |||
96 | private |
|
94 | private | |
97 | def find_project |
|
95 | def find_project | |
98 | @attachment = Attachment.find(params[:id]) |
|
96 | @attachment = Attachment.find(params[:id]) | |
99 | # Show 404 if the filename in the url is wrong |
|
97 | # Show 404 if the filename in the url is wrong | |
100 | raise ActiveRecord::RecordNotFound if params[:filename] && params[:filename] != @attachment.filename |
|
98 | raise ActiveRecord::RecordNotFound if params[:filename] && params[:filename] != @attachment.filename | |
101 | @project = @attachment.project |
|
99 | @project = @attachment.project | |
102 | rescue ActiveRecord::RecordNotFound |
|
100 | rescue ActiveRecord::RecordNotFound | |
103 | render_404 |
|
101 | render_404 | |
104 | end |
|
102 | end | |
105 |
|
103 | |||
106 | # Checks that the file exists and is readable |
|
104 | # Checks that the file exists and is readable | |
107 | def file_readable |
|
105 | def file_readable | |
108 | @attachment.readable? ? true : render_404 |
|
106 | @attachment.readable? ? true : render_404 | |
109 | end |
|
107 | end | |
110 |
|
108 | |||
111 | def read_authorize |
|
109 | def read_authorize | |
112 | @attachment.visible? ? true : deny_access |
|
110 | @attachment.visible? ? true : deny_access | |
113 | end |
|
111 | end | |
114 |
|
112 | |||
115 | def delete_authorize |
|
113 | def delete_authorize | |
116 | @attachment.deletable? ? true : deny_access |
|
114 | @attachment.deletable? ? true : deny_access | |
117 | end |
|
115 | end | |
118 |
|
116 | |||
119 | def detect_content_type(attachment) |
|
117 | def detect_content_type(attachment) | |
120 | content_type = attachment.content_type |
|
118 | content_type = attachment.content_type | |
121 | if content_type.blank? |
|
119 | if content_type.blank? | |
122 | content_type = Redmine::MimeType.of(attachment.filename) |
|
120 | content_type = Redmine::MimeType.of(attachment.filename) | |
123 | end |
|
121 | end | |
124 | content_type.to_s |
|
122 | content_type.to_s | |
125 | end |
|
123 | end | |
126 | end |
|
124 | end |
@@ -1,229 +1,227 | |||||
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 | class UsersController < ApplicationController |
|
18 | class UsersController < ApplicationController | |
19 | layout 'admin' |
|
19 | layout 'admin' | |
20 |
|
20 | |||
21 | before_filter :require_admin, :except => :show |
|
21 | before_filter :require_admin, :except => :show | |
22 | before_filter :find_user, :only => [:show, :edit, :update, :destroy, :edit_membership, :destroy_membership] |
|
22 | before_filter :find_user, :only => [:show, :edit, :update, :destroy, :edit_membership, :destroy_membership] | |
23 | accept_api_auth :index, :show, :create, :update, :destroy |
|
23 | accept_api_auth :index, :show, :create, :update, :destroy | |
24 |
|
24 | |||
25 | helper :sort |
|
25 | helper :sort | |
26 | include SortHelper |
|
26 | include SortHelper | |
27 | helper :custom_fields |
|
27 | helper :custom_fields | |
28 | include CustomFieldsHelper |
|
28 | include CustomFieldsHelper | |
29 |
|
29 | |||
30 | def index |
|
30 | def index | |
31 | sort_init 'login', 'asc' |
|
31 | sort_init 'login', 'asc' | |
32 | sort_update %w(login firstname lastname mail admin created_on last_login_on) |
|
32 | sort_update %w(login firstname lastname mail admin created_on last_login_on) | |
33 |
|
33 | |||
34 | case params[:format] |
|
34 | case params[:format] | |
35 | when 'xml', 'json' |
|
35 | when 'xml', 'json' | |
36 | @offset, @limit = api_offset_and_limit |
|
36 | @offset, @limit = api_offset_and_limit | |
37 | else |
|
37 | else | |
38 | @limit = per_page_option |
|
38 | @limit = per_page_option | |
39 | end |
|
39 | end | |
40 |
|
40 | |||
41 | @status = params[:status] || 1 |
|
41 | @status = params[:status] || 1 | |
42 |
|
42 | |||
43 | scope = User.logged.status(@status) |
|
43 | scope = User.logged.status(@status) | |
44 | scope = scope.like(params[:name]) if params[:name].present? |
|
44 | scope = scope.like(params[:name]) if params[:name].present? | |
45 | scope = scope.in_group(params[:group_id]) if params[:group_id].present? |
|
45 | scope = scope.in_group(params[:group_id]) if params[:group_id].present? | |
46 |
|
46 | |||
47 | @user_count = scope.count |
|
47 | @user_count = scope.count | |
48 | @user_pages = Paginator.new self, @user_count, @limit, params['page'] |
|
48 | @user_pages = Paginator.new self, @user_count, @limit, params['page'] | |
49 | @offset ||= @user_pages.current.offset |
|
49 | @offset ||= @user_pages.current.offset | |
50 | @users = scope.find :all, |
|
50 | @users = scope.find :all, | |
51 | :order => sort_clause, |
|
51 | :order => sort_clause, | |
52 | :limit => @limit, |
|
52 | :limit => @limit, | |
53 | :offset => @offset |
|
53 | :offset => @offset | |
54 |
|
54 | |||
55 | respond_to do |format| |
|
55 | respond_to do |format| | |
56 | format.html { |
|
56 | format.html { | |
57 | @groups = Group.all.sort |
|
57 | @groups = Group.all.sort | |
58 | render :layout => !request.xhr? |
|
58 | render :layout => !request.xhr? | |
59 | } |
|
59 | } | |
60 | format.api |
|
60 | format.api | |
61 | end |
|
61 | end | |
62 | end |
|
62 | end | |
63 |
|
63 | |||
64 | def show |
|
64 | def show | |
65 | # show projects based on current user visibility |
|
65 | # show projects based on current user visibility | |
66 | @memberships = @user.memberships.all(:conditions => Project.visible_condition(User.current)) |
|
66 | @memberships = @user.memberships.all(:conditions => Project.visible_condition(User.current)) | |
67 |
|
67 | |||
68 | events = Redmine::Activity::Fetcher.new(User.current, :author => @user).events(nil, nil, :limit => 10) |
|
68 | events = Redmine::Activity::Fetcher.new(User.current, :author => @user).events(nil, nil, :limit => 10) | |
69 | @events_by_day = events.group_by(&:event_date) |
|
69 | @events_by_day = events.group_by(&:event_date) | |
70 |
|
70 | |||
71 | unless User.current.admin? |
|
71 | unless User.current.admin? | |
72 | if !@user.active? || (@user != User.current && @memberships.empty? && events.empty?) |
|
72 | if !@user.active? || (@user != User.current && @memberships.empty? && events.empty?) | |
73 | render_404 |
|
73 | render_404 | |
74 | return |
|
74 | return | |
75 | end |
|
75 | end | |
76 | end |
|
76 | end | |
77 |
|
77 | |||
78 | respond_to do |format| |
|
78 | respond_to do |format| | |
79 | format.html { render :layout => 'base' } |
|
79 | format.html { render :layout => 'base' } | |
80 | format.api |
|
80 | format.api | |
81 | end |
|
81 | end | |
82 | end |
|
82 | end | |
83 |
|
83 | |||
84 | def new |
|
84 | def new | |
85 | @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option) |
|
85 | @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option) | |
86 | @auth_sources = AuthSource.find(:all) |
|
86 | @auth_sources = AuthSource.find(:all) | |
87 | end |
|
87 | end | |
88 |
|
88 | |||
89 | def create |
|
89 | def create | |
90 | @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option) |
|
90 | @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option) | |
91 | @user.safe_attributes = params[:user] |
|
91 | @user.safe_attributes = params[:user] | |
92 | @user.admin = params[:user][:admin] || false |
|
92 | @user.admin = params[:user][:admin] || false | |
93 | @user.login = params[:user][:login] |
|
93 | @user.login = params[:user][:login] | |
94 | @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation] unless @user.auth_source_id |
|
94 | @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation] unless @user.auth_source_id | |
95 |
|
95 | |||
96 | if @user.save |
|
96 | if @user.save | |
97 | @user.pref.attributes = params[:pref] |
|
97 | @user.pref.attributes = params[:pref] | |
98 | @user.pref[:no_self_notified] = (params[:no_self_notified] == '1') |
|
98 | @user.pref[:no_self_notified] = (params[:no_self_notified] == '1') | |
99 | @user.pref.save |
|
99 | @user.pref.save | |
100 | @user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : []) |
|
100 | @user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : []) | |
101 |
|
101 | |||
102 | Mailer.deliver_account_information(@user, params[:user][:password]) if params[:send_information] |
|
102 | Mailer.deliver_account_information(@user, params[:user][:password]) if params[:send_information] | |
103 |
|
103 | |||
104 | respond_to do |format| |
|
104 | respond_to do |format| | |
105 | format.html { |
|
105 | format.html { | |
106 | flash[:notice] = l(:notice_successful_create) |
|
106 | flash[:notice] = l(:notice_successful_create) | |
107 | redirect_to(params[:continue] ? |
|
107 | redirect_to(params[:continue] ? | |
108 | {:controller => 'users', :action => 'new'} : |
|
108 | {:controller => 'users', :action => 'new'} : | |
109 | {:controller => 'users', :action => 'edit', :id => @user} |
|
109 | {:controller => 'users', :action => 'edit', :id => @user} | |
110 | ) |
|
110 | ) | |
111 | } |
|
111 | } | |
112 | format.api { render :action => 'show', :status => :created, :location => user_url(@user) } |
|
112 | format.api { render :action => 'show', :status => :created, :location => user_url(@user) } | |
113 | end |
|
113 | end | |
114 | else |
|
114 | else | |
115 | @auth_sources = AuthSource.find(:all) |
|
115 | @auth_sources = AuthSource.find(:all) | |
116 | # Clear password input |
|
116 | # Clear password input | |
117 | @user.password = @user.password_confirmation = nil |
|
117 | @user.password = @user.password_confirmation = nil | |
118 |
|
118 | |||
119 | respond_to do |format| |
|
119 | respond_to do |format| | |
120 | format.html { render :action => 'new' } |
|
120 | format.html { render :action => 'new' } | |
121 | format.api { render_validation_errors(@user) } |
|
121 | format.api { render_validation_errors(@user) } | |
122 | end |
|
122 | end | |
123 | end |
|
123 | end | |
124 | end |
|
124 | end | |
125 |
|
125 | |||
126 | def edit |
|
126 | def edit | |
127 | @auth_sources = AuthSource.find(:all) |
|
127 | @auth_sources = AuthSource.find(:all) | |
128 | @membership ||= Member.new |
|
128 | @membership ||= Member.new | |
129 | end |
|
129 | end | |
130 |
|
130 | |||
131 | def update |
|
131 | def update | |
132 | @user.admin = params[:user][:admin] if params[:user][:admin] |
|
132 | @user.admin = params[:user][:admin] if params[:user][:admin] | |
133 | @user.login = params[:user][:login] if params[:user][:login] |
|
133 | @user.login = params[:user][:login] if params[:user][:login] | |
134 | if params[:user][:password].present? && (@user.auth_source_id.nil? || params[:user][:auth_source_id].blank?) |
|
134 | if params[:user][:password].present? && (@user.auth_source_id.nil? || params[:user][:auth_source_id].blank?) | |
135 | @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation] |
|
135 | @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation] | |
136 | end |
|
136 | end | |
137 | @user.safe_attributes = params[:user] |
|
137 | @user.safe_attributes = params[:user] | |
138 | # Was the account actived ? (do it before User#save clears the change) |
|
138 | # Was the account actived ? (do it before User#save clears the change) | |
139 | was_activated = (@user.status_change == [User::STATUS_REGISTERED, User::STATUS_ACTIVE]) |
|
139 | was_activated = (@user.status_change == [User::STATUS_REGISTERED, User::STATUS_ACTIVE]) | |
140 | # TODO: Similar to My#account |
|
140 | # TODO: Similar to My#account | |
141 | @user.pref.attributes = params[:pref] |
|
141 | @user.pref.attributes = params[:pref] | |
142 | @user.pref[:no_self_notified] = (params[:no_self_notified] == '1') |
|
142 | @user.pref[:no_self_notified] = (params[:no_self_notified] == '1') | |
143 |
|
143 | |||
144 | if @user.save |
|
144 | if @user.save | |
145 | @user.pref.save |
|
145 | @user.pref.save | |
146 | @user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : []) |
|
146 | @user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : []) | |
147 |
|
147 | |||
148 | if was_activated |
|
148 | if was_activated | |
149 | Mailer.deliver_account_activated(@user) |
|
149 | Mailer.deliver_account_activated(@user) | |
150 | elsif @user.active? && params[:send_information] && !params[:user][:password].blank? && @user.auth_source_id.nil? |
|
150 | elsif @user.active? && params[:send_information] && !params[:user][:password].blank? && @user.auth_source_id.nil? | |
151 | Mailer.deliver_account_information(@user, params[:user][:password]) |
|
151 | Mailer.deliver_account_information(@user, params[:user][:password]) | |
152 | end |
|
152 | end | |
153 |
|
153 | |||
154 | respond_to do |format| |
|
154 | respond_to do |format| | |
155 | format.html { |
|
155 | format.html { | |
156 | flash[:notice] = l(:notice_successful_update) |
|
156 | flash[:notice] = l(:notice_successful_update) | |
157 | redirect_to :back |
|
157 | redirect_to_referer_or edit_user_path(@user) | |
158 | } |
|
158 | } | |
159 | format.api { head :ok } |
|
159 | format.api { head :ok } | |
160 | end |
|
160 | end | |
161 | else |
|
161 | else | |
162 | @auth_sources = AuthSource.find(:all) |
|
162 | @auth_sources = AuthSource.find(:all) | |
163 | @membership ||= Member.new |
|
163 | @membership ||= Member.new | |
164 | # Clear password input |
|
164 | # Clear password input | |
165 | @user.password = @user.password_confirmation = nil |
|
165 | @user.password = @user.password_confirmation = nil | |
166 |
|
166 | |||
167 | respond_to do |format| |
|
167 | respond_to do |format| | |
168 | format.html { render :action => :edit } |
|
168 | format.html { render :action => :edit } | |
169 | format.api { render_validation_errors(@user) } |
|
169 | format.api { render_validation_errors(@user) } | |
170 | end |
|
170 | end | |
171 | end |
|
171 | end | |
172 | rescue ::ActionController::RedirectBackError |
|
|||
173 | redirect_to :controller => 'users', :action => 'edit', :id => @user |
|
|||
174 | end |
|
172 | end | |
175 |
|
173 | |||
176 | def destroy |
|
174 | def destroy | |
177 | @user.destroy |
|
175 | @user.destroy | |
178 | respond_to do |format| |
|
176 | respond_to do |format| | |
179 | format.html { redirect_to(users_url) } |
|
177 | format.html { redirect_to(users_url) } | |
180 | format.api { head :ok } |
|
178 | format.api { head :ok } | |
181 | end |
|
179 | end | |
182 | end |
|
180 | end | |
183 |
|
181 | |||
184 | def edit_membership |
|
182 | def edit_membership | |
185 | @membership = Member.edit_membership(params[:membership_id], params[:membership], @user) |
|
183 | @membership = Member.edit_membership(params[:membership_id], params[:membership], @user) | |
186 | @membership.save |
|
184 | @membership.save | |
187 | respond_to do |format| |
|
185 | respond_to do |format| | |
188 | if @membership.valid? |
|
186 | if @membership.valid? | |
189 | format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' } |
|
187 | format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' } | |
190 | format.js { |
|
188 | format.js { | |
191 | render(:update) {|page| |
|
189 | render(:update) {|page| | |
192 | page.replace_html "tab-content-memberships", :partial => 'users/memberships' |
|
190 | page.replace_html "tab-content-memberships", :partial => 'users/memberships' | |
193 | page.visual_effect(:highlight, "member-#{@membership.id}") |
|
191 | page.visual_effect(:highlight, "member-#{@membership.id}") | |
194 | } |
|
192 | } | |
195 | } |
|
193 | } | |
196 | else |
|
194 | else | |
197 | format.js { |
|
195 | format.js { | |
198 | render(:update) {|page| |
|
196 | render(:update) {|page| | |
199 | page.alert(l(:notice_failed_to_save_members, :errors => @membership.errors.full_messages.join(', '))) |
|
197 | page.alert(l(:notice_failed_to_save_members, :errors => @membership.errors.full_messages.join(', '))) | |
200 | } |
|
198 | } | |
201 | } |
|
199 | } | |
202 | end |
|
200 | end | |
203 | end |
|
201 | end | |
204 | end |
|
202 | end | |
205 |
|
203 | |||
206 | def destroy_membership |
|
204 | def destroy_membership | |
207 | @membership = Member.find(params[:membership_id]) |
|
205 | @membership = Member.find(params[:membership_id]) | |
208 | if @membership.deletable? |
|
206 | if @membership.deletable? | |
209 | @membership.destroy |
|
207 | @membership.destroy | |
210 | end |
|
208 | end | |
211 | respond_to do |format| |
|
209 | respond_to do |format| | |
212 | format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' } |
|
210 | format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' } | |
213 | format.js { render(:update) {|page| page.replace_html "tab-content-memberships", :partial => 'users/memberships'} } |
|
211 | format.js { render(:update) {|page| page.replace_html "tab-content-memberships", :partial => 'users/memberships'} } | |
214 | end |
|
212 | end | |
215 | end |
|
213 | end | |
216 |
|
214 | |||
217 | private |
|
215 | private | |
218 |
|
216 | |||
219 | def find_user |
|
217 | def find_user | |
220 | if params[:id] == 'current' |
|
218 | if params[:id] == 'current' | |
221 | require_login || return |
|
219 | require_login || return | |
222 | @user = User.current |
|
220 | @user = User.current | |
223 | else |
|
221 | else | |
224 | @user = User.find(params[:id]) |
|
222 | @user = User.find(params[:id]) | |
225 | end |
|
223 | end | |
226 | rescue ActiveRecord::RecordNotFound |
|
224 | rescue ActiveRecord::RecordNotFound | |
227 | render_404 |
|
225 | render_404 | |
228 | end |
|
226 | end | |
229 | end |
|
227 | end |
@@ -1,136 +1,132 | |||||
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 | class WatchersController < ApplicationController |
|
18 | class WatchersController < ApplicationController | |
19 | before_filter :find_project |
|
19 | before_filter :find_project | |
20 | before_filter :require_login, :check_project_privacy, :only => [:watch, :unwatch] |
|
20 | before_filter :require_login, :check_project_privacy, :only => [:watch, :unwatch] | |
21 | before_filter :authorize, :only => [:new, :destroy] |
|
21 | before_filter :authorize, :only => [:new, :destroy] | |
22 |
|
22 | |||
23 | def watch |
|
23 | def watch | |
24 | if @watched.respond_to?(:visible?) && !@watched.visible?(User.current) |
|
24 | if @watched.respond_to?(:visible?) && !@watched.visible?(User.current) | |
25 | render_403 |
|
25 | render_403 | |
26 | else |
|
26 | else | |
27 | set_watcher(User.current, true) |
|
27 | set_watcher(User.current, true) | |
28 | end |
|
28 | end | |
29 | end |
|
29 | end | |
30 |
|
30 | |||
31 | def unwatch |
|
31 | def unwatch | |
32 | set_watcher(User.current, false) |
|
32 | set_watcher(User.current, false) | |
33 | end |
|
33 | end | |
34 |
|
34 | |||
35 | def new |
|
35 | def new | |
36 | respond_to do |format| |
|
36 | respond_to do |format| | |
37 | format.js do |
|
37 | format.js do | |
38 | render :update do |page| |
|
38 | render :update do |page| | |
39 | page.replace_html 'ajax-modal', :partial => 'watchers/new', :locals => {:watched => @watched} |
|
39 | page.replace_html 'ajax-modal', :partial => 'watchers/new', :locals => {:watched => @watched} | |
40 | page << "showModal('ajax-modal', '400px');" |
|
40 | page << "showModal('ajax-modal', '400px');" | |
41 | page << "$('ajax-modal').addClassName('new-watcher');" |
|
41 | page << "$('ajax-modal').addClassName('new-watcher');" | |
42 | end |
|
42 | end | |
43 | end |
|
43 | end | |
44 | end |
|
44 | end | |
45 | end |
|
45 | end | |
46 |
|
46 | |||
47 | def create |
|
47 | def create | |
48 | if params[:watcher].is_a?(Hash) && request.post? |
|
48 | if params[:watcher].is_a?(Hash) && request.post? | |
49 | user_ids = params[:watcher][:user_ids] || [params[:watcher][:user_id]] |
|
49 | user_ids = params[:watcher][:user_ids] || [params[:watcher][:user_id]] | |
50 | user_ids.each do |user_id| |
|
50 | user_ids.each do |user_id| | |
51 | Watcher.create(:watchable => @watched, :user_id => user_id) |
|
51 | Watcher.create(:watchable => @watched, :user_id => user_id) | |
52 | end |
|
52 | end | |
53 | end |
|
53 | end | |
54 | respond_to do |format| |
|
54 | respond_to do |format| | |
55 | format.html { redirect_to :back } |
|
55 | format.html { redirect_to_referer_or {render :text => 'Watcher added.', :layout => true}} | |
56 | format.js do |
|
56 | format.js do | |
57 | render :update do |page| |
|
57 | render :update do |page| | |
58 | page.replace_html 'ajax-modal', :partial => 'watchers/new', :locals => {:watched => @watched} |
|
58 | page.replace_html 'ajax-modal', :partial => 'watchers/new', :locals => {:watched => @watched} | |
59 | page.replace_html 'watchers', :partial => 'watchers/watchers', :locals => {:watched => @watched} |
|
59 | page.replace_html 'watchers', :partial => 'watchers/watchers', :locals => {:watched => @watched} | |
60 | end |
|
60 | end | |
61 | end |
|
61 | end | |
62 | end |
|
62 | end | |
63 | rescue ::ActionController::RedirectBackError |
|
|||
64 | render :text => 'Watcher added.', :layout => true |
|
|||
65 | end |
|
63 | end | |
66 |
|
64 | |||
67 | def append |
|
65 | def append | |
68 | if params[:watcher].is_a?(Hash) |
|
66 | if params[:watcher].is_a?(Hash) | |
69 | user_ids = params[:watcher][:user_ids] || [params[:watcher][:user_id]] |
|
67 | user_ids = params[:watcher][:user_ids] || [params[:watcher][:user_id]] | |
70 | users = User.active.find_all_by_id(user_ids) |
|
68 | users = User.active.find_all_by_id(user_ids) | |
71 | respond_to do |format| |
|
69 | respond_to do |format| | |
72 | format.js do |
|
70 | format.js do | |
73 | render :update do |page| |
|
71 | render :update do |page| | |
74 | users.each do |user| |
|
72 | users.each do |user| | |
75 | page.select("#issue_watcher_user_ids_#{user.id}").each do |item| |
|
73 | page.select("#issue_watcher_user_ids_#{user.id}").each do |item| | |
76 | page.remove item |
|
74 | page.remove item | |
77 | end |
|
75 | end | |
78 | end |
|
76 | end | |
79 | page.insert_html :bottom, 'watchers_inputs', :text => watchers_checkboxes(nil, users, true) |
|
77 | page.insert_html :bottom, 'watchers_inputs', :text => watchers_checkboxes(nil, users, true) | |
80 | end |
|
78 | end | |
81 | end |
|
79 | end | |
82 | end |
|
80 | end | |
83 | end |
|
81 | end | |
84 | end |
|
82 | end | |
85 |
|
83 | |||
86 | def destroy |
|
84 | def destroy | |
87 | @watched.set_watcher(User.find(params[:user_id]), false) if request.post? |
|
85 | @watched.set_watcher(User.find(params[:user_id]), false) if request.post? | |
88 | respond_to do |format| |
|
86 | respond_to do |format| | |
89 | format.html { redirect_to :back } |
|
87 | format.html { redirect_to :back } | |
90 | format.js do |
|
88 | format.js do | |
91 | render :update do |page| |
|
89 | render :update do |page| | |
92 | page.replace_html 'watchers', :partial => 'watchers/watchers', :locals => {:watched => @watched} |
|
90 | page.replace_html 'watchers', :partial => 'watchers/watchers', :locals => {:watched => @watched} | |
93 | end |
|
91 | end | |
94 | end |
|
92 | end | |
95 | end |
|
93 | end | |
96 | end |
|
94 | end | |
97 |
|
95 | |||
98 | def autocomplete_for_user |
|
96 | def autocomplete_for_user | |
99 | @users = User.active.like(params[:q]).find(:all, :limit => 100) |
|
97 | @users = User.active.like(params[:q]).find(:all, :limit => 100) | |
100 | if @watched |
|
98 | if @watched | |
101 | @users -= @watched.watcher_users |
|
99 | @users -= @watched.watcher_users | |
102 | end |
|
100 | end | |
103 | render :layout => false |
|
101 | render :layout => false | |
104 | end |
|
102 | end | |
105 |
|
103 | |||
106 | private |
|
104 | private | |
107 | def find_project |
|
105 | def find_project | |
108 | if params[:object_type] && params[:object_id] |
|
106 | if params[:object_type] && params[:object_id] | |
109 | klass = Object.const_get(params[:object_type].camelcase) |
|
107 | klass = Object.const_get(params[:object_type].camelcase) | |
110 | return false unless klass.respond_to?('watched_by') |
|
108 | return false unless klass.respond_to?('watched_by') | |
111 | @watched = klass.find(params[:object_id]) |
|
109 | @watched = klass.find(params[:object_id]) | |
112 | @project = @watched.project |
|
110 | @project = @watched.project | |
113 | elsif params[:project_id] |
|
111 | elsif params[:project_id] | |
114 | @project = Project.visible.find(params[:project_id]) |
|
112 | @project = Project.visible.find(params[:project_id]) | |
115 | end |
|
113 | end | |
116 | rescue |
|
114 | rescue | |
117 | render_404 |
|
115 | render_404 | |
118 | end |
|
116 | end | |
119 |
|
117 | |||
120 | def set_watcher(user, watching) |
|
118 | def set_watcher(user, watching) | |
121 | @watched.set_watcher(user, watching) |
|
119 | @watched.set_watcher(user, watching) | |
122 | respond_to do |format| |
|
120 | respond_to do |format| | |
123 | format.html { redirect_to :back } |
|
121 | format.html { redirect_to_referer_or {render :text => (watching ? 'Watcher added.' : 'Watcher removed.'), :layout => true}} | |
124 | format.js do |
|
122 | format.js do | |
125 | render(:update) do |page| |
|
123 | render(:update) do |page| | |
126 | c = watcher_css(@watched) |
|
124 | c = watcher_css(@watched) | |
127 | page.select(".#{c}").each do |item| |
|
125 | page.select(".#{c}").each do |item| | |
128 | page.replace_html item, watcher_link(@watched, user) |
|
126 | page.replace_html item, watcher_link(@watched, user) | |
129 | end |
|
127 | end | |
130 | end |
|
128 | end | |
131 | end |
|
129 | end | |
132 | end |
|
130 | end | |
133 | rescue ::ActionController::RedirectBackError |
|
|||
134 | render :text => (watching ? 'Watcher added.' : 'Watcher removed.'), :layout => true |
|
|||
135 | end |
|
131 | end | |
136 | end |
|
132 | end |
General Comments 0
You need to be logged in to leave comments.
Login now