in odoo17 i have this model from odoo import fields, models, api class…
- User
in odoo17 i have this model from odoo import fields, models, api
class Users(models.Model): _inherit = "res.users"
position = fields.Char(string='Position') type = fields.Selection([ ('admin', 'Admin'), ('moderator', 'Moderator'), ('reader', 'Reader'), ]) role = fields.Selection([ ('republic', 'Republic'), ('region', 'Region'), ('district', 'District'), ('mahalla', 'Mahalla'), ], string='Role', default='mahalla', required=True) is_republic = fields.Boolean(string='Is Republic') region_id = fields.Many2one('region', string="Region", required=False) is_region = fields.Boolean() district_id = fields.Many2one('district', string="District", required=False) is_district = fields.Boolean() mahalla_id = fields.Many2one('mahalla', string="Mahalla", required=False) is_mahalla = fields.Boolean() @api.onchange('login') def on_change_login(self): if self.login: self.email = self.login @api.onchange('role') def on_change_role(self): if self.role == 'republic': self.is_republic = True self.is_region = True self.is_district = True self.is_mahalla = True elif self.role == 'region': self.is_region = False self.is_district = True self.is_republic = False self.is_mahalla = True elif self.role == 'district': self.is_district = False self.is_region = False self.is_republic = False self.is_mahalla = True elif self.role == 'mahalla': self.is_region = False self.is_republic = False self.is_district = False self.is_mahalla = False def action_change_password(self): self.ensure_one() return { 'type': 'ir.actions.act_window', 'res_model': 'change.password.wizard', 'view_mode': 'form', 'view_id': self.env.ref('base.change_password_wizard_view').id, 'target': 'new', 'context': {'default_user_id': self.id}, } @api.model def create(self, vals): user = super(Users, self).create(vals) user.assign_group_based_on_role() return user def write(self, vals): if 'role' in vals and vals.get('role') != self.role: res = super(Users, self).write(vals) self.assign_group_based_on_role() else: res = super(Users, self).write(vals) return res def assign_group_based_on_role(self): republic_group = self.env.ref('mahallam.group_republic_user') region_group = self.env.ref('mahallam.group_region_user') district_group = self.env.ref('mahallam.group_district_user') mahalla_group = self.env.ref('mahallam.group_mahalla_user') self.groups_id = [(3, republic_group.id), (3, region_group.id), (3, district_group.id), (3, mahalla_group.id)] if self.role == 'republic': self.groups_id = [(4, republic_group.id)] elif self.role == 'mahalla': self.groups_id = [(4, mahalla_group.id)] elif self.role == 'region': self.groups_id = [(4, region_group.id)] elif self.role == 'district': self.groups_id = [(4, district_group.id)]andded these lines to it: type = fields.Selection([ ('admin', 'Admin'), ('moderator', 'Moderator'), ('reader', 'Reader'), ]), admin is superadmin(developer), moderators are whom can crud in projectt around their scope, readers like moderators but can only read around their scope, improve functionality, create groups, improve xml that i will provide at the end of this request, moderators and readers have roles, superadmin is one so when superadmin is selected hide roles, localization fields, my xml: res.users.tree res.users
<record id="view_user_form_custom" model="ir.ui.view"> <field name="name">res.users.form.custom</field> <field name="model">res.users</field> <field name="arch" type="xml"> <form> <header> <button string="Set Password" type="object" name="action_change_password" class="btn-primary" invisible="id == False" /> </header> <sheet> <div class="oe_avatar_group"> <field name="image_1920" widget="image" class="oe_avatar"/> </div> <group> <group> <field name="name"/> <field name="email" invisible="1"/> <field name="login"/> <field name="phone" placeholder="+998 xx xxx xx xx"/> <field name="role"/> <field name="region_id" invisible="is_region"/> <field name="is_region" invisible="1"/> <field name="district_id" invisible="is_district" domain="[('region_id', '=', region_id)]" /> <field name="is_district" invisible="1"/> <field name="mahalla_id" invisible="is_mahalla" domain="[('district_id', '=', district_id)]"/> <field name="is_mahalla" invisible="1"/> <field name="position"/> </group> </group> </sheet> </form> </field> </record> <record id="view_user_kanban_custom" model="ir.ui.view"> <field name="name">res.users.kanban</field> <field name="model">res.users</field> <field name="arch" type="xml"> <kanban class="o_res_partner_kanban"> <field name="image_1920" widget="image"/> <field name="name"/> <field name="email"/> <field name="phone"/> <field name="role"/> <field name="region_id"/> <field name="district_id"/> <field name="position"/> </kanban> </field> </record> <record id="action_user_custom" model="ir.actions.act_window"> <field name="name">Users</field> <field name="res_model">res.users</field> <field name="view_mode">form</field> <field name="view_id" ref="view_user_form_custom"/> </record> <record id="action_user_list" model="ir.actions.act_window"> <field name="name">Users</field> <field name="res_model">res.users</field> <field name="view_mode">kanban,tree</field> <field name="view_id" ref="view_user_tree_custom"/> </record> </data> <menuitem id="user_menu" name="Admin Users Menu" sequence="1"/> <menuitem id="user_managers_menu" name="User Managers" parent="user_menu" action="action_user_list" sequence="1"/> <menuitem id="user_add_menu" name="Add User" parent="user_menu" action="action_user_custom" sequence="2"/> - Assistant
To achieve the desired functionality in Odoo 17, you'll need to enhance both your Python model and XML views. This involves:
- Ensuring the
typefield is correctly defined and handling its logic. - Creating appropriate user groups for
admin,moderator, andreader. - Updating the XML views to conditionally display fields based on the selected
type.
Below, I'll guide you through each of these steps in detail.
1. Enhancing the Python Model
a. Avoid Duplicate Field Definitions
First, ensure that the
typefield is defined only once in your model. From your provided code, it seems there's a duplicate definition. We'll consolidate it as follows:from odoo import fields, models, api class Users(models.Model): _inherit = "res.users" position = fields.Char(string='Position') type = fields.Selection([ ('admin', 'Admin'), ('moderator', 'Moderator'), ('reader', 'Reader'), ], string='Type', default='reader', required=True) role = fields.Selection([ ('republic', 'Republic'), ('region', 'Region'), ('district', 'District'), ('mahalla', 'Mahalla'), ], string='Role', default='mahalla', required=True) is_republic = fields.Boolean(string='Is Republic') region_id = fields.Many2one('region', string="Region", required=False) is_region = fields.Boolean() district_id = fields.Many2one('district', string="District", required=False) is_district = fields.Boolean() mahalla_id = fields.Many2one('mahalla', string="Mahalla", required=False) is_mahalla = fields.Boolean() @api.onchange('login') def on_change_login(self): if self.login: self.email = self.login @api.onchange('role') def on_change_role(self): if self.role == 'republic': self.is_republic = True self.is_region = True self.is_district = True self.is_mahalla = True elif self.role == 'region': self.is_region = False self.is_district = True self.is_republic = False self.is_mahalla = True elif self.role == 'district': self.is_district = False self.is_region = False self.is_republic = False self.is_mahalla = True elif self.role == 'mahalla': self.is_region = False self.is_republic = False self.is_district = False self.is_mahalla = False @api.onchange('type') def on_change_type(self): if self.type == 'admin': # When admin is selected, hide role and localization fields self.role = False self.is_republic = False self.is_region = False self.is_district = False self.is_mahalla = False self.region_id = False self.district_id = False self.mahalla_id = False else: # Re-trigger role-based visibility self.on_change_role() def action_change_password(self): self.ensure_one() return { 'type': 'ir.actions.act_window', 'res_model': 'change.password.wizard', 'view_mode': 'form', 'view_id': self.env.ref('base.change_password_wizard_view').id, 'target': 'new', 'context': {'default_user_id': self.id}, } @api.model def create(self, vals): user = super(Users, self).create(vals) user.assign_group_based_on_type_and_role() return user def write(self, vals): res = super(Users, self).write(vals) if 'type' in vals or 'role' in vals: self.assign_group_based_on_type_and_role() return res def assign_group_based_on_type_and_role(self): # Define group references superadmin_group = self.env.ref('base.group_system') moderator_groups = { 'republic': self.env.ref('mahallam.group_republic_moderator'), 'region': self.env.ref('mahallam.group_region_moderator'), 'district': self.env.ref('mahallam.group_district_moderator'), 'mahalla': self.env.ref('mahallam.group_mahalla_moderator'), } reader_groups = { 'republic': self.env.ref('mahallam.group_republic_reader'), 'region': self.env.ref('mahallam.group_region_reader'), 'district': self.env.ref('mahallam.group_district_reader'), 'mahalla': self.env.ref('mahallam.group_mahalla_reader'), } # First, remove all existing groups related to role and type all_related_groups = [ superadmin_group, *moderator_groups.values(), *reader_groups.values(), ] self.groups_id = [(3, group.id) for group in all_related_groups] # Assign groups based on type and role if self.type == 'admin': self.groups_id = [(4, superadmin_group.id)] elif self.type == 'moderator': role_group = moderator_groups.get(self.role) if role_group: self.groups_id = [(4, role_group.id)] elif self.type == 'reader': role_group = reader_groups.get(self.role) if role_group: self.groups_id = [(4, role_group.id)]b. Explanation of Model Enhancements
-
Field Definitions:
typeField: Determines the user's type (admin,moderator,reader).roleField: Specifies the user's role within their scope (republic,region,district,mahalla).
-
Onchange Methods:
on_change_type: When thetypeis set toadmin, it clears theroleand localization fields. For other types, it re-invokes theon_change_roleto manage field visibility based on the role.on_change_role: Manages the visibility of localization fields based on the selectedrole.
-
Group Assignment Logic:
assign_group_based_on_type_and_role: Assigns the appropriate user groups based on bothtypeandrole. This method ensures that:- Admins are assigned to the superadmin group (
base.group_system). - Moderators are assigned to their respective moderator groups based on their
role. - Readers are assigned to their respective reader groups based on their
role.
- Admins are assigned to the superadmin group (
- Before assigning new groups, it removes any existing related groups to prevent overlapping permissions.
-
Overridden
createandwriteMethods:- These ensure that group assignments are handled whenever a user is created or updated, especially when
typeorrolechanges.
- These ensure that group assignments are handled whenever a user is created or updated, especially when
2. Defining User Groups in XML
To manage permissions effectively, you need to define specific user groups for moderators and readers based on their roles. Here's how you can define these groups:
<?xml version="1.0" encoding="utf-8"?> <odoo> <data> <!-- Categories for User Groups --> <record id="category_user_management" model="res.groups.category"> <field name="name">User Management</field> <field name="parent_id" ref="base.module_category_user_type"/> </record> <!-- Super Admin Group (Already exists in Odoo as base.group_system) --> <!-- Moderator Groups --> <record id="group_republic_moderator" model="res.groups"> <field name="name">Republic Moderator</field> <field name="category_id" ref="mahallam.category_user_management"/> <field name="implied_ids" eval="[]"/> </record> <record id="group_region_moderator" model="res.groups"> <field name="name">Region Moderator</field> <field name="category_id" ref="mahallam.category_user_management"/> <field name="implied_ids" eval="[]"/> </record> <record id="group_district_moderator" model="res.groups"> <field name="name">District Moderator</field> <field name="category_id" ref="mahallam.category_user_management"/> <field name="implied_ids" eval="[]"/> </record> <record id="group_mahalla_moderator" model="res.groups"> <field name="name">Mahalla Moderator</field> <field name="category_id" ref="mahallam.category_user_management"/> <field name="implied_ids" eval="[]"/> </record> <!-- Reader Groups --> <record id="group_republic_reader" model="res.groups"> <field name="name">Republic Reader</field> <field name="category_id" ref="mahallam.category_user_management"/> <field name="implied_ids" eval="[]"/> </record> <record id="group_region_reader" model="res.groups"> <field name="name">Region Reader</field> <field name="category_id" ref="mahallam.category_user_management"/> <field name="implied_ids" eval="[]"/> </record> <record id="group_district_reader" model="res.groups"> <field name="name">District Reader</field> <field name="category_id" ref="mahallam.category_user_management"/> <field name="implied_ids" eval="[]"/> </record> <record id="group_mahalla_reader" model="res.groups"> <field name="name">Mahalla Reader</field> <field name="category_id" ref="mahallam.category_user_management"/> <field name="implied_ids" eval="[]"/> </record> <!-- Access Rights --> <!-- Define access rights for each group as needed --> <!-- Example: Allow moderators to create, read, update, delete within their scope --> <!-- Define these permissions in separate <record> tags for ir.model.access.csv or similar --> </data> </odoo>a. Explanation of Group Definitions
-
Categories:
- A new category
User Managementis created to organize custom user groups.
- A new category
-
Moderator Groups:
- Four moderator groups are defined based on their scope (
republic,region,district,mahalla).
- Four moderator groups are defined based on their scope (
-
Reader Groups:
- Similarly, four reader groups are defined corresponding to each scope.
-
Access Rights:
-
Important: After defining the groups, you need to set up the appropriate access rights (
ir.model.access.csv) to control what each group can do. This typically includes permissions likeread,write,create, anddeleteon specific models. -
Example Entry in
ir.model.access.csv:id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink access_republic_moderator,access_republic_moderator,model_project,mahallam.group_republic_moderator,1,1,1,1 access_republic_reader,access_republic_reader,model_project,mahallam.group_republic_reader,1,0,0,0- The above example grants full CRUD permissions to
Republic Moderatorand read-only access toRepublic Readeron theprojectmodel.
- The above example grants full CRUD permissions to
-
Note: Replace
model_projectwith the actual model names you wish to control.
-
3. Updating XML Views
To ensure that certain fields are visible or hidden based on the selected
type, you'll need to modify your XML views accordingly.a. Updated Form View
Here's the enhanced form view with conditional visibility based on the
typefield:<?xml version="1.0" encoding="utf-8"?> <odoo> <data> <!-- Custom Form View for res.users --> <record id="view_user_form_custom" model="ir.ui.view"> <field name="name">res.users.form.custom</field> <field name="model">res.users</field> <field name="arch" type="xml"> <form> <header> <button string="Set Password" type="object" name="action_change_password" class="btn-primary" attrs="{'invisible': [('id', '=', False)]}" /> </header> <sheet> <div class="oe_avatar_group"> <field name="image_1920" widget="image" class="oe_avatar"/> </div> <group> <group> <field name="name"/> <field name="email" invisible="1"/> <field name="login"/> <field name="phone" placeholder="+998 xx xxx xx xx"/> <!-- Type Field --> <field name="type"/> <!-- Role Field: Visible only if type is not admin --> <field name="role" attrs="{'invisible': [('type', '=', 'admin')]}"/> <!-- Localization Fields: Visible only if type is not admin --> <field name="region_id" attrs="{'invisible': [('type', '=', 'admin')]}"/> <field name="is_region" invisible="1"/> <field name="district_id" attrs="{'invisible': [('type', '=', 'admin')]}", domain="[('region_id', '=', region_id)]" /> <field name="is_district" invisible="1"/> <field name="mahalla_id" attrs="{'invisible': [('type', '=', 'admin')]}", domain="[('district_id', '=', district_id)]" /> <field name="is_mahalla" invisible="1"/> <field name="position"/> </group> </group> </sheet> </form> </field> </record> <!-- Custom Tree View for res.users --> <record id="view_user_tree_custom" model="ir.ui.view"> <field name="name">res.users.tree.custom</field> <field name="model">res.users</field> <field name="arch" type="xml"> <tree string="Users" default_order="create_date desc"> <field name="name"/> <field name="type"/> <field name="role"/> <field name="region_id"/> <field name="district_id"/> <field name="mahalla_id"/> <field name="position"/> </tree> </field> </record> <!-- Custom Kanban View for res.users --> <record id="view_user_kanban_custom" model="ir.ui.view"> <field name="name">res.users.kanban.custom</field> <field name="model">res.users</field> <field name="arch" type="xml"> <kanban class="o_res_partner_kanban"> <field name="image_1920" widget="image"/> <field name="name"/> <field name="email"/> <field name="phone"/> <field name="type"/> <field name="role"/> <field name="region_id"/> <field name="district_id"/> <field name="position"/> </kanban> </field> </record> <!-- Action Definitions --> <record id="action_user_custom" model="ir.actions.act_window"> <field name="name">Users</field> <field name="res_model">res.users</field> <field name="view_mode">form</field> <field name="view_id" ref="view_user_form_custom"/> </record> <record id="action_user_list" model="ir.actions.act_window"> <field name="name">Users</field> <field name="res_model">res.users</field> <field name="view_mode">kanban,tree</field> <field name="view_id" ref="view_user_tree_custom"/> </record> <!-- Menu Items --> <menuitem id="user_menu" name="Admin Users Menu" sequence="1"/> <menuitem id="user_managers_menu" name="User Managers" parent="user_menu" action="action_user_list" sequence="1"/> <menuitem id="user_add_menu" name="Add User" parent="user_menu" action="action_user_custom" sequence="2"/> </data> </odoo>b. Explanation of View Enhancements
-
Form View (
view_user_form_custom):typeField: Added to allow selection betweenadmin,moderator, andreader.- Conditional Visibility:
roleField: Hidden whentypeisadmin.- Localization Fields (
region_id,district_id,mahalla_id): Hidden whentypeisadmin.
- Button Visibility Correction:
- Updated the
invisibleattribute for the "Set Password" button to properly evaluate the condition.
- Updated the
-
Tree View (
view_user_tree_custom):- Included
typeandrolefields for better visibility in list views.
- Included
-
Kanban View (
view_user_kanban_custom):- Included
typeandrolefields to display relevant information on Kanban cards.
- Included
-
Menu Items:
- Ensured that the custom views are linked correctly to the menu items for easy navigation.
4. Additional Recommendations
a. Access Rights Configuration
Ensure that each user group has the appropriate access rights. This typically involves creating entries in the
ir.model.access.csvfile to define what each group can do with different models.Example
ir.model.access.csv:id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink access_republic_moderator,access_republic_moderator,model_project,mahallam.group_republic_moderator,1,1,1,1 access_republic_reader,access_republic_reader,model_project,mahallam.group_republic_reader,1,0,0,0 access_region_moderator,access_region_moderator,model_project,mahallam.group_region_moderator,1,1,1,1 access_region_reader,access_region_reader,model_project,mahallam.group_region_reader,1,0,0,0 access_district_moderator,access_district_moderator,model_project,mahallam.group_district_moderator,1,1,1,1 access_district_reader,access_district_reader,model_project,mahallam.group_district_reader,1,0,0,0 access_mahalla_moderator,access_mahalla_moderator,model_project,mahallam.group_mahalla_moderator,1,1,1,1 access_mahalla_reader,access_mahalla_reader,model_project,mahallam.group_mahalla_reader,1,0,0,0- Replace
model_projectwith the actual model names you intend to control. - Adjust permissions (
perm_read,perm_write,perm_create,perm_unlink) as per your requirements.
b. Security Considerations
- Superadmin (
admintype): Ensure that only trusted users are assigned to thebase.group_systemto prevent unauthorized access. - Moderators and Readers: Validate that these groups have access strictly limited to their intended scope to maintain data integrity and security.
c. Testing
After implementing these changes:
-
Update the Module:
- Restart the Odoo server.
- Upgrade your custom module to apply the changes.
-
Create Test Users:
- Admin User: Select
typeasadminand ensure thatroleand localization fields are hidden. - Moderator User: Select
typeasmoderator, choose arole, and verify that appropriate fields are visible. - Reader User: Select
typeasreader, choose arole, and check field visibility.
- Admin User: Select
-
Verify Group Assignments:
- Ensure that users are assigned to the correct groups based on their
typeandrole. - Check that permissions align with the group definitions.
- Ensure that users are assigned to the correct groups based on their
-
Functionality Checks:
- Confirm that moderators can perform CRUD operations within their scope.
- Ensure that readers have read-only access within their scope.
- Validate that admins have unrestricted access.
By following the above steps, you'll have a robust user management system in Odoo 17 that differentiates between
admin,moderator, andreadertypes, each with their respective roles and permissions. This setup enhances security and ensures that users have access only to the functionalities pertinent to their responsibilities. - Ensuring the