diff --git a/addons/portal/portal.py b/addons/portal/portal.py index ff50a1ddcf3..3b1448f3b2e 100644 --- a/addons/portal/portal.py +++ b/addons/portal/portal.py @@ -26,8 +26,8 @@ class portal(osv.osv): _description = 'Portal' _columns = { 'name': fields.char(string='Name', size=64, required=True), - 'menu_id': fields.many2one('ir.ui.menu', - string='Portal Menu', + 'menu_id': fields.many2one('ir.actions.actions', required="True", + string='Menu Action', help="The customized menu of the portal's users"), 'user_ids': fields.one2many('res.users', 'portal_id', string='Users', @@ -36,6 +36,42 @@ class portal(osv.osv): string='Groups', help='Users of this portal automatically belong to those groups'), } + + def create(self, cr, uid, values, context=None): + """ extend create() to assign the portal menu and groups to users """ + # as 'user_ids' is a many2one relation, values['user_ids'] must be a + # list of tuples of the form (0, 0, {values}) + for op, _, user_values in values['user_ids']: + assert op == 0 + user_values['menu_id'] = values['menu_id'] + user_values['groups_id'] = values['group_ids'] + + return super(portal, self).create(cr, uid, values, context) + + def write(self, cr, uid, ids, values, context=None): + """ extend write() to assign the portal menu and groups to users """ + user_object = self.pool.get('res.users') + + # first apply changes on the portals themselves + super(portal, self).write(cr, uid, ids, values, context) + + # then reflect changes on the users of each portal + # + # PERFORMANCE NOTE. The loop below performs N write() operations, where + # N=len(ids). This may seem inefficient, but in practice it is not, + # because: (1) N is pretty small (typically N=1), and (2) it is too + # complex (read: bug-prone) to write those updates as a single batch. + # + plist = self.browse(cr, uid, ids, context) + for p in plist: + user_ids = get_browse_ids(p.user_ids) + user_values = { + 'menu_id': get_browse_id(p.menu_id), + 'groups_id': [(6, 0, get_browse_ids(p.group_ids))], + } + user_object.write(cr, uid, user_ids, user_values, context) + + return True portal() @@ -49,3 +85,12 @@ class users(osv.osv): users() +# utils +def get_browse_id(obj): + """ return the id of a browse() object, or None """ + return (obj and obj.id or None) + +def get_browse_ids(objs): + """ return the ids of a list of browse() objects """ + return [(obj and obj.id or default) for obj in objs] + diff --git a/addons/portal/portal_view.xml b/addons/portal/portal_view.xml index 170ec9eedf7..c0608f29d06 100644 --- a/addons/portal/portal_view.xml +++ b/addons/portal/portal_view.xml @@ -23,7 +23,7 @@
- +