[IMP] portal: replace model 'res.portal' by 'res.groups' where field 'is_portal'...
[odoo/odoo.git] / addons / portal / wizard / share_wizard.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 osv import osv, fields
23 from tools.translate import _
24 import logging
25 _logger = logging.getLogger(__name__)
26
27 UID_ROOT = 1
28 SHARED_DOCS_MENU = "Documents"
29 SHARED_DOCS_CHILD_MENU = "Shared Documents"
30
31 class share_wizard_portal(osv.TransientModel):
32     """Inherited share wizard to automatically create appropriate
33        menus in the selected portal upon sharing with a portal group."""
34     _inherit = "share.wizard"
35
36     def _user_type_selection(self, cr, uid, context=None):
37         selection = super(share_wizard_portal, self)._user_type_selection(cr, uid, context=context)
38         selection.extend([('existing','Users you already shared with'),
39                           ('groups','Existing Groups (e.g Portal Groups)')])
40         return selection
41
42     _columns = {
43         'user_ids': fields.many2many('res.users', 'share_wizard_res_user_rel', 'share_id', 'user_id', 'Existing users', domain=[('share', '=', True)]),
44         'group_ids': fields.many2many('res.groups', 'share_wizard_res_group_rel', 'share_id', 'group_id', 'Existing groups', domain=[('share', '=', False)]),
45     }
46
47     def _check_preconditions(self, cr, uid, wizard_data, context=None):
48         if wizard_data.user_type == 'existing':
49             self._assert(wizard_data.user_ids,
50                      _('Please select at least one user to share with'),
51                      context=context)
52         elif wizard_data.user_type == 'groups':
53             self._assert(wizard_data.group_ids,
54                      _('Please select at least one group to share with'),
55                      context=context)
56         return super(share_wizard_portal, self)._check_preconditions(cr, uid, wizard_data, context=context)
57
58     def _create_or_get_submenu_named(self, cr, uid, parent_menu_id, menu_name, context=None):
59         if not parent_menu_id:
60             return
61         Menus = self.pool.get('ir.ui.menu')
62         parent_menu = Menus.browse(cr, uid, parent_menu_id) # No context
63         menu_id = None
64         max_seq = 10
65         for child_menu in parent_menu.child_id:
66             max_seq = max(max_seq, child_menu.sequence)
67             if child_menu.name == menu_name:
68                 menu_id = child_menu.id
69                 break
70         if not menu_id:
71             # not found, create it
72             menu_id = Menus.create(cr, UID_ROOT,
73                                     {'name': menu_name,
74                                      'parent_id': parent_menu.id,
75                                      'sequence': max_seq + 10, # at the bottom
76                                     })
77         return menu_id
78
79     def _sharing_root_menu_id(self, cr, uid, portal, context=None):
80         """Create or retrieve root ID of sharing menu in portal menu
81
82            :param portal: browse_record of portal, constructed with a context WITHOUT language
83         """
84         parent_menu_id = self._create_or_get_submenu_named(cr, uid, portal.parent_menu_id.id, SHARED_DOCS_MENU, context=context)
85         if parent_menu_id:
86             child_menu_id = self._create_or_get_submenu_named(cr, uid, parent_menu_id, SHARED_DOCS_CHILD_MENU, context=context)
87             return child_menu_id
88
89     def _create_shared_data_menu(self, cr, uid, wizard_data, portal, context=None):
90         """Create sharing menus in portal menu according to share wizard options.
91
92            :param wizard_data: browse_record of share.wizard
93            :param portal: browse_record of portal, constructed with a context WITHOUT language
94         """
95         root_menu_id = self._sharing_root_menu_id(cr, uid, portal, context=context)
96         if not root_menu_id:
97             # no specific parent menu, cannot create the sharing menu at all.
98             return
99         # Create the shared action and menu
100         action_def = self._shared_action_def(cr, uid, wizard_data, context=None)
101         action_id = self.pool.get('ir.actions.act_window').create(cr, UID_ROOT, action_def)
102         menu_data = {'name': action_def['name'],
103                      'sequence': 10,
104                      'action': 'ir.actions.act_window,'+str(action_id),
105                      'parent_id': root_menu_id,
106                      'icon': 'STOCK_JUSTIFY_FILL'}
107         menu_id =  self.pool.get('ir.ui.menu').create(cr, UID_ROOT, menu_data)
108         return menu_id
109
110     def _create_share_users_group(self, cr, uid, wizard_data, context=None):
111         # Override of super() to handle the possibly selected "existing users"
112         # and "existing groups".
113         # In both cases, we call super() to create the share group, but when
114         # sharing with existing groups, we will later delete it, and copy its
115         # access rights and rules to the selected groups.
116         super_result = super(share_wizard_portal,self)._create_share_users_group(cr, uid, wizard_data, context=context)
117
118         # For sharing with existing groups, we don't create a share group, instead we'll
119         # alter the rules of the groups so they can see the shared data
120         if wizard_data.group_ids:
121             # get the list of portals and the related groups to install their menus.
122             res_groups = self.pool.get('res.groups')
123             all_portal_group_ids = res_groups.search(cr, UID_ROOT, [('is_portal', '=', True)])
124
125             # populate result lines with the users of each group and
126             # setup the menu for portal groups
127             for group in wizard_data.group_ids:
128                 if group.id in all_portal_group_ids:
129                     self._create_shared_data_menu(cr, uid, wizard_data, group.id, context=context)
130
131                 for user in group.users:
132                     new_line = {'user_id': user.id,
133                                 'newly_created': False}
134                     wizard_data.write({'result_line_ids': [(0,0,new_line)]})
135
136         elif wizard_data.user_ids:
137             # must take care of existing users, by adding them to the new group, which is super_result[0],
138             # and adding the shortcut
139             selected_user_ids = [x.id for x in wizard_data.user_ids]
140             self.pool.get('res.users').write(cr, UID_ROOT, selected_user_ids, {'groups_id': [(4, super_result[0])]})
141             self._setup_action_and_shortcut(cr, uid, wizard_data, selected_user_ids, make_home=False, context=context)
142             # populate the result lines for existing users too
143             for user in wizard_data.user_ids:
144                 new_line = { 'user_id': user.id,
145                              'newly_created': False}
146                 wizard_data.write({'result_line_ids': [(0,0,new_line)]})
147
148         return super_result
149
150     def copy_share_group_access_and_delete(self, cr, wizard_data, share_group_id, context=None):
151         # In the case of sharing with existing groups, the strategy is to copy
152         # access rights and rules from the share group, so that we can
153         if not wizard_data.group_ids: return
154         Groups = self.pool.get('res.groups')
155         Rules = self.pool.get('ir.rule')
156         Rights = self.pool.get('ir.model.access')
157         share_group = Groups.browse(cr, UID_ROOT, share_group_id)
158         share_rule_ids = [r.id for r in share_group.rule_groups]
159         for target_group in wizard_data.group_ids:
160             # Link the rules to the group. This is appropriate because as of
161             # v6.1, the algorithm for combining them will OR the rules, hence
162             # extending the visible data.
163             Rules.write(cr, UID_ROOT, share_rule_ids, {'groups': [(4,target_group.id)]})
164             _logger.debug("Linked sharing rules from temporary sharing group to group %s", target_group)
165
166             # Copy the access rights. This is appropriate too because
167             # groups have the UNION of all permissions granted by their
168             # access right lines.
169             for access_line in share_group.model_access:
170                 Rights.copy(cr, UID_ROOT, access_line.id, default={'group_id': target_group.id})
171             _logger.debug("Copied access rights from temporary sharing group to group %s", target_group)
172
173         # finally, delete it after removing its users
174         Groups.write(cr, UID_ROOT, [share_group_id], {'users': [(6,0,[])]})
175         Groups.unlink(cr, UID_ROOT, [share_group_id])
176         _logger.debug("Deleted temporary sharing group %s", share_group_id)
177
178     def _finish_result_lines(self, cr, uid, wizard_data, share_group_id, context=None):
179         super(share_wizard_portal,self)._finish_result_lines(cr, uid, wizard_data, share_group_id, context=context)
180         self.copy_share_group_access_and_delete(cr, wizard_data, share_group_id, context=context)
181
182 share_wizard_portal()
183
184
185 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: