BigW Consortium Gitlab

profiles_controller.rb 2.36 KB
Newer Older
1
class ProfilesController < Profiles::ApplicationController
2 3
  include ActionView::Helpers::SanitizeHelper

4 5 6
  before_action :user
  before_action :authorize_change_username!, only: :update_username
  skip_before_action :require_email, only: [:show, :update]
7

gitlabhq committed
8 9 10
  def show
  end

11
  def update
12
    user_params.except!(:email) if @user.ldap_user?
13

14
    respond_to do |format|
15 16 17 18 19 20 21 22 23
      if @user.update_attributes(user_params)
        message = "Profile was successfully updated"
        format.html { redirect_back_or_default(default: { action: 'show' }, options: { notice: message }) }
        format.json { render json: { message: message } }
      else
        message = @user.errors.full_messages.uniq.join('. ')
        format.html { redirect_back_or_default(default: { action: 'show' }, options: { alert: "Failed to update profile. #{message}" }) }
        format.json { render json: { message: message }, status: :unprocessable_entity }
      end
24
    end
25 26
  end

27
  def reset_private_token
28
    if current_user.reset_authentication_token!
29
      flash[:notice] = "Private token was successfully reset"
30 31 32 33 34 35 36
    end

    redirect_to profile_account_path
  end

  def reset_incoming_email_token
    if current_user.reset_incoming_email_token!
37
      flash[:notice] = "Incoming email token was successfully reset"
38 39
    end

40
    redirect_to profile_account_path
41
  end
42

43 44 45
  def audit_log
    @events = AuditEvent.where(entity_type: "User", entity_id: current_user.id).
      order("created_at DESC").
46
      page(params[:page])
47 48
  end

49
  def update_username
50 51 52 53 54
    if @user.update_attributes(username: user_params[:username])
      options = { notice: "Username successfully changed" }
    else
      message = @user.errors.full_messages.uniq.join('. ')
      options = { alert: "Username change failed - #{message}" }
55
    end
56 57

    redirect_back_or_default(default: { action: 'show' }, options: options)
58 59
  end

60
  private
61 62 63 64

  def user
    @user = current_user
  end
65

66 67 68
  def authorize_change_username!
    return render_404 unless @user.can_change_username?
  end
69 70 71

  def user_params
    params.require(:user).permit(
72 73 74 75 76
      :avatar,
      :bio,
      :email,
      :hide_no_password,
      :hide_no_ssh_key,
77
      :hide_project_limit,
78 79 80 81 82 83 84 85 86
      :linkedin,
      :location,
      :name,
      :password,
      :password_confirmation,
      :public_email,
      :skype,
      :twitter,
      :username,
87 88
      :website_url,
      :organization
89 90
    )
  end
gitlabhq committed
91
end