Wednesday 26 October 2016

Best way to create See Own Customers rule in Odoo


Odoo does not provide by default a rule to limit users to see their own customers, at least at the time when I am writing this article. So many people are asking the question in different odoo forums, how they can limit users to see their own customer similar to the See Own Leads group rules of the Sale.

For the answer of this question, many have suggested  to create a record rule from Odoo Settings --> Technical --> Security --> Record Rules like ir.ui.view.custom object. Set the object as res.partner and Add related domain in Domain section as:



['|', ('user_id', '=', user.id), ('user_id', '=', False)]

like in the answers in this post and this and this

This is valid domain but I am not the one who suggest this domain on record rule for such use case, since the problem with this is that the res.partner object is not just for customers, its also has records for companies, users as well as suppliers. So what this domain does is it limit all that other records too. It is also possible that you will also got such access errors on different other objects as I got in sale module while approaching sale dashboard:





The requested operation cannot be completed due to security restrictions. Please contact your system administrator.
(Document type: res.users, Operation: read)


Hence we need a way to exclude all other than customer record from this domain.

This domain will I think is best suited if you want to limit just customers:


['|', '|', '|', ('user_id', '=', user.id), ('user_id', '=', False), ('create_uid', '=', user.id), ('customer','=',False)]

and to apply this via code :


<record model="ir.rule" id="custom_rule_see_own_customer">
    <field name="name">custom.rule.see.own.customer</field>
    <field name="model_id" ref="base.model_res_partner"/>
    <field name="domain_force">ABOVE DOMAIN HERE</field>
    <field name="groups" eval="[(4,ref('base.group_sale_salesman'))]"/>
    <field name="perm_read" eval="True"/>
    <field name="perm_write" eval="True"/> 
    <field name="perm_unlink" eval="True"/> 
    <field name="perm_create" eval="True"/>
</record>