Merge lp:~akretion-team/ocb-server/fix-user-creation-in-multi-company into lp:ocb-server

Proposed by arthru on 2013-08-12
Status: Merged
Merged at revision: 5085
Proposed branch: lp:~akretion-team/ocb-server/fix-user-creation-in-multi-company
Merge into: lp:ocb-server
Diff against target: 51 lines (+22/-1)
2 files modified
openerp/addons/base/res/res_partner.py (+10/-0)
openerp/addons/base/res/res_users.py (+12/-1)
To merge this branch: bzr merge lp:~akretion-team/ocb-server/fix-user-creation-in-multi-company
Reviewer Review Type Date Requested Status
Holger Brunn (Therp) Approve on 2013-09-02
Guewen Baconnier @ Camptocamp Approve on 2013-08-30
Stefan Rijnhart (Opener) core review, testing 2013-08-12 Approve on 2013-08-12
Review via email: mp+179762@code.launchpad.net

Description of the change

I propose to merge https://code.launchpad.net/~openerp-dev/openobject-server/7.0-opw-591308-jam/+merge/158311 to OCB as there is no update on merging it in main branch despite many approval.

I've tested it on revno 5061 of lp:ocb-server and it works just fine (res_users and its res_partner have the same company_id) and there is no more Access Denied error (as described in https://bugs.launchpad.net/openobject-server/+bug/1073087).

To post a comment you must log in.

Thanks! I tested the original fix and as far as I can see this is the exact same change.

review: Approve (core review, testing)

LGTM

review: Approve
Holger Brunn (Therp) (hbrunn) wrote :

I think the error message in 17 sounds weird. Wouldn't "You cannot change the company as the partner has multiple users linked with different companies." be better?

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'openerp/addons/base/res/res_partner.py'
2--- openerp/addons/base/res/res_partner.py 2013-06-12 09:53:00 +0000
3+++ openerp/addons/base/res/res_partner.py 2013-08-12 18:04:04 +0000
4@@ -489,6 +489,16 @@
5 def write(self, cr, uid, ids, vals, context=None):
6 if isinstance(ids, (int, long)):
7 ids = [ids]
8+ #res.partner must only allow to set the company_id of a partner if it
9+ #is the same as the company of all users that inherit from this partner
10+ #(this is to allow the code from res_users to write to the partner!) or
11+ #if setting the company_id to False (this is compatible with any user company)
12+ if vals.get('company_id'):
13+ for partner in self.browse(cr, uid, ids, context=context):
14+ if partner.user_ids:
15+ user_companies = set([user.company_id.id for user in partner.user_ids])
16+ if len(user_companies) > 1 or vals['company_id'] not in user_companies:
17+ raise osv.except_osv(_("Warning"),_("You can not change the company as the partner/user has multiple user linked with different companies."))
18 result = super(res_partner,self).write(cr, uid, ids, vals, context=context)
19 for partner in self.browse(cr, uid, ids, context=context):
20 self._fields_sync(cr, uid, partner, vals, context)
21
22=== modified file 'openerp/addons/base/res/res_users.py'
23--- openerp/addons/base/res/res_users.py 2013-06-07 09:30:57 +0000
24+++ openerp/addons/base/res/res_users.py 2013-08-12 18:04:04 +0000
25@@ -279,6 +279,13 @@
26
27 return result
28
29+ def create(self, cr, uid, vals, context=None):
30+ user_id = super(res_users, self).create(cr, uid, vals, context=context)
31+ user = self.browse(cr, uid, user_id, context=context)
32+ if user.partner_id.company_id:
33+ user.partner_id.write({'company_id': user.company_id.id})
34+ return user_id
35+
36 def write(self, cr, uid, ids, values, context=None):
37 if not hasattr(ids, '__iter__'):
38 ids = [ids]
39@@ -293,7 +300,11 @@
40 uid = 1 # safe fields only, so we write as super-user to bypass access rights
41
42 res = super(res_users, self).write(cr, uid, ids, values, context=context)
43-
44+ if 'company_id' in values:
45+ for user in self.browse(cr, uid, ids, context=context):
46+ # if partner is global we keep it that way
47+ if user.partner_id.company_id and user.partner_id.company_id.id != values['company_id']:
48+ user.partner_id.write({'company_id': user.company_id.id})
49 # clear caches linked to the users
50 self.pool.get('ir.model.access').call_cache_clearing_methods(cr)
51 clear = partial(self.pool.get('ir.rule').clear_cache, cr)

Subscribers

People subscribed via source and target branches

to status/vote changes: