~julie-w/unifield-wm/UTP-925

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2011 TeMPO Consulting, MSF. All Rights Reserved
#    Developer: Max Mumford
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from osv import osv
from osv import fields
from osv import orm
import logging

class button_access_rule(osv.osv):
    """
    Lets user create access rules for buttons in views.
    This class defines which model, view, button, and groups to target
    """

    _name = "msf_button_access_rights.button_access_rule"

    def _get_group_names(self, cr, uid, ids, field_name, arg, context):
        res = dict.fromkeys(ids, '')
        records = self.browse(cr, uid, ids)
        for record in records:
            res[record.id] = ', '.join([g.name for g in record.group_ids])
        return res

    _columns = {
        'name': fields.char('Name', size=256, required=True),
        'label': fields.char('Label', size=256),
        'type': fields.selection((('workflow','Workflow'), ('object','Object'), ('action', 'Action')), 'Button Type'),
        'model_id': fields.many2one('ir.model', 'Model', help='The type of data to which this rule applies', required=True, ondelete='cascade'),
        'view_id': fields.many2one('ir.ui.view', 'View', help='The view to which this rule applies', required=True, ondelete='cascade'),
        'group_ids': fields.many2many('res.groups', 'button_access_rule_groups_rel', 'button_access_rule_id', 'group_id', 'Groups', help='A list of groups who have access to this button. If you leave this empty, everybody will have access.'),
        'comment': fields.text('Comment', help='A description of what this rule does'),
        'group_names': fields.function(_get_group_names, type='char', method=True, string='Group Names', help='A list of all group names given button access by this rule'),
        'active': fields.boolean('Active', help='If checked, this rule will be applied.'),
    }

    _defaults = {
        'active': True,
    }

    _sql_constraints = [
        ('name_view_unique', 'unique (name, view_id)', "The combination of Button Name and View ID must be unique - i.e. you cannot have two rules for the same button in the same view"),
    ]

    def _get_family_ids(self, cr, view_id):
        """
        Return a list of ids for all the children of view_id (and contains the view_id itself)
        """
        family_ids = [view_id]
        last_ids = [view_id]
        view_pool = self.pool.get('ir.ui.view')

        while(last_ids):
            last_ids = view_pool.search(cr, 1, [('inherit_id','in',last_ids)])
            family_ids = family_ids + last_ids

        return family_ids

button_access_rule()