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