debian-mirror-gitlab/app/controllers/admin/users_controller.rb

206 lines
5 KiB
Ruby
Raw Normal View History

2014-09-02 18:07:02 +05:30
class Admin::UsersController < Admin::ApplicationController
2015-09-11 14:41:01 +05:30
before_action :user, except: [:index, :new, :create]
2014-09-02 18:07:02 +05:30
def index
2015-04-26 12:48:37 +05:30
@users = User.order_name_asc.filter(params[:filter])
2017-08-17 22:00:37 +05:30
@users = @users.search_with_secondary_emails(params[:search_query]) if params[:search_query].present?
2015-04-26 12:48:37 +05:30
@users = @users.sort(@sort = params[:sort])
@users = @users.page(params[:page])
2014-09-02 18:07:02 +05:30
end
def show
2015-09-11 14:41:01 +05:30
end
def projects
2014-09-02 18:07:02 +05:30
@personal_projects = user.personal_projects
@joined_projects = user.projects.joined(@user)
2015-09-11 14:41:01 +05:30
end
def keys
2015-04-26 12:48:37 +05:30
@keys = user.keys
2014-09-02 18:07:02 +05:30
end
def new
@user = User.new
end
def edit
user
end
2016-06-02 11:05:42 +05:30
def impersonate
2017-08-17 22:00:37 +05:30
if can?(user, :log_in)
2016-06-02 11:05:42 +05:30
session[:impersonator_id] = current_user.id
warden.set_user(user, scope: :user)
Gitlab::AppLogger.info("User #{current_user.username} has started impersonating #{user.username}")
flash[:alert] = "You are now impersonating #{user.username}"
redirect_to root_path
2017-08-17 22:00:37 +05:30
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-06-02 11:05:42 +05:30
end
end
2014-09-02 18:07:02 +05:30
def block
if user.block
2015-10-24 18:46:33 +05:30
redirect_back_or_admin_user(notice: "Successfully blocked")
2014-09-02 18:07:02 +05:30
else
2015-10-24 18:46:33 +05:30
redirect_back_or_admin_user(alert: "Error occurred. User was not blocked")
2014-09-02 18:07:02 +05:30
end
end
def unblock
if user.ldap_blocked?
redirect_back_or_admin_user(alert: "This user cannot be unlocked manually from GitLab")
elsif user.activate
2015-10-24 18:46:33 +05:30
redirect_back_or_admin_user(notice: "Successfully unblocked")
2014-09-02 18:07:02 +05:30
else
2015-10-24 18:46:33 +05:30
redirect_back_or_admin_user(alert: "Error occurred. User was not unblocked")
2014-09-02 18:07:02 +05:30
end
end
2015-09-11 14:41:01 +05:30
def unlock
if user.unlock_access!
2015-10-24 18:46:33 +05:30
redirect_back_or_admin_user(alert: "Successfully unlocked")
2015-09-11 14:41:01 +05:30
else
2015-10-24 18:46:33 +05:30
redirect_back_or_admin_user(alert: "Error occurred. User was not unlocked")
2015-09-11 14:41:01 +05:30
end
end
def confirm
2015-09-25 12:07:36 +05:30
if user.confirm
2015-10-24 18:46:33 +05:30
redirect_back_or_admin_user(notice: "Successfully confirmed")
2015-09-11 14:41:01 +05:30
else
2015-10-24 18:46:33 +05:30
redirect_back_or_admin_user(alert: "Error occurred. User was not confirmed")
2015-09-11 14:41:01 +05:30
end
end
def disable_two_factor
user.disable_two_factor!
redirect_to admin_user_path(user),
notice: 'Two-factor Authentication has been disabled for this user'
end
2014-09-02 18:07:02 +05:30
def create
opts = {
2017-08-17 22:00:37 +05:30
reset_password: true,
skip_confirmation: true
2014-09-02 18:07:02 +05:30
}
2017-08-17 22:00:37 +05:30
@user = Users::CreateService.new(current_user, user_params.merge(opts)).execute
2014-09-02 18:07:02 +05:30
respond_to do |format|
2017-08-17 22:00:37 +05:30
if @user.persisted?
2014-09-02 18:07:02 +05:30
format.html { redirect_to [:admin, @user], notice: 'User was successfully created.' }
format.json { render json: @user, status: :created, location: @user }
else
format.html { render "new" }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
def update
user_params_with_pass = user_params.dup
if params[:user][:password].present?
user_params_with_pass.merge!(
password: params[:user][:password],
password_confirmation: params[:user][:password_confirmation],
2016-06-02 11:05:42 +05:30
password_expires_at: Time.now
2014-09-02 18:07:02 +05:30
)
end
respond_to do |format|
2015-04-26 12:48:37 +05:30
user.skip_reconfirmation!
2014-09-02 18:07:02 +05:30
if user.update_attributes(user_params_with_pass)
format.html { redirect_to [:admin, user], notice: 'User was successfully updated.' }
format.json { head :ok }
else
# restore username to keep form action url.
user.username = params[:id]
format.html { render "edit" }
format.json { render json: user.errors, status: :unprocessable_entity }
end
end
end
def destroy
2016-06-02 11:05:42 +05:30
DeleteUserWorker.perform_async(current_user.id, user.id)
2014-09-02 18:07:02 +05:30
respond_to do |format|
2016-06-02 11:05:42 +05:30
format.html { redirect_to admin_users_path, notice: "The user is being deleted." }
2014-09-02 18:07:02 +05:30
format.json { head :ok }
end
end
def remove_email
email = user.emails.find(params[:email_id])
email.destroy
2015-09-11 14:41:01 +05:30
user.update_secondary_emails!
2015-04-26 12:48:37 +05:30
2014-09-02 18:07:02 +05:30
respond_to do |format|
2015-10-24 18:46:33 +05:30
format.html { redirect_back_or_admin_user(notice: "Successfully removed email.") }
2016-06-02 11:05:42 +05:30
format.js { head :ok }
2014-09-02 18:07:02 +05:30
end
end
protected
def user
@user ||= User.find_by!(username: params[:id])
end
2015-10-24 18:46:33 +05:30
def redirect_back_or_admin_user(options = {})
redirect_back_or_default(default: default_route, options: options)
end
def default_route
[:admin, @user]
end
2017-08-17 22:00:37 +05:30
def user_params
params.require(:user).permit(user_params_ce)
end
def user_params_ce
[
:access_level,
: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,
:twitter,
:username,
:website_url
]
end
2014-09-02 18:07:02 +05:30
end