[IMP] mail: alias_contact on alias. However, not sure this will be usefull, to discuss.

bzr revid: tde@openerp.com-20130322155734-9jfc7w8nw8oiytd4
This commit is contained in:
Thibault Delavallée 2013-03-22 16:57:34 +01:00
parent 1fe0a9afe9
commit dbaeee703f
3 changed files with 44 additions and 3 deletions

View File

@ -87,13 +87,21 @@ class mail_alias(osv.Model):
"messages will be attached, even if they did not reply to it. "
"If set, this will disable the creation of new records completely."),
'alias_domain': fields.function(_get_alias_domain, string="Alias domain", type='char', size=None),
'alias_contact': fields.selection([
('everyone', 'Everyone'),
('partners', 'Authenticated Partners'),
], string='Contact policy', required=True,
help="Allows to restrict the access to the alias. If set to partners, only emails coming "
"from a partner are accepted. Unknown emails are bounced. If set to everyone, every "
"incoming email is accepted for this alias."),
}
_defaults = {
'alias_defaults': '{}',
'alias_user_id': lambda self,cr,uid,context: uid,
'alias_user_id': lambda self, cr, uid, context: uid,
# looks better when creating new aliases - even if the field is informative only
'alias_domain': lambda self,cr,uid,context: self._get_alias_domain(cr, SUPERUSER_ID,[1],None,None)[1]
'alias_domain': lambda self, cr, uid, context: self._get_alias_domain(cr, SUPERUSER_ID, [1], None, None)[1],
'alias_contact': 'everyone',
}
_sql_constraints = [

View File

@ -553,9 +553,26 @@ class mail_thread(osv.AbstractModel):
mail_alias = self.pool.get('mail.alias')
alias_ids = mail_alias.search(cr, uid, [('alias_name', 'in', local_parts)])
if alias_ids:
# TDE note: at this stage, message parsing is not done yet - parse the from of the message
author_ids = self._message_find_partners(cr, uid, message, ['From'], context=context)
routes = []
for alias in mail_alias.browse(cr, uid, alias_ids, context=context):
user_id = alias.alias_user_id.id
if alias.alias_contact == 'partners' and not author_ids:
# TDE TODO: bounce
_logger.info('Routing mail with Message-Id %s: alias %s does not accept unknown emails, skipping', message_id, alias.alias_name)
mail_id = self.pool.get('mail.mail').create(cr, uid, {
'body_html': '<p>Hello,</p>'
'<p>The following email sent to %s cannot be accepted because this address'
'is private. Only known contacts are allowed to contact this address.</p>'
'<div>%s</div>' % (message.get('to'), message.get('body')),
'subject': message.get('subject'),
'email_to': message.get('from'),
'auto_delete': True,
}, context=context)
self.pool.get('mail.mail').send(cr, uid, [mail_id], context=context)
continue
if not user_id:
# TDE note: this could cause crashes, because no clue that the user
# that send the email has the right to create or modify a new document

View File

@ -105,7 +105,8 @@ class TestMailgateway(TestMailBase):
alias_id = self.mail_alias.create(cr, uid, {
'alias_name': 'groups',
'alias_user_id': False,
'alias_model_id': self.mail_group_model_id})
'alias_model_id': self.mail_group_model_id,
'alias_contact': 'everyone'})
# --------------------------------------------------
# Test1: new record creation
@ -146,6 +147,21 @@ class TestMailgateway(TestMailBase):
# Data: unlink group
frog_group.unlink()
# Do: incoming email from an unknown partner on a restricted alias
self._init_mock_build_email()
self.mail_alias.write(cr, uid, [alias_id], {'alias_contact': 'partners'})
frog_groups = format_and_process(MAIL_TEMPLATE, to='groups@example.com, other@gmail.com')
# Test: no group created
self.assertTrue(len(frog_groups) == 0)
# Test: email bounced
sent_emails = self._build_email_kwargs_list
self.assertEqual(len(sent_emails), 1,
'message_process: incoming email on private alias should send a bounce email')
self.assertIn('Frogs', sent_emails[0].get('subject'),
'message_process: bounce email on private alias should contain the original subject')
self.assertIn('test.sylvie.lelitre@agrolait.com', sent_emails[0].get('email_to'),
'message_process: bounce email on private alias should have original email sender as recipient')
# Do: incoming email from a known partner on an alias with known recipients, alias is owned by user that can create a group
self.mail_alias.write(cr, uid, [alias_id], {'alias_user_id': self.user_raoul_id})
p1id = self.res_partner.create(cr, uid, {'name': 'Sylvie Lelitre', 'email': 'test.sylvie.lelitre@agrolait.com'})