users_controller.rb 2.68 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.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.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 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 73 74
    if admin.present?
      user.admin = !admin.to_i.zero?
    end
gitlabhq's avatar
gitlabhq committed
75 76

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

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

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

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

  protected

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