Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions impersonate_login/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ following measures are in place:
- Mails and messages are sent from the original user.
- Impersonated logins are logged and can be consulted through the
Settings -> Technical menu.
-

There is an alternative module to allow logins as another user
(auth_admin_passkey), but it does not support these security mechanisms.
- You can optionally forbid impersonation of users with "Administration:
Settings" rights by enabling the related option in the settings. There
is an alternative module to allow logins as another user
(auth_admin_passkey), but it does not support these security
mechanisms.

**Table of contents**

Expand All @@ -59,6 +60,10 @@ Configuration

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

If you want to prevent impersonation of users with the *Administration:
Settings* rights, enable the *Restrict Impersonation of "Administration:
Settings" Users* option in the settings.

Usage
=====

Expand Down
1 change: 1 addition & 0 deletions impersonate_login/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"data": [
"security/group.xml",
"security/ir.model.access.csv",
"views/res_config_settings.xml",
"views/res_users.xml",
"views/impersonate_log.xml",
],
Expand Down
1 change: 1 addition & 0 deletions impersonate_login/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
from . import mail_message
from . import impersonate_log
from . import model
from . import res_config_settings
11 changes: 11 additions & 0 deletions impersonate_login/models/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,17 @@
class BaseModel(models.AbstractModel):
_inherit = "base"

def _keep_real_user_on_create_write(self):
# Avoid overriding the create_uid and write_uid
# when the model is abstract or transient
if self._abstract or self._transient:
return True
return False

