From 181538a6d0884e74beec8ad6aa3dcf460baec5ea Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Mon, 22 Dec 2014 14:42:01 +0100 Subject: [PATCH] SSF-25 SMTP configuration password --- .../email_configuration_controller.rb | 41 ++++++++-------- .../app/controllers/settings_controller.rb | 38 ++++++++++++++- .../webapp/WEB-INF/app/models/property.rb | 48 ++++++++++++------- .../views/email_configuration/index.html.erb | 8 +++- .../app/views/settings/_properties.html.erb | 38 +++++++-------- .../app/views/settings/_set_instance.html.erb | 9 ++-- .../views/settings/_type_PASSWORD.html.erb | 11 ++++- .../settings/_update_password_form.html.erb | 26 ++++++++++ 8 files changed, 153 insertions(+), 66 deletions(-) create mode 100644 server/sonar-web/src/main/webapp/WEB-INF/app/views/settings/_update_password_form.html.erb diff --git a/server/sonar-web/src/main/webapp/WEB-INF/app/controllers/email_configuration_controller.rb b/server/sonar-web/src/main/webapp/WEB-INF/app/controllers/email_configuration_controller.rb index 33b8600e4c5..9fb6c4d8f4b 100644 --- a/server/sonar-web/src/main/webapp/WEB-INF/app/controllers/email_configuration_controller.rb +++ b/server/sonar-web/src/main/webapp/WEB-INF/app/controllers/email_configuration_controller.rb @@ -23,27 +23,30 @@ 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]) + # Password can only be set when empty, the update will be done by the edit modal window + if !params[:smtp_password].blank? + Property.set(configuration::SMTP_PASSWORD, params[:smtp_password]) + end + 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 @@ -66,7 +69,7 @@ class EmailConfigurationController < ApplicationController private def configuration - java_facade.getComponentByClassname('emailnotifications', 'org.sonar.api.config.EmailSettings').class + java_facade.getComponentByClassname('emailnotifications', 'org.sonar.api.config.EmailSettings').class end def properties diff --git a/server/sonar-web/src/main/webapp/WEB-INF/app/controllers/settings_controller.rb b/server/sonar-web/src/main/webapp/WEB-INF/app/controllers/settings_controller.rb index 5a48b659558..b6879063884 100644 --- a/server/sonar-web/src/main/webapp/WEB-INF/app/controllers/settings_controller.rb +++ b/server/sonar-web/src/main/webapp/WEB-INF/app/controllers/settings_controller.rb @@ -44,6 +44,32 @@ class SettingsController < ApplicationController render :partial => 'settings/properties' end + def update_password_form + @key = params[:key] + @component_id = params[:component_id] + render :partial => 'settings/update_password_form' + end + + def update_password + property_key = params[:key] + component_id = params[:component_id] + not_found('Property key should be set') unless property_key + component_id = !component_id.blank? ? component_id : nil + password = params[:password] + + property = Property.by_key(property_key, component_id) + if property + if !password.blank? + property.text_value = password + property.save + else + property.delete + end + end + Property.setGlobalProperty(property_key, password, component_id, nil) + render :text => 'ok', :status => 200 + end + private def update_properties(resource_id) @@ -63,7 +89,7 @@ class SettingsController < ApplicationController max = (Time.now.to_f * 100000).to_i set_keys.each_with_index do |v, index| if v.blank? - max += 1; + max += 1 set_keys[index] = max.to_s end end @@ -80,7 +106,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 = '' + 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| diff --git a/server/sonar-web/src/main/webapp/WEB-INF/app/models/property.rb b/server/sonar-web/src/main/webapp/WEB-INF/app/models/property.rb index 97744feb51c..1c96bbbfe25 100644 --- a/server/sonar-web/src/main/webapp/WEB-INF/app/models/property.rb +++ b/server/sonar-web/src/main/webapp/WEB-INF/app/models/property.rb @@ -94,7 +94,11 @@ class Property < ActiveRecord::Base text_value = value.to_s if defined? value text_value = nil if text_value.blank? - if text_value.blank? + # Load Java property definition + property_def = field_property_def(key) || property_def(key) + + # Dot not delete password properties + if text_value.blank? && property_def.type().to_s != PropertyType::TYPE_PASSWORD return Property.clear(key, resource_id) end @@ -103,8 +107,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) + # Existing password should not be touched + elsif property_def.type().to_s == PropertyType::TYPE_PASSWORD && !prop.text_value.blank? + text_value = prop.text_value end prop.text_value = text_value @@ -139,22 +146,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 +169,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 +201,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 diff --git a/server/sonar-web/src/main/webapp/WEB-INF/app/views/email_configuration/index.html.erb b/server/sonar-web/src/main/webapp/WEB-INF/app/views/email_configuration/index.html.erb index bea86751e41..1e1f11bf2f4 100644 --- a/server/sonar-web/src/main/webapp/WEB-INF/app/views/email_configuration/index.html.erb +++ b/server/sonar-web/src/main/webapp/WEB-INF/app/views/email_configuration/index.html.erb @@ -32,7 +32,13 @@ - <%= password_field_tag 'smtp_password', @smtp_password, {:autocomplete => 'off'} %> + <% if @smtp_password.blank? %> + + <% else %> + ********** + Edit + <% end %>

