summaryrefslogtreecommitdiff
path: root/app/controllers/profiles/keys_controller.rb
blob: 31c758ac76331724490826fde303caf850771417 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# frozen_string_literal: true

class Profiles::KeysController < Profiles::ApplicationController
  feature_category :user_profile
  urgency :low, [:create, :index]

  def index
    @keys = current_user.keys.order_id_desc
    @key = Key.new
  end

  def show
    @key = current_user.keys.find(params[:id])
  end

  def create
    @key = Keys::CreateService.new(current_user, key_params.merge(ip_address: request.remote_ip)).execute

    if @key.persisted?
      redirect_to profile_key_path(@key)
    else
      @keys = current_user.keys.select(&:persisted?)
      render :index
    end
  end

  def destroy
    @key = current_user.keys.find(params[:id])
    Keys::DestroyService.new(current_user).execute(@key)

    respond_to do |format|
      format.html { redirect_to profile_keys_url, status: :found }
      format.js { head :ok }
    end
  end

  def revoke
    @key = current_user.keys.find(params[:id])
    Keys::RevokeService.new(current_user).execute(@key)

    respond_to do |format|
      format.html { redirect_to profile_keys_url, status: :found }
      format.js { head :ok }
    end
  end

  private

  def key_params
    params.require(:key).permit(:title, :key, :usage_type, :expires_at)
  end
end