users_controller.rb 3.02 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
12 13 14 15 16
    # Projects user can be added to
    @not_in_projects = Project.scoped
    @not_in_projects = @not_in_projects.without_user(admin_user) if admin_user.authorized_projects.present?

    # Projects he already own or joined
Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
17
    @projects = admin_user.authorized_projects
gitlabhq's avatar
gitlabhq committed
18 19
  end

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

27
    redirect_to [:admin, admin_user], notice: 'Teams were successfully updated.'
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
28 29 30
  end


gitlabhq's avatar
gitlabhq committed
31
  def new
32
    @admin_user = User.new.with_defaults
gitlabhq's avatar
gitlabhq committed
33 34 35
  end

  def edit
36
    admin_user
gitlabhq's avatar
gitlabhq committed
37 38
  end

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

47
  def unblock
48
    if admin_user.activate
49
      redirect_to :back, alert: "Successfully unblocked"
50
    else
51 52 53 54
      redirect_to :back, alert: "Error occured. User was not unblocked"
    end
  end

gitlabhq's avatar
gitlabhq committed
55 56 57
  def create
    admin = params[:user].delete("admin")

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

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

80
    admin_user.admin = (admin && admin.to_i > 0)
gitlabhq's avatar
gitlabhq committed
81 82

    respond_to do |format|
83 84
      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
85 86
        format.json { head :ok }
      else
87 88
        # restore username to keep form action url.
        admin_user.username = params[:id]
gitlabhq's avatar
gitlabhq committed
89
        format.html { render action: "edit" }
90
        format.json { render json: admin_user.errors, status: :unprocessable_entity }
gitlabhq's avatar
gitlabhq committed
91 92 93 94 95
      end
    end
  end

  def destroy
96
    if admin_user.personal_projects.count > 0
97 98
      redirect_to admin_users_path, alert: "User is a project owner and can't be removed." and return
    end
99
    admin_user.destroy
gitlabhq's avatar
gitlabhq committed
100 101

    respond_to do |format|
102
      format.html { redirect_to admin_users_path }
gitlabhq's avatar
gitlabhq committed
103 104 105
      format.json { head :ok }
    end
  end
106 107 108 109

  protected

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