2018-12-05 23:21:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
class Profiles::KeysController < Profiles::ApplicationController
|
|
|
|
skip_before_action :authenticate_user!, only: [:get_keys]
|
2014-09-02 18:07:02 +05:30
|
|
|
|
|
|
|
def index
|
2018-03-17 18:26:18 +05:30
|
|
|
@keys = current_user.keys.order_id_desc
|
2016-06-02 11:05:42 +05:30
|
|
|
@key = Key.new
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
|
|
|
@key = current_user.keys.find(params[:id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2018-03-17 18:26:18 +05:30
|
|
|
@key = Keys::CreateService.new(current_user, key_params.merge(ip_address: request.remote_ip)).execute
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
if @key.persisted?
|
2014-09-02 18:07:02 +05:30
|
|
|
redirect_to profile_key_path(@key)
|
|
|
|
else
|
2016-06-02 11:05:42 +05:30
|
|
|
@keys = current_user.keys.select(&:persisted?)
|
|
|
|
render :index
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
@key = current_user.keys.find(params[:id])
|
2018-11-08 19:23:39 +05:30
|
|
|
Keys::DestroyService.new(current_user).execute(@key)
|
2014-09-02 18:07:02 +05:30
|
|
|
|
|
|
|
respond_to do |format|
|
2018-11-18 11:00:15 +05:30
|
|
|
format.html { redirect_to profile_keys_url, status: :found }
|
2016-06-02 11:05:42 +05:30
|
|
|
format.js { head :ok }
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Get all keys of a user(params[:username]) in a text format
|
|
|
|
# Helpful for sysadmins to put in respective servers
|
|
|
|
def get_keys
|
|
|
|
if params[:username].present?
|
|
|
|
begin
|
2018-12-13 13:39:08 +05:30
|
|
|
user = UserFinder.new(params[:username]).find_by_username
|
2014-09-02 18:07:02 +05:30
|
|
|
if user.present?
|
2019-02-15 15:39:39 +05:30
|
|
|
render plain: user.all_ssh_keys.join("\n")
|
2014-09-02 18:07:02 +05:30
|
|
|
else
|
2017-08-17 22:00:37 +05:30
|
|
|
return render_404
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
rescue => e
|
2019-02-15 15:39:39 +05:30
|
|
|
render html: e.message
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
else
|
2017-08-17 22:00:37 +05:30
|
|
|
return render_404
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def key_params
|
|
|
|
params.require(:key).permit(:title, :key)
|
|
|
|
end
|
|
|
|
end
|