From 492f3a477940daf425aabc9dd4a33e7a1e9092c1 Mon Sep 17 00:00:00 2001 From: Marin Jankovski Date: Mon, 29 Dec 2014 17:07:23 +0100 Subject: Add user key actions to admins. --- app/controllers/admin/users_controller.rb | 24 +++++++++++++++++++++++- app/views/admin/users/show.html.haml | 27 +++++++++++++++++++++++++++ config/routes.rb | 2 ++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index baad9095b70..b11a0b04687 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -11,6 +11,7 @@ class Admin::UsersController < Admin::ApplicationController def show @personal_projects = user.personal_projects @joined_projects = user.projects.joined(@user) + @ssh_keys = user.keys.order('id DESC') end def new @@ -107,6 +108,27 @@ class Admin::UsersController < Admin::ApplicationController end end + def show_key + @key = user.keys.find(params[:key_id]) + + respond_to do |format| + format.html { render 'key' } + format.js { render nothing: true } + end + end + + def remove_key + key = user.keys.find(params[:key_id]) + + respond_to do |format| + if key.destroy + format.html { redirect_to [:admin, user], notice: 'User key was successfully removed.' } + else + format.html { redirect_to [:admin, user], alert: 'Failed to remove user key.' } + end + end + end + protected def user @@ -118,7 +140,7 @@ class Admin::UsersController < Admin::ApplicationController :email, :remember_me, :bio, :name, :username, :skype, :linkedin, :twitter, :website_url, :color_scheme_id, :theme_id, :force_random_password, :extern_uid, :provider, :password_expires_at, :avatar, :hide_no_ssh_key, - :projects_limit, :can_create_group, :admin + :projects_limit, :can_create_group, :admin, :key_id ) end end diff --git a/app/views/admin/users/show.html.haml b/app/views/admin/users/show.html.haml index 29717aedd80..ef873fb2298 100644 --- a/app/views/admin/users/show.html.haml +++ b/app/views/admin/users/show.html.haml @@ -20,6 +20,8 @@ %a{"data-toggle" => "tab", href: "#groups"} Groups %li %a{"data-toggle" => "tab", href: "#projects"} Projects + %li + %a{"data-toggle" => "tab", href: "#ssh-keys"} SSH keys .tab-content #account.tab-pane.active @@ -217,3 +219,28 @@ - if tm.respond_to? :project = link_to project_team_member_path(project, @user), data: { confirm: remove_from_project_team_message(project, @user) }, remote: true, method: :delete, class: "btn-tiny btn btn-remove", title: 'Remove user from project' do %i.fa.fa-times + #ssh-keys.tab-pane + - if @ssh_keys.any? + .panel.panel-default + %table.table + %thead.panel-heading + %tr + %th Title + %th Fingerprint + %th + %tbody + - @ssh_keys.each do |key| + %tr + %td + = link_to user_key_admin_user_path(@user, key) do + %strong= key.title + %td + %span + (#{key.fingerprint}) + %span.cgray + added #{time_ago_with_tooltip(key.created_at)} + %td + = link_to 'Remove', remove_user_key_admin_user_path(@user, key), data: { confirm: 'Are you sure?'}, method: :delete, class: "btn btn-small btn-remove delete-key pull-right" + + - else + .nothing-here-block User has no ssh keys diff --git a/config/routes.rb b/config/routes.rb index 9b99f0643a7..80a509976a1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -84,6 +84,8 @@ Gitlab::Application.routes.draw do put :team_update put :block put :unblock + get 'key/:key_id', action: 'show_key', as: 'user_key' + delete 'key/:key_id', action: 'remove_key', as: 'remove_user_key' delete 'remove/:email_id', action: 'remove_email', as: 'remove_email' end end -- cgit v1.2.1 From f0085d034b33adc78753e2952a5e04842ca979e3 Mon Sep 17 00:00:00 2001 From: Marin Jankovski Date: Mon, 29 Dec 2014 17:09:39 +0100 Subject: Reuse show page for user keys. --- app/views/admin/users/key.html.haml | 4 ++++ app/views/profiles/keys/_key_details.html.haml | 19 +++++++++++++++++++ app/views/profiles/keys/show.html.haml | 20 +------------------- 3 files changed, 24 insertions(+), 19 deletions(-) create mode 100644 app/views/admin/users/key.html.haml create mode 100644 app/views/profiles/keys/_key_details.html.haml diff --git a/app/views/admin/users/key.html.haml b/app/views/admin/users/key.html.haml new file mode 100644 index 00000000000..c2b6ffc1fa8 --- /dev/null +++ b/app/views/admin/users/key.html.haml @@ -0,0 +1,4 @@ += render "profiles/keys/key_details" + +.pull-right + = link_to 'Remove', remove_user_key_admin_user_path(@user, @key), data: {confirm: 'Are you sure?'}, method: :delete, class: "btn btn-remove delete-key" diff --git a/app/views/profiles/keys/_key_details.html.haml b/app/views/profiles/keys/_key_details.html.haml new file mode 100644 index 00000000000..b7e0029a8ac --- /dev/null +++ b/app/views/profiles/keys/_key_details.html.haml @@ -0,0 +1,19 @@ +.row + .col-md-4 + .panel.panel-default + .panel-heading + SSH Key + %ul.well-list + %li + %span.light Title: + %strong= @key.title + %li + %span.light Created on: + %strong= @key.created_at.stamp("Aug 21, 2011") + + .col-md-8 + %p + %span.light Fingerprint: + %strong= @key.fingerprint + %pre.well-pre + = @key.key diff --git a/app/views/profiles/keys/show.html.haml b/app/views/profiles/keys/show.html.haml index c4fc1bb269c..470b984d16c 100644 --- a/app/views/profiles/keys/show.html.haml +++ b/app/views/profiles/keys/show.html.haml @@ -1,22 +1,4 @@ -.row - .col-md-4 - .panel.panel-default - .panel-heading - SSH Key - %ul.well-list - %li - %span.light Title: - %strong= @key.title - %li - %span.light Created on: - %strong= @key.created_at.stamp("Aug 21, 2011") - - .col-md-8 - %p - %span.light Fingerprint: - %strong= @key.fingerprint - %pre.well-pre - = @key.key += render "key_details" .pull-right = link_to 'Remove', profile_key_path(@key), data: {confirm: 'Are you sure?'}, method: :delete, class: "btn btn-remove delete-key" -- cgit v1.2.1 From c1e57b47b81db4b3959b0ce63d7f65cb6cdf6f57 Mon Sep 17 00:00:00 2001 From: Marin Jankovski Date: Mon, 29 Dec 2014 18:40:25 +0100 Subject: Add feature spec for user ssh keys on admin page. --- features/admin/users.feature | 10 ++++++++++ features/steps/admin/users.rb | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/features/admin/users.feature b/features/admin/users.feature index 278f6a43e94..1a8720dd77e 100644 --- a/features/admin/users.feature +++ b/features/admin/users.feature @@ -35,3 +35,13 @@ Feature: Admin Users And I see the secondary email When I click remove secondary email Then I should not see secondary email anymore + + Scenario: Show user keys + Given user "Pete" with ssh keys + And I visit admin users page + And click on user "Pete" + Then I should see key list + And I click on the key title + Then I should see key details + And I click on remove key + Then I should see the key removed diff --git a/features/steps/admin/users.rb b/features/steps/admin/users.rb index 546c1bf2a12..e1383097248 100644 --- a/features/steps/admin/users.rb +++ b/features/steps/admin/users.rb @@ -82,4 +82,36 @@ class Spinach::Features::AdminUsers < Spinach::FeatureSteps page.should have_content 'Account' page.should have_content 'Personal projects limit' end + + step 'user "Pete" with ssh keys' do + user = create(:user, name: 'Pete') + create(:key, user: user, title: "ssh-rsa Key1", key: "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC4FIEBXGi4bPU8kzxMefudPIJ08/gNprdNTaO9BR/ndy3+58s2HCTw2xCHcsuBmq+TsAqgEidVq4skpqoTMB+Uot5Uzp9z4764rc48dZiI661izoREoKnuRQSsRqUTHg5wrLzwxlQbl1MVfRWQpqiz/5KjBC7yLEb9AbusjnWBk8wvC1bQPQ1uLAauEA7d836tgaIsym9BrLsMVnR4P1boWD3Xp1B1T/ImJwAGHvRmP/ycIqmKdSpMdJXwxcb40efWVj0Ibbe7ii9eeoLdHACqevUZi6fwfbymdow+FeqlkPoHyGg3Cu4vD/D8+8cRc7mE/zGCWcQ15Var83Tczour Key1") + create(:key, user: user, title: "ssh-rsa Key2", key: "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDQSTWXhJAX/He+nG78MiRRRn7m0Pb0XbcgTxE0etArgoFoh9WtvDf36HG6tOSg/0UUNcp0dICsNAmhBKdncp6cIyPaXJTURPRAGvhI0/VDk4bi27bRnccGbJ/hDaUxZMLhhrzY0r22mjVf8PF6dvv5QUIQVm1/LeaWYsHHvLgiIjwrXirUZPnFrZw6VLREoBKG8uWvfSXw1L5eapmstqfsME8099oi+vWLR8MgEysZQmD28M73fgW4zek6LDQzKQyJx9nB+hJkKUDvcuziZjGmRFlNgSA2mguERwL1OXonD8WYUrBDGKroIvBT39zS5d9tQDnidEJZ9Y8gv5ViYP7x Key2") + end + + step 'click on user "Pete"' do + click_link 'Pete' + end + + step 'I should see key list' do + page.should have_content 'ssh-rsa Key2' + page.should have_content 'ssh-rsa Key1' + end + + step 'I click on the key title' do + click_link 'ssh-rsa Key2' + end + + step 'I should see key details' do + page.should have_content 'ssh-rsa Key2' + page.should have_content 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDQSTWXhJAX/He+nG78MiRRRn7m0Pb0XbcgTxE0etArgoFoh9WtvDf36HG6tOSg/0UUNcp0dICsNAmhBKdncp6cIyPaXJTURPRAGvhI0/VDk4bi27bRnccGbJ/hDaUxZMLhhrzY0r22mjVf8PF6dvv5QUIQVm1/LeaWYsHHvLgiIjwrXirUZPnFrZw6VLREoBKG8uWvfSXw1L5eapmstqfsME8099oi+vWLR8MgEysZQmD28M73fgW4zek6LDQzKQyJx9nB+hJkKUDvcuziZjGmRFlNgSA2mguERwL1OXonD8WYUrBDGKroIvBT39zS5d9tQDnidEJZ9Y8gv5ViYP7x Key2' + end + + step 'I click on remove key' do + click_link 'Remove' + end + + step 'I should see the key removed' do + page.should_not have_content 'ssh-rsa Key2' + end end -- cgit v1.2.1 From 82829ed49e11a173275633cad63978e4ee07e927 Mon Sep 17 00:00:00 2001 From: Marin Jankovski Date: Tue, 30 Dec 2014 10:15:11 +0100 Subject: Move user key manipulation in admin section to a separate controller. --- app/controllers/admin/keys_controller.rb | 34 +++++++++++++++++++++++++++++++ app/controllers/admin/users_controller.rb | 21 ------------------- app/views/admin/keys/show.html.haml | 4 ++++ app/views/admin/users/key.html.haml | 4 ---- app/views/admin/users/show.html.haml | 4 ++-- config/routes.rb | 3 +-- 6 files changed, 41 insertions(+), 29 deletions(-) create mode 100644 app/controllers/admin/keys_controller.rb create mode 100644 app/views/admin/keys/show.html.haml delete mode 100644 app/views/admin/users/key.html.haml diff --git a/app/controllers/admin/keys_controller.rb b/app/controllers/admin/keys_controller.rb new file mode 100644 index 00000000000..21111bb44f5 --- /dev/null +++ b/app/controllers/admin/keys_controller.rb @@ -0,0 +1,34 @@ +class Admin::KeysController < Admin::ApplicationController + before_filter :user, only: [:show, :destroy] + + def show + @key = user.keys.find(params[:id]) + + respond_to do |format| + format.html + format.js { render nothing: true } + end + end + + def destroy + key = user.keys.find(params[:id]) + + respond_to do |format| + if key.destroy + format.html { redirect_to [:admin, user], notice: 'User key was successfully removed.' } + else + format.html { redirect_to [:admin, user], alert: 'Failed to remove user key.' } + end + end + end + + protected + + def user + @user ||= User.find_by!(username: params[:user_id]) + end + + def key_params + params.require(:user_id, :id) + end +end diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index b11a0b04687..86c671ed756 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -108,27 +108,6 @@ class Admin::UsersController < Admin::ApplicationController end end - def show_key - @key = user.keys.find(params[:key_id]) - - respond_to do |format| - format.html { render 'key' } - format.js { render nothing: true } - end - end - - def remove_key - key = user.keys.find(params[:key_id]) - - respond_to do |format| - if key.destroy - format.html { redirect_to [:admin, user], notice: 'User key was successfully removed.' } - else - format.html { redirect_to [:admin, user], alert: 'Failed to remove user key.' } - end - end - end - protected def user diff --git a/app/views/admin/keys/show.html.haml b/app/views/admin/keys/show.html.haml new file mode 100644 index 00000000000..2ea05b6aa00 --- /dev/null +++ b/app/views/admin/keys/show.html.haml @@ -0,0 +1,4 @@ += render "profiles/keys/key_details" + +.pull-right + = link_to 'Remove', admin_user_key_path(@user, @key), data: {confirm: 'Are you sure?'}, method: :delete, class: "btn btn-remove delete-key" diff --git a/app/views/admin/users/key.html.haml b/app/views/admin/users/key.html.haml deleted file mode 100644 index c2b6ffc1fa8..00000000000 --- a/app/views/admin/users/key.html.haml +++ /dev/null @@ -1,4 +0,0 @@ -= render "profiles/keys/key_details" - -.pull-right - = link_to 'Remove', remove_user_key_admin_user_path(@user, @key), data: {confirm: 'Are you sure?'}, method: :delete, class: "btn btn-remove delete-key" diff --git a/app/views/admin/users/show.html.haml b/app/views/admin/users/show.html.haml index ef873fb2298..5754f9448d7 100644 --- a/app/views/admin/users/show.html.haml +++ b/app/views/admin/users/show.html.haml @@ -232,7 +232,7 @@ - @ssh_keys.each do |key| %tr %td - = link_to user_key_admin_user_path(@user, key) do + = link_to admin_user_key_path(@user, key) do %strong= key.title %td %span @@ -240,7 +240,7 @@ %span.cgray added #{time_ago_with_tooltip(key.created_at)} %td - = link_to 'Remove', remove_user_key_admin_user_path(@user, key), data: { confirm: 'Are you sure?'}, method: :delete, class: "btn btn-small btn-remove delete-key pull-right" + = link_to 'Remove', admin_user_key_path(@user, key), data: { confirm: 'Are you sure?'}, method: :delete, class: "btn btn-small btn-remove delete-key pull-right" - else .nothing-here-block User has no ssh keys diff --git a/config/routes.rb b/config/routes.rb index 80a509976a1..a77352a5b0f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -80,12 +80,11 @@ Gitlab::Application.routes.draw do # namespace :admin do resources :users, constraints: { id: /[a-zA-Z.\/0-9_\-]+/ } do + resources :keys, only: [:show, :destroy] member do put :team_update put :block put :unblock - get 'key/:key_id', action: 'show_key', as: 'user_key' - delete 'key/:key_id', action: 'remove_key', as: 'remove_user_key' delete 'remove/:email_id', action: 'remove_email', as: 'remove_email' end end -- cgit v1.2.1 From 607ea7c6e5663542ae53de66a80f3e8beefe1341 Mon Sep 17 00:00:00 2001 From: Marin Jankovski Date: Tue, 30 Dec 2014 11:01:30 +0100 Subject: Share the key table between admin and profile resources. --- app/controllers/admin/users_controller.rb | 2 +- app/helpers/application_helper.rb | 8 ++++++++ app/views/admin/keys/show.html.haml | 5 +---- app/views/admin/users/show.html.haml | 25 +------------------------ app/views/profiles/keys/_key.html.haml | 21 ++++++++++++--------- app/views/profiles/keys/_key_details.html.haml | 3 +++ app/views/profiles/keys/_key_table.html.haml | 19 +++++++++++++++++++ app/views/profiles/keys/index.html.haml | 14 ++------------ app/views/profiles/keys/show.html.haml | 3 --- 9 files changed, 47 insertions(+), 53 deletions(-) create mode 100644 app/views/profiles/keys/_key_table.html.haml diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index 86c671ed756..aea8545d38e 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -11,7 +11,7 @@ class Admin::UsersController < Admin::ApplicationController def show @personal_projects = user.personal_projects @joined_projects = user.projects.joined(@user) - @ssh_keys = user.keys.order('id DESC') + @keys = user.keys.order('id DESC') end def new diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 54caaa0f7e5..092a1ba9229 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -297,4 +297,12 @@ module ApplicationHelper def outdated_browser? browser.ie? && browser.version.to_i < 10 end + + def path_to_key(key, admin = false) + if admin + admin_user_key_path(@user, key) + else + profile_key_path(key) + end + end end diff --git a/app/views/admin/keys/show.html.haml b/app/views/admin/keys/show.html.haml index 2ea05b6aa00..5b23027b3ab 100644 --- a/app/views/admin/keys/show.html.haml +++ b/app/views/admin/keys/show.html.haml @@ -1,4 +1 @@ -= render "profiles/keys/key_details" - -.pull-right - = link_to 'Remove', admin_user_key_path(@user, @key), data: {confirm: 'Are you sure?'}, method: :delete, class: "btn btn-remove delete-key" += render "profiles/keys/key_details", admin: true diff --git a/app/views/admin/users/show.html.haml b/app/views/admin/users/show.html.haml index 5754f9448d7..88e71aa170f 100644 --- a/app/views/admin/users/show.html.haml +++ b/app/views/admin/users/show.html.haml @@ -220,27 +220,4 @@ = link_to project_team_member_path(project, @user), data: { confirm: remove_from_project_team_message(project, @user) }, remote: true, method: :delete, class: "btn-tiny btn btn-remove", title: 'Remove user from project' do %i.fa.fa-times #ssh-keys.tab-pane - - if @ssh_keys.any? - .panel.panel-default - %table.table - %thead.panel-heading - %tr - %th Title - %th Fingerprint - %th - %tbody - - @ssh_keys.each do |key| - %tr - %td - = link_to admin_user_key_path(@user, key) do - %strong= key.title - %td - %span - (#{key.fingerprint}) - %span.cgray - added #{time_ago_with_tooltip(key.created_at)} - %td - = link_to 'Remove', admin_user_key_path(@user, key), data: { confirm: 'Are you sure?'}, method: :delete, class: "btn btn-small btn-remove delete-key pull-right" - - - else - .nothing-here-block User has no ssh keys + = render 'profiles/keys/key_table', admin: true diff --git a/app/views/profiles/keys/_key.html.haml b/app/views/profiles/keys/_key.html.haml index 81411a7565e..8892302e25d 100644 --- a/app/views/profiles/keys/_key.html.haml +++ b/app/views/profiles/keys/_key.html.haml @@ -1,9 +1,12 @@ -%li - = link_to profile_key_path(key) do - %strong= key.title - %span - (#{key.fingerprint}) - %span.cgray - added #{time_ago_with_tooltip(key.created_at)} - - = link_to 'Remove', profile_key_path(key), data: { confirm: 'Are you sure?'}, method: :delete, class: "btn btn-small btn-remove delete-key pull-right" +%tr + %td + = link_to path_to_key(key, is_admin) do + %strong= key.title + %td + %span + (#{key.fingerprint}) + %td + %span.cgray + added #{time_ago_with_tooltip(key.created_at)} + %td + = link_to 'Remove', path_to_key(key, is_admin), data: { confirm: 'Are you sure?'}, method: :delete, class: "btn btn-small btn-remove delete-key pull-right" diff --git a/app/views/profiles/keys/_key_details.html.haml b/app/views/profiles/keys/_key_details.html.haml index b7e0029a8ac..8bac22a2e1a 100644 --- a/app/views/profiles/keys/_key_details.html.haml +++ b/app/views/profiles/keys/_key_details.html.haml @@ -1,3 +1,4 @@ +- is_admin = defined?(admin) ? true : false .row .col-md-4 .panel.panel-default @@ -17,3 +18,5 @@ %strong= @key.fingerprint %pre.well-pre = @key.key + .pull-right + = link_to 'Remove', path_to_key(@key, is_admin), data: {confirm: 'Are you sure?'}, method: :delete, class: "btn btn-remove delete-key" diff --git a/app/views/profiles/keys/_key_table.html.haml b/app/views/profiles/keys/_key_table.html.haml new file mode 100644 index 00000000000..ef0075aad3b --- /dev/null +++ b/app/views/profiles/keys/_key_table.html.haml @@ -0,0 +1,19 @@ +- is_admin = defined?(admin) ? true : false +.panel.panel-default + - if @keys.any? + %table.table + %thead.panel-heading + %tr + %th Title + %th Fingerprint + %th Added at + %th + %tbody + - @keys.each do |key| + = render 'profiles/keys/key', key: key, is_admin: is_admin + - else + .nothing-here-block + - if is_admin + User has no ssh keys + - else + There are no SSH keys with access to your account. diff --git a/app/views/profiles/keys/index.html.haml b/app/views/profiles/keys/index.html.haml index a322f82f236..809953960bb 100644 --- a/app/views/profiles/keys/index.html.haml +++ b/app/views/profiles/keys/index.html.haml @@ -1,5 +1,5 @@ %h3.page-title - My SSH keys + My SSH keys (#{@keys.count}) .pull-right = link_to "Add SSH Key", new_profile_key_path, class: "btn btn-new" %p.light @@ -9,14 +9,4 @@ = link_to "generate it", help_page_path("ssh", "ssh") %hr - -.panel.panel-default - .panel-heading - SSH Keys (#{@keys.count}) - %ul.well-list#keys-table - = render @keys - - if @keys.blank? - %li - .nothing-here-block There are no SSH keys with access to your account. - - += render 'key_table' diff --git a/app/views/profiles/keys/show.html.haml b/app/views/profiles/keys/show.html.haml index 470b984d16c..cfd53298962 100644 --- a/app/views/profiles/keys/show.html.haml +++ b/app/views/profiles/keys/show.html.haml @@ -1,4 +1 @@ = render "key_details" - -.pull-right - = link_to 'Remove', profile_key_path(@key), data: {confirm: 'Are you sure?'}, method: :delete, class: "btn btn-remove delete-key" -- cgit v1.2.1 From eb29648bf4a9b2ad960a582eba0a51f088afa78f Mon Sep 17 00:00:00 2001 From: Marin Jankovski Date: Tue, 30 Dec 2014 11:47:13 +0100 Subject: Fix the ssh keys test. --- features/steps/profile/ssh_keys.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/features/steps/profile/ssh_keys.rb b/features/steps/profile/ssh_keys.rb index d1e87d40705..ea912e5b4da 100644 --- a/features/steps/profile/ssh_keys.rb +++ b/features/steps/profile/ssh_keys.rb @@ -37,9 +37,7 @@ class Spinach::Features::ProfileSshKeys < Spinach::FeatureSteps end step 'I should not see "Work" ssh key' do - within "#keys-table" do - page.should_not have_content "Work" - end + page.should_not have_content "Work" end step 'I have ssh key "ssh-rsa Work"' do -- cgit v1.2.1