Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorGabriel Mazetto <gabriel@gitlab.com>2016-07-20 21:28:24 +0300
committerRémy Coutable <remy@rymai.me>2016-07-21 12:19:44 +0300
commit9eb00bcda6e776638a249d491859d845364d9978 (patch)
treec6b71fd0a35ba52f16199b4106f0899300d06565 /lib
parenta751df572f12299d4eae4f8b73f054395579ae00 (diff)
Merge branch '14284-allow-all-deploy-keys-to-be-retrieved-via-api-regardless-of-project-affiliation' into 'master'
Add /deploy_keys API to retrieve all deploy keys regardless of project affiliation ## What does this MR do? Add /deploy_keys API to retrieve all deploy keys regardless of project affiliation ## Are there points in the code the reviewer needs to double check? Is documentation clear? ## Why was this MR needed? User request ## What are the relevant issue numbers? #14284 See merge request !4426
Diffstat (limited to 'lib')
-rw-r--r--lib/api/deploy_keys.rb119
1 files changed, 66 insertions, 53 deletions
diff --git a/lib/api/deploy_keys.rb b/lib/api/deploy_keys.rb
index 06eb7756841..5c570b5e5ca 100644
--- a/lib/api/deploy_keys.rb
+++ b/lib/api/deploy_keys.rb
@@ -2,74 +2,87 @@ module API
# Projects API
class DeployKeys < Grape::API
before { authenticate! }
- before { authorize_admin_project }
+
+ get "deploy_keys" do
+ authenticated_as_admin!
+
+ keys = DeployKey.all
+ present keys, with: Entities::SSHKey
+ end
resource :projects do
- # Get a specific project's keys
- #
- # Example Request:
- # GET /projects/:id/keys
- get ":id/keys" do
- present user_project.deploy_keys, with: Entities::SSHKey
- end
+ before { authorize_admin_project }
- # Get single key owned by currently authenticated user
+ # Routing "projects/:id/keys/..." is DEPRECATED and WILL BE REMOVED in version 9.0
+ # Use "projects/:id/deploy_keys/..." instead.
#
- # Example Request:
- # GET /projects/:id/keys/:id
- get ":id/keys/:key_id" do
- key = user_project.deploy_keys.find params[:key_id]
- present key, with: Entities::SSHKey
- end
+ %w(keys deploy_keys).each do |path|
+ # Get a specific project's deploy keys
+ #
+ # Example Request:
+ # GET /projects/:id/deploy_keys
+ get ":id/#{path}" do
+ present user_project.deploy_keys, with: Entities::SSHKey
+ end
- # Add new ssh key to currently authenticated user
- # If deploy key already exists - it will be joined to project
- # but only if original one was is accessible by same user
- #
- # Parameters:
- # key (required) - New SSH Key
- # title (required) - New SSH Key's title
- # Example Request:
- # POST /projects/:id/keys
- post ":id/keys" do
- attrs = attributes_for_keys [:title, :key]
+ # Get single deploy key owned by currently authenticated user
+ #
+ # Example Request:
+ # GET /projects/:id/deploy_keys/:key_id
+ get ":id/#{path}/:key_id" do
+ key = user_project.deploy_keys.find params[:key_id]
+ present key, with: Entities::SSHKey
+ end
- if attrs[:key].present?
- attrs[:key].strip!
+ # Add new deploy key to currently authenticated user
+ # If deploy key already exists - it will be joined to project
+ # but only if original one was accessible by same user
+ #
+ # Parameters:
+ # key (required) - New deploy Key
+ # title (required) - New deploy Key's title
+ # Example Request:
+ # POST /projects/:id/deploy_keys
+ post ":id/#{path}" do
+ attrs = attributes_for_keys [:title, :key]
- # check if key already exist in project
- key = user_project.deploy_keys.find_by(key: attrs[:key])
- if key
- present key, with: Entities::SSHKey
- return
+ if attrs[:key].present?
+ attrs[:key].strip!
+
+ # check if key already exist in project
+ key = user_project.deploy_keys.find_by(key: attrs[:key])
+ if key
+ present key, with: Entities::SSHKey
+ next
+ end
+
+ # Check for available deploy keys in other projects
+ key = current_user.accessible_deploy_keys.find_by(key: attrs[:key])
+ if key
+ user_project.deploy_keys << key
+ present key, with: Entities::SSHKey
+ next
+ end
end
- # Check for available deploy keys in other projects
- key = current_user.accessible_deploy_keys.find_by(key: attrs[:key])
- if key
- user_project.deploy_keys << key
+ key = DeployKey.new attrs
+
+ if key.valid? && user_project.deploy_keys << key
present key, with: Entities::SSHKey
- return
+ else
+ render_validation_error!(key)
end
end
- key = DeployKey.new attrs
-
- if key.valid? && user_project.deploy_keys << key
- present key, with: Entities::SSHKey
- else
- render_validation_error!(key)
+ # Delete existing deploy key of currently authenticated user
+ #
+ # Example Request:
+ # DELETE /projects/:id/deploy_keys/:key_id
+ delete ":id/#{path}/:key_id" do
+ key = user_project.deploy_keys.find params[:key_id]
+ key.destroy
end
end
-
- # Delete existed ssh key of currently authenticated user
- #
- # Example Request:
- # DELETE /projects/:id/keys/:id
- delete ":id/keys/:key_id" do
- key = user_project.deploy_keys.find params[:key_id]
- key.destroy
- end
end
end
end