Comment 73 for bug 1073087

Revision history for this message
Olivier Dony (Odoo) (odo-openerp) wrote :

Hi,

There's a bit of confusion regarding this issue, so let's try to clear it up.
The problem arises in multi-company environments when the company of a user, say "UserA" is different from the company of the partner "UserA" that is linked to that user. In this situation it is normal to see access errors in many cases, and the error will always mention "Document type: Partner".

This is *not* a problem with the implementation of the record rule system in OpenERP, nor a problem a with the record rules themselves, it's a problem of *data*.

In OpenERP 7.0 a user is always linked to a partner record (visible in the Customers menu if you remove the default "Customers" filter). When the error occurs it will look like this in the database:

   Partner "UserB" - Company A
     |
   User "UserB" - Company B

Why does this happen? In most cases it is because the admin who creates UserB for CompanyB is currently set to work in CompanyA. So both the user "UserB" and the partner "UserB" are initially created in CompanyA. Then the admin will change the user "UserB" to be in CompanyB, but the *partner* "UserB" will still be in CompanyA.

As Rifakat tried to explain in comment #29, this is simple to fix manually for each user if you want to convince yourself: go to the partner "UserB" and change its company to "Company B" and the problem will go away.

Then you can fix all your users at once by executing the following SQL queries:

  -- To show the partners with this issue
  SELECT p.id AS partner_id, p.name, p.company_id AS partner_company,
         u.company_id AS user_company
  FROM res_partner p JOIN res_users u ON (u.partner_id = p.id)
  WHERE p.company_id IS NOT NULL AND p.company_id != u.company_id;

  -- To fix the partners
  UPDATE res_partner p SET company_id = u.company_id
  FROM res_users u
  WHERE u.partner_id = p.id AND p.company_id IS NOT NULL;

There's a merge proposal that fixes the problem by making sure to always synchronize the company of the user with the company of the related partner: https://code.launchpad.net/~openerp-dev/openobject-server/7.0-opw-591308-jam/+merge/158311 This merge proposal is being reviewed right now and will solve the problem in the future.

Thanks,

PS: the patch proposed by Paulius Sladkevičius in comment #68 is fine as a temporary workaround but it will not solve the problem in 100% of cases (sometimes the company of the partner will still be used and it will not be correct), so it is best to fix the database with the suggested SQL query *and* to apply the patch to prevent this situation from happening in the future.