[FIX] project: on project template duplication, keep original tasks stages
[odoo/odoo.git] / addons / portal / mail_message.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2011 OpenERP S.A (<http://www.openerp.com>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 from openerp import SUPERUSER_ID
23 from openerp.osv import osv, orm
24 from openerp.tools.translate import _
25
26
27 class mail_message(osv.Model):
28     """ Update of mail_message class, to restrict mail access. """
29     _inherit = 'mail.message'
30
31     def _search(self, cr, uid, args, offset=0, limit=None, order=None,
32         context=None, count=False, access_rights_uid=None):
33         """ Override that adds specific access rights of mail.message, to remove
34             all internal notes if uid is a non-employee
35         """
36         if uid == SUPERUSER_ID:
37             return super(mail_message, self)._search(cr, uid, args, offset=offset, limit=limit, order=order,
38                 context=context, count=count, access_rights_uid=access_rights_uid)
39         group_ids = self.pool.get('res.users').browse(cr, uid, uid, context=context).groups_id
40         group_user_id = self.pool.get("ir.model.data").get_object_reference(cr, uid, 'base', 'group_user')[1]
41         if group_user_id not in [group.id for group in group_ids]:
42             args = ['&', '|', ('type', '!=', 'comment'), ('subtype_id', '!=', False)] + list(args)
43
44         return super(mail_message, self)._search(cr, uid, args, offset=offset, limit=limit, order=order,
45             context=context, count=count, access_rights_uid=access_rights_uid)
46
47     def check_access_rule(self, cr, uid, ids, operation, context=None):
48         """ Add Access rules of mail.message for non-employee user:
49             - read:
50                 - raise if the type is comment and subtype NULL (internal note)
51         """
52         if uid == SUPERUSER_ID:
53             return super(mail_message, self).check_access_rule(cr, uid, ids=ids, operation=operation, context=context)
54         group_ids = self.pool.get('res.users').browse(cr, uid, uid, context=context).groups_id
55         group_user_id = self.pool.get("ir.model.data").get_object_reference(cr, uid, 'base', 'group_user')[1]
56         if group_user_id not in [group.id for group in group_ids]:
57             cr.execute('SELECT DISTINCT id FROM "%s" WHERE type = %%s AND subtype_id IS NULL AND id = ANY (%%s)' % (self._table), ('comment', ids,))
58             if cr.fetchall():
59                 raise orm.except_orm(_('Access Denied'),
60                         _('The requested operation cannot be completed due to security restrictions. Please contact your system administrator.\n\n(Document type: %s, Operation: %s)') % \
61                         (self._description, operation))
62
63         return super(mail_message, self).check_access_rule(cr, uid, ids=ids, operation=operation, context=context)