def _prepare_create_values(self, vals_list):
result_vals_list = super()._prepare_create_values(vals_list)
if self._keep_real_user_on_create_write():
return result_vals_list
if (
request
and request.session.get("impersonate_from_uid")
Expand All @@ -23,6 +32,8 @@ def _prepare_create_values(self, vals_list):
def write(self, vals):
"""Overwrite the write_uid with the impersonating user"""
res = super().write(vals)
if self._keep_real_user_on_create_write():
return res
if (
request
and request.session.get("impersonate_from_uid")
Expand Down
15 changes: 15 additions & 0 deletions impersonate_login/models/res_config_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from odoo import fields, models


class ResConfigSettings(models.TransientModel):
_inherit = "res.config.settings"

restrict_impersonate_admin_settings = fields.Boolean(
string="Restrict Impersonation of 'Administration: Settings' Users",
config_parameter="impersonate_login.restrict_impersonate_admin_settings",
help=(
"If enabled, users with the 'Administration: Settings' access right"
" cannot be impersonated."
),
default=False,
)
14 changes: 14 additions & 0 deletions impersonate_login/models/res_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ def _is_impersonate_user(self):

def impersonate_login(self):
if request:
config_restrict = (
self.env["ir.config_parameter"]
.sudo()
.get_param("impersonate_login.restrict_impersonate_admin_settings")
)
if config_restrict:
admin_settings_group = self.env.ref("base.group_system")
if admin_settings_group in self.group_ids:
raise UserError(
self.env._(
"You cannot impersonate users with"
" 'Administration: Settings' access rights."
)
)
if request.session.get("impersonate_from_uid"):
if self.id == request.session.get("impersonate_from_uid"):
return self.back_to_origin_login()
Expand Down
4 changes: 4 additions & 0 deletions impersonate_login/readme/CONFIGURE.md
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
The impersonating user must belong to group "Impersonate Users".

If you want to prevent impersonation of users with the *Administration: Settings*
rights, enable the *Restrict Impersonation of "Administration: Settings" Users*
option in the settings.
3 changes: 2 additions & 1 deletion impersonate_login/readme/DESCRIPTION.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ To ensure that any abuse of this feature will not go unnoticed, the following me
* In the chatter, it is displayed who is the user that is logged as another user.
* Mails and messages are sent from the original user.
* Impersonated logins are logged and can be consulted through the Settings -> Technical menu.
*
* You can optionally forbid impersonation of users with "Administration: Settings"
rights by enabling the related option in the settings.
There is an alternative module to allow logins as another user (auth_admin_passkey),
but it does not support these security mechanisms.
11 changes: 8 additions & 3 deletions impersonate_login/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -386,10 +386,12 @@ <h1>Impersonate Login</h1>
<li>Mails and messages are sent from the original user.</li>
<li>Impersonated logins are logged and can be consulted through the
Settings -&gt; Technical menu.</li>
<li></li>
<li>You can optionally forbid impersonation of users with “Administration:
Settings” rights by enabling the related option in the settings. There
is an alternative module to allow logins as another user
(auth_admin_passkey), but it does not support these security
mechanisms.</li>
</ul>
<p>There is an alternative module to allow logins as another user
(auth_admin_passkey), but it does not support these security mechanisms.</p>
<p><strong>Table of contents</strong></p>
<div class="contents local topic" id="contents">
<ul class="simple">
Expand All @@ -407,6 +409,9 @@ <h1>Impersonate Login</h1>
<div class="section" id="configuration">
<h2><a class="toc-backref" href="#toc-entry-1">Configuration</a></h2>
<p>The impersonating user must belong to group “Impersonate Users”.</p>
<p>If you want to prevent impersonation of users with the <em>Administration:
Settings</em> rights, enable the <em>Restrict Impersonation of “Administration:
Settings” Users</em> option in the settings.</p>
</div>
<div class="section" id="usage">
<h2><a class="toc-backref" href="#toc-entry-2">Usage</a></h2>
Expand Down
74 changes: 74 additions & 0 deletions impersonate_login/tests/test_impersonate_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,3 +297,77 @@ def test_04_write_uid(self):
self.assertEqual(contact.id, contact_id)
self.assertEqual(contact.ref, "abc")
self.assertEqual(contact.write_uid, self.admin_user)

def test_05_create_uid_on_transient_model(self):
"""Check the create_uid of records created
during an impersonated session on a transient model"""
# Login as admin
self.authenticate(user="admin", password="admin")

# Impersonate demo user and create a wizard record
self._impersonate_user(self.demo_user)

response = self.url_open(
"/web/dataset/call_kw/mail.followers.edit/web_save",
data=json.dumps(
{
"params": {
"model": "mail.followers.edit",
"method": "web_save",
"args": [
[],
{
"res_model": "res.partner",
"message": "Hello",
},
{},
],
"kwargs": {},
},
}
),
headers={"Content-Type": "application/json"},
)
self.assertEqual(response.status_code, 200)
data = response.json()
result = data["result"]
settings_id = result[0]["id"]

wizard = self.env["mail.followers.edit"].browse(settings_id)
self.assertIn("Hello", wizard.message)
self.assertEqual(wizard.create_uid, self.demo_user)

def test_06_limit_access_to_admin(self):
"""
Test restriction on impersonating admin users
with 'Administration: Settings' access rights.
"""
config_settings = self.env["res.config.settings"].create(
{"restrict_impersonate_admin_settings": True}
)
config_settings.execute()

config_restrict = (
self.env["ir.config_parameter"]
.sudo()
.get_param("impersonate_login.restrict_impersonate_admin_settings")
)
self.assertTrue(config_restrict)

admin_settings_group = self.env.ref("base.group_system")
self.admin_user.group_ids += admin_settings_group

self.authenticate(user=self.demo_login, password=self.demo_password)
self.assertEqual(self.session.uid, self.demo_user.id)

self.demo_user.group_ids += self.env.ref(
"impersonate_login.group_impersonate_login"
)

with mute_logger("odoo.http"):
data = self._impersonate_user(self.admin_user)
self.assertEqual(
data["error"]["data"]["message"],
"You cannot impersonate users with "
"'Administration: Settings' access rights.",
)
20 changes: 20 additions & 0 deletions impersonate_login/views/res_config_settings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<odoo>
<record id="view_res_config_settings_impersonate" model="ir.ui.view">
<field name="name">res.config.settings.impersonate</field>
<field name="model">res.config.settings</field>
<field name="inherit_id" ref="base_setup.res_config_settings_view_form" />
<field name="arch" type="xml">
<block id="user_default_rights" position="after">
<block title="Impersonation Login" id="impersonate_login">
<setting
id="restrict_impersonate_admin_settings"
title="Restrict Impersonation of 'Administration: Settings' Users"
help="Prevents impersonating users that have the 'Administration: Settings' access rights."
>
<field name="restrict_impersonate_admin_settings" />
</setting>
</block>
</block>
</field>
</record>
</odoo>
Loading