Skip to content
Tutorials Center
My learning

Topics

Lesson 27 of 39 ยท Security and Access

Record Rules

Learn how Odoo record rules filter which rows a user may see or change: ir.rule domains, user and company_ids variables, global versus group rules and multi-company.

Access rights decide whether a user can read the model at all. Record rules decide which records. A sales person may read sales orders, but only their own; an employee may see colleagues' names but only their own payslips; a company A user must never see company B's invoices. These row-level filters are records of ir.rule, and each one holds a domain.

The Odoo XML below is read-only, and the Python stand-ins let you experiment with how the rules combine.

Defining a rule

XML
<record id="rule_book_own_loans" model="ir.rule">
  <field name="name">Library: members see their own loans</field>
  <field name="model_id" ref="model_library_loan"/>
  <field name="domain_force">[('member_id.user_id', '=', user.id)]</field>
  <field name="groups" eval="[(4, ref('library.group_library_user'))]"/>
  <field name="perm_read" eval="True"/>
  <field name="perm_write" eval="True"/>
  <field name="perm_create" eval="True"/>
  <field name="perm_unlink" eval="False"/>
</record>
  • model_id is the model to protect.
  • domain_force is the domain, evaluated for the current user. Variables available inside it include user (the current user record), company_id (the active company), company_ids (all allowed companies) and time.
  • groups limits the rule to members of the groups. With no groups, the rule is global.
  • The four perm_* flags choose which operations the rule filters. A rule with perm_unlink false does not restrict deletes.

When someone searches, reads or writes a protected model, the ORM adds the rule domain to the query. Rows outside it simply do not exist for that user; searching returns fewer rows, and trying to open one by ID gives an AccessError.

Global rules versus group rules

This is the subtle part, and a classic exam question.

  • Global rules (no groups) are combined with AND. Every global rule must allow the record.
  • Group rules apply to users in those groups, and they are combined with OR: if any of the user's group rules allows the record, it passes.
  • The final condition is (all global rules) AND (any group rule). If the user has no group rules for the model, only the global rules matter.
flowchart TD
    A["Record"] --> B{"All global rules<br/>allow it?"}
    B -- No --> X["Hidden"]
    B -- Yes --> C{"User has group<br/>rules for this model?"}
    C -- No --> V["Visible"]
    C -- Yes --> D{"Any group rule<br/>allows it?"}
    D -- Yes --> V
    D -- No --> X

Global rules narrow everyone down; group rules then open access in OR fashion.

That means a typical pattern is: a User group rule that shows only own records, and a Manager group rule with the domain [(1, '=', 1)] (always true) that shows all. A manager has both rules; OR makes everything visible. A multi-company rule, by contrast, is a global rule that every user must satisfy:

XML
<record id="rule_book_company" model="ir.rule">
  <field name="name">Book multi-company</field>
  <field name="model_id" ref="model_library_book"/>
  <field name="domain_force">['|', ('company_id', '=', False), ('company_id', 'in', company_ids)]</field>
</record>

Superuser and sudo bypass rules

Rules apply to the current user. Code running with sudo() or as the OdooBot superuser skips both access rights and record rules. That is sometimes needed (a portal user must create a system-generated record), but each sudo() is a place where you must decide manually what the user may do. See Field Security and sudo.

A rule engine stand-in

This stand-in stores rules as Python functions over a record and a user. It applies the global-AND, group-OR formula exactly as described.

Python Global AND, group OR

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Turning a domain into a filter

In Odoo, the rule domain becomes part of the SQL WHERE. The second stand-in builds a SQL query from a small domain, using SQLite in memory, to show that unseen rows are never even fetched. It uses parameters rather than string concatenation, as Odoo's ORM does.

Python Rule domains become SQL filters

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Writing good rules

  • Keep rules simple and indexed. They run on every query of that model, so a slow rule slows the whole app. Rules on company_id and user_id fields are cheap.
  • Prefer one clear global rule (multi-company) plus one rule per group.
  • Remember that rules apply to create and write too: a user can be blocked from saving a record that would fall outside their own rule.
  • Use perm_* flags to loosen rules per operation: for example let a user read all colleagues' records but write only their own.
  • Test as a user with only the intended group. Rules with groups do nothing for people outside it.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. How are global rules (no groups) combined with each other?

    Global rules narrow access: every one of them must allow the record.
  2. 2. How are rules from several groups of the same user combined?

    Group rules widen access, so a manager rule that shows everything overrides a user rule that shows only own records.
  3. 3. Which variable holds all companies the user is allowed to work in?

    company_ids contains every allowed company; company_id is just the active one.
  4. 4. What does code run with sudo() do regarding record rules?

    sudo skips access rights and record rules, so you must check permissions yourself.
  5. 5. Which domain makes a rule that allows every record?

    (1, '=', 1) is the conventional always-true domain for a manager rule. The explicit form is clearer than an empty domain.

Summary

  • Record rules (ir.rule) add a domain that limits which records a user can read, write, create or delete.
  • Global rules are ANDed; group rules are ORed; the result is global AND any-group.
  • Typical uses: own records, multi-company, portal access.
  • Rules are part of every query, so keep them simple and indexed.
  • sudo() bypasses rules, so use it deliberately.

Next: Field Security and sudo