diff --git a/CHANGELOG b/CHANGELOG index 71547b387e..1c8f832265 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,4 +1,5 @@ v 6.6.0 + - Retrieving user ssh keys publically(github style): http://__HOST__/__USERNAME__.keys - Permissions: Developer now can manage issue tracker (modify any issue) - Improve Code Compare page performance - Group avatar diff --git a/app/controllers/profiles/keys_controller.rb b/app/controllers/profiles/keys_controller.rb index 541319e8d0..e8237a1f22 100644 --- a/app/controllers/profiles/keys_controller.rb +++ b/app/controllers/profiles/keys_controller.rb @@ -1,5 +1,6 @@ class Profiles::KeysController < ApplicationController layout "profile" + skip_before_filter :authenticate_user!, only: [:get_keys] def index @keys = current_user.keys.order('id DESC') @@ -32,4 +33,24 @@ class Profiles::KeysController < ApplicationController format.js { render nothing: true } 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 = User.find_by_username(params[:username]) + if user.present? + render text: user.all_ssh_keys.join('\n') + else + render_404 and return + end + rescue => e + render text: e.message + end + else + render_404 and return + end + end + end diff --git a/app/models/user.rb b/app/models/user.rb index 1acf330cab..10f21d2350 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -435,4 +435,8 @@ class User < ActiveRecord::Base def short_website_url website_url.gsub(/https?:\/\//, '') end + + def all_ssh_keys + keys.map(&:key) + end end diff --git a/config/routes.rb b/config/routes.rb index 77247163de..8c66ad741f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -12,6 +12,9 @@ Gitlab::Application.routes.draw do API::API.logger Rails.logger mount API::API => '/api' + # Get all keys of user + get ':username.keys' => 'profiles/keys#get_keys' , constraints: { username: /.*/ } + constraint = lambda { |request| request.env["warden"].authenticate? and request.env['warden'].user.admin? } constraints constraint do mount Sidekiq::Web, at: "/admin/sidekiq", as: :sidekiq diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 5e53ed09b5..fd8d7133ae 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -303,6 +303,17 @@ describe User do end end + describe 'all_ssh_keys' do + it { should have_many(:keys).dependent(:destroy) } + + it "should have all ssh keys" do + user = create :user + key = create :key, key: "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD33bWLBxu48Sev9Fert1yzEO4WGcWglWF7K/AwblIUFselOt/QdOL9DSjpQGxLagO1s9wl53STIO8qGS4Ms0EJZyIXOEFMjFJ5xmjSy+S37By4sG7SsltQEHMxtbtFOaW5LV2wCrX+rUsRNqLMamZjgjcPO0/EgGCXIGMAYW4O7cwGZdXWYIhQ1Vwy+CsVMDdPkPgBXqK7nR/ey8KMs8ho5fMNgB5hBw/AL9fNGhRw3QTD6Q12Nkhl4VZES2EsZqlpNnJttnPdp847DUsT6yuLRlfiQfz5Cn9ysHFdXObMN5VYIiPFwHeYCZp1X2S4fDZooRE8uOLTfxWHPXwrhqSH", user_id: user.id + + user.all_ssh_keys.should include(key.key) + end + end + describe :avatar_type do let(:user) { create(:user) } diff --git a/spec/routing/routing_spec.rb b/spec/routing/routing_spec.rb index bcbf37e2ee..8929a48973 100644 --- a/spec/routing/routing_spec.rb +++ b/spec/routing/routing_spec.rb @@ -176,6 +176,11 @@ describe Profiles::KeysController, "routing" do it "to #destroy" do delete("/profile/keys/1").should route_to('profiles/keys#destroy', id: '1') end + + # get all the ssh-keys of a user + it "to #get_keys" do + get("/foo.keys").should route_to('profiles/keys#get_keys', username: 'foo') + end end # profile_avatar DELETE /profile/avatar(.:format) profiles/avatars#destroy