Odoo security rules decide who can see, edit, create and delete every record in your system. They are also one of the most misunderstood parts of Odoo development. A missing access line gives users an error. A badly written rule can show one company’s data to another. In my years of Odoo development, I have fixed both kinds of problems many times. This guide explains how the security layers work and how to set them up correctly in your own modules.
How Odoo Security Works: Three Layers
Odoo checks permissions in layers, and each layer answers a different question:
| Layer | Question it answers | Where you define it |
|---|---|---|
| Groups | Which role does this user have? | security.xml |
| Access rights | Can this role read, write, create or delete this model? | ir.model.access.csv |
| Record rules | Which specific records can this role touch? | security.xml (ir.rule) |
A user needs to pass the access rights check first. Record rules then filter which records they can actually work with. Think of access rights as the door to a room, and record rules as the list of shelves inside you are allowed to open.
Create Security Groups for Your Module
Groups represent roles such as User and Manager. Define them in security/security.xml:
<odoo>
<record id="module_category_my_module" model="ir.module.category">
<field name="name">My Module</field>
</record>
<record id="group_my_module_user" model="res.groups">
<field name="name">User</field>
<field name="category_id" ref="module_category_my_module"/>
</record>
<record id="group_my_module_manager" model="res.groups">
<field name="name">Manager</field>
<field name="category_id" ref="module_category_my_module"/>
<field name="implied_ids" eval="[(4, ref('group_my_module_user'))]"/>
</record>
</odoo>
The implied_ids line means every Manager automatically gets the User group too, so you only define extra permissions once. Keep your role list short and meaningful. Three or four clear groups are easier to manage than fifteen overlapping ones.
Define Access Rights with ir.model.access.csv
Every new model needs access rights. Create security/ir.model.access.csv:
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_my_model_user,my.model.user,model_my_model,group_my_module_user,1,1,1,0
access_my_model_manager,my.model.manager,model_my_model,group_my_module_manager,1,1,1,1
Key points to remember:
- The model reference format is
model_followed by the model name with dots changed to underscores. The modelmy.modelbecomesmodel_my_model. - Each line is additive. If a user belongs to several groups, they get the combined permissions of all matching lines.
- A line with no group applies to every user, so use it with great care.
- Manifest order matters. List
security.xmlbefore the CSV file in your manifest’sdatakey, because the CSV refers to groups that must exist first.
'data': [
'security/security.xml',
'security/ir.model.access.csv',
'views/my_model_views.xml',
],
Write Record Rules to Control Which Records Users See
Access rights are all or nothing for a model. Record rules narrow that down to specific records using a domain filter.
Example: users see only their own records
<record id="rule_my_model_own_records" model="ir.rule">
<field name="name">My Model: own records only</field>
<field name="model_id" ref="model_my_model"/>
<field name="domain_force">[('user_id', '=', user.id)]</field>
<field name="groups" eval="[(4, ref('group_my_module_user'))]"/>
</record>
Example: multi-company rule
<record id="rule_my_model_company" model="ir.rule">
<field name="name">My Model: multi-company</field>
<field name="model_id" ref="model_my_model"/>
<field name="domain_force">['|', ('company_id', '=', False), ('company_id', 'in', company_ids)]</field>
</record>
How rules combine
This is where most confusion happens:
- Rules linked to groups are combined with OR. If one of your groups allows a record, the user can access it.
- Global rules (no group) are combined with AND. They apply to everyone, and a record must pass all of them.
That is why a manager rule such as [(1, '=', 1)] can give managers full access while normal users stay limited to their own records. You can also set which operations a rule covers (read, write, create, delete) using the perm_* fields on the rule.
Field-Level and View-Level Security
Sometimes a user may see a record but not every detail on it.
Restrict a field by group
salary = fields.Float(groups='hr.group_hr_manager')
Users outside that group cannot read or write the field. Odoo also hides it from views for them.
Hide parts of a view
<field name="cost_price" groups="my_module.group_my_module_manager"/>
<button name="action_approve" string="Approve" type="object"
groups="my_module.group_my_module_manager"/>
Hiding a button in the view is not enough on its own. Also protect the Python method, because a user can still call it through an API request. Check permissions inside the method, or rely on the model’s access rights.
Common Odoo Security Rules Mistakes and How to Test Them
These mistakes cause most of the security problems I fix:
- Forgetting the access line for a new model. Regular users get an access error, while admins may not notice during testing.
- Using
sudo()to hide a permission problem. It bypasses all rules, so only use it when you have a clear reason and never with unchecked user input. - Wrong manifest order. The CSV loads before the groups exist and the module fails to install.
- Global rules that are too broad or too strict. Because global rules combine with AND, one bad rule can block everyone.
- No multi-company rule. Users in one company can then see another company’s records.
- Only testing as admin. Admin access hides most security bugs.
Test with real users
Create a test user for each group and try every action. You can also write automated tests that run code as a specific user:
from odoo.tests.common import TransactionCase
from odoo.exceptions import AccessError
class TestSecurity(TransactionCase):
def test_user_cannot_delete(self):
user = self.env.ref('base.user_demo')
record = self.env['my.model'].create({'name': 'Test'})
with self.assertRaises(AccessError):
record.with_user(user).unlink()
Security tests are cheap to write and catch problems before a client does. They also fit well with the habits I describe in my guide to Odoo custom module development.
Frequently Asked Questions
What is the difference between access rights and record rules in Odoo?
Access rights control which operations (read, write, create, delete) a group can perform on a whole model. Record rules filter which individual records those operations apply to.
Why do I get “You are not allowed to access” errors in Odoo?
This usually means the user’s group lacks an access right for the model, or a record rule blocks that record. Check ir.model.access.csv first, then the record rules.
Do record rules apply to the administrator?
Record rules apply to normal users, including administrators in most cases. Only the superuser and code running with sudo() bypass them.
How do I restrict a field to certain users in Odoo?
Add the groups attribute to the field definition in Python, and to the field in the view if you want it hidden from the interface too.
Should I use sudo() to fix permission errors?
Avoid it. Using sudo() bypasses security completely. Fix the underlying access right or rule instead, and use sudo() only for specific, well-checked actions.
Need a secure custom Odoo module or a security review of your existing setup? I build and audit Odoo modules for Odoo versions 8 to 19, including access control, multi-company rules and integrations. Start your project and tell me what you need.