users_controller.rb 3.33 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
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, alert: "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, alert: "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
      if user.update_attributes(user_params_with_pass)
76
        user.confirm!
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 106 107
    user.set_notification_email
    user.save if user.notification_email_changed?

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

114 115
  protected

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

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