keys_controller.rb 1.3 KB
Newer Older
1
class Profiles::KeysController < ApplicationController
G
gitlabhq 已提交
2
  layout "profile"
3
  skip_before_filter :authenticate_user!, only: [:get_keys]
G
gitlabhq 已提交
4 5

  def index
S
skv 已提交
6
    @keys = current_user.keys.order('id DESC')
G
gitlabhq 已提交
7 8
  end

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

G
gitlabhq 已提交
13 14 15 16 17
  def new
    @key = current_user.keys.new
  end

  def create
D
Dmitriy Zaporozhets 已提交
18
    @key = current_user.keys.new(key_params)
G
gitlabhq 已提交
19

20 21 22 23 24
    if @key.save
      redirect_to profile_key_path(@key)
    else
      render 'new'
    end
G
gitlabhq 已提交
25 26 27 28 29 30 31
  end

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

    respond_to do |format|
32
      format.html { redirect_to profile_keys_url }
33
      format.js { render nothing: true }
G
gitlabhq 已提交
34 35
    end
  end
36

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

D
Dmitriy Zaporozhets 已提交
56 57 58 59 60
  private

  def key_params
    params.require(:key).permit(:title, :key)
  end
G
gitlabhq 已提交
61
end