users_controller.rb 2.84 KB
Newer Older
1
class Admin::UsersController < Admin::ApplicationController
gitlabhq's avatar
gitlabhq committed
2
  def index
3 4
    @admin_users = User.scoped
    @admin_users = @admin_users.filter(params[:filter])
randx's avatar
randx committed
5
    @admin_users = @admin_users.search(params[:name]) if params[:name].present?
6
    @admin_users = @admin_users.alphabetically.page(params[:page])
gitlabhq's avatar
gitlabhq committed
7 8 9
  end

  def show
10
    projects = if admin_user.authorized_projects.empty?
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
11 12
               Project
             else
13
               Project.without_user(admin_user)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
14
             end.all
gitlabhq's avatar
gitlabhq committed
15 16
  end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
17
  def team_update
18
    UsersProject.add_users_into_projects(
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
19
      params[:project_ids],
20
      [admin_user.id],
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
21
      params[:project_access]
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
22 23
    )

24
    redirect_to [:admin, admin_user], notice: 'Teams were successfully updated.'
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
25 26 27
  end


gitlabhq's avatar
gitlabhq committed
28
  def new
29
    @admin_user = User.new({ projects_limit: Gitlab.config.gitlab.default_projects_limit }, as: :admin)
gitlabhq's avatar
gitlabhq committed
30 31 32
  end

  def edit
33
    admin_user
gitlabhq's avatar
gitlabhq committed
34 35
  end

36
  def block
37
    if admin_user.block
38
      redirect_to :back, alert: "Successfully blocked"
39
    else
40 41 42 43
      redirect_to :back, alert: "Error occured. User was not blocked"
    end
  end

44
  def unblock
45
    if admin_user.update_attribute(:blocked, false)
46
      redirect_to :back, alert: "Successfully unblocked"
47
    else
48 49 50 51
      redirect_to :back, alert: "Error occured. User was not unblocked"
    end
  end

gitlabhq's avatar
gitlabhq committed
52 53 54
  def create
    admin = params[:user].delete("admin")

55
    @admin_user = User.new(params[:user], as: :admin)
gitlabhq's avatar
gitlabhq committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
    @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
71

72
    if params[:user][:password].blank?
Nihad Abbasov's avatar
Nihad Abbasov committed
73 74
      params[:user].delete(:password)
      params[:user].delete(:password_confirmation)
gitlabhq's avatar
gitlabhq committed
75 76
    end

77
    admin_user.admin = (admin && admin.to_i > 0)
gitlabhq's avatar
gitlabhq committed
78 79

    respond_to do |format|
80 81
      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
82 83 84
        format.json { head :ok }
      else
        format.html { render action: "edit" }
85
        format.json { render json: admin_user.errors, status: :unprocessable_entity }
gitlabhq's avatar
gitlabhq committed
86 87 88 89 90
      end
    end
  end

  def destroy
91
    if admin_user.personal_projects.count > 0
92 93
      redirect_to admin_users_path, alert: "User is a project owner and can't be removed." and return
    end
94
    admin_user.destroy
gitlabhq's avatar
gitlabhq committed
95 96

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

  protected

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