how-to-customize-access-rights-in-odoo-18-using-python-code

How to Customize Access Rights in Odoo 18 Using Python Code?

In any business, managing user access to critical data and operations is not just a best practice – it’s essential. Odoo 18, with its highly customizable ERP framework, encourages businesses to fine-tune user access rights. Whether safeguarding sensitive information or streamlining workflows, configuring user permissions ensures your system is secure and efficient.

But how do you ensure the right people access the correct data without overwhelming them with unnecessary privileges? The answer lies in Odoo’s powerful access control mechanisms. This blog takes you step-by-step through customizing user access rights in Odoo 18 using Python code. Whether you are a beginner or an experienced developer, this guide will show you how to create user groups, enforce model-level restrictions, and apply record rules that adapt to your unique business needs.

By the end of this blog, you’ll understand how to control access precisely and why this customisation is an innovation for your organisation’s security and productivity.

Why Access Rights Matter

Access rights in Odoo 18 define what a user can see, create, edit, or delete in the system. Properly managing access rights:

  • Protects sensitive information.
  • Automate workflows by showing users only relevant data.
  • Prevents accidental or unauthorized modifications.
  • Ensures compliance with organizational policies.

Utilising Odoo’s tools – Access Control Lists (ACLs), record rules, and Python overrides – allows you to achieve fine-grained control over user access.

1. Create Custom User Groups

The first step in customizing access rights in Odoo 18 is defining user groups. Groups represent roles within your organization, such as “Custom Manager” or “Custom User.”

				
					security/user_groups.xml


   
       <!-- Custom Manager Group -->
       
           Custom Manager
           
       

       <!-- Custom User Group -->
       
           Custom User
           
       

   


				
			

What it does:

  • This defines two groups: Custom Manager and Custom User.
  • These groups will later be used to restrict access.
how-to-customize-access-rights-in-odoo-18-using-python-code

2. Restrict Access with ACLs

Access Control Lists (ACLs) allow you to define permissions at the model level. You can control whether a group can read, create, write, or delete records for a specific model.

				
					security/ir.model.access.csv
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
user_restrict.access_user_restriction,access_user_restriction,user_restrict.model_user_restriction,user_restrict.group_custom_manager,1,1,1,1
user_restrict.access_user_restriction,access_user_restriction,user_restrict.model_user_restriction,user_restrict.group_custom_user,1,0,0,0



				
			

Explanation:

  • Custom Managers can read, write, create, and delete records.
  • Custom Users can only read records (no editing, creating, or deleting).

3. Apply Record Rules

To restrict users from accessing specific records, define record rules in an XML file:

				
					
security/record_rules.xml


   
       <!-- Restrict users to see only their records -->
       
           Custom User: Only See Own Records
           
           [('user_id', '=', user.id)]
           
       
   


				
			

Explanation:

  • Users in the Custom User group can only see records they created.

  • Custom Managers: This rule does not restrict them.

4. Restrict Access in Python Code

Sometimes, you may want to enforce additional restrictions at the model level. You can override the create, write, and unlink methods in your model.

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

class UserRestriction(models.Model):
   _name = 'user.restriction'
   _description = 'UserRestriction'

   name = fields.Char(string="Name", required=True)
   user_id = fields.Many2one('res.users', string="Owner", default=lambda self: self.env.user)

   @api.model
   def create(self, vals):
       if not self.env.user.has_group('user_restrict.group_custom_manager'):
           raise AccessError("Only managers can create records.")
       return super(UserRestriction, self).create(vals)

   def write(self, vals):
       if not self.env.user.has_group('user_restrict.group_custom_manager'):
           raise AccessError("Only managers can edit records.")
       return super(UserRestriction, self).write(vals)

   def unlink(self):
       if not self.env.user.has_group('user_restrict.group_custom_manager'):
           raise AccessError("Only managers can delete records.")
       return super(UserRestriction, self).unlink()

class SaleOrder(models.Model):
   _inherit = 'sale.order'

   def unlink(self):
       if not self.env.user.has_group('user_restrict.group_custom_manager'):
           raise AccessError("You are not allowed to delete quotations.")
       return super(SaleOrder, self).unlink()

				
			

Explanation:

  • Users in the Custom User group cannot create, edit, or delete records.
  • If a user lacks the Custom Manager role, they will see an AccessError.

5. Testing Access Rights

After implementing the above changes, it’s essential to test them:

  1. Log in as a Custom User:

    • Verify that you can only view records you own.

    • Confirm you cannot create, edit, or delete records.

  2. Log in as a Custom Manager:

    • Ensure you have full access to all records and functionality.

  3. Test Boundary Cases:

    • Attempt unauthorized actions to confirm that restrictions work as intended.

how-to-customize-access-rights-in-odoo-18-using-python-code

Best Practices for Managing Access Rights in Odoo 18

  • Plan Ahead:

    Before customizing access rights, map out the roles and permissions needed for your organization.

  • Keep Security Files Organized:

    To maintain clarity, use separate XML files for groups, ACLs, and record rules.

  • Test in a Development Environment:

    Always test changes in a non-production environment to avoid disrupting operations.

  • Document Your Changes:

    Keep detailed documentation for future reference or debugging.

Customizing Access Rights in Odoo 18

Customizing access rights in Odoo 18 is more than just a technical exercise – it’s a strategic decision that shapes how efficiently your team collaborates and how securely your data is handled. Utilizing Python to create user groups, enforce model restrictions, and apply record-level rules encourages your business to operate smarter and safer.

As you refine these settings, always keep your organizational goals and workflows in mind. A well-configured access system doesn’t just protect – it enables.

With Odoo 18, you manage permissions and build a foundation for secure, updated success.

Picture of Christiya Joseph

Christiya Joseph

I'm Christiya Joseph, a Junior Odoo Developer passionate about creating efficient ERP solutions. I specialize in customizing modules, writing backend logic in Python, and designing intuitive views with XML. This blog is where I share practical tips, insights, and solutions from my Odoo journey.

Get a free quote