odoo13 permission to view only yourself

There are two ways to achieve this: customers only view the data they created

  • Use permission group directly

Use record rules (ir.rule) to filter (domain_force)

        <record id="wg_sale_partner_data_query_self" model="res.groups">
            <field name="name">客户资料查询(仅自己)</field>
            <field name="category_id" ref="module_category_wg_production_function"/>
        </record>

        <record id="wg_sale_partner_my" model="ir.rule">
            <field name="name">客户资料查询规则(仅自己)</field>
            <field ref="base.model_res_partner" name="model_id"/>
            <field name="domain_force">[('create_uid','in',[user.id,False])]</field>
            <field name="groups" eval="[(4, ref('wg_sale_partner_data_query_self'))]"/>
        </record>

Note: If the record rule does not have a permission group, it will take effect globally, that is, it does not target a certain permission group.

  • Use python code to
    use the _search() function.
    You don’t need to write rules, you can directly determine whether to select a permission group, and if there is, filter it.
from odoo.osv import expression


    @api.model
    def _search(self, args, offset=0, limit=None, order=None, count=False, access_rights_uid=None):
        if self.user_has_groups('production_management.wg_sale_partner_data_query_self'):
            args = expression.AND([[('create_uid', 'in', [self.env.user.id,False])], list(args)])

        return super(SupplierManager, self)._search(args, offset=offset, limit=limit, order=order,
                                                count=count, access_rights_uid=access_rights_uid)

Both methods can be implemented. I personally test.

Guess you like

Origin blog.csdn.net/weixin_42464956/article/details/113122466