From 22343a35be62dbeb9068d3a1c325ec35adadbf66 Mon Sep 17 00:00:00 2001 From: Guillaume Pothier Date: Tue, 20 Jun 2023 15:07:47 -0400 Subject: [PATCH 01/13] Add "Create role from user" feature --- base_user_role/README.rst | 5 +++ base_user_role/__init__.py | 1 + base_user_role/__manifest__.py | 1 + base_user_role/security/ir.model.access.csv | 1 + base_user_role/wizard/__init__.py | 1 + base_user_role/wizard/create_from_user.py | 46 +++++++++++++++++++++ base_user_role/wizard/create_from_user.xml | 34 +++++++++++++++ 7 files changed, 89 insertions(+) create mode 100644 base_user_role/wizard/__init__.py create mode 100644 base_user_role/wizard/create_from_user.py create mode 100644 base_user_role/wizard/create_from_user.xml diff --git a/base_user_role/README.rst b/base_user_role/README.rst index 98720bd6b..fefc91014 100644 --- a/base_user_role/README.rst +++ b/base_user_role/README.rst @@ -78,6 +78,10 @@ Add users (with dates or not): :width: 80 % :align: center +Instead of creating roles from scratch, it is possible to create a role +based on the groups of an existing user: select or open the user and choose +"Create role from user" in the action menu. + Usage ===== @@ -116,6 +120,7 @@ Contributors * Kevin Khao * Tatiana Deribina (https://sprintit.fi) * Guillem Casassas +* Guillaume Pothier Do not contact contributors directly about support or help with technical issues. diff --git a/base_user_role/__init__.py b/base_user_role/__init__.py index 0650744f6..9b4296142 100644 --- a/base_user_role/__init__.py +++ b/base_user_role/__init__.py @@ -1 +1,2 @@ from . import models +from . import wizard diff --git a/base_user_role/__manifest__.py b/base_user_role/__manifest__.py index 97812cd10..acce0f209 100644 --- a/base_user_role/__manifest__.py +++ b/base_user_role/__manifest__.py @@ -19,6 +19,7 @@ "views/role.xml", "views/user.xml", "views/group.xml", + "wizard/create_from_user.xml", ], "installable": True, } diff --git a/base_user_role/security/ir.model.access.csv b/base_user_role/security/ir.model.access.csv index 04790a3d5..d625a8d77 100644 --- a/base_user_role/security/ir.model.access.csv +++ b/base_user_role/security/ir.model.access.csv @@ -1,3 +1,4 @@ id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink access_res_users_role,access_res_users_role,model_res_users_role,"base.group_erp_manager",1,1,1,1 access_res_users_role_line,access_res_users_role_line,model_res_users_role_line,"base.group_erp_manager",1,1,1,1 +access_base_user_role_create_from_user_wizard,access_base_user_role_create_from_user_wizard,model_base_user_role_create_from_user_wizard,"base.group_erp_manager",1,1,1,1 diff --git a/base_user_role/wizard/__init__.py b/base_user_role/wizard/__init__.py new file mode 100644 index 000000000..245cf1a9a --- /dev/null +++ b/base_user_role/wizard/__init__.py @@ -0,0 +1 @@ +from . import create_from_user diff --git a/base_user_role/wizard/create_from_user.py b/base_user_role/wizard/create_from_user.py new file mode 100644 index 000000000..d99048b56 --- /dev/null +++ b/base_user_role/wizard/create_from_user.py @@ -0,0 +1,46 @@ +from odoo import models, fields + + +class CreateFromUserWizard(models.TransientModel): + _name = "base_user_role.create_from_user_wizard" + _description = "Create role from user wizard" + + name = fields.Char("Name") + assign_to_user = fields.Boolean("Assign to user", default=True) + + def create_from_user(self): + self.ensure_one() + + user_ids = self.env.context.get("active_ids", []) + assert len(user_ids) == 1 + + user_id = user_ids[0] + + role_obj = self.env["res.users.role"] + role_line_obj = self.env["res.users.role.line"] + user_obj = self.env["res.users"] + + user = user_obj.browse(user_id) + + role = role_obj.create({ + "name": self.name, + }) + + role.implied_ids = [(6, 0, user.groups_id.ids)] + + if self.assign_to_user: + role_line_obj.create({ + "role_id": role.id, + "user_id": user_id, + }) + + return { + "context": self.env.context, + "name": "Role", + "view_type": "form", + "view_mode": "form", + "res_model": "res.users.role", + "res_id": role.id, + "target": "current", + "type": "ir.actions.act_window", + } diff --git a/base_user_role/wizard/create_from_user.xml b/base_user_role/wizard/create_from_user.xml new file mode 100644 index 000000000..6dd293f9e --- /dev/null +++ b/base_user_role/wizard/create_from_user.xml @@ -0,0 +1,34 @@ + + + + Create role from user + base_user_role.create_from_user_wizard + form + new + + + + + Create role from user + base_user_role.create_from_user_wizard + +
+ + + + + +
+
+
+
+
+
From b075ed6b0f09fe1676ba6df79aacffc1bb2f45fc Mon Sep 17 00:00:00 2001 From: Guillaume Pothier Date: Wed, 21 Jun 2023 14:53:06 -0400 Subject: [PATCH 02/13] Fix formatting according to pre-commit hook --- base_user_role/wizard/create_from_user.py | 20 ++++++++++++-------- base_user_role/wizard/create_from_user.xml | 12 ++++++------ 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/base_user_role/wizard/create_from_user.py b/base_user_role/wizard/create_from_user.py index d99048b56..68d87233d 100644 --- a/base_user_role/wizard/create_from_user.py +++ b/base_user_role/wizard/create_from_user.py @@ -1,4 +1,4 @@ -from odoo import models, fields +from odoo import fields, models class CreateFromUserWizard(models.TransientModel): @@ -22,17 +22,21 @@ def create_from_user(self): user = user_obj.browse(user_id) - role = role_obj.create({ - "name": self.name, - }) + role = role_obj.create( + { + "name": self.name, + } + ) role.implied_ids = [(6, 0, user.groups_id.ids)] if self.assign_to_user: - role_line_obj.create({ - "role_id": role.id, - "user_id": user_id, - }) + role_line_obj.create( + { + "role_id": role.id, + "user_id": user_id, + } + ) return { "context": self.env.context, diff --git a/base_user_role/wizard/create_from_user.xml b/base_user_role/wizard/create_from_user.xml index 6dd293f9e..200ba5254 100644 --- a/base_user_role/wizard/create_from_user.xml +++ b/base_user_role/wizard/create_from_user.xml @@ -1,11 +1,11 @@ - + Create role from user base_user_role.create_from_user_wizard form new - + @@ -20,10 +20,10 @@