Skip to Content
Menu
This question has been flagged
943 Views

Hi,

I want to restrict the article category to certain users
for that I created a field in the model res.users [user_products_ids]
I created 2 rules on the registration for the 2 models [product.template], [product.category] The functionality works very well except that if I have a subcategory and the mother

category is restricted I have an error that appears:
The requested operation ("read" on "Item category" (product.category)) was rejected due to the following rules:
- Restriction on item category


Here is my code :

security.xml:

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <data>
        <record id="group_restrict_product" model="res.groups">
            <field name="name">Restrict Products</field>
        </record>

        <record id="filter_user_product_allowed" model="ir.rule">
            <field name="name">Filter Product Allowed</field>
            <field name="model_id" ref="product.model_product_template"/>
            <field name="groups" eval="[(4, ref('group_restrict_product'))]"/>
            <field name="domain_force">[('categ_id','in', [ p.id for p in user.user_product_ids ])]</field>

        </record>
             <record id="filter_user_product_category_allowed" model="ir.rule">
            <field name="name">Restriction sur categorie d'article</field>
            <field name="model_id" ref="product.model_product_category"/>
            <field name="groups" eval="[(4, ref('group_restrict_product'))]"/>
            <field name="domain_force">[('id','in', [ p.id for p in user.user_product_ids ])]</field>

        </record>
       
    </data>
</odoo>

models.py

from odoo import models, fields, api
from odoo.exceptions import Warning

class ResUsers(models.Model):
    _inherit = 'res.users'

    user_product_ids = fields.Many2many(
        'product.category',
        'product_user_rel',
        'user_id',
        'product_category_id',
        'Allowd Products')

user_view.xml

<?xml version="1.0" encoding="utf-8"?>
<openerp>
    <data>
        <record id="view_allowed_products_users_form" model="ir.ui.view">
            <field name="name">user_product_restriction.users.form</field>
            <field name="model">res.users</field>
            <field name="inherit_id" ref="base.view_users_form"/>
            <field name="arch" type="xml">
                <notebook position="inside">
                    <page string="Allowed Products" attrs="{'invisible': [('name', '==', 'Administrator')]}">
                        <group>
                            <field name="user_product_ids" nolabel="1" colspan="2"/>
                        </group>
                    </page>
                </notebook>
            </field>
        </record>
    </data>
</openerp>

Tanks for your help
Avatar
Discard