]> source.dussan.org Git - sonarqube.git/commitdiff
SSF-25 SMTP configuration password
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 22 Dec 2014 14:27:20 +0000 (15:27 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 22 Dec 2014 14:27:20 +0000 (15:27 +0100)
server/sonar-web/src/main/webapp/WEB-INF/app/controllers/email_configuration_controller.rb
server/sonar-web/src/main/webapp/WEB-INF/app/controllers/settings_controller.rb
server/sonar-web/src/main/webapp/WEB-INF/app/models/property.rb
server/sonar-web/src/main/webapp/WEB-INF/app/views/email_configuration/index.html.erb
server/sonar-web/src/main/webapp/WEB-INF/app/views/settings/_type_PASSWORD.html.erb

index 33b8600e4c500dae2961c5725fa58b3dc15c86b7..6f1f4ef36d6e110506f0a06b9bf61d543412c326 100644 (file)
@@ -23,27 +23,28 @@ class EmailConfigurationController < ApplicationController
   before_filter :admin_required
 
   def index
-  @smtp_host = Property.value(configuration::SMTP_HOST, nil, configuration::SMTP_HOST_DEFAULT)
-  @smtp_port = Property.value(configuration::SMTP_PORT, nil, configuration::SMTP_PORT_DEFAULT)
-  @smtp_secure_connection = Property.value(configuration::SMTP_SECURE_CONNECTION, nil, configuration::SMTP_SECURE_CONNECTION)
-  @smtp_username = Property.value(configuration::SMTP_USERNAME, nil, configuration::SMTP_USERNAME_DEFAULT)
-  @smtp_password = Property.value(configuration::SMTP_PASSWORD, nil, configuration::SMTP_PASSWORD_DEFAULT)
-  @email_from = Property.value(configuration::FROM, nil, configuration::FROM_DEFAULT)
-  @email_prefix = Property.value(configuration::PREFIX, nil, configuration::PREFIX_DEFAULT)
-  @server_base_url = Property.value(properties::SERVER_BASE_URL, nil, properties::SERVER_BASE_URL_DEFAULT_VALUE)
-  params[:layout]='false'
+    @smtp_host = Property.value(configuration::SMTP_HOST, nil, configuration::SMTP_HOST_DEFAULT)
+    @smtp_port = Property.value(configuration::SMTP_PORT, nil, configuration::SMTP_PORT_DEFAULT)
+    @smtp_secure_connection = Property.value(configuration::SMTP_SECURE_CONNECTION, nil, configuration::SMTP_SECURE_CONNECTION)
+    @smtp_username = Property.value(configuration::SMTP_USERNAME, nil, configuration::SMTP_USERNAME_DEFAULT)
+    @smtp_password = Property.value(configuration::SMTP_PASSWORD, nil, configuration::SMTP_PASSWORD_DEFAULT)
+    @email_from = Property.value(configuration::FROM, nil, configuration::FROM_DEFAULT)
+    @email_prefix = Property.value(configuration::PREFIX, nil, configuration::PREFIX_DEFAULT)
+    @server_base_url = Property.value(properties::SERVER_BASE_URL, nil, properties::SERVER_BASE_URL_DEFAULT_VALUE)
+    params[:layout]='false'
   end
 
   def save
-  Property.set(configuration::SMTP_HOST, params[:smtp_host])
-  Property.set(configuration::SMTP_PORT, params[:smtp_port])
-  Property.set(configuration::SMTP_SECURE_CONNECTION, params[:smtp_secure_connection])
-  Property.set(configuration::SMTP_USERNAME, params[:smtp_username])
-  Property.set(configuration::SMTP_PASSWORD, params[:smtp_password])
-  Property.set(configuration::FROM, params[:email_from])
-  Property.set(configuration::PREFIX, params[:email_prefix])
-  flash[:notice] = message('email_configuration.settings_saved')
-  redirect_to :action => 'index'
+    Property.set(configuration::SMTP_HOST, params[:smtp_host])
+    Property.set(configuration::SMTP_PORT, params[:smtp_port])
+    Property.set(configuration::SMTP_SECURE_CONNECTION, params[:smtp_secure_connection])
+    Property.set(configuration::SMTP_USERNAME, params[:smtp_username])
+    # Do not update password that wasn't updated
+    Property.set(configuration::SMTP_PASSWORD, params[:smtp_password]) unless params[:smtp_password] == Property::EXISTING_PASSWORD
+    Property.set(configuration::FROM, params[:email_from])
+    Property.set(configuration::PREFIX, params[:email_prefix])
+    flash[:notice] = message('email_configuration.settings_saved')
+    redirect_to :action => 'index'
   end
 
   def send_test_email
index 5a48b65955827d0bcf582b7eb52236dd6ceaab89..e5bdc3f0c4f5e2f5e97deb7b235b1dfe232f562e 100644 (file)
@@ -80,7 +80,15 @@ class SettingsController < ApplicationController
     set_keys.reject! { |set_key| set_key.blank? || (auto_generate && set_key_values[set_key].values.all?(&:blank?)) }
 
     Property.transaction do
-      Property.with_key_prefix(key + '.').with_resource(resource_id).delete_all
+      # Delete only property sets that are no more existing
+      condition = "prop_key LIKE '" + key + ".%' AND "
+      set_keys.each {|set_key| condition += "prop_key NOT LIKE ('#{key + '.' + set_key + '.%'}') AND "}
+      if resource_id
+        condition += 'resource_id=' + resource_id
+      else
+        condition += 'resource_id IS NULL'
+      end
+      Property.delete_all(condition)
 
       update_property(key, set_keys, resource_id)
       set_keys.each do |set_key|
index 97744feb51cce92754ae5542d4937ae33a6b8e4f..a66e0e452d517fcc5e1472101946fa78c37ccbce 100644 (file)
@@ -29,6 +29,8 @@ class Property < ActiveRecord::Base
   named_scope :with_resources, :conditions => 'resource_id is not null'
   named_scope :with_users, :conditions => 'user_id is not null'
 
+  EXISTING_PASSWORD = '{{*******************}}'
+
   def key
     prop_key
   end
@@ -94,6 +96,9 @@ class Property < ActiveRecord::Base
     text_value = value.to_s if defined? value
     text_value = nil if text_value.blank?
 
+    # Load Java property definition
+    property_def = field_property_def(key) || property_def(key)
+
     if text_value.blank?
       return Property.clear(key, resource_id)
     end
@@ -103,8 +108,11 @@ class Property < ActiveRecord::Base
       return prop
     end
 
-    unless prop
+    if !prop
       prop = Property.new(:prop_key => key, :resource_id => resource_id, :user_id => user_id)
+      # Do not update password that wasn't updated
+    elsif property_def.type().to_s == PropertyType::TYPE_PASSWORD && text_value == EXISTING_PASSWORD
+      text_value = prop.text_value
     end
 
     prop.text_value = text_value
@@ -139,22 +147,11 @@ class Property < ActiveRecord::Base
   end
 
   def java_definition
-    @java_definition ||=
-      begin
-        Api::Utils.java_facade.propertyDefinitions.get(key)
-      end
+    @java_definition ||= Property.property_def(key)
   end
 
   def java_field_definition
-    @java_field_definition ||=
-      begin
-        if /(.*)\..*\.(.*)/.match(key)
-          property_definition = Api::Utils.java_facade.propertyDefinitions.get(Regexp.last_match(1))
-          if property_definition
-            property_definition.fields.find { |field| field.key == Regexp.last_match(2) }
-          end
-        end
-      end
+    @java_field_definition ||= Property.field_property_def(key)
   end
 
   def validation_error_message
@@ -173,12 +170,12 @@ class Property < ActiveRecord::Base
     array.map { |v| v.gsub(',', '%2C') }.join(',')
   end
 
-  private
-
   def self.setGlobalProperty(key, value, resource_id, user_id)
     Api::Utils.java_facade.setGlobalProperty(key, value) unless (resource_id || user_id)
   end
 
+  private
+
   def self.all(key, resource_id=nil, user_id=nil)
     Property.with_key(key).with_resource(resource_id).with_user(user_id)
   end
@@ -205,4 +202,22 @@ class Property < ActiveRecord::Base
       errors.add_to_base(validation_result.errorKey) unless validation_result.isValid()
     end
   end
+
+  def self.property_def(key)
+    begin
+      Api::Utils.java_facade.propertyDefinitions.get(key)
+    end
+  end
+
+  def self.field_property_def(key)
+    begin
+      if /(.*)\..*\.(.*)/.match(key)
+        property_definition = Api::Utils.java_facade.propertyDefinitions.get(Regexp.last_match(1))
+        if property_definition
+          property_definition.fields.find { |field| field.key == Regexp.last_match(2) }
+        end
+      end
+    end
+  end
+
 end
index bea86751e415b1b538d1cfe44aeb6ea048a11290..76265ab10972288fd3cb1c4278cea8301e131025 100644 (file)
@@ -32,7 +32,8 @@
     <tr class="property">
       <th><label for="smtp_password"><h3><%= message('email_configuration.smtp_password') -%></h3></label></th>
       <td>
-        <%= password_field_tag 'smtp_password', @smtp_password, {:autocomplete => 'off'}  %>
+        <% value = Property::EXISTING_PASSWORD unless @smtp_password.blank? %>
+        <%= password_field_tag 'smtp_password', value, {:autocomplete => 'off'}  %>
         <p class="marginbottom10"><%= message('email_configuration.smtp_password.description') -%></p>
       </td>
     </tr>
index 68bc7edb71a1bebe299bdbeb43fe48f0517e8a36..d8c1556936c895ed4ddb17ea61e84af0c570826b 100644 (file)
@@ -2,4 +2,6 @@
    options = {:id => id}
    options[:size] = (defined? size) ? size : nil
 %>
-<%= property_input_field(name, PropertyType::TYPE_PASSWORD, value, PropertiesHelper::SCREEN_SETTINGS, options) %>
\ No newline at end of file
+
+<% value = Property::EXISTING_PASSWORD unless value.blank? %>
+<%= property_input_field(name, PropertyType::TYPE_PASSWORD, value, PropertiesHelper::SCREEN_SETTINGS, options) %>