users_controller.rb 2.6 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
    user.admin = (admin && admin.to_i > 0)
gitlabhq's avatar
gitlabhq committed
72 73

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

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

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

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

  protected

102 103
  def user
    @user ||= User.find_by_username!(params[:id])
104
  end
gitlabhq's avatar
gitlabhq committed
105
end