diff options
author | Alejandro RodrÃguez <alejorro70@gmail.com> | 2016-06-01 22:25:22 -0400 |
---|---|---|
committer | Alejandro RodrÃguez <alejorro70@gmail.com> | 2016-07-20 13:39:19 -0400 |
commit | 818ad89ea5f9ac3cf05506e7b81fd0c2fca5d9dc (patch) | |
tree | d76bcb35392778861ef253e8af002baa468cd3af /lib | |
parent | b4717017e7ad601eaa1d53c9238a242c7fdf0daa (diff) | |
download | gitlab-ce-818ad89ea5f9ac3cf05506e7b81fd0c2fca5d9dc.tar.gz |
Add /deploy_keys API to retrieve all deploy keys regardless of project affiliation
Also, in favour of consistency, deprecate `/projects/:id/keys/...` routes in favour of
`/projects/:id/deploy_keys/...`
Diffstat (limited to 'lib')
-rw-r--r-- | lib/api/deploy_keys.rb | 119 |
1 files changed, 66 insertions, 53 deletions
diff --git a/lib/api/deploy_keys.rb b/lib/api/deploy_keys.rb index 06eb7756841..63355e4968d 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 + return + 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 + return + 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 |