users_controller.rb 3.24 KB
Newer Older
1
class Admin::UsersController < Admin::ApplicationController
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
2
  before_action :user, except: [:index, :new, :create]
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
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
12 13 14
  end

  def projects
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
15 16
    @personal_projects = user.personal_projects
    @joined_projects = user.projects.joined(@user)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
17 18 19 20 21 22
  end

  def groups
  end

  def keys
23
    @keys = user.keys
gitlabhq's avatar
gitlabhq committed
24 25 26
  end

  def new
27
    @user = User.new
gitlabhq's avatar
gitlabhq committed
28 29 30
  end

  def edit
31
    user
gitlabhq's avatar
gitlabhq committed
32 33
  end

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

42
  def unblock
43
    if user.activate
44
      redirect_to :back, notice: "Successfully unblocked"
45
    else
Johannes Schleifenbaum's avatar
Johannes Schleifenbaum committed
46
      redirect_to :back, alert: "Error occurred. User was not unblocked"
47 48 49
    end
  end

gitlabhq's avatar
gitlabhq committed
50
  def create
51 52
    opts = {
      force_random_password: true,
Marin Jankovski's avatar
Marin Jankovski committed
53
      password_expires_at: nil
54 55
    }

56
    @user = User.new(user_params.merge(opts))
57
    @user.created_by_id = current_user.id
arul's avatar
arul committed
58
    @user.generate_password
59
    @user.generate_reset_token
60
    @user.skip_confirmation!
gitlabhq's avatar
gitlabhq committed
61 62

    respond_to do |format|
63 64 65
      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
66
      else
67
        format.html { render "new" }
68
        format.json { render json: @user.errors, status: :unprocessable_entity }
gitlabhq's avatar
gitlabhq committed
69 70 71 72 73
      end
    end
  end

  def update
74 75
    user_params_with_pass = user_params.dup

76
    if params[:user][:password].present?
77
      user_params_with_pass.merge!(
78 79 80
        password: params[:user][:password],
        password_confirmation: params[:user][:password_confirmation],
      )
81
    end
gitlabhq's avatar
gitlabhq committed
82 83

    respond_to do |format|
84
      user.skip_reconfirmation!
85
      if user.update_attributes(user_params_with_pass)
86
        format.html { redirect_to [:admin, user], notice: 'User was successfully updated.' }
gitlabhq's avatar
gitlabhq committed
87 88
        format.json { head :ok }
      else
89
        # restore username to keep form action url.
90
        user.username = params[:id]
91
        format.html { render "edit" }
92
        format.json { render json: user.errors, status: :unprocessable_entity }
gitlabhq's avatar
gitlabhq committed
93 94 95 96 97
      end
    end
  end

  def destroy
98
    DeleteUserService.new(current_user).execute(user)
gitlabhq's avatar
gitlabhq committed
99 100

    respond_to do |format|
101
      format.html { redirect_to admin_users_path }
gitlabhq's avatar
gitlabhq committed
102 103 104
      format.json { head :ok }
    end
  end
105

106 107 108 109
  def remove_email
    email = user.emails.find(params[:email_id])
    email.destroy

110
    user.update_secondary_emails!
111

112 113 114 115 116 117
    respond_to do |format|
      format.html { redirect_to :back, notice: "Successfully removed email." }
      format.js { render nothing: true }
    end
  end

118 119
  protected

120
  def user
skv's avatar
skv committed
121
    @user ||= User.find_by!(username: params[:id])
122
  end
123 124 125

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