diff --git a/app/models/group.rb b/app/models/group.rb index fd0f583f7..d1b2cb966 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -30,6 +30,7 @@ class Group < Principal before_destroy :remove_references_before_destroy scope :sorted, lambda { order("#{table_name}.lastname ASC") } + scope :named, lambda {|arg| where("LOWER(#{table_name}.lastname) = LOWER(?)", arg.to_s.strip)} safe_attributes 'name', 'user_ids', diff --git a/app/models/mail_handler.rb b/app/models/mail_handler.rb index febee3d1c..f1efaa97c 100644 --- a/app/models/mail_handler.rb +++ b/app/models/mail_handler.rb @@ -97,6 +97,7 @@ class MailHandler < ActionMailer::Base if logger && logger.info logger.info "MailHandler: [#{@user.login}] account created" end + add_user_to_group(@@handler_options[:default_group]) Mailer.account_information(@user, @user.password).deliver else if logger && logger.error @@ -465,6 +466,19 @@ class MailHandler < ActionMailer::Base end end + # Adds the newly created user to default group + def add_user_to_group(default_group) + if default_group.present? + default_group.split(',').each do |group_name| + if group = Group.named(group_name).first + group.users << @user + elsif logger + logger.warn "MailHandler: could not add user to [#{group_name}], group not found" + end + end + end + end + # 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)} diff --git a/extra/mail_handler/rdm-mailhandler.rb b/extra/mail_handler/rdm-mailhandler.rb index d04dd4151..e922eb033 100644 --- a/extra/mail_handler/rdm-mailhandler.rb +++ b/extra/mail_handler/rdm-mailhandler.rb @@ -23,9 +23,9 @@ module Net end class RedmineMailHandler - VERSION = '0.2.1' + VERSION = '0.2.2' - attr_accessor :verbose, :issue_attributes, :allow_override, :unknown_user, :no_permission_check, :url, :key, :no_check_certificate + attr_accessor :verbose, :issue_attributes, :allow_override, :unknown_user, :default_group, :no_permission_check, :url, :key, :no_check_certificate def initialize self.issue_attributes = {} @@ -40,11 +40,6 @@ class RedmineMailHandler opts.on("-k", "--key KEY", "Redmine API key") {|v| self.key = v} opts.separator("") opts.separator("General options:") - opts.on("--unknown-user ACTION", "how to handle emails from an unknown user", - "ACTION can be one of the following values:", - "* ignore: email is ignored (default)", - "* accept: accept as anonymous user", - "* create: create a user account") {|v| self.unknown_user = v} opts.on("--no-permission-check", "disable permission checking when receiving", "the email") {self.no_permission_check = '1'} opts.on("--key-file FILE", "path to a file that contains the Redmine", @@ -56,6 +51,15 @@ class RedmineMailHandler opts.on("-v", "--verbose", "show extra information") {self.verbose = true} opts.on("-V", "--version", "show version information and exit") {puts VERSION; exit} opts.separator("") + opts.separator("User creation options:") + opts.on("--unknown-user ACTION", "how to handle emails from an unknown user", + "ACTION can be one of the following values:", + "* ignore: email is ignored (default)", + "* accept: accept as anonymous user", + "* create: create a user account") {|v| self.unknown_user = v} + opts.on("--default-group GROUP", "add created user to GROUP (none by default)", + "GROUP can be a comma separated list of groups") { |v| self.default_group = v} + opts.separator("") opts.separator("Issue attributes control options:") opts.on("-p", "--project PROJECT", "identifier of the target project") {|v| self.issue_attributes['project'] = v} opts.on("-s", "--status STATUS", "name of the target status") {|v| self.issue_attributes['status'] = v} @@ -95,6 +99,7 @@ class RedmineMailHandler data = { 'key' => key, 'email' => email, 'allow_override' => allow_override, 'unknown_user' => unknown_user, + 'default_group' => default_group, 'no_permission_check' => no_permission_check} issue_attributes.each { |attr, value| data["issue[#{attr}]"] = value } diff --git a/test/unit/mail_handler_test.rb b/test/unit/mail_handler_test.rb index 7b4ffde13..bd30e5d3f 100644 --- a/test/unit/mail_handler_test.rb +++ b/test/unit/mail_handler_test.rb @@ -304,6 +304,22 @@ class MailHandlerTest < ActiveSupport::TestCase end end + def test_created_user_should_be_added_to_groups + group1 = Group.generate! + group2 = Group.generate! + + assert_difference 'User.count' do + submit_email( + 'ticket_by_unknown_user.eml', + :issue => {:project => 'ecookbook'}, + :unknown_user => 'create', + :default_group => "#{group1.name},#{group2.name}" + ) + end + user = User.order('id DESC').first + assert_same_elements [group1, group2], user.groups + end + def test_add_issue_without_from_header Role.anonymous.add_permission!(:add_issues) assert_equal false, submit_email('ticket_without_from_header.eml')