Skip to content

Commit

Permalink
[IMP] base_user_role: Adding alert in user when role is used
Browse files Browse the repository at this point in the history
  • Loading branch information
jesusramoneda authored and CRogos committed Jun 21, 2024
1 parent fe74ac9 commit 9b9c847
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
8 changes: 8 additions & 0 deletions base_user_role/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ class ResUsers(models.Model):
string="Role lines",
default=lambda self: self._default_role_lines(),
)

show_alert = fields.Boolean(compute="_compute_show_alert")

@api.depends("role_line_ids")
def _compute_show_alert(self):
for user in self:
user.show_alert = user.role_line_ids.filtered(lambda rec: rec.is_enabled)

role_ids = fields.One2many(
comodel_name="res.users.role",
string="Roles",
Expand Down
9 changes: 9 additions & 0 deletions base_user_role/tests/test_user_role.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,15 @@ def test_create_role_from_user(self):
role_group_ids = sorted(set(role.trans_implied_ids.ids))
self.assertEqual(user_group_ids, role_group_ids)

def test_show_alert_computation(self):
"""Test the computation of the `show_alert` field."""
self.user_id.write({"role_line_ids": [(0, 0, {"role_id": self.role1_id.id})]})
self.assertTrue(self.user_id.show_alert)

# disable role
self.user_id.role_line_ids.unlink()
self.assertFalse(self.user_id.show_alert)

def test_group_groups_into_role(self):
user_group_ids = [group.id for group in self.user_id.groups_id]
# Check that there is not a role with name: Test Role
Expand Down
20 changes: 20 additions & 0 deletions base_user_role/views/user.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,28 @@
</field>
</page>
</xpath>
<xpath expr="//page[@name='access_rights']/group" position="before">
<field name="show_alert" invisible="1" />
<div
class="alert alert-info text-center o_form_header"
attrs="{'invisible': [('show_alert', '!=', True)]}"
role="alert"
>
<div>
<strong
>The access rights of this user are managed by roles.</strong>
</div>
<div>
<em
>Any configuration changes made here will not be persistent.</em>
</div>
<a class="close" data-bs-dismiss="alert" href="#">x</a>
</div>
</xpath>

</field>
</record>

<record id="view_res_users_search_inherit" model="ir.ui.view">
<field name="name">res.users.search.inherit</field>
<field name="model">res.users</field>
Expand Down

0 comments on commit 9b9c847

Please sign in to comment.