users_controller.rb 3.22 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
    @users = @users.search(params[:name]) if params[:name].present?
Valery Sizov's avatar
Valery Sizov committed
7
    @users = @users.sort(@sort = params[:sort])
8
    @users = @users.alphabetically.page(params[:page])
gitlabhq's avatar
gitlabhq committed
9 10 11
  end

  def show
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
12 13
    @personal_projects = user.personal_projects
    @joined_projects = user.projects.joined(@user)
gitlabhq's avatar
gitlabhq committed
14 15 16
  end

  def new
17
    @user = User.new
gitlabhq's avatar
gitlabhq committed
18 19 20
  end

  def edit
21
    user
gitlabhq's avatar
gitlabhq committed
22 23
  end

24
  def block
25
    if user.block
26
      redirect_to :back, alert: "Successfully blocked"
27
    else
Johannes Schleifenbaum's avatar
Johannes Schleifenbaum committed
28
      redirect_to :back, alert: "Error occurred. User was not blocked"
29 30 31
    end
  end

32
  def unblock
33
    if user.activate
34
      redirect_to :back, alert: "Successfully unblocked"
35
    else
Johannes Schleifenbaum's avatar
Johannes Schleifenbaum committed
36
      redirect_to :back, alert: "Error occurred. User was not unblocked"
37 38 39
    end
  end

gitlabhq's avatar
gitlabhq committed
40
  def create
41 42
    opts = {
      force_random_password: true,
Marin Jankovski's avatar
Marin Jankovski committed
43
      password_expires_at: nil
44 45
    }

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

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

  def update
64 65
    user_params_with_pass = user_params.dup

66
    if params[:user][:password].present?
67
      user_params_with_pass.merge!(
68 69 70
        password: params[:user][:password],
        password_confirmation: params[:user][:password_confirmation],
      )
71
    end
gitlabhq's avatar
gitlabhq committed
72 73

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

  def destroy
88 89
    # 1. Remove groups where user is the only owner
    user.solo_owned_groups.map(&:destroy)
90

91
    # 2. Remove user with all authored content including personal projects
92
    user.destroy
gitlabhq's avatar
gitlabhq committed
93 94

    respond_to do |format|
95
      format.html { redirect_to admin_users_path }
gitlabhq's avatar
gitlabhq committed
96 97 98
      format.json { head :ok }
    end
  end
99

100 101 102 103 104 105 106 107 108 109
  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

110 111
  protected

112
  def user
skv's avatar
skv committed
113
    @user ||= User.find_by!(username: params[:id])
114
  end
115 116 117

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