BigW Consortium Gitlab

chat_names_controller.rb 1.45 KB
Newer Older
1 2 3 4 5
class Profiles::ChatNamesController < Profiles::ApplicationController
  before_action :chat_name_token, only: [:new]
  before_action :chat_name_params, only: [:new, :create, :deny]

  def index
6
    @chat_names = current_user.chat_names
7 8 9 10 11 12 13 14 15
  end

  def new
  end

  def create
    new_chat_name = current_user.chat_names.new(chat_name_params)

    if new_chat_name.save
16
      flash[:notice] = "Authorized #{new_chat_name.chat_name}"
17 18 19 20 21 22 23 24 25 26 27
    else
      flash[:alert] = "Could not authorize chat nickname. Try again!"
    end

    delete_chat_name_token
    redirect_to profile_chat_names_path
  end

  def deny
    delete_chat_name_token

Kamil Trzcinski committed
28
    flash[:notice] = "Denied authorization of chat nickname #{chat_name_params[:user_name]}."
29 30 31 32 33 34 35 36

    redirect_to profile_chat_names_path
  end

  def destroy
    @chat_name = chat_names.find(params[:id])

    if @chat_name.destroy
37
      flash[:notice] = "Deleted chat nickname: #{@chat_name.chat_name}!"
38 39 40 41
    else
      flash[:alert] = "Could not delete chat nickname #{@chat_name.chat_name}."
    end

42
    redirect_to profile_chat_names_path, status: 302
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
  end

  private

  def delete_chat_name_token
    chat_name_token.delete
  end

  def chat_name_params
    @chat_name_params ||= chat_name_token.get || render_404
  end

  def chat_name_token
    return render_404 unless params[:token] || render_404

    @chat_name_token ||= Gitlab::ChatNameToken.new(params[:token])
  end

  def chat_names
    @chat_names ||= current_user.chat_names
  end
end