code layout cleanup app/models/mail_handler.rb

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@8745 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Toshi MARUYAMA 2012-01-31 12:49:06 +00:00
parent 7b600cdb91
commit 5dd08133ef
1 changed files with 42 additions and 13 deletions

View File

@ -29,7 +29,9 @@ class MailHandler < ActionMailer::Base
@@handler_options[:issue] ||= {}
@@handler_options[:allow_override] = @@handler_options[:allow_override].split(',').collect(&:strip) if @@handler_options[:allow_override].is_a?(String)
if @@handler_options[:allow_override].is_a?(String)
@@handler_options[:allow_override] = @@handler_options[:allow_override].split(',').collect(&:strip)
end
@@handler_options[:allow_override] ||= []
# Project needs to be overridable if not specified
@@handler_options[:allow_override] << 'project' unless @@handler_options[:issue].has_key?(:project)
@ -47,12 +49,16 @@ class MailHandler < ActionMailer::Base
sender_email = email.from.to_a.first.to_s.strip
# Ignore emails received from the application emission address to avoid hell cycles
if sender_email.downcase == Setting.mail_from.to_s.strip.downcase
logger.info "MailHandler: ignoring email from Redmine emission address [#{sender_email}]" if logger && logger.info
if logger && logger.info
logger.info "MailHandler: ignoring email from Redmine emission address [#{sender_email}]"
end
return false
end
@user = User.find_by_mail(sender_email) if sender_email.present?
if @user && !@user.active?
logger.info "MailHandler: ignoring email from non-active user [#{@user.login}]" if logger && logger.info
if logger && logger.info
logger.info "MailHandler: ignoring email from non-active user [#{@user.login}]"
end
return false
end
if @user.nil?
@ -63,15 +69,21 @@ class MailHandler < ActionMailer::Base
when 'create'
@user = create_user_from_email
if @user
logger.info "MailHandler: [#{@user.login}] account created" if logger && logger.info
if logger && logger.info
logger.info "MailHandler: [#{@user.login}] account created"
end
Mailer.deliver_account_information(@user, @user.password)
else
logger.error "MailHandler: could not create account for [#{sender_email}]" if logger && logger.error
if logger && logger.error
logger.error "MailHandler: could not create account for [#{sender_email}]"
end
return false
end
else
# Default behaviour, emails from unknown users are ignored
logger.info "MailHandler: ignoring email from unknown user [#{sender_email}]" if logger && logger.info
if logger && logger.info
logger.info "MailHandler: ignoring email from unknown user [#{sender_email}]"
end
return false
end
end
@ -149,7 +161,10 @@ class MailHandler < ActionMailer::Base
return unless issue
# check permission
unless @@handler_options[:no_permission_check]
raise UnauthorizedAction unless user.allowed_to?(:add_issue_notes, issue.project) || user.allowed_to?(:edit_issues, issue.project)
unless user.allowed_to?(:add_issue_notes, issue.project) ||
user.allowed_to?(:edit_issues, issue.project)
raise UnauthorizedAction
end
end
# ignore CLI-supplied defaults for new issues
@ -161,7 +176,9 @@ class MailHandler < ActionMailer::Base
journal.notes = cleaned_up_text_body
add_attachments(issue)
issue.save!
logger.info "MailHandler: issue ##{issue.id} updated by #{user}" if logger && logger.info
if logger && logger.info
logger.info "MailHandler: issue ##{issue.id} updated by #{user}"
end
journal
end
@ -192,7 +209,9 @@ class MailHandler < ActionMailer::Base
add_attachments(reply)
reply
else
logger.info "MailHandler: ignoring reply from [#{sender_email}] to a locked topic" if logger && logger.info
if logger && logger.info
logger.info "MailHandler: ignoring reply from [#{sender_email}] to a locked topic"
end
end
end
end
@ -241,8 +260,12 @@ class MailHandler < ActionMailer::Base
def extract_keyword!(text, attr, format=nil)
keys = [attr.to_s.humanize]
if attr.is_a?(Symbol)
keys << l("field_#{attr}", :default => '', :locale => user.language) if user && user.language.present?
keys << l("field_#{attr}", :default => '', :locale => Setting.default_language) if Setting.default_language.present?
if user && user.language.present?
keys << l("field_#{attr}", :default => '', :locale => user.language)
end
if Setting.default_language.present?
keys << l("field_#{attr}", :default => '', :locale => Setting.default_language)
end
end
keys.reject! {|k| k.blank?}
keys.collect! {|k| Regexp.escape(k)}
@ -388,10 +411,16 @@ class MailHandler < ActionMailer::Base
keyword = keyword.to_s.downcase
assignable = issue.assignable_users
assignee = nil
assignee ||= assignable.detect {|a| a.mail.to_s.downcase == keyword || a.login.to_s.downcase == keyword}
assignee ||= assignable.detect {|a|
a.mail.to_s.downcase == keyword ||
a.login.to_s.downcase == keyword
}
if assignee.nil? && keyword.match(/ /)
firstname, lastname = *(keyword.split) # "First Last Throwaway"
assignee ||= assignable.detect {|a| a.is_a?(User) && a.firstname.to_s.downcase == firstname && a.lastname.to_s.downcase == lastname}
assignee ||= assignable.detect {|a|
a.is_a?(User) && a.firstname.to_s.downcase == firstname &&
a.lastname.to_s.downcase == lastname
}
end
if assignee.nil?
assignee ||= assignable.detect {|a| a.is_a?(Group) && a.name.downcase == keyword}