| 
									
										
										
										
											2009-10-29 18:37:00 +00:00
										 |  |  | # Redmine - project management software | 
					
						
							| 
									
										
										
										
											2013-01-12 09:29:31 +00:00
										 |  |  | # Copyright (C) 2006-2013  Jean-Philippe Lang | 
					
						
							| 
									
										
										
										
											2007-03-12 17:59:02 +00:00
										 |  |  | # | 
					
						
							|  |  |  | # This program is free software; you can redistribute it and/or | 
					
						
							|  |  |  | # modify it under the terms of the GNU General Public License | 
					
						
							|  |  |  | # as published by the Free Software Foundation; either version 2 | 
					
						
							|  |  |  | # of the License, or (at your option) any later version. | 
					
						
							| 
									
										
										
										
											2011-08-31 08:42:38 +00:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											2007-03-12 17:59:02 +00:00
										 |  |  | # This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  | # but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  | # GNU General Public License for more details. | 
					
						
							| 
									
										
										
										
											2011-08-31 08:42:38 +00:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											2007-03-12 17:59:02 +00:00
										 |  |  | # You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  | # along with this program; if not, write to the Free Software | 
					
						
							|  |  |  | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class UsersController < ApplicationController | 
					
						
							| 
									
										
										
										
											2009-12-17 18:21:02 +00:00
										 |  |  |   layout 'admin' | 
					
						
							| 
									
										
										
										
											2011-08-31 08:42:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-29 18:37:00 +00:00
										 |  |  |   before_filter :require_admin, :except => :show | 
					
						
							| 
									
										
										
										
											2011-01-16 15:23:11 +00:00
										 |  |  |   before_filter :find_user, :only => [:show, :edit, :update, :destroy, :edit_membership, :destroy_membership] | 
					
						
							| 
									
										
										
										
											2011-07-09 08:56:07 +00:00
										 |  |  |   accept_api_auth :index, :show, :create, :update, :destroy | 
					
						
							| 
									
										
										
										
											2007-03-12 17:59:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   helper :sort | 
					
						
							|  |  |  |   include SortHelper | 
					
						
							|  |  |  |   helper :custom_fields | 
					
						
							| 
									
										
										
										
											2011-08-31 08:42:38 +00:00
										 |  |  |   include CustomFieldsHelper | 
					
						
							| 
									
										
										
										
											2006-07-09 16:30:01 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   def index | 
					
						
							| 
									
										
										
										
											2007-03-12 17:59:02 +00:00
										 |  |  |     sort_init 'login', 'asc' | 
					
						
							| 
									
										
										
										
											2008-12-24 10:03:13 +00:00
										 |  |  |     sort_update %w(login firstname lastname mail admin created_on last_login_on) | 
					
						
							| 
									
										
										
										
											2011-08-31 08:42:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-11 13:13:49 +00:00
										 |  |  |     case params[:format] | 
					
						
							|  |  |  |     when 'xml', 'json' | 
					
						
							|  |  |  |       @offset, @limit = api_offset_and_limit | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       @limit = per_page_option | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2011-08-31 08:42:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-04 22:24:33 +00:00
										 |  |  |     @status = params[:status] || 1
 | 
					
						
							| 
									
										
										
										
											2011-08-31 08:42:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-04 22:24:33 +00:00
										 |  |  |     scope = User.logged.status(@status) | 
					
						
							|  |  |  |     scope = scope.like(params[:name]) if params[:name].present? | 
					
						
							|  |  |  |     scope = scope.in_group(params[:group_id]) if params[:group_id].present? | 
					
						
							| 
									
										
										
										
											2008-10-24 16:59:15 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-04 22:24:33 +00:00
										 |  |  |     @user_count = scope.count | 
					
						
							| 
									
										
										
										
											2013-01-08 20:18:12 +00:00
										 |  |  |     @user_pages = Paginator.new @user_count, @limit, params['page'] | 
					
						
							|  |  |  |     @offset ||= @user_pages.offset | 
					
						
							| 
									
										
										
										
											2012-12-03 21:30:10 +00:00
										 |  |  |     @users =  scope.order(sort_clause).limit(@limit).offset(@offset).all | 
					
						
							| 
									
										
										
										
											2007-03-12 17:59:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-16 19:36:53 +00:00
										 |  |  |     respond_to do |format| | 
					
						
							|  |  |  |       format.html { | 
					
						
							| 
									
										
										
										
											2011-03-16 18:20:08 +00:00
										 |  |  |         @groups = Group.all.sort | 
					
						
							|  |  |  |         render :layout => !request.xhr? | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2010-12-04 17:43:39 +00:00
										 |  |  |       format.api | 
					
						
							| 
									
										
										
										
											2013-02-17 10:15:44 +00:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2006-07-09 16:30:01 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-08-31 08:42:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-29 18:37:00 +00:00
										 |  |  |   def show | 
					
						
							| 
									
										
										
										
											2010-08-10 23:07:44 +00:00
										 |  |  |     # show projects based on current user visibility | 
					
						
							| 
									
										
										
										
											2013-06-12 19:13:25 +00:00
										 |  |  |     @memberships = @user.memberships.where(Project.visible_condition(User.current)).all | 
					
						
							| 
									
										
										
										
											2011-08-31 08:42:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-29 18:37:00 +00:00
										 |  |  |     events = Redmine::Activity::Fetcher.new(User.current, :author => @user).events(nil, nil, :limit => 10) | 
					
						
							|  |  |  |     @events_by_day = events.group_by(&:event_date) | 
					
						
							| 
									
										
										
										
											2011-08-31 08:42:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-26 15:50:16 +00:00
										 |  |  |     unless User.current.admin? | 
					
						
							|  |  |  |       if !@user.active? || (@user != User.current  && @memberships.empty? && events.empty?) | 
					
						
							|  |  |  |         render_404 | 
					
						
							|  |  |  |         return | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2009-10-29 18:37:00 +00:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2011-08-31 08:42:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-03 09:39:56 +00:00
										 |  |  |     respond_to do |format| | 
					
						
							|  |  |  |       format.html { render :layout => 'base' } | 
					
						
							| 
									
										
										
										
											2010-12-04 17:43:39 +00:00
										 |  |  |       format.api | 
					
						
							| 
									
										
										
										
											2010-12-03 09:39:56 +00:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2009-10-29 18:37:00 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2006-06-28 18:11:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-29 16:00:45 +00:00
										 |  |  |   def new | 
					
						
							| 
									
										
										
										
											2010-12-12 14:19:24 +00:00
										 |  |  |     @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option) | 
					
						
							| 
									
										
										
										
											2013-02-23 16:50:07 +00:00
										 |  |  |     @user.safe_attributes = params[:user] | 
					
						
							| 
									
										
										
										
											2012-12-02 19:09:42 +00:00
										 |  |  |     @auth_sources = AuthSource.all | 
					
						
							| 
									
										
										
										
											2010-09-28 15:28:50 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-08-31 08:42:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-28 15:28:50 +00:00
										 |  |  |   def create | 
					
						
							| 
									
										
										
										
											2010-12-12 14:19:24 +00:00
										 |  |  |     @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option) | 
					
						
							| 
									
										
										
										
											2010-12-12 13:19:07 +00:00
										 |  |  |     @user.safe_attributes = params[:user] | 
					
						
							| 
									
										
										
										
											2010-09-28 15:28:50 +00:00
										 |  |  |     @user.admin = params[:user][:admin] || false | 
					
						
							|  |  |  |     @user.login = params[:user][:login] | 
					
						
							| 
									
										
										
										
											2010-12-12 13:39:55 +00:00
										 |  |  |     @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation] unless @user.auth_source_id | 
					
						
							| 
									
										
										
										
											2010-09-28 22:13:06 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-28 15:28:50 +00:00
										 |  |  |     if @user.save | 
					
						
							| 
									
										
										
										
											2012-03-04 10:44:59 +00:00
										 |  |  |       @user.pref.attributes = params[:pref] | 
					
						
							| 
									
										
										
										
											2010-09-28 22:13:06 +00:00
										 |  |  |       @user.pref.save | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-23 16:50:07 +00:00
										 |  |  |       Mailer.account_information(@user, @user.password).deliver if params[:send_information] | 
					
						
							| 
									
										
										
										
											2011-08-31 08:42:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-03 09:39:56 +00:00
										 |  |  |       respond_to do |format| | 
					
						
							|  |  |  |         format.html { | 
					
						
							| 
									
										
										
										
											2012-06-26 17:14:44 +00:00
										 |  |  |           flash[:notice] = l(:notice_user_successful_create, :id => view_context.link_to(@user.login, user_path(@user))) | 
					
						
							| 
									
										
										
										
											2012-12-11 19:39:47 +00:00
										 |  |  |           if params[:continue] | 
					
						
							| 
									
										
										
										
											2013-02-23 16:50:07 +00:00
										 |  |  |             attrs = params[:user].slice(:generate_password) | 
					
						
							|  |  |  |             redirect_to new_user_path(:user => attrs) | 
					
						
							| 
									
										
										
										
											2012-12-11 19:39:47 +00:00
										 |  |  |           else | 
					
						
							|  |  |  |             redirect_to edit_user_path(@user) | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2010-12-03 09:39:56 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2010-12-04 17:43:39 +00:00
										 |  |  |         format.api  { render :action => 'show', :status => :created, :location => user_url(@user) } | 
					
						
							| 
									
										
										
										
											2010-12-03 09:39:56 +00:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2006-07-09 16:30:01 +00:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2012-12-02 19:09:42 +00:00
										 |  |  |       @auth_sources = AuthSource.all | 
					
						
							| 
									
										
										
										
											2010-12-12 13:39:55 +00:00
										 |  |  |       # Clear password input | 
					
						
							|  |  |  |       @user.password = @user.password_confirmation = nil | 
					
						
							| 
									
										
										
										
											2010-09-28 22:13:06 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-03 09:39:56 +00:00
										 |  |  |       respond_to do |format| | 
					
						
							|  |  |  |         format.html { render :action => 'new' } | 
					
						
							| 
									
										
										
										
											2010-12-03 11:51:06 +00:00
										 |  |  |         format.api  { render_validation_errors(@user) } | 
					
						
							| 
									
										
										
										
											2010-12-03 09:39:56 +00:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2007-03-12 17:59:02 +00:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2006-07-09 16:30:01 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2006-06-28 18:11:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-07-09 16:30:01 +00:00
										 |  |  |   def edit | 
					
						
							| 
									
										
										
										
											2012-12-02 19:09:42 +00:00
										 |  |  |     @auth_sources = AuthSource.all | 
					
						
							| 
									
										
										
										
											2007-03-12 17:59:02 +00:00
										 |  |  |     @membership ||= Member.new | 
					
						
							| 
									
										
										
										
											2010-09-30 18:22:46 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-08-31 08:42:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-30 18:22:46 +00:00
										 |  |  |   def update | 
					
						
							|  |  |  |     @user.admin = params[:user][:admin] if params[:user][:admin] | 
					
						
							|  |  |  |     @user.login = params[:user][:login] if params[:user][:login] | 
					
						
							| 
									
										
										
										
											2010-12-12 13:39:55 +00:00
										 |  |  |     if params[:user][:password].present? && (@user.auth_source_id.nil? || params[:user][:auth_source_id].blank?) | 
					
						
							|  |  |  |       @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation] | 
					
						
							| 
									
										
										
										
											2010-09-30 18:22:46 +00:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2010-12-12 13:19:07 +00:00
										 |  |  |     @user.safe_attributes = params[:user] | 
					
						
							| 
									
										
										
										
											2010-09-30 18:22:46 +00:00
										 |  |  |     # Was the account actived ? (do it before User#save clears the change) | 
					
						
							|  |  |  |     was_activated = (@user.status_change == [User::STATUS_REGISTERED, User::STATUS_ACTIVE]) | 
					
						
							|  |  |  |     # TODO: Similar to My#account | 
					
						
							|  |  |  |     @user.pref.attributes = params[:pref] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if @user.save | 
					
						
							|  |  |  |       @user.pref.save | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if was_activated | 
					
						
							| 
									
										
										
										
											2012-05-05 13:07:12 +00:00
										 |  |  |         Mailer.account_activated(@user).deliver | 
					
						
							| 
									
										
										
										
											2013-02-23 16:50:07 +00:00
										 |  |  |       elsif @user.active? && params[:send_information] && @user.password.present? && @user.auth_source_id.nil? | 
					
						
							|  |  |  |         Mailer.account_information(@user, @user.password).deliver | 
					
						
							| 
									
										
										
										
											2010-09-30 18:22:46 +00:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2011-08-31 08:42:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-03 09:39:56 +00:00
										 |  |  |       respond_to do |format| | 
					
						
							|  |  |  |         format.html { | 
					
						
							|  |  |  |           flash[:notice] = l(:notice_successful_update) | 
					
						
							| 
									
										
										
										
											2012-04-06 16:51:10 +00:00
										 |  |  |           redirect_to_referer_or edit_user_path(@user) | 
					
						
							| 
									
										
										
										
											2010-12-03 09:39:56 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2012-07-14 08:13:55 +00:00
										 |  |  |         format.api  { render_api_ok } | 
					
						
							| 
									
										
										
										
											2010-12-03 09:39:56 +00:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2010-09-30 18:22:46 +00:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2012-12-02 19:09:42 +00:00
										 |  |  |       @auth_sources = AuthSource.all | 
					
						
							| 
									
										
										
										
											2010-09-30 18:22:46 +00:00
										 |  |  |       @membership ||= Member.new | 
					
						
							| 
									
										
										
										
											2010-12-12 13:39:55 +00:00
										 |  |  |       # Clear password input | 
					
						
							|  |  |  |       @user.password = @user.password_confirmation = nil | 
					
						
							| 
									
										
										
										
											2010-09-30 18:22:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-03 09:39:56 +00:00
										 |  |  |       respond_to do |format| | 
					
						
							|  |  |  |         format.html { render :action => :edit } | 
					
						
							| 
									
										
										
										
											2010-12-03 11:51:06 +00:00
										 |  |  |         format.api  { render_validation_errors(@user) } | 
					
						
							| 
									
										
										
										
											2010-12-03 09:39:56 +00:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2010-09-30 18:22:46 +00:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2007-03-12 17:59:02 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2010-09-30 18:22:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-01-16 15:23:11 +00:00
										 |  |  |   def destroy | 
					
						
							|  |  |  |     @user.destroy | 
					
						
							|  |  |  |     respond_to do |format| | 
					
						
							| 
									
										
										
										
											2012-12-11 19:39:47 +00:00
										 |  |  |       format.html { redirect_back_or_default(users_path) } | 
					
						
							| 
									
										
										
										
											2012-07-14 08:13:55 +00:00
										 |  |  |       format.api  { render_api_ok } | 
					
						
							| 
									
										
										
										
											2011-01-16 15:23:11 +00:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-03-12 17:59:02 +00:00
										 |  |  |   def edit_membership | 
					
						
							| 
									
										
										
										
											2010-03-18 15:49:11 +00:00
										 |  |  |     @membership = Member.edit_membership(params[:membership_id], params[:membership], @user) | 
					
						
							| 
									
										
										
										
											2011-12-10 08:37:48 +00:00
										 |  |  |     @membership.save | 
					
						
							| 
									
										
										
										
											2009-05-10 10:54:31 +00:00
										 |  |  |     respond_to do |format| | 
					
						
							| 
									
										
										
										
											2012-12-11 19:39:47 +00:00
										 |  |  |       format.html { redirect_to edit_user_path(@user, :tab => 'memberships') } | 
					
						
							| 
									
										
										
										
											2012-07-21 06:35:49 +00:00
										 |  |  |       format.js | 
					
						
							| 
									
										
										
										
											2010-08-10 22:37:00 +00:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2007-03-12 17:59:02 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-08-31 08:42:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-03-12 17:59:02 +00:00
										 |  |  |   def destroy_membership | 
					
						
							| 
									
										
										
										
											2009-09-12 08:36:46 +00:00
										 |  |  |     @membership = Member.find(params[:membership_id]) | 
					
						
							| 
									
										
										
										
											2011-12-10 08:37:48 +00:00
										 |  |  |     if @membership.deletable? | 
					
						
							| 
									
										
										
										
											2009-09-12 08:36:46 +00:00
										 |  |  |       @membership.destroy | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2009-05-10 10:54:31 +00:00
										 |  |  |     respond_to do |format| | 
					
						
							| 
									
										
										
										
											2012-12-11 19:39:47 +00:00
										 |  |  |       format.html { redirect_to edit_user_path(@user, :tab => 'memberships') } | 
					
						
							| 
									
										
										
										
											2012-07-21 06:35:49 +00:00
										 |  |  |       format.js | 
					
						
							| 
									
										
										
										
											2009-05-10 10:54:31 +00:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2006-07-09 16:30:01 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-08-31 08:42:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-20 17:45:09 +00:00
										 |  |  |   private | 
					
						
							| 
									
										
										
										
											2011-08-31 08:42:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-20 17:45:09 +00:00
										 |  |  |   def find_user | 
					
						
							|  |  |  |     if params[:id] == 'current' | 
					
						
							|  |  |  |       require_login || return | 
					
						
							|  |  |  |       @user = User.current | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       @user = User.find(params[:id]) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   rescue ActiveRecord::RecordNotFound | 
					
						
							|  |  |  |     render_404 | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2006-06-28 18:11:03 +00:00
										 |  |  | end |