d6a38f273785ffddb581b1b2eb210eb2c93781d3
[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.osv import osv, orm
23 from openerp.tools.translate import _
24
25
26 class mail_message(osv.Model):
27     """ Update of mail_message class, to restrict mail access. """
28     _inherit = 'mail.message'
29
30     def _search(self, cr, uid, args, offset=0, limit=None, order=None,
31         context=None, count=False, access_rights_uid=None):
32         """ Override that adds specific access rights of mail.message, to remove
33             all internal notes if uid is a non-employee
34         """
35         group_ids = self.pool.get('res.users').browse(cr, uid, uid, context=context).groups_id
36         group_user_id = self.pool.get("ir.model.data").get_object_reference(cr, uid, 'base', 'group_user')[1]
37         if group_user_id not in [group.id for group in group_ids]:
38             args = ['&', '|', ('type', '!=', 'comment'), ('subtype_id', '!=', False)] + list(args)
39
40         return super(mail_message, self)._search(cr, uid, args, offset=offset, limit=limit, order=order,
41             context=context, count=False, access_rights_uid=access_rights_uid)
42
43     def check_access_rule(self, cr, uid, ids, operation, context=None):
44         """ Add Access rules of mail.message for non-employee user:
45             - read:
46                 - raise if the type is comment and subtype NULL (internal note)
47         """
48         group_ids = self.pool.get('res.users').browse(cr, uid, uid, context=context).groups_id
49         group_user_id = self.pool.get("ir.model.data").get_object_reference(cr, uid, 'base', 'group_user')[1]
50         if group_user_id not in [group.id for group in group_ids]:
51             cr.execute('SELECT DISTINCT id FROM "%s" WHERE type = %%s AND subtype_id IS NULL AND id = ANY (%%s)' % (self._table), ('comment', ids,))
52             if cr.fetchall():
53                 raise orm.except_orm(_('Access Denied'),
54                         _('The requested operation cannot be completed due to security restrictions. Please contact your system administrator.\n\n(Document type: %s, Operation: %s)') % \
55                         (self._description, operation))
56
57         return super(mail_message, self).check_access_rule(cr, uid, ids=ids, operation=operation, context=context)