Skip to content

Commit d245ce4

Browse files
pcastelovigoSirPyTech
authored andcommitted
[IMP] impersonate_login: restrict impersonate admins
1 parent 646ddfd commit d245ce4

10 files changed

Lines changed: 123 additions & 32 deletions

File tree

impersonate_login/README.rst

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
.. image:: https://odoo-community.org/readme-banner-image
2-
:target: https://odoo-community.org/get-involved?utm_source=readme
3-
:alt: Odoo Community Association
4-
51
=================
62
Impersonate Login
73
=================
@@ -11,13 +7,13 @@ Impersonate Login
117
!! This file is generated by oca-gen-addon-readme !!
128
!! changes will be overwritten. !!
139
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
14-
!! source digest: sha256:7a065218446f1a1c3d7c8df01e153960f15c80d7fc12534272a2e700896ca757
10+
!! source digest: sha256:495e3835d8bd5706184290ecbbc105723329293d90630889e42c95d73e28bd09
1511
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1612
1713
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
1814
:target: https://odoo-community.org/page/development-status
1915
:alt: Beta
20-
.. |badge2| image:: https://img.shields.io/badge/license-AGPL--3-blue.png
16+
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
2117
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
2218
:alt: License: AGPL-3
2319
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fserver--auth-lightgray.png?logo=github
@@ -44,10 +40,11 @@ following measures are in place:
4440
- Mails and messages are sent from the original user.
4541
- Impersonated logins are logged and can be consulted through the
4642
Settings -> Technical menu.
47-
-
48-
49-
There is an alternative module to allow logins as another user
50-
(auth_admin_passkey), but it does not support these security mechanisms.
43+
- You can optionally forbid impersonation of users with "Administration:
44+
Settings" rights by enabling the related option in the settings. There
45+
is an alternative module to allow logins as another user
46+
(auth_admin_passkey), but it does not support these security
47+
mechanisms.
5148

5249
**Table of contents**
5350

@@ -59,6 +56,10 @@ Configuration
5956

6057
The impersonating user must belong to group "Impersonate Users".
6158

