users_controller.rb 3.12 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 42 43 44
    opts = {
      force_random_password: true,
      password_expires_at: Time.now
    }

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 65 66 67
    if params[:user][:password].present?
      user_params.merge(
        password: params[:user][:password],
        password_confirmation: params[:user][:password_confirmation],
      )
68
    end
gitlabhq's avatar
gitlabhq committed
69 70

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

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

88
    # 2. Remove user with all authored content including personal projects
89
    user.destroy
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 101 102 103 104 105 106
  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

107 108
  protected

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

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