users_controller.rb 3.17 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
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
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,
Marin Jankovski's avatar
Marin Jankovski committed
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
    DeleteUserService.new.execute(user)
gitlabhq's avatar
gitlabhq committed
90 91

    respond_to do |format|
92
      format.html { redirect_to admin_users_path }
gitlabhq's avatar
gitlabhq committed
93 94 95
      format.json { head :ok }
    end
  end
96

97 98 99 100
  def remove_email
    email = user.emails.find(params[:email_id])
    email.destroy

101
    user.update_secondary_emails!
102

103 104 105 106 107 108
    respond_to do |format|
      format.html { redirect_to :back, notice: "Successfully removed email." }
      format.js { render nothing: true }
    end
  end

109 110
  protected

111
  def user
skv's avatar
skv committed
112
    @user ||= User.find_by!(username: params[:id])
113
  end
114 115 116

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