This question has been flagged

I have a model 'shop' inherited from 'stock.location'. It keeps necessary data for shops. It has a field 'shop_manager_id' which is many2one to 'res.users'. Now this shop managers will be users as you can see. I want a logged shop manager(user) should see only his shop in tree/form view. Admin will see every shop. I tried following code in xml but admin can not see any shop as he is not shop manager of any shop! Do you have any idea how to do it so that users can see only shops where they are managers and administrator can see all the shops. Thanks in advance.

<record id="inherited_shop_action" model="ir.actions.act_window">
            <field name="name">Shops</field>
            <field name="res_model">stock.location</field>
            <field name="view_type">form</field>
            <field name="view_mode">tree,form</field>
            <field name="view_id" ref="inherited_stock_location_view_form_inherit_store" />
            <field name="context">{'default_is_shop':
                True,'search_default_state':
                'done','default_usage':
                'internal'}
            </field>
            <field name="domain">[('is_shop','=',True), ('shop_manager_id', '=', uid)]</field>
            <field name="search_view_id"
                ref="inherited_stock_location_view_search_inherit_store" />
            <field name="help" type="html">
                <p class="oe_view_nocontent_create">Create Shops
                </p>
            </field>
        </record>

Avatar
Discard
Author Best Answer

thanks for your answer!! @\thompsonn 

I did it almost the same way.....it is more specific for my case.

<record id="shop_manager_access" model="ir.rule">
            <field name="name">Shops for corresponding managers only</field>
            <field name="model_id" ref="model_stock_location" />
            <field name="groups" eval="[(4, ref('merchandising_shop_manager'))]" />
            <field name="perm_read" eval="1" />
            <field name="perm_write" eval="0" />
            <field name="perm_create" eval="0" />
            <field name="perm_unlink" eval="0" />
            <field name="domain_force">[('store_manager_id', '=', user.id)]</field>
        </record>

Avatar
Discard
Best Answer

Row-level access rules are defined in the ir.rule model and can be added/edited either via Technical menu or by adding a corresponding xml file to the module. The file is usually stored under security/ folder in your module directory and should look like this in your case:

<?xml version="1.0" encoding="utf-8"?>
<odoo>
  <data>
    <record id="shop_managers_user_rule" model="ir.rule">
      <field name="name">Shops for corresponding managers only</field>
      <field name="model_id" ref="model_stock_location"/>
      <field name="domain_force">[('create_uid','=',user.id)]
</field>
      <field name="groups" eval="[(4,ref('base.group_user'))]"/>
    </record>
  </data>
</odoo>

More information on Odoo special tuples -- (4, id, _) in this case -- you can find in the docs: https://www.odoo.com/documentation/10.0/reference/orm.html#model-reference (CRUD section).


Don't forget to add the path to the created file to the manifest of your module (__openerp__.py / __manifest__.py):

'data': [...,
         security/shop_managers_access_rules.xml]
Avatar
Discard