From 6af31383782f707fbff85f27b61dad9d7bc9c6b5 Mon Sep 17 00:00:00 2001
From: Alex Shulgin <ash@commandprompt.com>
Date: Wed, 30 May 2012 07:38:33 -0700
Subject: [PATCH] Confirm admin flag change on a separate page.

Having admin flag on the general tab in user's profile makes it
way too easy to flip the flag accidentally.  We show the flag status
using disabled check box and have an 'Edit' link nearby which points
to the confirmation page where only the admin flag can be changed.
---
 app/controllers/users_controller.rb |   26 +++++++++++++++++++++++---
 app/views/users/_form.html.erb      |    5 ++++-
 app/views/users/edit_admin.html.erb |   13 +++++++++++++
 config/locales/en.yml               |    1 +
 config/routes.rb                    |    4 +++-
 5 files changed, 44 insertions(+), 5 deletions(-)
 create mode 100644 app/views/users/edit_admin.html.erb

diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb
index c153adf..f509f47 100644
--- a/app/controllers/users_controller.rb
+++ b/app/controllers/users_controller.rb
@@ -19,7 +19,8 @@ class UsersController < ApplicationController
   layout 'admin'
 
   before_filter :require_admin, :except => :show
-  before_filter :find_user, :only => [:show, :edit, :update, :destroy, :edit_membership, :destroy_membership]
+  before_filter :find_user, :only => [:show, :edit, :update, :destroy, :edit_membership, :destroy_membership, :edit_admin, :update_admin]
+  before_filter :check_self, :only => [:edit_admin, :update_admin]
   accept_api_auth :index, :show, :create, :update, :destroy
 
   helper :sort
@@ -96,7 +97,7 @@ class UsersController < ApplicationController
   def create
     @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option)
     @user.safe_attributes = params[:user]
-    @user.admin = params[:user][:admin] || false
+    @user.admin = false
     @user.login = params[:user][:login]
     @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation] unless @user.auth_source_id
 
@@ -139,7 +140,6 @@ class UsersController < ApplicationController
 
   verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
   def update
-    @user.admin = params[:user][:admin] if params[:user][:admin]
     @user.login = params[:user][:login] if params[:user][:login]
     if params[:user][:password].present? && (@user.auth_source_id.nil? || params[:user][:auth_source_id].blank?)
       @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation]
@@ -225,6 +225,21 @@ class UsersController < ApplicationController
     end
   end
 
+  def edit_admin
+  end
+
+  def update_admin
+    @user.admin = params[:admin] if params[:admin]
+    if @user.save
+      respond_to do |format|
+        format.html {
+          flash[:notice] = l(:notice_successful_update)
+          redirect_to :controller => 'users', :action => 'edit', :id => @user
+        }
+      end
+    end
+  end
+
   private
 
   def find_user
@@ -237,4 +252,9 @@ class UsersController < ApplicationController
   rescue ActiveRecord::RecordNotFound
     render_404
   end
+
+  def check_self
+    # prevent user from unsetting admin flag on himself
+    redirect_to(:controller => 'users', :action => 'edit', :id => @user) if @user == User.current
+  end
 end
diff --git a/app/views/users/_form.html.erb b/app/views/users/_form.html.erb
index 72367fe..d82d75e 100644
--- a/app/views/users/_form.html.erb
+++ b/app/views/users/_form.html.erb
@@ -18,7 +18,10 @@
     <p><%= custom_field_tag_with_label :user, value %></p>
   <% end %>
 
-  <p><%= f.check_box :admin, :disabled => (@user == User.current) %></p>
+  <p>
+   <%= f.check_box :admin, :disabled => true %>
+   <%= link_to(l(:button_edit), edit_admin_user_path(@user)) unless @user == User.current %>
+  </p>
   <%= call_hook(:view_users_form, :user => @user, :form => f) %>
 </fieldset>
 
diff --git a/app/views/users/edit_admin.html.erb b/app/views/users/edit_admin.html.erb
new file mode 100644
index 0000000..142d112
--- /dev/null
+++ b/app/views/users/edit_admin.html.erb
@@ -0,0 +1,13 @@
+<h2><%=l(:label_confirmation)%></h2>
+<div class="warning">
+<p><strong><%=h @user.login %></strong><br />
+<%=l(:text_user_admin_flag_confirmation)%>
+
+<p>
+ <% form_tag(update_admin_user_path(@user), :method => :post) do %>
+   <%= hidden_field_tag 'admin', '0' %>
+   <label><%= check_box_tag 'admin', 1, @user.admin? %> <%= l(:field_admin) %></label>
+   <%= submit_tag l(:button_save) %>
+ <% end %>
+</p>
+</div>
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 4816971..77542eb 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -961,6 +961,7 @@ en:
   text_scm_command_version: Version
   text_scm_config: You can configure your scm commands in config/configuration.yml. Please restart the application after editing it.
   text_scm_command_not_available: Scm command is not available. Please check settings on the administration panel.
+  text_user_admin_flag_confirmation: "You can change the administrator flag for this user below:"
 
   default_role_manager: Manager
   default_role_developer: Developer
diff --git a/config/routes.rb b/config/routes.rb
index 5f3ab4a..1386043 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -132,7 +132,9 @@ ActionController::Routing::Routes.draw do |map|
 
   map.resources :users, :member => {
     :edit_membership => :post,
-    :destroy_membership => :post
+    :destroy_membership => :post,
+    :edit_admin => :get,
+    :update_admin => :post
   }
 
   # For nice "roadmap" in the url for the index action
-- 
1.7.0.4

