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