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

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

  def show
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
11 12
    @personal_projects = user.personal_projects
    @joined_projects = user.projects.joined(@user)
gitlabhq's avatar
gitlabhq committed
13 14 15
  end

  def new
16
    @user = User.new
gitlabhq's avatar
gitlabhq committed
17 18 19
  end

  def edit
20
    user
gitlabhq's avatar
gitlabhq committed
21 22
  end

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

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

gitlabhq's avatar
gitlabhq committed
39
  def create
40 41
    opts = {
      force_random_password: true,
Marin Jankovski's avatar
Marin Jankovski committed
42
      password_expires_at: nil
43 44
    }

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

    respond_to do |format|
52 53 54
      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
55
      else
56
        format.html { render "new" }
57
        format.json { render json: @user.errors, status: :unprocessable_entity }
gitlabhq's avatar
gitlabhq committed
58 59 60 61 62
      end
    end
  end

  def update
63 64
    user_params_with_pass = user_params.dup

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

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

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

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

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

99 100 101 102 103 104 105 106 107 108
  def remove_email
    email = user.emails.find(params[:email_id])
    email.destroy

    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 119
      :skype, :linkedin, :twitter, :website_url, :color_scheme_id, :theme_id, :force_random_password,
      :extern_uid, :provider, :password_expires_at, :avatar, :hide_no_ssh_key,
120
      :projects_limit, :can_create_group, :admin
121 122
    )
  end
gitlabhq's avatar
gitlabhq committed
123
end