keys_controller.rb 1.36 KB
Newer Older
1
class Profiles::KeysController < Profiles::ApplicationController
2
  skip_before_action :authenticate_user!, only: [:get_keys]
gitlabhq's avatar
gitlabhq committed
3 4

  def index
5
    @keys = current_user.keys.order_id_desc
6
    @key = Key.new
gitlabhq's avatar
gitlabhq committed
7 8
  end

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

gitlabhq's avatar
gitlabhq committed
13
  def create
14
    @key = Keys::CreateService.new(current_user, key_params.merge(ip_address: request.remote_ip)).execute
gitlabhq's avatar
gitlabhq committed
15

haseeb's avatar
haseeb committed
16
    if @key.persisted?
17
      redirect_to profile_key_path(@key)
18
    else
19 20
      @keys = current_user.keys.select(&:persisted?)
      render :index
21
    end
gitlabhq's avatar
gitlabhq committed
22 23 24 25
  end

  def destroy
    @key = current_user.keys.find(params[:id])
26
    Keys::DestroyService.new(current_user).execute(@key)
gitlabhq's avatar
gitlabhq committed
27 28

    respond_to do |format|
Lin Jen-Shin's avatar
Lin Jen-Shin committed
29
      format.html { redirect_to profile_keys_url, status: :found }
30
      format.js { head :ok }
gitlabhq's avatar
gitlabhq committed
31 32
    end
  end
33

GitLab's avatar
GitLab committed
34 35
  # Get all keys of a user(params[:username]) in a text format
  # Helpful for sysadmins to put in respective servers
36 37 38 39
  def get_keys
    if params[:username].present?
      begin
        user = User.find_by_username(params[:username])
GitLab's avatar
GitLab committed
40
        if user.present?
41
          render text: user.all_ssh_keys.join("\n"), content_type: "text/plain"
GitLab's avatar
GitLab committed
42
        else
Douwe Maan's avatar
Douwe Maan committed
43
          return render_404
GitLab's avatar
GitLab committed
44
        end
45 46 47 48
      rescue => e
        render text: e.message
      end
    else
Douwe Maan's avatar
Douwe Maan committed
49
      return render_404
50 51 52
    end
  end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
53 54 55 56 57
  private

  def key_params
    params.require(:key).permit(:title, :key)
  end
gitlabhq's avatar
gitlabhq committed
58
end