[PATCH] BZ859998 disable certain fields from editing if in ldap mode

tzumainn at redhat.com tzumainn at redhat.com
Tue Oct 2 18:12:23 UTC 2012


From: Tzu-Mainn Chen <tzumainn at redhat.com>

---
 src/app/controllers/users_controller.rb |    1 +
 src/app/models/user.rb                  |    9 +++++++++
 src/app/views/users/_form.html.haml     |   12 ++++++------
 src/config/locales/en.yml               |    1 +
 src/features/support/custom.rb          |    1 +
 5 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/src/app/controllers/users_controller.rb b/src/app/controllers/users_controller.rb
index d26028d..4a2868f 100644
--- a/src/app/controllers/users_controller.rb
+++ b/src/app/controllers/users_controller.rb
@@ -97,6 +97,7 @@ class UsersController < ApplicationController
     @user = params[:id] ? User.find(params[:id]) : current_user
     require_privilege(Privilege::MODIFY, User) unless @user == current_user
     @title = t'users.edit.edit_user'
+    @ldap_user = (SETTINGS_CONFIG[:auth][:strategy] == "ldap")
   end
 
   def update
diff --git a/src/app/models/user.rb b/src/app/models/user.rb
index e8bee26..ee7ba1f 100644
--- a/src/app/models/user.rb
+++ b/src/app/models/user.rb
@@ -77,6 +77,8 @@ class User < ActiveRecord::Base
   before_validation :strip_whitespace
   after_save :update_entity
 
+  validate :validate_ldap_changes, :if => Proc.new {|user|
+    !user.new_record? && SETTINGS_CONFIG[:auth][:strategy] == "ldap"}
   validates_presence_of :quota
   validates_length_of :first_name, :maximum => 255, :allow_blank => true
   validates_length_of :last_name,  :maximum => 255, :allow_blank => true
@@ -185,6 +187,13 @@ class User < ActiveRecord::Base
     end
   end
 
+  def validate_ldap_changes
+    if self.first_name_changed? || self.last_name_changed? || self.email_changed? ||
+        self.username_changed? || self.crypted_password_changed? then
+      errors.add(:base, I18n.t("users.errors.cannot_edit_ldap_user"))
+    end
+  end
+
   def encrypt_password
     self.crypted_password = Password::update(password) unless password.blank?
   end
diff --git a/src/app/views/users/_form.html.haml b/src/app/views/users/_form.html.haml
index 859d920..abbb89a 100644
--- a/src/app/views/users/_form.html.haml
+++ b/src/app/views/users/_form.html.haml
@@ -5,28 +5,28 @@
   .field
     = form.label :first_name
     .input
-      = form.text_field :first_name, :class =>"check_change"
+      = form.text_field :first_name, :class =>"check_change", :disabled => @ldap_user
   .field
     = form.label :last_name
     .input
-      = form.text_field :last_name, :class =>"check_change"
+      = form.text_field :last_name, :class =>"check_change", :disabled => @ldap_user
   .field
     = form.label :email, t(:email), :required => true
     .input
-      = form.text_field :email, :class =>"check_change"
+      = form.text_field :email, :class =>"check_change", :disabled => @ldap_user
 %fieldset
   .field
     = form.label :username, t(:choose_name), :required => true
     .input
-      = form.text_field :username, :class => "check_change"
+      = form.text_field :username, :class => "check_change", :disabled => @ldap_user
   .field
     = form.label :password, form.object.new_record? ? t(:choose_password) : t(:change_password), :required => form.object.new_record?
     .input
-      = form.password_field :password
+      = form.password_field :password, :disabled => @ldap_user
   .field
     = form.label :password_confirmation, t(:confirm_password), :required => form.object.new_record?
     .input
-      = form.password_field :password_confirmation
+      = form.password_field :password_confirmation, :disabled => @ldap_user
 
 - if check_privilege(Privilege::MODIFY, User)
   %fieldset
diff --git a/src/config/locales/en.yml b/src/config/locales/en.yml
index cd43c3b..df75c38 100644
--- a/src/config/locales/en.yml
+++ b/src/config/locales/en.yml
@@ -153,6 +153,7 @@ en:
       name_starts_with_B: "Name starts with B"
     errors:
       has_running_instances: '%{username} has running instances'
+      cannot_edit_ldap_user: Cannot edit LDAP user
   user_groups:
     groups: "User Groups"
     local: Local
diff --git a/src/features/support/custom.rb b/src/features/support/custom.rb
index 02a60a4..85eb794 100644
--- a/src/features/support/custom.rb
+++ b/src/features/support/custom.rb
@@ -64,6 +64,7 @@ end
 # not LDAP
 User.class_eval do
   class << self
+    SETTINGS_CONFIG[:auth][:strategy] = "database"
     alias authenticate_using_ldap authenticate
   end
 end
-- 
1.7.6.5




More information about the aeolus-devel mailing list