2013-01-20 06:20:50 -05:00
|
|
|
class Admin::UsersController < Admin::ApplicationController
|
2015-06-22 11:12:20 -04:00
|
|
|
before_action :user, except: [:index, :new, :create]
|
2013-01-26 05:08:34 -05:00
|
|
|
|
2011-10-08 17:36:38 -04:00
|
|
|
def index
|
2015-02-05 22:15:05 -05:00
|
|
|
@users = User.order_name_asc.filter(params[:filter])
|
2016-10-25 13:14:58 -04:00
|
|
|
@users = @users.search_with_secondary_emails(params[:search_query]) if params[:search_query].present?
|
2014-10-10 08:15:34 -04:00
|
|
|
@users = @users.sort(@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)
|
|
|
|
|
2016-05-03 23:55:35 -04:00
|
|
|
Gitlab::AppLogger.info("User #{current_user.username} has started impersonating #{user.username}")
|
|
|
|
|
2016-04-22 17:19:55 -04:00
|
|
|
flash[:alert] = "You are now impersonating #{user.username}"
|
|
|
|
|
|
|
|
redirect_to root_path
|
2017-03-07 22:05:01 -05:00
|
|
|
else
|
|
|
|
flash[:alert] =
|
|
|
|
if user.blocked?
|
|
|
|
"You cannot impersonate a blocked user"
|
|
|
|
elsif user.internal?
|
|
|
|
"You cannot impersonate an internal user"
|
|
|
|
else
|
|
|
|
"You cannot impersonate a user who cannot log in"
|
|
|
|
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 }
|
2015-10-20 03:28:28 -04:00
|
|
|
redirect_back_or_admin_user(notice: "Successfully blocked")
|
2012-09-16 07:44:54 -04:00
|
|
|
else
|
2015-10-20 03:28:28 -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?
|
|
|
|
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 }
|
2015-10-20 03:28:28 -04:00
|
|
|
redirect_back_or_admin_user(notice: "Successfully unblocked")
|
2012-09-16 07:44:54 -04:00
|
|
|
else
|
2015-10-20 03:28:28 -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! }
|
2015-10-20 03:28:28 -04:00
|
|
|
redirect_back_or_admin_user(alert: "Successfully unlocked")
|
2015-07-02 01:26:14 -04:00
|
|
|
else
|
2015-10-20 03:28:28 -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 }
|
2015-10-20 03:28:28 -04:00
|
|
|
redirect_back_or_admin_user(notice: "Successfully confirmed")
|
2015-07-29 10:32:01 -04:00
|
|
|
else
|
2015-10-20 03:28:28 -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),
|
|
|
|
notice: 'Two-factor Authentication has been disabled for this user'
|
|
|
|
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?
|
2013-06-26 08:57:02 -04:00
|
|
|
format.html { redirect_to [:admin, @user], notice: 'User was successfully created.' }
|
|
|
|
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
|
2013-06-26 08:57:02 -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|
|
2017-06-06 18:45:16 -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
|
2017-06-22 10:54:54 -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
|
2017-06-22 10:54:54 -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: 400 }
|
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
|
2014-01-19 13:55:59 -05:00
|
|
|
@user ||= User.find_by!(username: params[:id])
|
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
|
|
|
|
params.require(:user).permit(user_params_ce)
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_params_ce
|
|
|
|
[
|
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
|
2011-10-08 17:36:38 -04:00
|
|
|
end
|