From 82829ed49e11a173275633cad63978e4ee07e927 Mon Sep 17 00:00:00 2001
From: Marin Jankovski <maxlazio@gmail.com>
Date: Tue, 30 Dec 2014 10:15:11 +0100
Subject: [PATCH] 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
-- 
GitLab