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
5 6 7 8
    @users = User.scoped
    @users = @users.filter(params[:filter])
    @users = @users.search(params[:name]) if params[:name].present?
    @users = @users.alphabetically.page(params[:page])
gitlabhq's avatar
gitlabhq committed
9 10 11
  end

  def show
12
    @projects = user.authorized_projects
gitlabhq's avatar
gitlabhq committed
13 14 15
  end

  def new
16
    @user = User.build_user
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 40 41
  def create
    admin = params[:user].delete("admin")

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

47
    @user = User.build_user(params[:user].merge(opts), as: :admin)
48 49
    @user.admin = (admin && admin.to_i > 0)
    @user.created_by_id = current_user.id
arul's avatar
arul committed
50
    @user.generate_password
51
    @user.confirm!
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 65
      end
    end
  end

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

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

72
    user.admin = (admin && admin.to_i > 0)
gitlabhq's avatar
gitlabhq committed
73 74

    respond_to do |format|
75
      if user.update_attributes(params[:user], as: :admin)
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

  protected

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