users_controller.rb 2.72 KB
Newer Older
1
class Admin::UsersController < Admin::ApplicationController
2 3
  before_filter :admin_user, only: [:show, :edit, :update, :destroy]

gitlabhq's avatar
gitlabhq committed
4
  def index
5 6
    @admin_users = User.scoped
    @admin_users = @admin_users.filter(params[:filter])
randx's avatar
randx committed
7
    @admin_users = @admin_users.search(params[:name]) if params[:name].present?
8
    @admin_users = @admin_users.alphabetically.page(params[:page])
gitlabhq's avatar
gitlabhq committed
9 10 11
  end

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

  def new
16
    @admin_user = User.new.with_defaults
gitlabhq's avatar
gitlabhq committed
17 18 19
  end

  def edit
20
    admin_user
gitlabhq's avatar
gitlabhq committed
21 22
  end

23
  def block
24
    if admin_user.block
25
      redirect_to :back, alert: "Successfully blocked"
26
    else
27 28 29 30
      redirect_to :back, alert: "Error occured. User was not blocked"
    end
  end

31
  def unblock
32
    if admin_user.activate
33
      redirect_to :back, alert: "Successfully unblocked"
34
    else
35 36 37 38
      redirect_to :back, alert: "Error occured. User was not unblocked"
    end
  end

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

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

    @admin_user = User.new(params[:user].merge(opts), as: :admin)
gitlabhq's avatar
gitlabhq committed
48
    @admin_user.admin = (admin && admin.to_i > 0)
49
    @admin_user.created_by_id = current_user.id
gitlabhq's avatar
gitlabhq committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63

    respond_to do |format|
      if @admin_user.save
        format.html { redirect_to [:admin, @admin_user], notice: 'User was successfully created.' }
        format.json { render json: @admin_user, status: :created, location: @admin_user }
      else
        format.html { render action: "new" }
        format.json { render json: @admin_user.errors, status: :unprocessable_entity }
      end
    end
  end

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

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

70
    admin_user.admin = (admin && admin.to_i > 0)
gitlabhq's avatar
gitlabhq committed
71 72

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

  def destroy
86
    if admin_user.personal_projects.count > 0
87 88
      redirect_to admin_users_path, alert: "User is a project owner and can't be removed." and return
    end
89
    admin_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

  protected

  def admin_user
100
    @admin_user ||= User.find_by_username!(params[:id])
101
  end
gitlabhq's avatar
gitlabhq committed
102
end