Skip to content

Commit cdf6a61

Browse files
committed
impersonate_login: fw restrict impersonate user w admin
1 parent 6286d73 commit cdf6a61

File tree

10 files changed

+122
-3
lines changed

10 files changed

+122
-3
lines changed

impersonate_login/README.rst

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ following measures are in place:
4040
- Mails and messages are sent from the original user.
4141
- Impersonated logins are logged and can be consulted through the
4242
Settings -> Technical menu.
43-
-
43+
- To prevent users with "Administration: Settings" rights from being
44+
impersonated, enable the restrict_impersonate_admin_settings field in
45+
the settings. This will restrict the ability to impersonate users
46+
with administrative access to the settings.
4447

4548
There is an alternative module to allow logins as another user
4649
(auth_admin_passkey), but it does not support these security mechanisms.
@@ -55,6 +58,10 @@ Configuration
5558

5659
The impersonating user must belong to group "Impersonate Users".
5760

61+
If you want to forbid impersonation of users with the "Administration:
62+
Settings" access rights, enable the *Restrict Impersonation of
63+
"Administration: Settings" Users* option in the settings.
64+
5865
Usage
5966
=====
6067

impersonate_login/__manifest__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"data": [
2121
"security/group.xml",
2222
"security/ir.model.access.csv",
23+
"views/res_config_settings.xml",
2324
"views/res_users.xml",
2425
"views/impersonate_log.xml",
2526
],

impersonate_login/models/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
from . import mail_message
55
from . import impersonate_log
66
from . import model
7+
from . import res_config_settings
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from odoo import fields, models
2+
3+
4+
class ResConfigSettings(models.TransientModel):
5+
_inherit = "res.config.settings"
6+
7+
restrict_impersonate_admin_settings = fields.Boolean(
8+
string="Restrict Impersonation of 'Administration: Settings' Users",
9+
config_parameter="impersonate_login.restrict_impersonate_admin_settings",
10+
help=(
11+
"If enabled, users with the 'Administration: Settings' access right"
12+
" cannot be impersonated."
13+
),
14+
default=False,
15+
)

impersonate_login/models/res_users.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,20 @@ def _is_impersonate_user(self):
2424

2525
def impersonate_login(self):
2626
if request:
27+
config_restrict = (
28+
self.env["ir.config_parameter"]
29+
.sudo()
30+
.get_param("impersonate_login.restrict_impersonate_admin_settings")
31+
)
32+
if config_restrict:
33+
admin_settings_group = self.env.ref("base.group_system")
34+
if admin_settings_group in self.groups_id:
35+
raise UserError(
36+
_(
37+
"You cannot impersonate users with"
38+
" 'Administration: Settings' access rights."
39+
)
40+
)
2741
if request.session.impersonate_from_uid:
2842
if self.id == request.session.impersonate_from_uid:
2943
return self.back_to_origin_login()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
The impersonating user must belong to group "Impersonate Users".
2+
3+
If you want to prevent impersonation of users with the *Administration: Settings*
4+
rights, enable the *Restrict Impersonation of "Administration: Settings" Users*
5+
option in the settings.

impersonate_login/readme/DESCRIPTION.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ To ensure that any abuse of this feature will not go unnoticed, the following me
66
* In the chatter, it is displayed who is the user that is logged as another user.
77
* Mails and messages are sent from the original user.
88
* Impersonated logins are logged and can be consulted through the Settings -> Technical menu.
9-
*
9+
* You can optionally forbid impersonation of users with "Administration: Settings"
10+
rights by enabling the related option in the settings.
1011
There is an alternative module to allow logins as another user (auth_admin_passkey),
1112
but it does not support these security mechanisms.

