summaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
authorJean-Philippe Lang <jp_lang@yahoo.fr>2016-12-13 18:46:29 +0000
committerJean-Philippe Lang <jp_lang@yahoo.fr>2016-12-13 18:46:29 +0000
commitf30462595e83f512cd5f29246a4542773595a224 (patch)
tree56eb7ce08ea5a8c6e0c21806e067c8c652e652b9 /app
parent3186130966cea85d842faf5c038006f8fb40bb61 (diff)
downloadredmine-f30462595e83f512cd5f29246a4542773595a224.tar.gz
redmine-f30462595e83f512cd5f29246a4542773595a224.zip
Optional Regex delimiters to truncate incoming emails (#5864).
git-svn-id: http://svn.redmine.org/redmine/trunk@16065 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'app')
-rw-r--r--app/controllers/settings_controller.rb31
-rw-r--r--app/helpers/settings_helper.rb29
-rw-r--r--app/models/mail_handler.rb13
-rw-r--r--app/models/setting.rb30
-rw-r--r--app/views/settings/_mail_handler.html.erb1
-rw-r--r--app/views/settings/edit.html.erb2
6 files changed, 83 insertions, 23 deletions
diff --git a/app/controllers/settings_controller.rb b/app/controllers/settings_controller.rb
index 6b7e51874..7ef7e9945 100644
--- a/app/controllers/settings_controller.rb
+++ b/app/controllers/settings_controller.rb
@@ -34,24 +34,29 @@ class SettingsController < ApplicationController
def edit
@notifiables = Redmine::Notifiable.all
if request.post?
- if Setting.set_all_from_params(params[:settings])
+ errors = Setting.set_all_from_params(params[:settings])
+ if errors.blank?
flash[:notice] = l(:notice_successful_update)
+ redirect_to settings_path(:tab => params[:tab])
+ return
+ else
+ @setting_errors = errors
+ # render the edit form with error messages
end
- redirect_to settings_path(:tab => params[:tab])
- else
- @options = {}
- user_format = User::USER_FORMATS.collect{|key, value| [key, value[:setting_order]]}.sort{|a, b| a[1] <=> b[1]}
- @options[:user_format] = user_format.collect{|f| [User.current.name(f[0]), f[0].to_s]}
- @deliveries = ActionMailer::Base.perform_deliveries
+ end
- @guessed_host_and_path = request.host_with_port.dup
- @guessed_host_and_path << ('/'+ Redmine::Utils.relative_url_root.gsub(%r{^\/}, '')) unless Redmine::Utils.relative_url_root.blank?
+ @options = {}
+ user_format = User::USER_FORMATS.collect{|key, value| [key, value[:setting_order]]}.sort{|a, b| a[1] <=> b[1]}
+ @options[:user_format] = user_format.collect{|f| [User.current.name(f[0]), f[0].to_s]}
+ @deliveries = ActionMailer::Base.perform_deliveries
- @commit_update_keywords = Setting.commit_update_keywords.dup
- @commit_update_keywords = [{}] unless @commit_update_keywords.is_a?(Array) && @commit_update_keywords.any?
+ @guessed_host_and_path = request.host_with_port.dup
+ @guessed_host_and_path << ('/'+ Redmine::Utils.relative_url_root.gsub(%r{^\/}, '')) unless Redmine::Utils.relative_url_root.blank?
- Redmine::Themes.rescan
- end
+ @commit_update_keywords = Setting.commit_update_keywords.dup
+ @commit_update_keywords = [{}] unless @commit_update_keywords.is_a?(Array) && @commit_update_keywords.any?
+
+ Redmine::Themes.rescan
end
def plugin
diff --git a/app/helpers/settings_helper.rb b/app/helpers/settings_helper.rb
index 2c657f0df..1bba0f9fa 100644
--- a/app/helpers/settings_helper.rb
+++ b/app/helpers/settings_helper.rb
@@ -32,18 +32,35 @@ module SettingsHelper
]
end
+ def render_settings_error(errors)
+ return if errors.blank?
+ s = ''.html_safe
+ errors.each do |name, message|
+ s << content_tag('li', content_tag('b', l("setting_#{name}")) + " " + message)
+ end
+ content_tag('div', content_tag('ul', s), :id => 'errorExplanation')
+ end
+
+ def setting_value(setting)
+ value = nil
+ if params[:settings]
+ value = params[:settings][setting]
+ end
+ value || Setting.send(setting)
+ end
+
def setting_select(setting, choices, options={})
if blank_text = options.delete(:blank)
choices = [[blank_text.is_a?(Symbol) ? l(blank_text) : blank_text, '']] + choices
end
setting_label(setting, options).html_safe +
select_tag("settings[#{setting}]",
- options_for_select(choices, Setting.send(setting).to_s),
+ options_for_select(choices, setting_value(setting).to_s),
options).html_safe
end
def setting_multiselect(setting, choices, options={})
- setting_values = Setting.send(setting)
+ setting_values = setting_value(setting)
setting_values = [] unless setting_values.is_a?(Array)
content_tag("label", l(options[:label] || "setting_#{setting}")) +
@@ -65,18 +82,18 @@ module SettingsHelper
def setting_text_field(setting, options={})
setting_label(setting, options).html_safe +
- text_field_tag("settings[#{setting}]", Setting.send(setting), options).html_safe
+ text_field_tag("settings[#{setting}]", setting_value(setting), options).html_safe
end
def setting_text_area(setting, options={})
setting_label(setting, options).html_safe +
- text_area_tag("settings[#{setting}]", Setting.send(setting), options).html_safe
+ text_area_tag("settings[#{setting}]", setting_value(setting), options).html_safe
end
def setting_check_box(setting, options={})
setting_label(setting, options).html_safe +
hidden_field_tag("settings[#{setting}]", 0, :id => nil).html_safe +
- check_box_tag("settings[#{setting}]", 1, Setting.send("#{setting}?"), options).html_safe
+ check_box_tag("settings[#{setting}]", 1, setting_value(setting).to_s != '0', options).html_safe
end
def setting_label(setting, options={})
@@ -97,7 +114,7 @@ module SettingsHelper
tag = check_box_tag('settings[notified_events][]',
notifiable.name,
- Setting.notified_events.include?(notifiable.name),
+ setting_value('notified_events').include?(notifiable.name),
:id => nil,
:data => tag_data)
diff --git a/app/models/mail_handler.rb b/app/models/mail_handler.rb
index 28841310e..402be4f51 100644
--- a/app/models/mail_handler.rb
+++ b/app/models/mail_handler.rb
@@ -561,9 +561,18 @@ class MailHandler < ActionMailer::Base
# Removes the email body of text after the truncation configurations.
def cleanup_body(body)
- delimiters = Setting.mail_handler_body_delimiters.to_s.split(/[\r\n]+/).reject(&:blank?).map {|s| Regexp.escape(s)}
+ delimiters = Setting.mail_handler_body_delimiters.to_s.split(/[\r\n]+/).reject(&:blank?)
+
+ if Setting.mail_handler_enable_regex_delimiters?
+ begin
+ delimiters = delimiters.map {|s| Regexp.new(s)}
+ rescue RegexpError => e
+ logger.error "MailHandler: invalid regexp delimiter found in mail_handler_body_delimiters setting (#{e.message})" if logger
+ end
+ end
+
unless delimiters.empty?
- regex = Regexp.new("^[> ]*(#{ delimiters.join('|') })\s*[\r\n].*", Regexp::MULTILINE)
+ regex = Regexp.new("^[> ]*(#{ Regexp.union(delimiters) })\s*[\r\n].*", Regexp::MULTILINE)
body = body.gsub(regex, '')
end
body.strip
diff --git a/app/models/setting.rb b/app/models/setting.rb
index 934e6bf2a..321b1cd38 100644
--- a/app/models/setting.rb
+++ b/app/models/setting.rb
@@ -120,8 +120,12 @@ class Setting < ActiveRecord::Base
# Updates multiple settings from params and sends a security notification if needed
def self.set_all_from_params(settings)
- return false unless settings.is_a?(Hash)
+ return nil unless settings.is_a?(Hash)
settings = settings.dup.symbolize_keys
+
+ errors = validate_all_from_params(settings)
+ return errors if errors.present?
+
changes = []
settings.each do |name, value|
next unless available_settings[name.to_s]
@@ -134,7 +138,29 @@ class Setting < ActiveRecord::Base
if changes.any?
Mailer.security_settings_updated(changes)
end
- true
+ nil
+ end
+
+ def self.validate_all_from_params(settings)
+ messages = []
+
+ if settings.key?(:mail_handler_body_delimiters) || settings.key?(:mail_handler_enable_regex_delimiters)
+ regexp = Setting.mail_handler_enable_regex_delimiters?
+ if settings.key?(:mail_handler_enable_regex_delimiters)
+ regexp = settings[:mail_handler_enable_regex_delimiters].to_s != '0'
+ end
+ if regexp
+ settings[:mail_handler_body_delimiters].to_s.split(/[\r\n]+/).each do |delimiter|
+ begin
+ Regexp.new(delimiter)
+ rescue RegexpError => e
+ messages << [:mail_handler_body_delimiters, "#{l('activerecord.errors.messages.not_a_regexp')} (#{e.message})"]
+ end
+ end
+ end
+ end
+
+ messages
end
# Sets a setting value from params
diff --git a/app/views/settings/_mail_handler.html.erb b/app/views/settings/_mail_handler.html.erb
index f255b4adc..02dfe5264 100644
--- a/app/views/settings/_mail_handler.html.erb
+++ b/app/views/settings/_mail_handler.html.erb
@@ -3,6 +3,7 @@
<div class="box tabular settings">
<p>
<%= setting_text_area :mail_handler_body_delimiters, :rows => 5 %>
+ <%= setting_check_box :mail_handler_enable_regex_delimiters, :label => false %> <%= l(:setting_mail_handler_enable_regex_delimiters) %>
<em class="info"><%= l(:text_line_separated) %></em>
</p>
<p>
diff --git a/app/views/settings/edit.html.erb b/app/views/settings/edit.html.erb
index de64e3bb2..3db255e29 100644
--- a/app/views/settings/edit.html.erb
+++ b/app/views/settings/edit.html.erb
@@ -1,5 +1,7 @@
<h2><%= l(:label_settings) %></h2>
+<%= render_settings_error @setting_errors %>
+
<%= render_tabs administration_settings_tabs %>
<% html_title(l(:label_settings), l(:label_administration)) -%>