users_controller.rb 3.32 KB
Newer Older
1
class Admin::UsersController < Admin::ApplicationController
2
  before_action :user, only: [:show, :edit, :update, :destroy]
3

gitlabhq's avatar
gitlabhq committed
4
  def index
5
    @users = User.order_name_asc.filter(params[:filter])
6
    @users = @users.search(params[:name]) if params[:name].present?
Valery Sizov's avatar
Valery Sizov committed
7
    @users = @users.sort(@sort = params[:sort])
8
    @users = @users.page(params[:page])
gitlabhq's avatar
gitlabhq committed
9 10 11
  end

  def show
12 13
    @personal_projects = user.personal_projects
    @joined_projects = user.projects.joined(@user)
14
    @keys = user.keys
gitlabhq's avatar
gitlabhq committed
15 16 17
  end

  def new
18
    @user = User.new
gitlabhq's avatar
gitlabhq committed
19 20 21
  end

  def edit
22
    user
gitlabhq's avatar
gitlabhq committed
23 24
  end

25
  def block
26
    if user.block
27
      redirect_to :back, notice: "Successfully blocked"
28
    else
Johannes Schleifenbaum's avatar
Johannes Schleifenbaum committed
29
      redirect_to :back, alert: "Error occurred. User was not blocked"
30 31 32
    end
  end

33
  def unblock
34
    if user.activate
35
      redirect_to :back, notice: "Successfully unblocked"
36
    else
Johannes Schleifenbaum's avatar
Johannes Schleifenbaum committed
37
      redirect_to :back, alert: "Error occurred. User was not unblocked"
38 39 40
    end
  end

gitlabhq's avatar
gitlabhq committed
41
  def create
42 43
    opts = {
      force_random_password: true,
44
      password_expires_at: nil
45 46
    }

47
    @user = User.new(user_params.merge(opts))
48
    @user.created_by_id = current_user.id
arul's avatar
arul committed
49
    @user.generate_password
50
    @user.generate_reset_token
51
    @user.skip_confirmation!
gitlabhq's avatar
gitlabhq committed
52 53

    respond_to do |format|
54 55 56
      if @user.save
        format.html { redirect_to [:admin, @user], notice: 'User was successfully created.' }
        format.json { render json: @user, status: :created, location: @user }
gitlabhq's avatar
gitlabhq committed
57
      else
58
        format.html { render "new" }
59
        format.json { render json: @user.errors, status: :unprocessable_entity }
gitlabhq's avatar
gitlabhq committed
60 61 62 63 64
      end
    end
  end

  def update
65 66
    user_params_with_pass = user_params.dup

67
    if params[:user][:password].present?
68
      user_params_with_pass.merge!(
69 70 71
        password: params[:user][:password],
        password_confirmation: params[:user][:password_confirmation],
      )
72
    end
gitlabhq's avatar
gitlabhq committed
73 74

    respond_to do |format|
75
      user.skip_reconfirmation!
76
      if user.update_attributes(user_params_with_pass)
77
        format.html { redirect_to [:admin, user], notice: 'User was successfully updated.' }
gitlabhq's avatar
gitlabhq committed
78 79
        format.json { head :ok }
      else
80
        # restore username to keep form action url.
81
        user.username = params[:id]
82
        format.html { render "edit" }
83
        format.json { render json: user.errors, status: :unprocessable_entity }
gitlabhq's avatar
gitlabhq committed
84 85 86 87 88
      end
    end
  end

  def destroy
89 90
    # 1. Remove groups where user is the only owner
    user.solo_owned_groups.map(&:destroy)
91

92
    # 2. Remove user with all authored content including personal projects
93
    user.destroy
gitlabhq's avatar
gitlabhq committed
94 95

    respond_to do |format|
96
      format.html { redirect_to admin_users_path }
gitlabhq's avatar
gitlabhq committed
97 98 99
      format.json { head :ok }
    end
  end
100

101 102 103 104
  def remove_email
    email = user.emails.find(params[:email_id])
    email.destroy

105
    user.update_secondary_emails!
106

107 108 109 110 111 112
    respond_to do |format|
      format.html { redirect_to :back, notice: "Successfully removed email." }
      format.js { render nothing: true }
    end
  end

113 114
  protected

115
  def user
skv's avatar
skv committed
116
    @user ||= User.find_by!(username: params[:id])
117
  end
118 119 120

  def user_params
    params.require(:user).permit(
121
      :email, :remember_me, :bio, :name, :username,
122
      :skype, :linkedin, :twitter, :website_url, :color_scheme_id, :theme_id, :force_random_password,
123
      :extern_uid, :provider, :password_expires_at, :avatar, :hide_no_ssh_key, :hide_no_password,
124
      :projects_limit, :can_create_group, :admin, :key_id
125 126
    )
  end
gitlabhq's avatar
gitlabhq committed
127
end