impersonate_login/static/description/index.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,10 @@ <h1 class="title">Impersonate Login</h1>
381381
<li>Mails and messages are sent from the original user.</li>
382382
<li>Impersonated logins are logged and can be consulted through the
383383
Settings -&gt; Technical menu.</li>
384-
<li></li>
384+
<li>To prevent users with “Administration: Settings” rights from being
385+
impersonated, enable the <cite>restrict_impersonate_admin_settings</cite> field in
386+
the settings. This will restrict the ability to impersonate users
387+
with administrative access to the settings.</li>
385388
</ul>
386389
<p>There is an alternative module to allow logins as another user
387390
(auth_admin_passkey), but it does not support these security mechanisms.</p>
@@ -402,6 +405,9 @@ <h1 class="title">Impersonate Login</h1>
402405
<div class="section" id="configuration">
403406
<h1><a class="toc-backref" href="#toc-entry-1">Configuration</a></h1>
404407
<p>The impersonating user must belong to group “Impersonate Users”.</p>
408+
<p>If you want to forbid impersonation of users with the “Administration:
409+
Settings” access rights, enable the <em>Restrict Impersonation of “Administration:
410+
Settings” Users</em> option in the settings.</p>
405411
</div>
406412
<div class="section" id="usage">
407413
<h1><a class="toc-backref" href="#toc-entry-2">Usage</a></h1>

impersonate_login/tests/test_impersonate_login.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,3 +258,37 @@ def test_04_write_uid(self):
258258
self.assertEqual(result, True)
259259
self.assertEqual(contact.ref, "abc")
260260
self.assertEqual(contact.write_uid, self.admin_user)
261+
262+
def test_05_limit_access_to_admin(self):
263+
"""
264+
Test restriction on impersonating admin users
265+
with 'Administration: Settings' access rights.
266+
"""
267+
config_settings = self.env["res.config.settings"].create(
268+
{"restrict_impersonate_admin_settings": True}
269+
)
270+
config_settings.execute()
271+
272+
config_restrict = (
273+
self.env["ir.config_parameter"]
274+
.sudo()
275+
.get_param("impersonate_login.restrict_impersonate_admin_settings")
276+
)
277+
self.assertTrue(config_restrict)
278+
279+
admin_settings_group = self.env.ref("base.group_system")
280+
self.admin_user.groups_id += admin_settings_group
281+
282+
self.authenticate(user="demo", password="demo")
283+
self.assertEqual(self.session.uid, self.demo_user.id)
284+
285+
self.demo_user.groups_id += self.env.ref(
286+
"impersonate_login.group_impersonate_login"
287+
)
288+
289+
with mute_logger("odoo.http"):
290+
data = self._impersonate_user(self.admin_user)
291+
self.assertEqual(
292+
data["error"]["data"]["message"],
293+
"You cannot impersonate users with 'Administration: Settings' access rights.",
294+
)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<odoo>
2+
<record id="view_res_config_settings_impersonate" model="ir.ui.view">
3+
<field name="name">res.config.settings.impersonate</field>
4+
<field name="model">res.config.settings</field>
5+
<field name="inherit_id" ref="base_setup.res_config_settings_view_form" />
6+
<field name="arch" type="xml">
7+
<xpath expr="//div[@id='user_default_rights']" position="after">
8+
<div id="impersonate_login">
9+
<h2>Impersonation Login</h2>
10+
<div
11+
class="row mt16 o_settings_container"
12+
name="impersonate_login_settings_container"
13+
>
14+
<div
15+
class="col-12 col-lg-6 o_setting_box"
16+
id="impersonate_login_settings"
17+
>
18+
<div class="o_setting_left_pane">
19+
<field name="restrict_impersonate_admin_settings" />
20+
</div>
21+
<div class="o_setting_right_pane">
22+
<label for="restrict_impersonate_admin_settings">
23+
Restrict Impersonation of 'Administration: Settings' Users
24+
</label>
25+
<div class="text-muted">
26+
Prevents impersonating users that have the
27+
"Administration: Settings" access rights.
28+
</div>
29+
</div>
30+
</div>
31+
</div>
32+
</div>
33+
</xpath>
34+
</field>
35+
</record>
36+
</odoo>

0 commit comments

Comments
 (0)