This question has been flagged

I have created an app with a form. The form has a many to one field called Access Groups. The user will input a certain existing group e.g. Group A. I want only people from group A to have access to that record. 

If the user selects group B, only users from group B must have access to that record.

What code should I use in my record rule?



Avatar
Discard
Best Answer

Hi Feryial,

You can easily handle this one with a access rule definition,

So this example is based on your explanation:

1- Inherit res.users to compute on the fly groups

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

user_group = fields.Many2one('res.groups',compute='_compute_user_group')

def _compute_user_group(self):
for user in self:
if self.env.user.has_group('my_module.group_a'):
user.user_group = self.env('res.groups').search([('name','=','Group A')])
else:
user.user_group = self.env('res.groups').search([('name','=','Group B')])

2- Define the rule;

record model="ir.rule" id="my_module_record_based_rule">
field name="name">Users can only see their records
field name="model_id" ref="model_example_form"/>
field eval="1" name="perm_unlink"/>
field eval="1" name="perm_write"/>
field eval="1" name="perm_read"/>
field eval="1" name="perm_create"/>
field name="domain_force">[('access_groups', '=',user.user_group.id)]
field name="groups"
eval="[(6, 0, [ref('proc_pre_request.group_ppr_user')])]"/>
record>


Avatar
Discard
Author

@serkan Thank you for the reply.

Sorry beginner here,
I am using Odoo Studio to build the app for Odoo Online Database.

Where should I implement these codes? Do I simply put it into the record rule in Settings > Technical ?

Thank you