users_controller.rb 2.91 KB
Newer Older
gitlabhq's avatar
gitlabhq committed
1
class Admin::UsersController < ApplicationController
gitlabhq's avatar
gitlabhq committed
2
  layout "admin"
gitlabhq's avatar
gitlabhq committed
3 4 5 6
  before_filter :authenticate_user!
  before_filter :authenticate_admin!

  def index
7 8
    @admin_users = User.scoped
    @admin_users = @admin_users.filter(params[:filter])
randx's avatar
randx committed
9
    @admin_users = @admin_users.search(params[:name]) if params[:name].present?
10
    @admin_users = @admin_users.order("updated_at DESC").page(params[:page])
gitlabhq's avatar
gitlabhq committed
11 12 13 14
  end

  def show
    @admin_user = User.find(params[:id])
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
15 16 17 18 19 20

    @projects = if @admin_user.projects.empty?
               Project
             else
               Project.without_user(@admin_user)
             end.all
gitlabhq's avatar
gitlabhq committed
21 22
  end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
23 24 25 26 27 28
  def team_update
    @admin_user = User.find(params[:id])

    UsersProject.user_bulk_import(
      @admin_user, 
      params[:project_ids],
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
29
      params[:project_access]
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
30 31 32 33 34 35
    )

    redirect_to [:admin, @admin_user], notice: 'Teams were successfully updated.'
  end


gitlabhq's avatar
gitlabhq committed
36
  def new
37
    @admin_user = User.new(projects_limit: Gitlab.config.default_projects_limit)
gitlabhq's avatar
gitlabhq committed
38 39 40 41 42 43
  end

  def edit
    @admin_user = User.find(params[:id])
  end

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
  def block 
    @admin_user = User.find(params[:id])

    if @admin_user.block
      redirect_to :back, alert: "Successfully blocked"
    else 
      redirect_to :back, alert: "Error occured. User was not blocked"
    end
  end

  def unblock 
    @admin_user = User.find(params[:id])

    if @admin_user.update_attribute(:blocked, false)
      redirect_to :back, alert: "Successfully unblocked"
    else 
      redirect_to :back, alert: "Error occured. User was not unblocked"
    end
  end

gitlabhq's avatar
gitlabhq committed
64 65 66
  def create
    admin = params[:user].delete("admin")

Nihad Abbasov's avatar
Nihad Abbasov committed
67
    @admin_user = User.new(params[:user])
gitlabhq's avatar
gitlabhq committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    @admin_user.admin = (admin && admin.to_i > 0)

    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
83

84
    if params[:user][:password].blank?
Nihad Abbasov's avatar
Nihad Abbasov committed
85 86
      params[:user].delete(:password)
      params[:user].delete(:password_confirmation)
gitlabhq's avatar
gitlabhq committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    end

    @admin_user = User.find(params[:id])
    @admin_user.admin = (admin && admin.to_i > 0)

    respond_to do |format|
      if @admin_user.update_attributes(params[:user])
        format.html { redirect_to [:admin, @admin_user], notice: 'User was successfully updated.' }
        format.json { head :ok }
      else
        format.html { render action: "edit" }
        format.json { render json: @admin_user.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @admin_user = User.find(params[:id])
    @admin_user.destroy

    respond_to do |format|
      format.html { redirect_to admin_users_url }
      format.json { head :ok }
    end
  end
end