|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +#-- copyright |
| 4 | +# OpenProject is an open source project management software. |
| 5 | +# Copyright (C) the OpenProject GmbH |
| 6 | +# |
| 7 | +# This program is free software; you can redistribute it and/or |
| 8 | +# modify it under the terms of the GNU General Public License version 3. |
| 9 | +# |
| 10 | +# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: |
| 11 | +# Copyright (C) 2006-2013 Jean-Philippe Lang |
| 12 | +# Copyright (C) 2010-2013 the ChiliProject Team |
| 13 | +# |
| 14 | +# This program is free software; you can redistribute it and/or |
| 15 | +# modify it under the terms of the GNU General Public License |
| 16 | +# as published by the Free Software Foundation; either version 2 |
| 17 | +# of the License, or (at your option) any later version. |
| 18 | +# |
| 19 | +# This program is distributed in the hope that it will be useful, |
| 20 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | +# GNU General Public License for more details. |
| 23 | +# |
| 24 | +# You should have received a copy of the GNU General Public License |
| 25 | +# along with this program; if not, write to the Free Software |
| 26 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 27 | +# |
| 28 | +# See COPYRIGHT and LICENSE files for more details. |
| 29 | +#++ |
| 30 | + |
| 31 | +module Interceptors |
| 32 | + # Removes recipients on a domain listed in the `blocked_email_domains` setting. |
| 33 | + # The validation on User only applies to addresses as they are entered, so this |
| 34 | + # covers users whose domain was blocked after their account already existed. |
| 35 | + # |
| 36 | + # Mails left without any recipient are dropped by DoNotSendMailsWithoutRecipient, |
| 37 | + # which is why this has to run before it. |
| 38 | + module RemoveBlockedRecipients |
| 39 | + FIELDS = %i[to cc bcc].freeze |
| 40 | + |
| 41 | + module_function |
| 42 | + |
| 43 | + def delivering_email(mail) |
| 44 | + domains = ::OpenProject::BlockedEmailDomains.domains |
| 45 | + |
| 46 | + return if domains.empty? |
| 47 | + |
| 48 | + FIELDS.each { |field| remove_blocked(mail, field, domains) } |
| 49 | + end |
| 50 | + |
| 51 | + def remove_blocked(mail, field, domains) |
| 52 | + addresses = Array(mail.send(field)) |
| 53 | + allowed = addresses.reject { |address| ::OpenProject::BlockedEmailDomains.blocked?(address, domains:) } |
| 54 | + |
| 55 | + return if allowed.size == addresses.size |
| 56 | + |
| 57 | + Rails.logger.info do |
| 58 | + "Removed blocked #{field} recipients from '#{mail.subject}': #{(addresses - allowed).join(', ')}" |
| 59 | + end |
| 60 | + |
| 61 | + mail.send(:"#{field}=", allowed) |
| 62 | + end |
| 63 | + end |
| 64 | +end |
0 commit comments