2018-09-14 01:42:05 -04:00
# frozen_string_literal: true
2013-01-20 06:20:50 -05:00
class Admin :: UsersController < Admin :: ApplicationController
2019-02-12 17:26:07 -05:00
include RoutableActions
2015-06-22 11:12:20 -04:00
before_action :user , except : [ :index , :new , :create ]
2018-11-24 07:39:16 -05:00
before_action :check_impersonation_availability , only : :impersonate
2013-01-26 05:08:34 -05:00
2011-10-08 17:36:38 -04:00
def index
2019-02-16 17:26:15 -05:00
@users = User . filter_items ( params [ :filter ] ) . order_name_asc
2016-10-25 13:14:58 -04:00
@users = @users . search_with_secondary_emails ( params [ :search_query ] ) if params [ :search_query ] . present?
2018-04-04 05:19:47 -04:00
@users = @users . sort_by_attribute ( @sort = params [ :sort ] )
2015-02-05 22:15:05 -05:00
@users = @users . page ( params [ :page ] )
2011-10-08 17:36:38 -04:00
end
def show
2015-06-22 11:12:20 -04:00
end
def projects
2014-06-03 05:44:22 -04:00
@personal_projects = user . personal_projects
@joined_projects = user . projects . joined ( @user )
2015-06-22 11:12:20 -04:00
end
def keys
2017-08-15 06:27:37 -04:00
@keys = user . keys . order_id_desc
2011-10-08 17:36:38 -04:00
end
def new
2014-06-26 16:24:17 -04:00
@user = User . new
2011-10-08 17:36:38 -04:00
end
def edit
2013-06-26 08:57:02 -04:00
user
2011-10-08 17:36:38 -04:00
end
2016-04-22 17:19:55 -04:00
def impersonate
2017-03-09 15:02:56 -05:00
if can? ( user , :log_in )
2016-04-22 17:19:55 -04:00
session [ :impersonator_id ] = current_user . id
warden . set_user ( user , scope : :user )
2019-03-21 09:31:05 -04:00
Gitlab :: AppLogger . info ( _ ( " User %{current_user_username} has started impersonating %{username} " ) % { current_user_username : current_user . username , username : user . username } )
2016-05-03 23:55:35 -04:00
2019-03-21 09:31:05 -04:00
flash [ :alert ] = _ ( " You are now impersonating %{username} " ) % { username : user . username }
2016-04-22 17:19:55 -04:00
redirect_to root_path
2017-03-07 22:05:01 -05:00
else
flash [ :alert ] =
if user . blocked?
2019-03-21 09:31:05 -04:00
_ ( " You cannot impersonate a blocked user " )
2017-03-07 22:05:01 -05:00
elsif user . internal?
2019-03-21 09:31:05 -04:00
_ ( " You cannot impersonate an internal user " )
2017-03-07 22:05:01 -05:00
else
2019-03-21 09:31:05 -04:00
_ ( " You cannot impersonate a user who cannot log in " )
2017-03-07 22:05:01 -05:00
end
redirect_to admin_user_path ( user )
2016-04-22 17:19:55 -04:00
end
end
2012-09-16 07:44:54 -04:00
def block
2017-06-15 04:57:27 -04:00
if update_user { | user | user . block }
2019-03-21 09:31:05 -04:00
redirect_back_or_admin_user ( notice : _ ( " Successfully blocked " ) )
2012-09-16 07:44:54 -04:00
else
2019-03-21 09:31:05 -04:00
redirect_back_or_admin_user ( alert : _ ( " Error occurred. User was not blocked " ) )
2012-04-16 16:33:03 -04:00
end
end
2012-09-16 07:44:54 -04:00
def unblock
2015-12-29 15:58:38 -05:00
if user . ldap_blocked?
2019-03-21 09:31:05 -04:00
redirect_back_or_admin_user ( alert : _ ( " This user cannot be unlocked manually from GitLab " ) )
2017-06-15 04:57:27 -04:00
elsif update_user { | user | user . activate }
2019-03-21 09:31:05 -04:00
redirect_back_or_admin_user ( notice : _ ( " Successfully unblocked " ) )
2012-09-16 07:44:54 -04:00
else
2019-03-21 09:31:05 -04:00
redirect_back_or_admin_user ( alert : _ ( " Error occurred. User was not unblocked " ) )
2012-04-16 16:33:03 -04:00
end
end
2015-07-02 01:26:14 -04:00
def unlock
2017-06-15 04:57:27 -04:00
if update_user { | user | user . unlock_access! }
2019-03-21 09:31:05 -04:00
redirect_back_or_admin_user ( alert : _ ( " Successfully unlocked " ) )
2015-07-02 01:26:14 -04:00
else
2019-03-21 09:31:05 -04:00
redirect_back_or_admin_user ( alert : _ ( " Error occurred. User was not unlocked " ) )
2015-07-02 01:26:14 -04:00
end
end
2015-07-29 10:32:01 -04:00
def confirm
2017-06-15 04:57:27 -04:00
if update_user { | user | user . confirm }
2019-03-21 09:31:05 -04:00
redirect_back_or_admin_user ( notice : _ ( " Successfully confirmed " ) )
2015-07-29 10:32:01 -04:00
else
2019-03-21 09:31:05 -04:00
redirect_back_or_admin_user ( alert : _ ( " Error occurred. User was not confirmed " ) )
2015-07-29 10:32:01 -04:00
end
end
2015-07-10 17:11:18 -04:00
def disable_two_factor
2017-06-15 04:57:27 -04:00
update_user { | user | user . disable_two_factor! }
2015-07-10 17:11:18 -04:00
redirect_to admin_user_path ( user ) ,
2019-03-21 09:31:05 -04:00
notice : _ ( 'Two-factor Authentication has been disabled for this user' )
2015-07-10 17:11:18 -04:00
end
2011-10-08 17:36:38 -04:00
def create
2013-06-13 14:06:27 -04:00
opts = {
2017-03-27 05:37:24 -04:00
reset_password : true ,
skip_confirmation : true
2013-06-13 14:06:27 -04:00
}
2017-03-27 05:37:24 -04:00
@user = Users :: CreateService . new ( current_user , user_params . merge ( opts ) ) . execute
2011-10-08 17:36:38 -04:00
respond_to do | format |
2017-03-27 05:37:24 -04:00
if @user . persisted?
2019-03-21 09:31:05 -04:00
format . html { redirect_to [ :admin , @user ] , notice : _ ( 'User was successfully created.' ) }
2013-06-26 08:57:02 -04:00
format . json { render json : @user , status : :created , location : @user }
2011-10-08 17:36:38 -04:00
else
2013-06-22 08:08:11 -04:00
format . html { render " new " }
2013-06-26 08:57:02 -04:00
format . json { render json : @user . errors , status : :unprocessable_entity }
2011-10-08 17:36:38 -04:00
end
end
end
def update
2014-07-16 08:12:33 -04:00
user_params_with_pass = user_params . dup
2014-06-26 16:24:17 -04:00
if params [ :user ] [ :password ] . present?
2017-08-29 10:15:48 -04:00
password_params = {
2014-06-26 16:24:17 -04:00
password : params [ :user ] [ :password ] ,
2017-08-29 10:15:48 -04:00
password_confirmation : params [ :user ] [ :password_confirmation ]
}
password_params [ :password_expires_at ] = Time . now unless changing_own_password?
user_params_with_pass . merge! ( password_params )
2014-02-25 03:15:43 -05:00
end
2011-10-08 17:36:38 -04:00
respond_to do | format |
2017-09-27 05:48:33 -04:00
result = Users :: UpdateService . new ( current_user , user_params_with_pass . merge ( user : user ) ) . execute do | user |
2017-06-22 05:37:36 -04:00
user . skip_reconfirmation!
end
2017-06-14 11:41:19 -04:00
2017-06-15 04:57:27 -04:00
if result [ :status ] == :success
2019-03-21 09:31:05 -04:00
format . html { redirect_to [ :admin , user ] , notice : _ ( 'User was successfully updated.' ) }
2011-10-08 17:36:38 -04:00
format . json { head :ok }
else
2013-03-26 01:42:08 -04:00
# restore username to keep form action url.
2013-06-26 08:57:02 -04:00
user . username = params [ :id ]
2013-06-22 08:08:11 -04:00
format . html { render " edit " }
2017-06-22 02:55:07 -04:00
format . json { render json : [ result [ :message ] ] , status : result [ :status ] }
2011-10-08 17:36:38 -04:00
end
end
end
def destroy
2017-06-02 08:35:37 -04:00
user . delete_async ( deleted_by : current_user , params : params . permit ( :hard_delete ) )
2011-10-08 17:36:38 -04:00
respond_to do | format |
2019-03-21 09:31:05 -04:00
format . html { redirect_to admin_users_path , status : 302 , notice : _ ( " The user is being deleted. " ) }
2011-10-08 17:36:38 -04:00
format . json { head :ok }
end
end
2013-01-25 13:38:24 -05:00
2014-06-23 03:45:26 -04:00
def remove_email
email = user . emails . find ( params [ :email_id ] )
2017-10-05 06:48:22 -04:00
success = Emails :: DestroyService . new ( current_user , user : user ) . execute ( email )
2015-02-06 18:23:58 -05:00
2017-06-16 03:30:24 -04:00
respond_to do | format |
2017-06-22 05:27:37 -04:00
if success
2019-03-21 09:31:05 -04:00
format . html { redirect_back_or_admin_user ( notice : _ ( 'Successfully removed email.' ) ) }
2017-06-16 03:30:24 -04:00
format . json { head :ok }
else
2019-03-21 09:31:05 -04:00
format . html { redirect_back_or_admin_user ( alert : _ ( 'There was an error removing the e-mail.' ) ) }
format . json { render json : _ ( 'There was an error removing the e-mail.' ) , status : :bad_request }
2017-06-16 03:30:24 -04:00
end
2014-06-23 03:45:26 -04:00
end
end
2013-01-25 13:38:24 -05:00
protected
2017-08-29 10:15:48 -04:00
def changing_own_password?
user == current_user
end
2013-06-26 08:57:02 -04:00
def user
2019-02-12 17:26:07 -05:00
@user || = find_routable! ( User , params [ :id ] )
end
def build_canonical_path ( user )
url_for ( safe_params . merge ( id : user . to_param ) )
2013-01-25 13:38:24 -05:00
end
2014-06-26 08:11:45 -04:00
2015-10-20 03:28:28 -04:00
def redirect_back_or_admin_user ( options = { } )
redirect_back_or_default ( default : default_route , options : options )
end
def default_route
[ :admin , @user ]
end
2017-01-10 08:53:20 -05:00
def user_params
2018-06-25 09:11:00 -04:00
params . require ( :user ) . permit ( allowed_user_params )
2017-01-10 08:53:20 -05:00
end
2018-06-25 09:11:00 -04:00
def allowed_user_params
2017-01-10 08:53:20 -05:00
[
2017-02-06 16:38:08 -05:00
:access_level ,
2017-01-10 08:53:20 -05:00
:avatar ,
:bio ,
:can_create_group ,
:color_scheme_id ,
:email ,
:extern_uid ,
:external ,
:force_random_password ,
:hide_no_password ,
:hide_no_ssh_key ,
:key_id ,
:linkedin ,
:name ,
:password_expires_at ,
:projects_limit ,
:provider ,
:remember_me ,
:skype ,
2017-09-11 11:44:42 -04:00
:theme_id ,
2017-01-10 08:53:20 -05:00
:twitter ,
:username ,
:website_url
]
end
2017-06-15 04:57:27 -04:00
2017-06-23 11:11:31 -04:00
def update_user ( & block )
2017-09-27 05:48:33 -04:00
result = Users :: UpdateService . new ( current_user , user : user ) . execute ( & block )
2017-06-15 04:57:27 -04:00
result [ :status ] == :success
end
2018-11-24 07:39:16 -05:00
def check_impersonation_availability
access_denied! unless Gitlab . config . gitlab . impersonation_enabled
end
2011-10-08 17:36:38 -04:00
end