deploy_keys_controller.rb 2.01 KB
Newer Older
1
class Projects::DeployKeysController < Projects::ApplicationController
2
  include RepositorySettingsRedirect
3
  respond_to :html
4 5

  # Authorize
6
  before_action :authorize_admin_project!
7
  before_action :authorize_update_deploy_key!, only: [:edit, :update]
miks's avatar
miks committed
8

9
  layout 'project_settings'
10

miks's avatar
miks committed
11
  def index
12 13 14 15 16 17
    respond_to do |format|
      format.html { redirect_to_repository_settings(@project) }
      format.json do
        render json: Projects::Settings::DeployKeysPresenter.new(@project, current_user: current_user).as_json
      end
    end
miks's avatar
miks committed
18 19 20
  end

  def new
21
    redirect_to_repository_settings(@project)
miks's avatar
miks committed
22 23 24
  end

  def create
haseeb's avatar
haseeb committed
25
    @key = DeployKeys::CreateService.new(current_user, create_params).execute
26

27
    unless @key.valid? && @project.deploy_keys << @key
28
      flash[:alert] = @key.errors.full_messages.join(', ').html_safe
29
    end
30
    redirect_to_repository_settings(@project)
miks's avatar
miks committed
31 32
  end

33 34 35 36 37 38 39 40 41 42 43 44
  def edit
  end

  def update
    if deploy_key.update_attributes(update_params)
      flash[:notice] = 'Deploy key was successfully updated.'
      redirect_to_repository_settings(@project)
    else
      render 'edit'
    end
  end

45
  def enable
46
    Projects::EnableDeployKeyService.new(@project, current_user, params).execute
47

48 49 50 51
    respond_to do |format|
      format.html { redirect_to_repository_settings(@project) }
      format.json { head :ok }
    end
52 53 54
  end

  def disable
55 56
    deploy_key_project = @project.deploy_keys_projects.find_by(deploy_key_id: params[:id])
    return render_404 unless deploy_key_project
57

58
    deploy_key_project.destroy!
59 60 61 62 63

    respond_to do |format|
      format.html { redirect_to_repository_settings(@project) }
      format.json { head :ok }
    end
64 65 66 67
  end

  protected

68
  def deploy_key
69
    @deploy_key ||= DeployKey.find(params[:id])
70 71 72
  end

  def create_params
73
    params.require(:deploy_key).permit(:key, :title, :can_push)
74
  end
75 76 77 78 79 80 81 82

  def update_params
    params.require(:deploy_key).permit(:title, :can_push)
  end

  def authorize_update_deploy_key!
    access_denied! unless can?(current_user, :update_deploy_key, deploy_key)
  end
miks's avatar
miks committed
83
end