<%= message('email_configuration.smtp_password.description') -%>

diff --git a/server/sonar-web/src/main/webapp/WEB-INF/app/views/settings/_properties.html.erb b/server/sonar-web/src/main/webapp/WEB-INF/app/views/settings/_properties.html.erb index 9b965ee07b7..ee764458289 100644 --- a/server/sonar-web/src/main/webapp/WEB-INF/app/views/settings/_properties.html.erb +++ b/server/sonar-web/src/main/webapp/WEB-INF/app/views/settings/_properties.html.erb @@ -5,7 +5,7 @@ $j('#loading_settings').show(); $j.ajax({ url:'<%= url_for :controller => 'settings', :action => 'update', :category => @category.key, :subcategory => @subcategory.key, :resource_id => (@resource && @resource.id) -%>', type:'post', - success:function(responseHTML){$j('#properties').html($j(responseHTML));$j('#loading_settings').hide();$j('#submit_settings').show()}, + success:function(responseHTML){$j('#properties').html($j(responseHTML));$j('#loading_settings').hide();$j('#submit_settings').show();$j('#properties').find('.open-modal').modal()}, data:$j(this).serialize()}); return false;" method='post' @@ -174,14 +174,14 @@ - <% else + <% else help = category_help(@category) unless help.blank? -%>
<%= help -%>
- <% end + <% end end -%> @@ -189,24 +189,22 @@ <% end -%> - - diff --git a/server/sonar-web/src/main/webapp/WEB-INF/app/views/settings/_set_instance.html.erb b/server/sonar-web/src/main/webapp/WEB-INF/app/views/settings/_set_instance.html.erb index 57d5a57d7c7..c198581ba7a 100644 --- a/server/sonar-web/src/main/webapp/WEB-INF/app/views/settings/_set_instance.html.erb +++ b/server/sonar-web/src/main/webapp/WEB-INF/app/views/settings/_set_instance.html.erb @@ -12,12 +12,9 @@ <% value = Property.value(key, resource_id) -%> <% errors << (render "settings/error", :key => key) -%> <% end -%> - - <% if field == key_field -%> - <%= render "settings/type_#{field.type}", :property => field, :field => field, :value => value, :name => "property_sets[#{property.key}][]", :id => "input_#{h field.key}", :size => field.indicativeSize -%> - <% else -%> - <%= render "settings/type_#{field.type}", :property => field, :field => field, :value => value, :name => "#{property.key}[#{field.key}][]", :id => "input_#{h field.key}", :size => field.indicativeSize -%> - <% end -%> + <% name = field == key_field ? "property_sets[#{property.key}][]" : "#{property.key}[#{field.key}][]" %> + <%= render "settings/type_#{field.type}", :property => field, :field => field, :value => value, :name => name, :property_key => key, + :id => "input_#{h field.key}", :size => field.indicativeSize -%> <% end -%> diff --git a/server/sonar-web/src/main/webapp/WEB-INF/app/views/settings/_type_PASSWORD.html.erb b/server/sonar-web/src/main/webapp/WEB-INF/app/views/settings/_type_PASSWORD.html.erb index 68bc7edb71a..66a1ded8ae7 100644 --- a/server/sonar-web/src/main/webapp/WEB-INF/app/views/settings/_type_PASSWORD.html.erb +++ b/server/sonar-web/src/main/webapp/WEB-INF/app/views/settings/_type_PASSWORD.html.erb @@ -2,4 +2,13 @@ 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 + +<% if value.blank? %> + <%= property_input_field(name, PropertyType::TYPE_PASSWORD, value, PropertiesHelper::SCREEN_SETTINGS, options) %> +<% else %> + <% key = (defined? property_key) ? property_key : property.key %> + ********** + Edit + +<% end %> diff --git a/server/sonar-web/src/main/webapp/WEB-INF/app/views/settings/_update_password_form.html.erb b/server/sonar-web/src/main/webapp/WEB-INF/app/views/settings/_update_password_form.html.erb new file mode 100644 index 00000000000..0d3d738e55e --- /dev/null +++ b/server/sonar-web/src/main/webapp/WEB-INF/app/views/settings/_update_password_form.html.erb @@ -0,0 +1,26 @@ +
+
+ + + +
+
+ + -- 2.39.5