From dbaeee703fc16027bf17552acc6bb3c409d21958 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Fri, 22 Mar 2013 16:57:34 +0100 Subject: [IMP] mail: alias_contact on alias. However, not sure this will be usefull, to discuss. bzr revid: tde@openerp.com-20130322155734-9jfc7w8nw8oiytd4 --- addons/mail/mail_alias.py | 12 ++++++++++-- addons/mail/mail_thread.py | 17 +++++++++++++++++ addons/mail/tests/test_mail_gateway.py | 18 +++++++++++++++++- 3 files changed, 44 insertions(+), 3 deletions(-) (limited to 'addons/mail') diff --git a/addons/mail/mail_alias.py b/addons/mail/mail_alias.py index 55746b11678..dad69fd8be3 100644 --- a/addons/mail/mail_alias.py +++ b/addons/mail/mail_alias.py @@ -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 = [ diff --git a/addons/mail/mail_thread.py b/addons/mail/mail_thread.py index 7ca9fcb7b53..12e280594d4 100644 --- a/addons/mail/mail_thread.py +++ b/addons/mail/mail_thread.py @@ -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': '

Hello,

' + '

The following email sent to %s cannot be accepted because this address' + 'is private. Only known contacts are allowed to contact this address.

' + '
%s
' % (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 diff --git a/addons/mail/tests/test_mail_gateway.py b/addons/mail/tests/test_mail_gateway.py index a68011298c7..74cdd06ed63 100644 --- a/addons/mail/tests/test_mail_gateway.py +++ b/addons/mail/tests/test_mail_gateway.py @@ -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'}) -- cgit v1.2.3 From a3de5c872f7c26ec085dc62b95cfc62382ab970a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Tue, 9 Apr 2013 13:11:58 +0200 Subject: [REF] [IMP] mail_alias improvement alias_name is not required anymore crm, hr_job, project, mail_group, res_users: alias creation is now different and done more like other inherits, except an alias_model_name that is given to the context to find the alias alias_model_id. crm, jr_job, project, mail_group, res_users: updated form views using aliases mail_alias: added a filter on 'inactive' aliases (alias_name False) and added a button to redirect to the related document if any bzr revid: tde@openerp.com-20130409111158-bv04n5o01z9l3c17 --- addons/mail/mail_alias.py | 63 +++++++++++++++++++++------------ addons/mail/mail_alias_view.xml | 11 ++++-- addons/mail/mail_group.py | 21 +++++------ addons/mail/mail_group_view.xml | 14 ++++---- addons/mail/res_users.py | 23 ++++-------- addons/mail/res_users_view.xml | 10 ++++-- addons/mail/tests/test_mail_base.py | 6 ++-- addons/mail/tests/test_mail_features.py | 9 +++-- 8 files changed, 87 insertions(+), 70 deletions(-) (limited to 'addons/mail') diff --git a/addons/mail/mail_alias.py b/addons/mail/mail_alias.py index dad69fd8be3..51f9d36ab8d 100644 --- a/addons/mail/mail_alias.py +++ b/addons/mail/mail_alias.py @@ -39,6 +39,7 @@ def remove_accents(input_str): nkfd_form = unicodedata.normalize('NFKD', input_str) return u''.join([c for c in nkfd_form if not unicodedata.combining(c)]) + class mail_alias(osv.Model): """A Mail Alias is a mapping of an email address with a given OpenERP Document model. It is used by OpenERP's mail gateway when processing incoming emails @@ -47,7 +48,7 @@ class mail_alias(osv.Model): of that alias. If the message is a reply it will be attached to the existing discussion on the corresponding record, otherwise a new record of the corresponding model will be created. - + This is meant to be used in combination with a catch-all email configuration on the company's mail server, so that as soon as a new mail.alias is created, it becomes immediately usable and OpenERP will accept email for it. @@ -63,7 +64,7 @@ class mail_alias(osv.Model): return dict.fromkeys(ids, domain or "") _columns = { - 'alias_name': fields.char('Alias', required=True, + 'alias_name': fields.char('Alias', help="The name of the email alias, e.g. 'jobs' " "if you want to catch emails for ",), 'alias_model_id': fields.many2one('ir.model', 'Aliased Model', required=True, ondelete="cascade", @@ -147,7 +148,8 @@ class mail_alias(osv.Model): return new_name def migrate_to_alias(self, cr, child_model_name, child_table_name, child_model_auto_init_fct, - alias_id_column, alias_key, alias_prefix = '', alias_force_key = '', alias_defaults = {}, context=None): + alias_id_column, alias_key, alias_prefix='', alias_force_key='', alias_defaults={}, + alias_generate_name=False, context=None): """ Installation hook to create aliases for all users and avoid constraint errors. :param child_model_name: model name of the child class (i.e. res.users) @@ -156,14 +158,12 @@ class mail_alias(osv.Model): (i.e. super(res_users,self)._auto_init(cr, context=context)) :param alias_id_column: alias_id column (i.e. self._columns['alias_id']) :param alias_key: name of the column used for the unique name (i.e. 'login') - :param alias_prefix: prefix for the unique name (i.e. 'jobs' + ...) :param alias_force_key': name of the column for force_thread_id; if empty string, not taken into account :param alias_defaults: dict, keys = mail.alias columns, values = child model column name used for default values (i.e. {'job_id': 'id'}) """ - - # disable the unique alias_id not null constraint, to avoid spurious warning during + # disable the unique alias_id not null constraint, to avoid spurious warning during # super.auto_init. We'll reinstall it afterwards. alias_id_column.required = False @@ -173,16 +173,18 @@ class mail_alias(osv.Model): registry = RegistryManager.get(cr.dbname) mail_alias = registry.get('mail.alias') child_class_model = registry.get(child_model_name) - no_alias_ids = child_class_model.search(cr, SUPERUSER_ID, [('alias_id', '=', False)], context={'active_test':False}) + no_alias_ids = child_class_model.search(cr, SUPERUSER_ID, [('alias_id', '=', False)], context={'active_test': False}) # Use read() not browse(), to avoid prefetching uninitialized inherited fields for obj_data in child_class_model.read(cr, SUPERUSER_ID, no_alias_ids, [alias_key]): - alias_vals = {'alias_name': '%s%s' % (alias_prefix, obj_data[alias_key]) } + alias_vals = {'alias_name': False} + if alias_generate_name: + alias_vals['alias_name'] = '%s%s' % (alias_prefix, obj_data[alias_key]) if alias_force_key: alias_vals['alias_force_thread_id'] = obj_data[alias_force_key] - alias_vals['alias_defaults'] = dict( (k, obj_data[v]) for k, v in alias_defaults.iteritems()) - alias_id = mail_alias.create_unique_alias(cr, SUPERUSER_ID, alias_vals, model_name=child_model_name) + alias_vals['alias_defaults'] = dict((k, obj_data[v]) for k, v in alias_defaults.iteritems()) + alias_id = mail_alias.create(cr, SUPERUSER_ID, alias_vals, {'alias_model_name': child_model_name}) child_class_model.write(cr, SUPERUSER_ID, obj_data['id'], {'alias_id': alias_id}) - _logger.info('Mail alias created for %s %s (uid %s)', child_model_name, obj_data[alias_key], obj_data['id']) + _logger.info('Mail alias created for %s %s (id %s)', child_model_name, obj_data[alias_key], obj_data['id']) # Finally attempt to reinstate the missing constraint try: @@ -196,19 +198,36 @@ class mail_alias(osv.Model): # set back the unique alias_id constraint alias_id_column.required = True - def create_unique_alias(self, cr, uid, vals, model_name=None, context=None): - """Creates an email.alias record according to the values provided in ``vals``, - with 2 alterations: the ``alias_name`` value may be suffixed in order to - make it unique (and certain unsafe characters replaced), and - he ``alias_model_id`` value will set to the model ID of the ``model_name`` - value, if provided, + def create(self, cr, uid, vals, context=None): + """ Creates an email.alias record according to the values provided in ``vals``, + with 2 alterations: the ``alias_name`` value may be suffixed in order to + make it unique (and certain unsafe characters replaced), and + he ``alias_model_id`` value will set to the model ID of the ``model_name`` + context value, if provided. """ + if context is None: + context = {} + model_name = context.get('alias_model_name') # when an alias name appears to already be an email, we keep the local part only - alias_name = remove_accents(vals['alias_name']).lower().split('@')[0] - alias_name = re.sub(r'[^\w+.]+', '-', alias_name) - alias_name = self._find_unique(cr, uid, alias_name, context=context) - vals['alias_name'] = alias_name + if vals.get('alias_name'): + alias_name = remove_accents(vals['alias_name']).lower().split('@')[0] + alias_name = re.sub(r'[^\w+.]+', '-', alias_name) + alias_name = self._find_unique(cr, uid, alias_name, context=context) + vals['alias_name'] = alias_name if model_name: model_id = self.pool.get('ir.model').search(cr, uid, [('model', '=', model_name)], context=context)[0] vals['alias_model_id'] = model_id - return self.create(cr, uid, vals, context=context) + res = super(mail_alias, self).create(cr, uid, vals, context=context) + return res + + def open_document(self, cr, uid, ids, context=None): + alias = self.browse(cr, uid, ids, context=context)[0] + if not alias.alias_model_id or not alias.alias_force_thread_id: + return False + return { + 'view_type': 'form', + 'view_mode': 'form', + 'res_model': alias.alias_model_id.model, + 'res_id': alias.alias_force_thread_id, + 'type': 'ir.actions.act_window', + } diff --git a/addons/mail/mail_alias_view.xml b/addons/mail/mail_alias_view.xml index e0d8e173ebc..53b673b51b6 100644 --- a/addons/mail/mail_alias_view.xml +++ b/addons/mail/mail_alias_view.xml @@ -9,7 +9,6 @@
-