]> source.dussan.org Git - redmine.git/commitdiff
Don't use a class variable for mail handler options.
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Sun, 26 Apr 2015 14:55:18 +0000 (14:55 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Sun, 26 Apr 2015 14:55:18 +0000 (14:55 +0000)
git-svn-id: http://svn.redmine.org/redmine/trunk@14228 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/models/mail_handler.rb

index d4e57c79d36a774f9a076ea66ca86dd17704ed08..a0a52fe2bd9b162817b7d264cdf4b821d33c568b 100644 (file)
@@ -22,28 +22,33 @@ class MailHandler < ActionMailer::Base
   class UnauthorizedAction < StandardError; end
   class MissingInformation < StandardError; end
 
-  attr_reader :email, :user
+  attr_reader :email, :user, :handler_options
 
-  def self.receive(email, options={})
-    @@handler_options = options.deep_dup
+  def self.receive(raw_mail, options={})
+    options = options.deep_dup
 
-    @@handler_options[:issue] ||= {}
+    options[:issue] ||= {}
 
-    if @@handler_options[:allow_override].is_a?(String)
-      @@handler_options[:allow_override] = @@handler_options[:allow_override].split(',').collect(&:strip)
+    if options[:allow_override].is_a?(String)
+      options[:allow_override] = options[:allow_override].split(',').collect(&:strip)
     end
-    @@handler_options[:allow_override] ||= []
+    options[:allow_override] ||= []
     # Project needs to be overridable if not specified
-    @@handler_options[:allow_override] << 'project' unless @@handler_options[:issue].has_key?(:project)
+    options[:allow_override] << 'project' unless options[:issue].has_key?(:project)
     # Status overridable by default
-    @@handler_options[:allow_override] << 'status' unless @@handler_options[:issue].has_key?(:status)
+    options[:allow_override] << 'status' unless options[:issue].has_key?(:status)
 
-    @@handler_options[:no_account_notice] = (@@handler_options[:no_account_notice].to_s == '1')
-    @@handler_options[:no_notification] = (@@handler_options[:no_notification].to_s == '1')
-    @@handler_options[:no_permission_check] = (@@handler_options[:no_permission_check].to_s == '1')
+    options[:no_account_notice] = (options[:no_account_notice].to_s == '1')
+    options[:no_notification] = (options[:no_notification].to_s == '1')
+    options[:no_permission_check] = (options[:no_permission_check].to_s == '1')
 
-    email.force_encoding('ASCII-8BIT')
-    super(email)
+    raw_mail.force_encoding('ASCII-8BIT')
+
+    ActiveSupport::Notifications.instrument("receive.action_mailer") do |payload|
+      mail = Mail.new(raw_mail)
+      set_payload_for_mail(payload, mail)
+      new.receive(mail, options)
+    end
   end
 
   # Receives an email and rescues any exception
@@ -79,8 +84,9 @@ class MailHandler < ActionMailer::Base
 
   # Processes incoming emails
   # Returns the created object (eg. an issue, a message) or false
-  def receive(email)
+  def receive(email, options={})
     @email = email
+    @handler_options = options
     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
@@ -111,7 +117,7 @@ class MailHandler < ActionMailer::Base
     end
     if @user.nil?
       # Email was submitted by an unknown user
-      case @@handler_options[:unknown_user]
+      case handler_options[:unknown_user]
       when 'accept'
         @user = User.anonymous
       when 'create'
@@ -120,8 +126,8 @@ class MailHandler < ActionMailer::Base
           if logger
             logger.info "MailHandler: [#{@user.login}] account created"
           end
-          add_user_to_group(@@handler_options[:default_group])
-          unless @@handler_options[:no_account_notice]
+          add_user_to_group(handler_options[:default_group])
+          unless handler_options[:no_account_notice]
             Mailer.account_information(@user, @user.password).deliver
           end
         else
@@ -186,7 +192,7 @@ class MailHandler < ActionMailer::Base
   def receive_issue
     project = target_project
     # check permission
-    unless @@handler_options[:no_permission_check]
+    unless handler_options[:no_permission_check]
       raise UnauthorizedAction unless user.allowed_to?(:add_issues, project)
     end
 
@@ -213,7 +219,7 @@ class MailHandler < ActionMailer::Base
     issue = Issue.find_by_id(issue_id)
     return unless issue
     # check permission
-    unless @@handler_options[:no_permission_check]
+    unless handler_options[:no_permission_check]
       unless user.allowed_to?(:add_issue_notes, issue.project) ||
                user.allowed_to?(:edit_issues, issue.project)
         raise UnauthorizedAction
@@ -221,7 +227,7 @@ class MailHandler < ActionMailer::Base
     end
 
     # ignore CLI-supplied defaults for new issues
-    @@handler_options[:issue].clear
+    handler_options[:issue].clear
 
     journal = issue.init_journal(user)
     if from_journal && from_journal.private_notes?
@@ -253,7 +259,7 @@ class MailHandler < ActionMailer::Base
     if message
       message = message.root
 
-      unless @@handler_options[:no_permission_check]
+      unless handler_options[:no_permission_check]
         raise UnauthorizedAction unless user.allowed_to?(:add_messages, message.project)
       end
 
@@ -318,11 +324,11 @@ class MailHandler < ActionMailer::Base
       @keywords[attr]
     else
       @keywords[attr] = begin
-        if (options[:override] || @@handler_options[:allow_override].include?(attr.to_s)) &&
+        if (options[:override] || handler_options[:allow_override].include?(attr.to_s)) &&
               (v = extract_keyword!(cleaned_up_text_body, attr, options[:format]))
           v
-        elsif !@@handler_options[:issue][attr].blank?
-          @@handler_options[:issue][attr]
+        elsif !handler_options[:issue][attr].blank?
+          handler_options[:issue][attr]
         end
       end
     end
@@ -359,7 +365,7 @@ class MailHandler < ActionMailer::Base
     target = Project.find_by_identifier(get_keyword(:project))
     if target.nil?
       # Invalid project keyword, use the project specified as the default one
-      default_project = @@handler_options[:issue][:project]
+      default_project = handler_options[:issue][:project]
       if default_project.present?
         target = Project.find_by_identifier(default_project)
       end
@@ -485,7 +491,7 @@ class MailHandler < ActionMailer::Base
     end
     if addr.present?
       user = self.class.new_user_from_attributes(addr, name)
-      if @@handler_options[:no_notification]
+      if handler_options[:no_notification]
         user.mail_notification = 'none'
       end
       if user.save