]> source.dussan.org Git - redmine.git/commitdiff
Merged r11522 from trunk (#13340).
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Sun, 10 Mar 2013 12:35:20 +0000 (12:35 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Sun, 10 Mar 2013 12:35:20 +0000 (12:35 +0000)
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/branches/2.3-stable@11580 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/models/group.rb
app/models/mail_handler.rb
extra/mail_handler/rdm-mailhandler.rb
test/unit/mail_handler_test.rb

index fd0f583f70503f2ce850ef7baafca42d5a3d3ea4..d1b2cb966ad0a65e816afd51289b403b5168af9c 100644 (file)
@@ -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',
index febee3d1c0568ee3b7068bef261c659a360c99bb..f1efaa97c270c931b62c18a185b9a74a6645eb23 100644 (file)
@@ -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)}
index d04dd41511611a6d2f8de2b4d945b7c25b244557..e922eb033891d09efc5c7f395ee2cc22ce372db4 100644 (file)
@@ -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 }
 
index 7b4ffde13d41c73fedfaff02bebf093cb6816bc0..bd30e5d3f6d1ec2352fbe2c71b8c4d97c931ef6f 100644 (file)
@@ -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')