59+
If you want to prevent impersonation of users with the *Administration:
60+
Settings* rights, enable the *Restrict Impersonation of "Administration:
61+
Settings" Users* option in the settings.
62+
6263
Usage
6364
=====
6465

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+
self.env._(
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: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<head>
44
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
55
<meta name="generator" content="Docutils: https://docutils.sourceforge.io/" />
6-
<title>README.rst</title>
6+
<title>Impersonate Login</title>
77
<style type="text/css">
88

99
/*
@@ -360,21 +360,16 @@
360360
</style>
361361
</head>
362362
<body>
363-
<div class="document">
363+
<div class="document" id="impersonate-login">
364+
<h1 class="title">Impersonate Login</h1>
364365

365-
366-
<a class="reference external image-reference" href="https://odoo-community.org/get-involved?utm_source=readme">
367-
<img alt="Odoo Community Association" src="https://odoo-community.org/readme-banner-image" />
368-
</a>
369-
<div class="section" id="impersonate-login">
370-
<h1>Impersonate Login</h1>
371366
<!-- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
372367
!! This file is generated by oca-gen-addon-readme !!
373368
!! changes will be overwritten. !!
374369
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
375-
!! source digest: sha256:7a065218446f1a1c3d7c8df01e153960f15c80d7fc12534272a2e700896ca757
370+
!! source digest: sha256:495e3835d8bd5706184290ecbbc105723329293d90630889e42c95d73e28bd09
376371
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
377-
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/license-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/server-auth/tree/18.0/impersonate_login"><img alt="OCA/server-auth" src="https://img.shields.io/badge/github-OCA%2Fserver--auth-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/server-auth-18-0/server-auth-18-0-impersonate_login"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/server-auth&amp;target_branch=18.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
372+
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/server-auth/tree/18.0/impersonate_login"><img alt="OCA/server-auth" src="https://img.shields.io/badge/github-OCA%2Fserver--auth-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/server-auth-18-0/server-auth-18-0-impersonate_login"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/server-auth&amp;target_branch=18.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
378373
<p>This module allows one user (for example, a member of the support team)
379374
to log in as another user. The impersonation session can be exited by
380375
clicking on the button “Back to Original User”.</p>
@@ -386,10 +381,12 @@ <h1>Impersonate Login</h1>
386381
<li>Mails and messages are sent from the original user.</li>
387382
<li>Impersonated logins are logged and can be consulted through the
388383
Settings -&gt; Technical menu.</li>
389-
<li></li>
384+
<li>You can optionally forbid impersonation of users with “Administration:
385+
Settings” rights by enabling the related option in the settings. There
386+
is an alternative module to allow logins as another user
387+
(auth_admin_passkey), but it does not support these security
388+
mechanisms.</li>
390389
</ul>
391-
<p>There is an alternative module to allow logins as another user
392-
(auth_admin_passkey), but it does not support these security mechanisms.</p>
393390
<p><strong>Table of contents</strong></p>
394391
<div class="contents local topic" id="contents">
395392
<ul class="simple">
@@ -405,11 +402,14 @@ <h1>Impersonate Login</h1>
405402
</ul>
406403
</div>
407404
<div class="section" id="configuration">
408-
<h2><a class="toc-backref" href="#toc-entry-1">Configuration</a></h2>
405+
<h1><a class="toc-backref" href="#toc-entry-1">Configuration</a></h1>
409406
<p>The impersonating user must belong to group “Impersonate Users”.</p>
407+
<p>If you want to prevent impersonation of users with the <em>Administration:
408+
Settings</em> rights, enable the <em>Restrict Impersonation of “Administration:
409+
Settings” Users</em> option in the settings.</p>
410410
</div>
411411
<div class="section" id="usage">
412-
<h2><a class="toc-backref" href="#toc-entry-2">Usage</a></h2>
412+
<h1><a class="toc-backref" href="#toc-entry-2">Usage</a></h1>
413413
<ol class="arabic simple">
414414
<li>In the menu that is displayed when clicking on the user avatar on the
415415
top right corner, or in the res.users list, click “Switch Login” to
@@ -419,23 +419,23 @@ <h2><a class="toc-backref" href="#toc-entry-2">Usage</a></h2>
419419
</ol>
420420
</div>
421421
<div class="section" id="bug-tracker">
422-
<h2><a class="toc-backref" href="#toc-entry-3">Bug Tracker</a></h2>
422+
<h1><a class="toc-backref" href="#toc-entry-3">Bug Tracker</a></h1>
423423
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/server-auth/issues">GitHub Issues</a>.
424424
In case of trouble, please check there if your issue has already been reported.
425425
If you spotted it first, help us to smash it by providing a detailed and welcomed
426426
<a class="reference external" href="https://github.com/OCA/server-auth/issues/new?body=module:%20impersonate_login%0Aversion:%2018.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
427427
<p>Do not contact contributors directly about support or help with technical issues.</p>
428428
</div>
429429
<div class="section" id="credits">
430-
<h2><a class="toc-backref" href="#toc-entry-4">Credits</a></h2>
430+
<h1><a class="toc-backref" href="#toc-entry-4">Credits</a></h1>
431431
<div class="section" id="authors">
432-
<h3><a class="toc-backref" href="#toc-entry-5">Authors</a></h3>
432+
<h2><a class="toc-backref" href="#toc-entry-5">Authors</a></h2>
433433
<ul class="simple">
434434
<li>Akretion</li>
435435
</ul>
436436
</div>
437437
<div class="section" id="contributors">
438-
<h3><a class="toc-backref" href="#toc-entry-6">Contributors</a></h3>
438+
<h2><a class="toc-backref" href="#toc-entry-6">Contributors</a></h2>
439439
<ul class="simple">
440440
<li>Kévin Roche &lt;<a class="reference external" href="mailto:kevin.roche&#64;akretion.com">kevin.roche&#64;akretion.com</a>&gt;</li>
441441
<li><a class="reference external" href="https://www.360erp.com">360ERP</a>:<ul>
@@ -445,7 +445,7 @@ <h3><a class="toc-backref" href="#toc-entry-6">Contributors</a></h3>
445445
</ul>
446446
</div>
447447
<div class="section" id="maintainers">
448-
<h3><a class="toc-backref" href="#toc-entry-7">Maintainers</a></h3>
448+
<h2><a class="toc-backref" href="#toc-entry-7">Maintainers</a></h2>
449449
<p>This module is maintained by the OCA.</p>
450450
<a class="reference external image-reference" href="https://odoo-community.org">
451451
<img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" />
@@ -460,6 +460,5 @@ <h3><a class="toc-backref" href="#toc-entry-7">Maintainers</a></h3>
460460
</div>
461461
</div>
462462
</div>
463-
</div>
464463
</body>
465464
</html>

impersonate_login/tests/test_impersonate_login.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,3 +302,38 @@ def test_05_create_uid_on_transient_model(self):
302302
wizard = self.env["mail.wizard.invite"].browse(settings_id)
303303
self.assertIn("Hello", wizard.message)
304304
self.assertEqual(wizard.create_uid, self.demo_user)
305+
306+
def test_06_limit_access_to_admin(self):
307+
"""
308+
Test restriction on impersonating admin users
309+
with 'Administration: Settings' access rights.
310+
"""
311+
config_settings = self.env["res.config.settings"].create(
312+
{"restrict_impersonate_admin_settings": True}
313+
)
314+
config_settings.execute()
315+
316+
config_restrict = (
317+
self.env["ir.config_parameter"]
318+
.sudo()
319+
.get_param("impersonate_login.restrict_impersonate_admin_settings")
320+
)
321+
self.assertTrue(config_restrict)
322+
323+
admin_settings_group = self.env.ref("base.group_system")
324+
self.admin_user.groups_id += admin_settings_group
325+
326+
self.authenticate(user="demo", password="demo")
327+
self.assertEqual(self.session.uid, self.demo_user.id)
328+
329+
self.demo_user.groups_id += self.env.ref(
330+
"impersonate_login.group_impersonate_login"
331+
)
332+
333+
with mute_logger("odoo.http"):
334+
data = self._impersonate_user(self.admin_user)
335+
self.assertEqual(
336+
data["error"]["data"]["message"],
337+
"You cannot impersonate users with "
338+
"'Administration: Settings' access rights.",
339+
)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
<block id="user_default_rights" position="after">
8+
<block title="Impersonation Login" id="impersonate_login">
9+
<setting
10+
id="restrict_impersonate_admin_settings"
11+
title="Restrict Impersonation of 'Administration: Settings' Users"
12+
help="Prevents impersonating users that have the 'Administration: Settings' access rights."
13+
>
14+
<field name="restrict_impersonate_admin_settings" />
15+
</setting>
16+
</block>
17+
</block>
18+
</field>
19+
</record>
20+
</odoo>

0 commit comments

Comments
 (0)