Mail handler: adds --default-group option to add created user to one or more groups (#13340).

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@11522 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang 2013-03-03 07:59:29 +00:00
parent d5ca514688
commit b25d496c24
4 changed files with 43 additions and 7 deletions

View 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',

View 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
@ -463,6 +464,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)}

View 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 }

View 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')