Kai Ning
Kai Ning

Reputation: 197

Filter Contact Records based on user's allowed companies in multi-companies

I am trying to filter a record according to user's allowed company that will display in Contacts. Any idea on how to accomplish it? Thanks in advance. I highly appreciate your answer/suggestions in advance.
Ex. User has allowed companies: Company A and Company B
Records with this kind of companies should only be displayed.

<record id="rule_custom_user_contacts" model="ir.rule">
        <field name="name"> Record for CUstom Users Only</field>
        <field name="model_id" ref="base.model_res_partner" />
        <field name="domain_force">['|',('company_id','!=', False),('company_id','=', user.company_id.id)]</field>
        <field name="perm_read" eval="True" />
        <field name="perm_write" eval="False" />
        <field name="perm_create" eval="False" />
        <field name="perm_unlink" eval="False" />
        <field name="groups" eval="[(4, ref('mymdule.group_custom_user'))]" />
    </record>

Upvotes: 0

Views: 89

Answers (1)

reyhane janboori
reyhane janboori

Reputation: 341

for better understand and trace the code I would suggest to define a function in res.partner model and return your domain there with any logic that you want so call it in your record rule, for doing that look at the sample

class ResPartner(models.Model):
_inherit = 'res.partner'
 def set_contract_domain(self):
    #do any logic to get ids of user's companies and put it in user_companies_id  
    domain = [('company_id', 'in', user_companies_id)]
    return domain

and in xml file you have to call it like that:

<record id="user_access_rule" model="ir.rule">
        <field name="name"> Record for Custom Users Only</field>
        <field name="model_id" ref="model_model_res_partner"/>
        <field name="global" eval="True"/>
        <field name="domain_force">user.env['res.partner'].set_contract_domain()</field>
    </record>

I hope it works for you :)

Upvotes: 1

Related Questions