Browse Source

[IMP] Use default user to define default roles to create for a new user

14.0
Benjamin Willig 7 years ago
committed by Jesús Alan Ramos Rodríguez
parent
commit
65f2806095
  1. 3
      base_user_role/README.rst
  2. 2
      base_user_role/__manifest__.py
  3. 20
      base_user_role/models/user.py
  4. 19
      base_user_role/tests/test_user_role.py
  5. 6
      base_user_role/views/role.xml
  6. 2
      base_user_role/views/user.xml

3
base_user_role/README.rst

@ -33,6 +33,9 @@ To configure this module, you need to go to *Configuration / Users / Roles*,
and create a new role. From there, you can add groups to compose your role,
and then associate users to it.
You can also define default roles for a new user by editing the user called
"Default User".
Roles:
.. image:: /base_user_role/static/description/roles.png

2
base_user_role/__manifest__.py

@ -3,7 +3,7 @@
{
'name': 'User roles',
'version': '11.0.1.0.0',
'version': '11.0.1.0.1',
'category': 'Tools',
'author': 'ABF OSIELL, Odoo Community Association (OCA)',
'license': 'AGPL-3',

20
base_user_role/models/user.py

@ -8,11 +8,29 @@ class ResUsers(models.Model):
role_line_ids = fields.One2many(
comodel_name='res.users.role.line',
inverse_name='user_id', string="Role lines")
inverse_name='user_id',
string="Role lines",
default=lambda self: self._default_role_lines()
)
role_ids = fields.One2many(
comodel_name='res.users.role', string="Roles",
compute='_compute_role_ids')
@api.model
def _default_role_lines(self):
default_user = self.env.ref(
'base.default_user', raise_if_not_found=False)
default_values = []
if default_user:
for role_line in default_user.role_line_ids:
default_values.append({
'role_id': role_line.role_id.id,
'date_from': role_line.date_from,
'date_to': role_line.date_to,
'is_enabled': role_line.is_enabled,
})
return default_values
@api.multi
@api.depends('role_line_ids.role_id')
def _compute_role_ids(self):

19
base_user_role/tests/test_user_role.py

@ -13,6 +13,7 @@ class TestUserRole(TransactionCase):
self.user_model = self.env['res.users']
self.role_model = self.env['res.users.role']
self.default_user = self.env.ref('base.default_user')
self.user_id = self.user_model.create(
{'name': "USER TEST (ROLES)", 'login': 'user_test_roles'})
@ -140,3 +141,21 @@ class TestUserRole(TransactionCase):
user_group_ids = sorted(set([
group.id for group in self.user_id.groups_id]))
self.assertEqual(user_group_ids, [])
def test_default_user_roles(self):
self.default_user.write({
'role_line_ids': [
(0, 0, {
'role_id': self.role1_id.id,
}),
(0, 0, {
'role_id': self.role2_id.id,
})
]
})
user = self.user_model.create({
'name': "USER TEST (DEFAULT ROLES)",
'login': 'user_test_default_roles'
})
roles = self.role_model.browse([self.role1_id.id, self.role2_id.id])
self.assertEqual(user.role_ids, roles)

6
base_user_role/views/role.xml

@ -7,7 +7,7 @@
<field name="name">res.users.role.form</field>
<field name="model">res.users.role</field>
<field name="arch" type="xml">
<form string="Role">
<form>
<sheet>
<group>
<field name="name"/>
@ -19,7 +19,7 @@
</page>
<page string="Users">
<field name="line_ids" nolabel="1">
<tree editable="bottom" colors="grey: not is_enabled;">
<tree editable="bottom" decoration-muted="not is_enabled">
<field name="user_id"/>
<field name="date_from"/>
<field name="date_to"/>
@ -37,7 +37,7 @@
<field name="name">res.users.role.tree</field>
<field name="model">res.users.role</field>
<field name="arch" type="xml">
<tree string="Role">
<tree>
<field name="name"/>
<field name="user_ids"/>
</tree>

2
base_user_role/views/user.xml

@ -11,7 +11,7 @@
<xpath expr="//notebook/page[1]" position="before">
<page string="Roles">
<field name="role_line_ids" nolabel="1">
<tree editable="bottom" colors="grey: not is_enabled;">
<tree editable="bottom" decoration-muted="not is_enabled">
<field name="role_id" required="1"/>
<field name="date_from"/>
<field name="date_to"/>

Loading…
Cancel
Save