users_controller.rb 2.62 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
11
    @projects = user.authorized_projects
gitlabhq's avatar
gitlabhq committed
12 13 14
  end

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

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

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

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

gitlabhq's avatar
gitlabhq committed
38 39 40
  def create
    admin = params[:user].delete("admin")

41 42 43 44 45
    opts = {
      force_random_password: true,
      password_expires_at: Time.now
    }

46
    @user = User.build_user(params[:user].merge(opts), as: :admin)
47 48
    @user.admin = (admin && admin.to_i > 0)
    @user.created_by_id = current_user.id
arul's avatar
arul committed
49
    @user.generate_password
50
    @user.skip_confirmation!
gitlabhq's avatar
gitlabhq committed
51 52

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

  def update
    admin = params[:user].delete("admin")
randx's avatar
randx committed
65

66
    if params[:user][:password].blank?
Nihad Abbasov's avatar
Nihad Abbasov committed
67 68
      params[:user].delete(:password)
      params[:user].delete(:password_confirmation)
gitlabhq's avatar
gitlabhq committed
69 70
    end

71 72 73
    if admin.present?
      user.admin = !admin.to_i.zero?
    end
gitlabhq's avatar
gitlabhq committed
74 75

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

  def destroy
90 91
    # 1. Remove groups where user is the only owner
    user.solo_owned_groups.map(&:destroy)
92

93
    # 2. Remove user with all authored content including personal projects
94
    user.destroy
gitlabhq's avatar
gitlabhq committed
95 96

    respond_to do |format|
97
      format.html { redirect_to admin_users_path }
gitlabhq's avatar
gitlabhq committed
98 99 100
      format.json { head :ok }
    end
  end
101 102 103

  protected

104
  def user
skv's avatar
skv committed
105
    @user ||= User.find_by!(username: params[:id])
106
  end
gitlabhq's avatar
gitlabhq committed
107
end