diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2014-12-22 14:42:01 +0100 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2014-12-22 14:42:10 +0100 |
commit | 181538a6d0884e74beec8ad6aa3dcf460baec5ea (patch) | |
tree | 16435d7d6f9a8aea7e5ea06ec34fa53a3827506d | |
parent | 4b7fab382d55892a540af7b5cfebdf059dcf6d4d (diff) | |
download | sonarqube-181538a6d0884e74beec8ad6aa3dcf460baec5ea.tar.gz sonarqube-181538a6d0884e74beec8ad6aa3dcf460baec5ea.zip |
SSF-25 SMTP configuration password
8 files changed, 153 insertions, 66 deletions
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 @@ <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'} %> + <% if @smtp_password.blank? %> + <input type="password" name="smtp_password" id="smtp_password"> + <% else %> + <span class="marginright10">**********</span> + <a id="update-smtp-password" href="<%=ApplicationController.root_context-%>/settings/update_password_form?key=<%= Api::Utils.java_facade.getComponentByClassname('emailnotifications', 'org.sonar.api.config.EmailSettings').class::SMTP_PASSWORD.to_s %>" + class="open-modal link-action">Edit</a> + <% end %> <p class="marginbottom10"><%= message('email_configuration.smtp_password.description') -%></p> </td> </tr> 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 @@ </tbody> </table> </div> - <% else + <% else help = category_help(@category) unless help.blank? -%> <div class="help marginbottom10" style="margin-left: -1px"> <%= help -%> </div> - <% end + <% end end -%> @@ -189,24 +189,22 @@ </form> <% end -%> - - <script> $j(document) - .on('click', '.delete', function () { - $j(this).parents('.multi_value').remove(); - return false; - }) - .on('click', '.add_value', function () { - var template = $j(this).parents('.property').find('.template').last(); - template.clone().insertBefore(template).show(); - return false; - }) - .on('keypress', 'form', function (e) { - if (e.which == 13 && e.target.nodeName != "TEXTAREA") { - /* See https://jira.codehaus.org/browse/SONAR-4363 */ - submit_settings.click(); + .on('click', '.delete', function () { + $j(this).parents('.multi_value').remove(); + return false; + }) + .on('click', '.add_value', function () { + var template = $j(this).parents('.property').find('.template').last(); + template.clone().insertBefore(template).show(); return false; - } - }); + }) + .on('keypress', 'form', function (e) { + if (e.which == 13 && e.target.nodeName != "TEXTAREA") { + /* See https://jira.codehaus.org/browse/SONAR-4363 */ + submit_settings.click(); + return false; + } + }); </script> 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 -%> - <td><%= render "settings/type_#{field.type}", :property => field, :field => field, :value => value, :name => "property_sets[#{property.key}][]", :id => "input_#{h field.key}", :size => field.indicativeSize -%></td> - <% else -%> - <td><%= render "settings/type_#{field.type}", :property => field, :field => field, :value => value, :name => "#{property.key}[#{field.key}][]", :id => "input_#{h field.key}", :size => field.indicativeSize -%></td> - <% end -%> + <% name = field == key_field ? "property_sets[#{property.key}][]" : "#{property.key}[#{field.key}][]" %> + <td><%= render "settings/type_#{field.type}", :property => field, :field => field, :value => value, :name => name, :property_key => key, + :id => "input_#{h field.key}", :size => field.indicativeSize -%></td> <% end -%> <td style="width: 60px;"> 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 %> + <span class="marginright10">**********</span> + <a id="update-password-<%= u id %>" href="<%=ApplicationController.root_context-%>/settings/update_password_form?key=<%= u key %>&component_id=<%= u @resource && @resource.id %>" + class="open-modal link-action">Edit</a> + <input type="hidden" name="<%= name %>"> +<% 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 @@ +<form id="edit_password" method="post" action="<%= ApplicationController.root_context -%>/settings/update_password"> + <fieldset> + <div class="modal-head"> + <h2>Update Password '<%= @key %>'</h2> + </div> + <div class="modal-body"> + <div class="modal-error"></div> + <div class="modal-field"> + <input type="hidden" value="<%= @key %>" name="key" id="key"/> + <input type="hidden" value="<%= @component_id %>" name="component_id" id="component_id"/> + </div> + <div class="modal-field"> + <label for="password">Password</label> + <input type="password" size="50" name="password" id="password" autocomplete="off"> + </div> + </div> + <div class="modal-foot"> + <input type="submit" value="Save" name="commit"> + <a class="action" href="#" onclick="return closeModalWindow()"><%= h message('cancel') -%></a> + </div> + </fieldset> +</form> + +<script> + $j("#edit_password").modalForm(); +</script> |