gitlab-org--gitlab-foss/app/controllers/profiles/keys_controller.rb

63 lines
1.4 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2015-04-30 13:06:18 -04:00
class Profiles::KeysController < Profiles::ApplicationController
skip_before_action :authenticate_user!, only: [:get_keys]
feature_category :users
2011-10-08 17:36:38 -04:00
def index
2017-08-15 06:27:37 -04:00
@keys = current_user.keys.order_id_desc
@key = Key.new
2011-10-08 17:36:38 -04:00
end
def show
@key = current_user.keys.find(params[:id])
end
2011-10-08 17:36:38 -04:00
def create
2017-10-24 02:52:47 -04:00
@key = Keys::CreateService.new(current_user, key_params.merge(ip_address: request.remote_ip)).execute
2011-10-08 17:36:38 -04:00
2017-09-15 11:35:24 -04:00
if @key.persisted?
2017-10-24 02:52:47 -04:00
redirect_to profile_key_path(@key)
else
@keys = current_user.keys.select(&:persisted?)
render :index
end
2011-10-08 17:36:38 -04:00
end
def destroy
@key = current_user.keys.find(params[:id])
Keys::DestroyService.new(current_user).execute(@key)
2011-10-08 17:36:38 -04:00
respond_to do |format|
2018-07-02 06:43:06 -04:00
format.html { redirect_to profile_keys_url, status: :found }
format.js { head :ok }
2011-10-08 17:36:38 -04:00
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
user = UserFinder.new(params[:username]).find_by_username
if user.present?
render plain: user.all_ssh_keys.join("\n")
else
render_404
end
rescue => e
render html: e.message
end
else
render_404
end
end
private
def key_params
params.require(:key).permit(:title, :key, :expires_at)
end
2011-10-08 17:36:38 -04:00
end