Creating security groups in Odoo
Security groups are one of the most used principles in Odoo. By creating security groups you can define which users should be able to see and access what values. Security groups allow you to deny permissions models, to hide menus or to hide fields on views. In this tutorial we will create a security group, configure it to automatically add some users and we will see how to hide or show menu's for specific users within a group.
The first thing to do is to create a model. If you already have an existing model you can skip this chapter and continue with chapter 3. Still reading? Alright, let us create a new model. In this tutorial I will create a new model named 'demo.access.right', with just one simple field. Create a new Python file under the 'models' directory and create a new model with a field:
# -*- coding: utf-8 -*-from odoo import models, fields, apiclass DemoAccessRight(models.Model): _name = 'demo.access.right' name = fields.Char(string='Name', required=True)
We will use this model later on in this tutorial to show you how you can limit access on menuitems depending on the security group the user is in.
Alright, now that we have a model, the next thing to do is to create the security groups with the specific rights that you would like to give to this group.First of all open up the ir.model.access.csv file (under security/) and look at the top of the file. When you look at this file you'll see that there are a few columns. Let me explain them a bit into detail:
Let us start writing security groups. In my example I will create two groups: one with full rights for reading, writing, creating and deleting (admin behaviour) and one group that can only read records. Let me show you the result and I will then explain it further!
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink--ENTER--demo_admin,Model admin access,model_demo_access_right,tutorial_create_security_groups.group_manager,1,1,1,1--ENTER--demo_user,Model user access,model_demo_access_right,tutorial_create_security_groups.group_user,1,0,0,0
Look at those two rules for a minute and try to figure out what they do.I've created two lines, which means two groups, that are both for the model 'demo.access.right', where the first group has all rights on the model and the second group can only read data (look at the 1,1,1,1 and the 1,0,0,0).Do you notice something else? There is a group_id in the field named 'tutorial_create_security_groups.group_manager' on the first line and 'tutorial_create_security_groups.group_user' on the second line. What do these two mean? Well, the first part ('tutorial_create_security_groups') is the name of the module where you are creating groups for. The second part, 'group_manager' will link to an XML record in which we specify the rest of the details. Add those lines to your CSV file, save it and then close it.
Now that you've written the groups in the CSV we have the rules but in the CSV file they link to 'group_manager' and 'group_user', which we still haven't made anywhere. So let us make them! Create a new XML file under the 'security' folder so that we can write the XML code. In this tutorial I'll name the file 'user_groups.xml'. We now need three things:
Let's start by writing a record that will show our both groups in a dropdown:
This will create an option in the user his form that has the label 'Demo module access' and a description 'User access level for this module'. The sequence can be used to specify where it should come in the view. One down, two to go. We now need to create two group records ('group_user' and 'group_manager') so that the CSV can find and use these groups. The code should look like this:
Let me explain the code a bit more. The 'name' field will be the text that is shown to the user in the front-end.By setting the 'users' field (which links to another group user) you can say that by default users that belong to the other group should be added to this specific group. For example: with users set to 'base.user_root' I will by default enable this value for the user. By setting the field 'implied_ids' on the 'group_manager' I'm saying that if the user has the manager rights that he should also have rights as a user (ref('tutorial_create_security_groups.group_user') does this).
That is all! You've just created your own security groups and made them available on the user his form so that you can configure this for every user on his own.
So what if you have a menuitem that you do not want to show to the user group but only to the manager group? Or the other way around? You can easily solve this by adding the "groups=" attribute on the menuitem. By setting a group on the menuitem Odoo will automatically remove - or show - the menuitem. Let us first create a parent menuitem:
Now let us create a child menuitem that is only visible for the user group:
And last but not least a menuitem that is only visible for the manager group:
By simply adding the 'groups=' tag on the menuitems we can specify which group can access which menuitem! It is as simple as that. When you would now create a new user with the 'User' rights he would only see the menuitem for the user and not the admin menu.
The security groups and security rules are not very hard to set-up but allow you a very broad range of configuration. You can specify what a user can see just by in which groups or security rules the user is in. This allows you to show or hide menuitems, fields and options with very little effort. The possibilities are endless! Take some time to fully understand security groups and to get familiar with them, you'll need them quite often probably.