users_controller.rb 3.09 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.new
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
  def create
40 41 42 43 44
    opts = {
      force_random_password: true,
      password_expires_at: Time.now
    }

45
    @user = User.new(user_params.merge(opts))
46
    @user.created_by_id = current_user.id
arul's avatar
arul committed
47
    @user.generate_password
48
    @user.skip_confirmation!
gitlabhq's avatar
gitlabhq committed
49 50

    respond_to do |format|
51 52 53
      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
54
      else
55
        format.html { render "new" }
56
        format.json { render json: @user.errors, status: :unprocessable_entity }
gitlabhq's avatar
gitlabhq committed
57 58 59 60 61
      end
    end
  end

  def update
62 63 64 65 66
    if params[:user][:password].present?
      user_params.merge(
        password: params[:user][:password],
        password_confirmation: params[:user][:password_confirmation],
      )
67
    end
gitlabhq's avatar
gitlabhq committed
68 69

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

  def destroy
84 85
    # 1. Remove groups where user is the only owner
    user.solo_owned_groups.map(&:destroy)
86

87
    # 2. Remove user with all authored content including personal projects
88
    user.destroy
gitlabhq's avatar
gitlabhq committed
89 90

    respond_to do |format|
91
      format.html { redirect_to admin_users_path }
gitlabhq's avatar
gitlabhq committed
92 93 94
      format.json { head :ok }
    end
  end
95

96 97 98 99 100 101 102 103 104 105
  def remove_email
    email = user.emails.find(params[:email_id])
    email.destroy

    respond_to do |format|
      format.html { redirect_to :back, notice: "Successfully removed email." }
      format.js { render nothing: true }
    end
  end

106 107
  protected

108
  def user
skv's avatar
skv committed
109
    @user ||= User.find_by!(username: params[:id])
110
  end
111 112 113

  def user_params
    params.require(:user).permit(
114
      :email, :remember_me, :bio, :name, :username,
115 116
      :skype, :linkedin, :twitter, :website_url, :color_scheme_id, :theme_id, :force_random_password,
      :extern_uid, :provider, :password_expires_at, :avatar, :hide_no_ssh_key,
117
      :projects_limit, :can_create_group, :admin
118 119
    )
  end
gitlabhq's avatar
gitlabhq committed
120
end