Skip to content

Commit

Permalink
[FIX] mail: correctly extract email from email field in computed blac…
Browse files Browse the repository at this point in the history
…klist fields

Purpose of this commit is to correctly extract email address from the email
field of models inheriting from the blacklist mixin. Indeed email field
could contain a formatted address like "Raoul Grosbedon <[email protected]>".
Related blacklist entry would be [email protected]. We have to extract
the email from the email field and lowerize it in the SQL query or the
computation in order to have a fully working blacklist mixin. This fixes
some bits left at commit 2ff9b37.

This commit is related to task ID 33224 (original blacklist implementation
done for v12) and its PR odoo#25966 as well as task ID 1889703 (tests and fixes)
and its PR odoo#27330. Done with collaboration of @dbeguin.
  • Loading branch information
tde-banana-odoo committed Oct 2, 2018
1 parent bbe4690 commit 2b73bed
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions addons/mail/models/mail_blacklist.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,17 @@ def _search_is_blacklisted(self, operator, value):
if value:
query = """
SELECT m.id
FROM mail_blacklist bl
JOIN %s m on (LOWER(m.%s) = LOWER(bl.email) AND bl.active)
FROM mail_blacklist bl
JOIN %s m
ON (LOWER(substring(m.%s, '([^ ,;<@]+@[^> ,;]+)')) = bl.email AND bl.active)
"""
else:
query = """
SELECT m.id
SELECT m.id
FROM %s m
LEFT JOIN mail_blacklist bl
ON (LOWER(m.%s) = LOWER(bl.email) AND bl.active)
WHERE bl.id IS NULL
LEFT JOIN mail_blacklist bl
ON (LOWER(substring(m.%s, '([^ ,;<@]+@[^> ,;]+)')) = bl.email AND bl.active)
WHERE bl.id IS NULL
"""
self._cr.execute(query % (self._table, email_field))
res = self._cr.fetchall()
Expand All @@ -147,10 +148,7 @@ def _compute_is_blacklisted(self):
[email_field] = self._primary_email
# TODO : Should remove the sudo as compute_sudo defined on methods.
# But if user doesn't have access to mail.blacklist, doen't work without sudo().
BL_sudo = self.env['mail.blacklist'].sudo()
emails_lower = [(email or '').lower() for email in self.mapped(email_field)]
blacklist = set(e.lower()
for e in BL_sudo.search([('email', 'in', emails_lower)]).mapped('email'))
sanitized = [self.env['mail.blacklist']._sanitize_email(email) for email in self.mapped(email_field)]
blacklist = set(self.env['mail.blacklist'].sudo().search([('email', 'in', sanitized)]).mapped('email'))
for record in self:
email_lower = (record[email_field] or '').lower()
record.is_blacklisted = email_lower in blacklist
record.is_blacklisted = self.env['mail.blacklist']._sanitize_email(record[email_field]) in blacklist

0 comments on commit 2b73bed

Please sign in to comment.