BigW Consortium Gitlab

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

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

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

  # Back-compat: We need to support this URL since git-annex webapp points to it
  def new
    redirect_to profile_keys_path
  end
17

gitlabhq committed
18
  def create
Dmitriy Zaporozhets committed
19
    @key = current_user.keys.new(key_params)
gitlabhq committed
20

21 22 23
    if @key.save
      redirect_to profile_key_path(@key)
    else
24 25
      @keys = current_user.keys.select(&:persisted?)
      render :index
26
    end
gitlabhq committed
27 28 29 30 31 32 33
  end

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

    respond_to do |format|
34
      format.html { redirect_to profile_keys_url }
35
      format.js { head :ok }
gitlabhq committed
36 37
    end
  end
38

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

Dmitriy Zaporozhets committed
58 59 60 61 62
  private

  def key_params
    params.require(:key).permit(:title, :key)
  end
gitlabhq committed
63
end