gitlab-org--gitlab-foss/app/controllers/projects/deploy_keys_controller.rb

92 lines
2.5 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
class Projects::DeployKeysController < Projects::ApplicationController
include RepositorySettingsRedirect
2012-01-17 17:46:13 -05:00
respond_to :html
# Authorize
before_action :authorize_admin_project!
before_action :authorize_update_deploy_key!, only: [:edit, :update]
2011-12-29 14:44:16 -05:00
layout 'project_settings'
2013-06-19 15:44:57 -04:00
2011-12-29 14:44:16 -05:00
def index
2017-03-24 06:11:36 -04:00
respond_to do |format|
format.html { redirect_to_repository_settings(@project, anchor: 'js-deploy-keys-settings') }
2017-03-24 06:11:36 -04:00
format.json do
render json: Projects::Settings::DeployKeysPresenter.new(@project, current_user: current_user).as_json
end
end
2011-12-29 14:44:16 -05:00
end
def new
redirect_to_repository_settings(@project, anchor: 'js-deploy-keys-settings')
2011-12-29 14:44:16 -05:00
end
def create
2017-09-15 11:35:24 -04:00
@key = DeployKeys::CreateService.new(current_user, create_params).execute
2013-05-06 08:10:55 -04:00
unless @key.valid?
2017-03-24 06:11:36 -04:00
flash[:alert] = @key.errors.full_messages.join(', ').html_safe
2012-01-17 18:00:40 -05:00
end
redirect_to_repository_settings(@project, anchor: 'js-deploy-keys-settings')
2011-12-29 14:44:16 -05:00
end
def edit
end
def update
2018-07-02 06:43:06 -04:00
if deploy_key.update(update_params)
flash[:notice] = 'Deploy key was successfully updated.'
redirect_to_repository_settings(@project, anchor: 'js-deploy-keys-settings')
else
render 'edit'
end
end
2013-05-06 08:10:55 -04:00
def enable
Projects::EnableDeployKeyService.new(@project, current_user, params).execute
2013-05-06 08:10:55 -04:00
respond_to do |format|
format.html { redirect_to_repository_settings(@project, anchor: 'js-deploy-keys-settings') }
format.json { head :ok }
end
2013-05-06 08:10:55 -04:00
end
# rubocop: disable CodeReuse/ActiveRecord
2013-05-06 08:10:55 -04:00
def disable
deploy_key_project = @project.deploy_keys_projects.find_by(deploy_key_id: params[:id])
return render_404 unless deploy_key_project
2013-05-06 08:10:55 -04:00
deploy_key_project.destroy!
respond_to do |format|
format.html { redirect_to_repository_settings(@project, anchor: 'js-deploy-keys-settings') }
format.json { head :ok }
end
2013-05-06 08:10:55 -04:00
end
# rubocop: enable CodeReuse/ActiveRecord
2013-05-06 08:10:55 -04:00
protected
def deploy_key
@deploy_key ||= DeployKey.find(params[:id])
end
def create_params
create_params = params.require(:deploy_key)
.permit(:key, :title, deploy_keys_projects_attributes: [:can_push])
create_params.dig(:deploy_keys_projects_attributes, '0')&.merge!(project_id: @project.id)
create_params
end
def update_params
params.require(:deploy_key).permit(:title, deploy_keys_projects_attributes: [:id, :can_push])
end
def authorize_update_deploy_key!
access_denied! unless can?(current_user, :update_deploy_key, deploy_key)
end
2011-12-29 14:44:16 -05:00
end