@@ -1,417 +1,429 | |||
|
1 | 1 | # redMine - project management software |
|
2 | 2 | # Copyright (C) 2006-2007 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | require 'uri' |
|
19 | 19 | require 'cgi' |
|
20 | 20 | |
|
21 | 21 | class ApplicationController < ActionController::Base |
|
22 | 22 | include Redmine::I18n |
|
23 | 23 | |
|
24 | 24 | layout 'base' |
|
25 | 25 | exempt_from_layout 'builder', 'apit' |
|
26 | 26 | |
|
27 | 27 | # Remove broken cookie after upgrade from 0.8.x (#4292) |
|
28 | 28 | # See https://rails.lighthouseapp.com/projects/8994/tickets/3360 |
|
29 | 29 | # TODO: remove it when Rails is fixed |
|
30 | 30 | before_filter :delete_broken_cookies |
|
31 | 31 | def delete_broken_cookies |
|
32 | 32 | if cookies['_redmine_session'] && cookies['_redmine_session'] !~ /--/ |
|
33 | 33 | cookies.delete '_redmine_session' |
|
34 | 34 | redirect_to home_path |
|
35 | 35 | return false |
|
36 | 36 | end |
|
37 | 37 | end |
|
38 | 38 | |
|
39 | 39 | before_filter :user_setup, :check_if_login_required, :set_localization |
|
40 | 40 | filter_parameter_logging :password |
|
41 | 41 | protect_from_forgery |
|
42 | 42 | |
|
43 | 43 | rescue_from ActionController::InvalidAuthenticityToken, :with => :invalid_authenticity_token |
|
44 | 44 | |
|
45 | 45 | include Redmine::Search::Controller |
|
46 | 46 | include Redmine::MenuManager::MenuController |
|
47 | 47 | helper Redmine::MenuManager::MenuHelper |
|
48 | 48 | |
|
49 | 49 | Redmine::Scm::Base.all.each do |scm| |
|
50 | 50 | require_dependency "repository/#{scm.underscore}" |
|
51 | 51 | end |
|
52 | 52 | |
|
53 | 53 | def user_setup |
|
54 | 54 | # Check the settings cache for each request |
|
55 | 55 | Setting.check_cache |
|
56 | 56 | # Find the current user |
|
57 | 57 | User.current = find_current_user |
|
58 | 58 | end |
|
59 | 59 | |
|
60 | 60 | # Returns the current user or nil if no user is logged in |
|
61 | 61 | # and starts a session if needed |
|
62 | 62 | def find_current_user |
|
63 | 63 | if session[:user_id] |
|
64 | 64 | # existing session |
|
65 | 65 | (User.active.find(session[:user_id]) rescue nil) |
|
66 | 66 | elsif cookies[:autologin] && Setting.autologin? |
|
67 | 67 | # auto-login feature starts a new session |
|
68 | 68 | user = User.try_to_autologin(cookies[:autologin]) |
|
69 | 69 | session[:user_id] = user.id if user |
|
70 | 70 | user |
|
71 | 71 | elsif params[:format] == 'atom' && params[:key] && accept_key_auth_actions.include?(params[:action]) |
|
72 | 72 | # RSS key authentication does not start a session |
|
73 | 73 | User.find_by_rss_key(params[:key]) |
|
74 | 74 | elsif Setting.rest_api_enabled? && ['xml', 'json'].include?(params[:format]) |
|
75 | 75 | if params[:key].present? && accept_key_auth_actions.include?(params[:action]) |
|
76 | 76 | # Use API key |
|
77 | 77 | User.find_by_api_key(params[:key]) |
|
78 | 78 | else |
|
79 | 79 | # HTTP Basic, either username/password or API key/random |
|
80 | 80 | authenticate_with_http_basic do |username, password| |
|
81 | 81 | User.try_to_login(username, password) || User.find_by_api_key(username) |
|
82 | 82 | end |
|
83 | 83 | end |
|
84 | 84 | end |
|
85 | 85 | end |
|
86 | 86 | |
|
87 | 87 | # Sets the logged in user |
|
88 | 88 | def logged_user=(user) |
|
89 | 89 | reset_session |
|
90 | 90 | if user && user.is_a?(User) |
|
91 | 91 | User.current = user |
|
92 | 92 | session[:user_id] = user.id |
|
93 | 93 | else |
|
94 | 94 | User.current = User.anonymous |
|
95 | 95 | end |
|
96 | 96 | end |
|
97 | 97 | |
|
98 | 98 | # check if login is globally required to access the application |
|
99 | 99 | def check_if_login_required |
|
100 | 100 | # no check needed if user is already logged in |
|
101 | 101 | return true if User.current.logged? |
|
102 | 102 | require_login if Setting.login_required? |
|
103 | 103 | end |
|
104 | 104 | |
|
105 | 105 | def set_localization |
|
106 | 106 | lang = nil |
|
107 | 107 | if User.current.logged? |
|
108 | 108 | lang = find_language(User.current.language) |
|
109 | 109 | end |
|
110 | 110 | if lang.nil? && request.env['HTTP_ACCEPT_LANGUAGE'] |
|
111 | 111 | accept_lang = parse_qvalues(request.env['HTTP_ACCEPT_LANGUAGE']).first |
|
112 | 112 | if !accept_lang.blank? |
|
113 | 113 | accept_lang = accept_lang.downcase |
|
114 | 114 | lang = find_language(accept_lang) || find_language(accept_lang.split('-').first) |
|
115 | 115 | end |
|
116 | 116 | end |
|
117 | 117 | lang ||= Setting.default_language |
|
118 | 118 | set_language_if_valid(lang) |
|
119 | 119 | end |
|
120 | 120 | |
|
121 | 121 | def require_login |
|
122 | 122 | if !User.current.logged? |
|
123 | 123 | # Extract only the basic url parameters on non-GET requests |
|
124 | 124 | if request.get? |
|
125 | 125 | url = url_for(params) |
|
126 | 126 | else |
|
127 | 127 | url = url_for(:controller => params[:controller], :action => params[:action], :id => params[:id], :project_id => params[:project_id]) |
|
128 | 128 | end |
|
129 | 129 | respond_to do |format| |
|
130 | 130 | format.html { redirect_to :controller => "account", :action => "login", :back_url => url } |
|
131 | 131 | format.atom { redirect_to :controller => "account", :action => "login", :back_url => url } |
|
132 | 132 | format.xml { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' } |
|
133 | 133 | format.js { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' } |
|
134 | 134 | format.json { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' } |
|
135 | 135 | end |
|
136 | 136 | return false |
|
137 | 137 | end |
|
138 | 138 | true |
|
139 | 139 | end |
|
140 | 140 | |
|
141 | 141 | def require_admin |
|
142 | 142 | return unless require_login |
|
143 | 143 | if !User.current.admin? |
|
144 | 144 | render_403 |
|
145 | 145 | return false |
|
146 | 146 | end |
|
147 | 147 | true |
|
148 | 148 | end |
|
149 | 149 | |
|
150 | 150 | def deny_access |
|
151 | 151 | User.current.logged? ? render_403 : require_login |
|
152 | 152 | end |
|
153 | 153 | |
|
154 | 154 | # Authorize the user for the requested action |
|
155 | 155 | def authorize(ctrl = params[:controller], action = params[:action], global = false) |
|
156 | 156 | allowed = User.current.allowed_to?({:controller => ctrl, :action => action}, @project || @projects, :global => global) |
|
157 | 157 | if allowed |
|
158 | 158 | true |
|
159 | 159 | else |
|
160 | 160 | if @project && @project.archived? |
|
161 | 161 | render_403 :message => :notice_not_authorized_archived_project |
|
162 | 162 | else |
|
163 | 163 | deny_access |
|
164 | 164 | end |
|
165 | 165 | end |
|
166 | 166 | end |
|
167 | 167 | |
|
168 | 168 | # Authorize the user for the requested action outside a project |
|
169 | 169 | def authorize_global(ctrl = params[:controller], action = params[:action], global = true) |
|
170 | 170 | authorize(ctrl, action, global) |
|
171 | 171 | end |
|
172 | 172 | |
|
173 | 173 | # Find project of id params[:id] |
|
174 | 174 | def find_project |
|
175 | 175 | @project = Project.find(params[:id]) |
|
176 | 176 | rescue ActiveRecord::RecordNotFound |
|
177 | 177 | render_404 |
|
178 | 178 | end |
|
179 | 179 | |
|
180 | 180 | # Find project of id params[:project_id] |
|
181 | 181 | def find_project_by_project_id |
|
182 | 182 | @project = Project.find(params[:project_id]) |
|
183 | 183 | rescue ActiveRecord::RecordNotFound |
|
184 | 184 | render_404 |
|
185 | 185 | end |
|
186 | 186 | |
|
187 | 187 | # Find a project based on params[:project_id] |
|
188 | 188 | # TODO: some subclasses override this, see about merging their logic |
|
189 | 189 | def find_optional_project |
|
190 | 190 | @project = Project.find(params[:project_id]) unless params[:project_id].blank? |
|
191 | 191 | allowed = User.current.allowed_to?({:controller => params[:controller], :action => params[:action]}, @project, :global => true) |
|
192 | 192 | allowed ? true : deny_access |
|
193 | 193 | rescue ActiveRecord::RecordNotFound |
|
194 | 194 | render_404 |
|
195 | 195 | end |
|
196 | 196 | |
|
197 | 197 | # Finds and sets @project based on @object.project |
|
198 | 198 | def find_project_from_association |
|
199 | 199 | render_404 unless @object.present? |
|
200 | 200 | |
|
201 | 201 | @project = @object.project |
|
202 | 202 | rescue ActiveRecord::RecordNotFound |
|
203 | 203 | render_404 |
|
204 | 204 | end |
|
205 | 205 | |
|
206 | 206 | def find_model_object |
|
207 | 207 | model = self.class.read_inheritable_attribute('model_object') |
|
208 | 208 | if model |
|
209 | 209 | @object = model.find(params[:id]) |
|
210 | 210 | self.instance_variable_set('@' + controller_name.singularize, @object) if @object |
|
211 | 211 | end |
|
212 | 212 | rescue ActiveRecord::RecordNotFound |
|
213 | 213 | render_404 |
|
214 | 214 | end |
|
215 | 215 | |
|
216 | 216 | def self.model_object(model) |
|
217 | 217 | write_inheritable_attribute('model_object', model) |
|
218 | 218 | end |
|
219 | 219 | |
|
220 | 220 | # Filter for bulk issue operations |
|
221 | 221 | def find_issues |
|
222 | 222 | @issues = Issue.find_all_by_id(params[:id] || params[:ids]) |
|
223 | 223 | raise ActiveRecord::RecordNotFound if @issues.empty? |
|
224 | 224 | @projects = @issues.collect(&:project).compact.uniq |
|
225 | 225 | @project = @projects.first if @projects.size == 1 |
|
226 | 226 | rescue ActiveRecord::RecordNotFound |
|
227 | 227 | render_404 |
|
228 | 228 | end |
|
229 | 229 | |
|
230 | 230 | # Check if project is unique before bulk operations |
|
231 | 231 | def check_project_uniqueness |
|
232 | 232 | unless @project |
|
233 | 233 | # TODO: let users bulk edit/move/destroy issues from different projects |
|
234 | 234 | render_error 'Can not bulk edit/move/destroy issues from different projects' |
|
235 | 235 | return false |
|
236 | 236 | end |
|
237 | 237 | end |
|
238 | 238 | |
|
239 | 239 | # make sure that the user is a member of the project (or admin) if project is private |
|
240 | 240 | # used as a before_filter for actions that do not require any particular permission on the project |
|
241 | 241 | def check_project_privacy |
|
242 | 242 | if @project && @project.active? |
|
243 | 243 | if @project.is_public? || User.current.member_of?(@project) || User.current.admin? |
|
244 | 244 | true |
|
245 | 245 | else |
|
246 | 246 | User.current.logged? ? render_403 : require_login |
|
247 | 247 | end |
|
248 | 248 | else |
|
249 | 249 | @project = nil |
|
250 | 250 | render_404 |
|
251 | 251 | false |
|
252 | 252 | end |
|
253 | 253 | end |
|
254 | 254 | |
|
255 | 255 | def back_url |
|
256 | 256 | params[:back_url] || request.env['HTTP_REFERER'] |
|
257 | 257 | end |
|
258 | 258 | |
|
259 | 259 | def redirect_back_or_default(default) |
|
260 | 260 | back_url = CGI.unescape(params[:back_url].to_s) |
|
261 | 261 | if !back_url.blank? |
|
262 | 262 | begin |
|
263 | 263 | uri = URI.parse(back_url) |
|
264 | 264 | # do not redirect user to another host or to the login or register page |
|
265 | 265 | if (uri.relative? || (uri.host == request.host)) && !uri.path.match(%r{/(login|account/register)}) |
|
266 | 266 | redirect_to(back_url) |
|
267 | 267 | return |
|
268 | 268 | end |
|
269 | 269 | rescue URI::InvalidURIError |
|
270 | 270 | # redirect to default |
|
271 | 271 | end |
|
272 | 272 | end |
|
273 | 273 | redirect_to default |
|
274 | 274 | end |
|
275 | 275 | |
|
276 | 276 | def render_403(options={}) |
|
277 | 277 | @project = nil |
|
278 | 278 | render_error({:message => :notice_not_authorized, :status => 403}.merge(options)) |
|
279 | 279 | return false |
|
280 | 280 | end |
|
281 | 281 | |
|
282 | 282 | def render_404(options={}) |
|
283 | 283 | render_error({:message => :notice_file_not_found, :status => 404}.merge(options)) |
|
284 | 284 | return false |
|
285 | 285 | end |
|
286 | 286 | |
|
287 | 287 | # Renders an error response |
|
288 | 288 | def render_error(arg) |
|
289 | 289 | arg = {:message => arg} unless arg.is_a?(Hash) |
|
290 | 290 | |
|
291 | 291 | @message = arg[:message] |
|
292 | 292 | @message = l(@message) if @message.is_a?(Symbol) |
|
293 | 293 | @status = arg[:status] || 500 |
|
294 | 294 | |
|
295 | 295 | respond_to do |format| |
|
296 | 296 | format.html { |
|
297 | 297 | render :template => 'common/error', :layout => use_layout, :status => @status |
|
298 | 298 | } |
|
299 | 299 | format.atom { head @status } |
|
300 | 300 | format.xml { head @status } |
|
301 | 301 | format.js { head @status } |
|
302 | 302 | format.json { head @status } |
|
303 | 303 | end |
|
304 | 304 | end |
|
305 | 305 | |
|
306 | 306 | # Picks which layout to use based on the request |
|
307 | 307 | # |
|
308 | 308 | # @return [boolean, string] name of the layout to use or false for no layout |
|
309 | 309 | def use_layout |
|
310 | 310 | request.xhr? ? false : 'base' |
|
311 | 311 | end |
|
312 | 312 | |
|
313 | 313 | def invalid_authenticity_token |
|
314 | 314 | if api_request? |
|
315 | 315 | logger.error "Form authenticity token is missing or is invalid. API calls must include a proper Content-type header (text/xml or text/json)." |
|
316 | 316 | end |
|
317 | 317 | render_error "Invalid form authenticity token." |
|
318 | 318 | end |
|
319 | 319 | |
|
320 | 320 | def render_feed(items, options={}) |
|
321 | 321 | @items = items || [] |
|
322 | 322 | @items.sort! {|x,y| y.event_datetime <=> x.event_datetime } |
|
323 | 323 | @items = @items.slice(0, Setting.feeds_limit.to_i) |
|
324 | 324 | @title = options[:title] || Setting.app_title |
|
325 | 325 | render :template => "common/feed.atom.rxml", :layout => false, :content_type => 'application/atom+xml' |
|
326 | 326 | end |
|
327 | 327 | |
|
328 | 328 | def self.accept_key_auth(*actions) |
|
329 | 329 | actions = actions.flatten.map(&:to_s) |
|
330 | 330 | write_inheritable_attribute('accept_key_auth_actions', actions) |
|
331 | 331 | end |
|
332 | 332 | |
|
333 | 333 | def accept_key_auth_actions |
|
334 | 334 | self.class.read_inheritable_attribute('accept_key_auth_actions') || [] |
|
335 | 335 | end |
|
336 | 336 | |
|
337 | 337 | # Returns the number of objects that should be displayed |
|
338 | 338 | # on the paginated list |
|
339 | 339 | def per_page_option |
|
340 | 340 | per_page = nil |
|
341 | 341 | if params[:per_page] && Setting.per_page_options_array.include?(params[:per_page].to_s.to_i) |
|
342 | 342 | per_page = params[:per_page].to_s.to_i |
|
343 | 343 | session[:per_page] = per_page |
|
344 | 344 | elsif session[:per_page] |
|
345 | 345 | per_page = session[:per_page] |
|
346 | 346 | else |
|
347 | 347 | per_page = Setting.per_page_options_array.first || 25 |
|
348 | 348 | end |
|
349 | 349 | per_page |
|
350 | 350 | end |
|
351 | 351 | |
|
352 | 352 | # qvalues http header parser |
|
353 | 353 | # code taken from webrick |
|
354 | 354 | def parse_qvalues(value) |
|
355 | 355 | tmp = [] |
|
356 | 356 | if value |
|
357 | 357 | parts = value.split(/,\s*/) |
|
358 | 358 | parts.each {|part| |
|
359 | 359 | if m = %r{^([^\s,]+?)(?:;\s*q=(\d+(?:\.\d+)?))?$}.match(part) |
|
360 | 360 | val = m[1] |
|
361 | 361 | q = (m[2] or 1).to_f |
|
362 | 362 | tmp.push([val, q]) |
|
363 | 363 | end |
|
364 | 364 | } |
|
365 | 365 | tmp = tmp.sort_by{|val, q| -q} |
|
366 | 366 | tmp.collect!{|val, q| val} |
|
367 | 367 | end |
|
368 | 368 | return tmp |
|
369 | 369 | rescue |
|
370 | 370 | nil |
|
371 | 371 | end |
|
372 | 372 | |
|
373 | 373 | # Returns a string that can be used as filename value in Content-Disposition header |
|
374 | 374 | def filename_for_content_disposition(name) |
|
375 | 375 | request.env['HTTP_USER_AGENT'] =~ %r{MSIE} ? ERB::Util.url_encode(name) : name |
|
376 | 376 | end |
|
377 | 377 | |
|
378 | 378 | def api_request? |
|
379 | 379 | %w(xml json).include? params[:format] |
|
380 | 380 | end |
|
381 | 381 | |
|
382 | 382 | # Renders a warning flash if obj has unsaved attachments |
|
383 | 383 | def render_attachment_warning_if_needed(obj) |
|
384 | 384 | flash[:warning] = l(:warning_attachments_not_saved, obj.unsaved_attachments.size) if obj.unsaved_attachments.present? |
|
385 | 385 | end |
|
386 | 386 | |
|
387 | 387 | # Sets the `flash` notice or error based the number of issues that did not save |
|
388 | 388 | # |
|
389 | 389 | # @param [Array, Issue] issues all of the saved and unsaved Issues |
|
390 | 390 | # @param [Array, Integer] unsaved_issue_ids the issue ids that were not saved |
|
391 | 391 | def set_flash_from_bulk_issue_save(issues, unsaved_issue_ids) |
|
392 | 392 | if unsaved_issue_ids.empty? |
|
393 | 393 | flash[:notice] = l(:notice_successful_update) unless issues.empty? |
|
394 | 394 | else |
|
395 | 395 | flash[:error] = l(:notice_failed_to_save_issues, |
|
396 | 396 | :count => unsaved_issue_ids.size, |
|
397 | 397 | :total => issues.size, |
|
398 | 398 | :ids => '#' + unsaved_issue_ids.join(', #')) |
|
399 | 399 | end |
|
400 | 400 | end |
|
401 | 401 | |
|
402 | 402 | # Rescues an invalid query statement. Just in case... |
|
403 | 403 | def query_statement_invalid(exception) |
|
404 | 404 | logger.error "Query::StatementInvalid: #{exception.message}" if logger |
|
405 | 405 | session.delete(:query) |
|
406 | 406 | sort_clear if respond_to?(:sort_clear) |
|
407 | 407 | render_error "An error occurred while executing the query and has been logged. Please report this error to your Redmine administrator." |
|
408 | 408 | end |
|
409 | 409 | |
|
410 | 410 | # Converts the errors on an ActiveRecord object into a common JSON format |
|
411 | 411 | def object_errors_to_json(object) |
|
412 | 412 | object.errors.collect do |attribute, error| |
|
413 | 413 | { attribute => error } |
|
414 | 414 | end.to_json |
|
415 | 415 | end |
|
416 | ||
|
416 | ||
|
417 | # Renders API response on validation failure | |
|
418 | def render_validation_errors(object) | |
|
419 | options = { :status => :unprocessable_entity, :layout => false } | |
|
420 | options.merge!(case params[:format] | |
|
421 | when 'xml'; { :xml => object.errors } | |
|
422 | when 'json'; { :json => {'errors' => object.errors} } # ActiveResource client compliance | |
|
423 | else | |
|
424 | raise "Unknown format #{params[:format]} in #render_validation_errors" | |
|
425 | end | |
|
426 | ) | |
|
427 | render options | |
|
428 | end | |
|
417 | 429 | end |
@@ -1,225 +1,223 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2010 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 | accept_key_auth :index, :show, :create, :update |
|
23 | 23 | |
|
24 | 24 | helper :sort |
|
25 | 25 | include SortHelper |
|
26 | 26 | helper :custom_fields |
|
27 | 27 | include CustomFieldsHelper |
|
28 | 28 | |
|
29 | 29 | def index |
|
30 | 30 | sort_init 'login', 'asc' |
|
31 | 31 | sort_update %w(login firstname lastname mail admin created_on last_login_on) |
|
32 | 32 | |
|
33 | 33 | @status = params[:status] ? params[:status].to_i : 1 |
|
34 | 34 | c = ARCondition.new(@status == 0 ? "status <> 0" : ["status = ?", @status]) |
|
35 | 35 | |
|
36 | 36 | unless params[:name].blank? |
|
37 | 37 | name = "%#{params[:name].strip.downcase}%" |
|
38 | 38 | c << ["LOWER(login) LIKE ? OR LOWER(firstname) LIKE ? OR LOWER(lastname) LIKE ? OR LOWER(mail) LIKE ?", name, name, name, name] |
|
39 | 39 | end |
|
40 | 40 | |
|
41 | 41 | @user_count = User.count(:conditions => c.conditions) |
|
42 | 42 | @user_pages = Paginator.new self, @user_count, |
|
43 | 43 | per_page_option, |
|
44 | 44 | params['page'] |
|
45 | 45 | @users = User.find :all,:order => sort_clause, |
|
46 | 46 | :conditions => c.conditions, |
|
47 | 47 | :limit => @user_pages.items_per_page, |
|
48 | 48 | :offset => @user_pages.current.offset |
|
49 | 49 | |
|
50 | 50 | respond_to do |format| |
|
51 | 51 | format.html { render :layout => !request.xhr? } |
|
52 | 52 | format.api { render :template => 'users/index.apit' } |
|
53 | 53 | end |
|
54 | 54 | end |
|
55 | 55 | |
|
56 | 56 | def show |
|
57 | 57 | @user = User.find(params[:id]) |
|
58 | 58 | |
|
59 | 59 | # show projects based on current user visibility |
|
60 | 60 | @memberships = @user.memberships.all(:conditions => Project.visible_by(User.current)) |
|
61 | 61 | |
|
62 | 62 | events = Redmine::Activity::Fetcher.new(User.current, :author => @user).events(nil, nil, :limit => 10) |
|
63 | 63 | @events_by_day = events.group_by(&:event_date) |
|
64 | 64 | |
|
65 | 65 | unless User.current.admin? |
|
66 | 66 | if !@user.active? || (@user != User.current && @memberships.empty? && events.empty?) |
|
67 | 67 | render_404 |
|
68 | 68 | return |
|
69 | 69 | end |
|
70 | 70 | end |
|
71 | 71 | |
|
72 | 72 | respond_to do |format| |
|
73 | 73 | format.html { render :layout => 'base' } |
|
74 | 74 | format.api { render :template => 'users/show.apit' } |
|
75 | 75 | end |
|
76 | 76 | rescue ActiveRecord::RecordNotFound |
|
77 | 77 | render_404 |
|
78 | 78 | end |
|
79 | 79 | |
|
80 | 80 | def new |
|
81 | 81 | @notification_options = User::MAIL_NOTIFICATION_OPTIONS |
|
82 | 82 | @notification_option = Setting.default_notification_option |
|
83 | 83 | |
|
84 | 84 | @user = User.new(:language => Setting.default_language) |
|
85 | 85 | @auth_sources = AuthSource.find(:all) |
|
86 | 86 | end |
|
87 | 87 | |
|
88 | 88 | verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed } |
|
89 | 89 | def create |
|
90 | 90 | @notification_options = User::MAIL_NOTIFICATION_OPTIONS |
|
91 | 91 | @notification_option = Setting.default_notification_option |
|
92 | 92 | |
|
93 | 93 | @user = User.new(params[:user]) |
|
94 | 94 | @user.admin = params[:user][:admin] || false |
|
95 | 95 | @user.login = params[:user][:login] |
|
96 | 96 | @user.password, @user.password_confirmation = params[:password], params[:password_confirmation] unless @user.auth_source_id |
|
97 | 97 | |
|
98 | 98 | # TODO: Similar to My#account |
|
99 | 99 | @user.mail_notification = params[:notification_option] || 'only_my_events' |
|
100 | 100 | @user.pref.attributes = params[:pref] |
|
101 | 101 | @user.pref[:no_self_notified] = (params[:no_self_notified] == '1') |
|
102 | 102 | |
|
103 | 103 | if @user.save |
|
104 | 104 | @user.pref.save |
|
105 | 105 | @user.notified_project_ids = (params[:notification_option] == 'selected' ? params[:notified_project_ids] : []) |
|
106 | 106 | |
|
107 | 107 | Mailer.deliver_account_information(@user, params[:password]) if params[:send_information] |
|
108 | 108 | |
|
109 | 109 | respond_to do |format| |
|
110 | 110 | format.html { |
|
111 | 111 | flash[:notice] = l(:notice_successful_create) |
|
112 | 112 | redirect_to(params[:continue] ? |
|
113 | 113 | {:controller => 'users', :action => 'new'} : |
|
114 | 114 | {:controller => 'users', :action => 'edit', :id => @user} |
|
115 | 115 | ) |
|
116 | 116 | } |
|
117 | 117 | format.api { render :template => 'users/show.apit', :status => :created, :location => user_url(@user) } |
|
118 | 118 | end |
|
119 | 119 | else |
|
120 | 120 | @auth_sources = AuthSource.find(:all) |
|
121 | 121 | @notification_option = @user.mail_notification |
|
122 | 122 | |
|
123 | 123 | respond_to do |format| |
|
124 | 124 | format.html { render :action => 'new' } |
|
125 | format.json { render :json => {:errors => @user.errors}, :status => :unprocessable_entity, :layout => false } | |
|
126 | format.xml { render :xml => @user.errors, :status => :unprocessable_entity, :layout => false } | |
|
125 | format.api { render_validation_errors(@user) } | |
|
127 | 126 | end |
|
128 | 127 | end |
|
129 | 128 | end |
|
130 | 129 | |
|
131 | 130 | def edit |
|
132 | 131 | @user = User.find(params[:id]) |
|
133 | 132 | @notification_options = @user.valid_notification_options |
|
134 | 133 | @notification_option = @user.mail_notification |
|
135 | 134 | |
|
136 | 135 | @auth_sources = AuthSource.find(:all) |
|
137 | 136 | @membership ||= Member.new |
|
138 | 137 | end |
|
139 | 138 | |
|
140 | 139 | verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed } |
|
141 | 140 | def update |
|
142 | 141 | @user = User.find(params[:id]) |
|
143 | 142 | @notification_options = @user.valid_notification_options |
|
144 | 143 | @notification_option = @user.mail_notification |
|
145 | 144 | |
|
146 | 145 | @user.admin = params[:user][:admin] if params[:user][:admin] |
|
147 | 146 | @user.login = params[:user][:login] if params[:user][:login] |
|
148 | 147 | if params[:password].present? && (@user.auth_source_id.nil? || params[:user][:auth_source_id].blank?) |
|
149 | 148 | @user.password, @user.password_confirmation = params[:password], params[:password_confirmation] |
|
150 | 149 | end |
|
151 | 150 | @user.group_ids = params[:user][:group_ids] if params[:user][:group_ids] |
|
152 | 151 | @user.attributes = params[:user] |
|
153 | 152 | # Was the account actived ? (do it before User#save clears the change) |
|
154 | 153 | was_activated = (@user.status_change == [User::STATUS_REGISTERED, User::STATUS_ACTIVE]) |
|
155 | 154 | # TODO: Similar to My#account |
|
156 | 155 | @user.mail_notification = params[:notification_option] || 'only_my_events' |
|
157 | 156 | @user.pref.attributes = params[:pref] |
|
158 | 157 | @user.pref[:no_self_notified] = (params[:no_self_notified] == '1') |
|
159 | 158 | |
|
160 | 159 | if @user.save |
|
161 | 160 | @user.pref.save |
|
162 | 161 | @user.notified_project_ids = (params[:notification_option] == 'selected' ? params[:notified_project_ids] : []) |
|
163 | 162 | |
|
164 | 163 | if was_activated |
|
165 | 164 | Mailer.deliver_account_activated(@user) |
|
166 | 165 | elsif @user.active? && params[:send_information] && !params[:password].blank? && @user.auth_source_id.nil? |
|
167 | 166 | Mailer.deliver_account_information(@user, params[:password]) |
|
168 | 167 | end |
|
169 | 168 | |
|
170 | 169 | respond_to do |format| |
|
171 | 170 | format.html { |
|
172 | 171 | flash[:notice] = l(:notice_successful_update) |
|
173 | 172 | redirect_to :back |
|
174 | 173 | } |
|
175 | 174 | format.api { head :ok } |
|
176 | 175 | end |
|
177 | 176 | else |
|
178 | 177 | @auth_sources = AuthSource.find(:all) |
|
179 | 178 | @membership ||= Member.new |
|
180 | 179 | |
|
181 | 180 | respond_to do |format| |
|
182 | 181 | format.html { render :action => :edit } |
|
183 | format.json { render :json => {:errors => @user.errors}, :status => :unprocessable_entity, :layout => false } | |
|
184 | format.xml { render :xml => @user.errors, :status => :unprocessable_entity, :layout => false } | |
|
182 | format.api { render_validation_errors(@user) } | |
|
185 | 183 | end |
|
186 | 184 | end |
|
187 | 185 | rescue ::ActionController::RedirectBackError |
|
188 | 186 | redirect_to :controller => 'users', :action => 'edit', :id => @user |
|
189 | 187 | end |
|
190 | 188 | |
|
191 | 189 | def edit_membership |
|
192 | 190 | @user = User.find(params[:id]) |
|
193 | 191 | @membership = Member.edit_membership(params[:membership_id], params[:membership], @user) |
|
194 | 192 | @membership.save if request.post? |
|
195 | 193 | respond_to do |format| |
|
196 | 194 | if @membership.valid? |
|
197 | 195 | format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' } |
|
198 | 196 | format.js { |
|
199 | 197 | render(:update) {|page| |
|
200 | 198 | page.replace_html "tab-content-memberships", :partial => 'users/memberships' |
|
201 | 199 | page.visual_effect(:highlight, "member-#{@membership.id}") |
|
202 | 200 | } |
|
203 | 201 | } |
|
204 | 202 | else |
|
205 | 203 | format.js { |
|
206 | 204 | render(:update) {|page| |
|
207 | 205 | page.alert(l(:notice_failed_to_save_members, :errors => @membership.errors.full_messages.join(', '))) |
|
208 | 206 | } |
|
209 | 207 | } |
|
210 | 208 | end |
|
211 | 209 | end |
|
212 | 210 | end |
|
213 | 211 | |
|
214 | 212 | def destroy_membership |
|
215 | 213 | @user = User.find(params[:id]) |
|
216 | 214 | @membership = Member.find(params[:membership_id]) |
|
217 | 215 | if request.post? && @membership.deletable? |
|
218 | 216 | @membership.destroy |
|
219 | 217 | end |
|
220 | 218 | respond_to do |format| |
|
221 | 219 | format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' } |
|
222 | 220 | format.js { render(:update) {|page| page.replace_html "tab-content-memberships", :partial => 'users/memberships'} } |
|
223 | 221 | end |
|
224 | 222 | end |
|
225 | 223 | end |
General Comments 0
You need to be logged in to leave comments.
Login now