From de7012c4fbb998c3599213f9eba62ac8192d2a68 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jaakko=20Kantoj=C3=A4rvi?= <jaakko@n-1.fi>
Date: Wed, 30 Jan 2013 22:14:34 +0200
Subject: [PATCH 1/2] Add option to disable username changing

---
 app/controllers/profiles_controller.rb |  4 ++-
 app/views/profiles/account.html.haml   | 47 +++++++++++++-------------
 config/gitlab.yml.example              |  1 +
 config/initializers/1_settings.rb      |  1 +
 4 files changed, 29 insertions(+), 24 deletions(-)

diff --git a/app/controllers/profiles_controller.rb b/app/controllers/profiles_controller.rb
index 1d1efb16f04..1d1850cfedc 100644
--- a/app/controllers/profiles_controller.rb
+++ b/app/controllers/profiles_controller.rb
@@ -51,7 +51,9 @@ class ProfilesController < ApplicationController
   end
 
   def update_username
-    @user.update_attributes(username: params[:user][:username])
+    if Gitlab.config.gitlab.username_changing_enabled
+      @user.update_attributes(username: params[:user][:username])
+    end
 
     respond_to do |format|
       format.js
diff --git a/app/views/profiles/account.html.haml b/app/views/profiles/account.html.haml
index 522e45e637a..b1c02d3ae92 100644
--- a/app/views/profiles/account.html.haml
+++ b/app/views/profiles/account.html.haml
@@ -53,28 +53,29 @@
 
 
 
-%fieldset.update-username
-  %legend
-    Username
-    %small.cred.right
-      Changing your username can have unintended side effects!
-  = form_for @user, url: update_username_profile_path,  method: :put, remote: true do |f|
-    .padded
-      = f.label :username
-      .input
-        = f.text_field :username, required: true
-        &nbsp;
-        %span.loading-gif.hide= image_tag "ajax_loader.gif"
-        %span.update-success.cgreen.hide
-          %i.icon-ok
-          Saved
-        %span.update-failed.cred.hide
-          %i.icon-remove
-          Failed
-        %ul.cred
-          %li It will change web url for personal projects.
-          %li It will change the git path to repositories for personal projects.
-      .input
-        = f.submit 'Save username', class: "btn save-btn"
+- if Gitlab.config.gitlab.username_changing_enabled
+  %fieldset.update-username
+    %legend
+      Username
+      %small.cred.right
+        Changing your username can have unintended side effects!
+    = form_for @user, url: update_username_profile_path,  method: :put, remote: true do |f|
+      .padded
+        = f.label :username
+        .input
+          = f.text_field :username, required: true
+          &nbsp;
+          %span.loading-gif.hide= image_tag "ajax_loader.gif"
+          %span.update-success.cgreen.hide
+            %i.icon-ok
+            Saved
+          %span.update-failed.cred.hide
+            %i.icon-remove
+            Failed
+          %ul.cred
+            %li It will change web url for personal projects.
+            %li It will change the git path to repositories for personal projects.
+        .input
+          = f.submit 'Save username', class: "btn save-btn"
 
 
diff --git a/config/gitlab.yml.example b/config/gitlab.yml.example
index b2dccbe3d4c..4b2f69ab5fe 100644
--- a/config/gitlab.yml.example
+++ b/config/gitlab.yml.example
@@ -35,6 +35,7 @@ gitlab:
   ## Project settings
   default_projects_limit: 10
   # signup_enabled: true          # default: false - Account passwords are not sent via the email if signup is enabled.
+  # username_changing_enabled: false # default: true - User can change her username/namespace
 
 ## Gravatar
 gravatar:
diff --git a/config/initializers/1_settings.rb b/config/initializers/1_settings.rb
index a1afa5b22c4..0334b35ec91 100644
--- a/config/initializers/1_settings.rb
+++ b/config/initializers/1_settings.rb
@@ -53,6 +53,7 @@ Settings.gitlab['support_email']  ||= Settings.gitlab.email_from
 Settings.gitlab['url']        ||= Settings.send(:build_gitlab_url)
 Settings.gitlab['user']       ||= 'gitlab'
 Settings.gitlab['signup_enabled'] ||= false
+Settings.gitlab['username_changing_enabled'] = true if Settings.gitlab['username_changing_enabled'].nil?
 
 Settings['gravatar'] ||= Settingslogic.new({})
 Settings.gravatar['enabled']      = true if Settings.gravatar['enabled'].nil?
-- 
GitLab


From fcffb4c3813d037346c7c967fcc490dc529c976b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jaakko=20Kantoj=C3=A4rvi?= <jaakko@n-1.fi>
Date: Sat, 2 Feb 2013 21:25:03 +0200
Subject: [PATCH 2/2] Move username change decision into user model

---
 app/controllers/profiles_controller.rb | 2 +-
 app/models/user.rb                     | 4 ++++
 app/views/profiles/account.html.haml   | 2 +-
 3 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/app/controllers/profiles_controller.rb b/app/controllers/profiles_controller.rb
index 1d1850cfedc..051a6664519 100644
--- a/app/controllers/profiles_controller.rb
+++ b/app/controllers/profiles_controller.rb
@@ -51,7 +51,7 @@ class ProfilesController < ApplicationController
   end
 
   def update_username
-    if Gitlab.config.gitlab.username_changing_enabled
+    if @user.can_change_username?
       @user.update_attributes(username: params[:user][:username])
     end
 
diff --git a/app/models/user.rb b/app/models/user.rb
index 35a693fdb1c..d66ce411286 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -215,6 +215,10 @@ class User < ActiveRecord::Base
     keys.count == 0
   end
 
+  def can_change_username?
+    Gitlab.config.gitlab.username_changing_enabled
+  end
+
   def can_create_project?
     projects_limit > personal_projects.count
   end
diff --git a/app/views/profiles/account.html.haml b/app/views/profiles/account.html.haml
index b1c02d3ae92..ea9bcb648de 100644
--- a/app/views/profiles/account.html.haml
+++ b/app/views/profiles/account.html.haml
@@ -53,7 +53,7 @@
 
 
 
-- if Gitlab.config.gitlab.username_changing_enabled
+- if current_user.can_change_username?
   %fieldset.update-username
     %legend
       Username
-- 
GitLab