Skip to content

Commit

Permalink
[IMP] add improvements from V17
Browse files Browse the repository at this point in the history
  • Loading branch information
Kev-Roche committed Sep 1, 2024
1 parent 84a704f commit 5c45c07
Show file tree
Hide file tree
Showing 15 changed files with 450 additions and 73 deletions.
34 changes: 23 additions & 11 deletions impersonate_login/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Impersonate Login
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:1fca331cbc5f2dcb804e5612e5669a9ab4998d80f22d46d6683266580f9ca40f
!! source digest: sha256:a9d881ab6f6e5777204c45f152c0af22753629ca4b46ac913fe8da3792573ca8
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
Expand All @@ -28,13 +28,22 @@ Impersonate Login

|badge1| |badge2| |badge3| |badge4| |badge5|

This module allows to login as another user.
In the chatter, the user who is logged as another user is displayed.
The mails and messages are sent from the orignal user.
A table diplays the impersonated logins in technical.
The user can return to his own user by clicking on the button "Return to my user".
This module is very useful for the support team.
An alternative module will be auth_admin_passkey.
This module allows one user (for example, a member of the support team)
to log in as another user. The impersonation session can be exited by
clicking on the button "Back to Original User".

To ensure that any abuse of this feature will not go unnoticed, the
following measures are in place:

- 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.
-

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 @@ -44,8 +53,9 @@ An alternative module will be auth_admin_passkey.
Usage
=====

1. On the top right corner, click my user and "switch login"
2. Same place to "return to my login"
The impersonating user must belong to group "Impersonate Users".
- In the menu that is displayed when clicking on the user avatar on the top right corner, or in the res.users list, click "Switch Login" toi mpersonate another user.
- On the top-right corner, the button "Back to Original User" is displayed in case the current user is being impersonated.

Bug Tracker
===========
Expand All @@ -68,7 +78,9 @@ Authors
Contributors
~~~~~~~~~~~~

* Kévin Roche <[email protected]>
- Kévin Roche <[email protected]>
- [360ERP](https://www.360erp.com):
- Andrea Stirpe

Maintainers
~~~~~~~~~~~
Expand Down
1 change: 1 addition & 0 deletions impersonate_login/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from . import models
from .hooks import pre_init_hook
1 change: 1 addition & 0 deletions impersonate_login/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@
"qweb": [
"static/src/xml/user_menu.xml",
],
"pre_init_hook": "pre_init_hook",
}
19 changes: 19 additions & 0 deletions impersonate_login/hooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2024 360ERP (<https://www.360erp.com>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

import logging


def pre_init_hook(cr):
"""
Pre-create the impersonated_author_id column in the mail_message table
to prevent the ORM from invoking its compute method on a large volume
of existing mail messages.
"""
logger = logging.getLogger(__name__)
logger.info("Add mail_message.impersonated_author_id column if not exists")
cr.execute(
"ALTER TABLE mail_message "
"ADD COLUMN IF NOT EXISTS "
"impersonated_author_id INTEGER"
)
3 changes: 1 addition & 2 deletions impersonate_login/models/impersonate_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ class ImpersonateLog(models.Model):
_description = "Impersonate Logs"

user_id = fields.Many2one(
comodel_name="res.partner",
string="User",
comodel_name="res.users",
)
impersonated_partner_id = fields.Many2one(
comodel_name="res.partner",
Expand Down
33 changes: 30 additions & 3 deletions impersonate_login/models/mail_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@

from odoo import _, api, fields, models
from odoo.http import request
from odoo.tools import html_escape


class Message(models.Model):
_inherit = "mail.message"

impersonated_author_id = fields.Many2one(
comodel_name="res.partner",
string="Impersonated Author",
compute="_compute_impersonated_author_id",
store=True,
)

body = fields.Html(
compute="_compute_message_body",
inverse="_inverse_message_body",
store=True,
readonly=False,
)

@api.depends("author_id")
Expand All @@ -45,8 +47,33 @@ def _compute_message_body(self):
current_partner = (
self.env["res.users"].browse(request.session.uid).partner_id
)
additional_info = _(f"Logged as {current_partner.name}")
additional_info = _("Logged in as {}").format(
html_escape(current_partner.name)
)
if rec.body and additional_info:
rec.body = f"<b>{additional_info}</b><br/>{rec.body}"
else:
rec.body = additional_info
rec.body = rec.body

def _inverse_message_body(self):
for rec in self:
additional_info = ""
if (
request
and request.session.impersonate_from_uid
and rec.impersonated_author_id
):
current_partner = (
self.env["res.users"].browse(request.session.uid).partner_id
)
additional_info = _("Logged in as {}").format(
html_escape(current_partner.name)
)
if additional_info:
start_with = f"<b>{additional_info}</b><br/>"
if rec.body and rec.body.startswith(start_with):
rec.body = rec.body
else:
rec.body = f"{start_with}{rec.body}"
else:
rec.body = rec.body
32 changes: 16 additions & 16 deletions impersonate_login/models/mail_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ class MailThread(models.AbstractModel):
def _message_compute_author(
self, author_id=None, email_from=None, raise_exception=True
):
if (
request
and request.session.impersonate_from_uid
and author_id in [request.session.uid, None]
):
author = (
self.env["res.users"]
.browse(request.session.impersonate_from_uid)
.partner_id
)
email = author.email_formatted
return author.id, email
else:
return super()._message_compute_author(
author_id, email_from, raise_exception
)
if request and request.session.impersonate_from_uid:
author = self.env["res.users"].browse(request.session.uid).partner_id
if author_id == author.id or author_id is None:
impersonate_from_author = (
self.env["res.users"]
.browse(request.session.impersonate_from_uid)
.partner_id
)
email = impersonate_from_author.email_formatted
return impersonate_from_author.id, email

return super()._message_compute_author(
author_id,
email_from,
raise_exception,
)
28 changes: 18 additions & 10 deletions impersonate_login/models/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,31 @@
# @author Kévin Roche <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import api, models
from odoo import models
from odoo.http import request


class BaseModel(models.AbstractModel):
_inherit = "base"

@api.model_create_multi
def _create(self, data_list):
res = super()._create(data_list)
if request and request.session.impersonate_from_uid:
for rec in res:
rec.create_uid = request.session.impersonate_from_uid
return res
def _prepare_create_values(self, vals_list):
result_vals_list = super()._prepare_create_values(vals_list)
if (
request
and request.session.impersonate_from_uid
and "create_uid" in self._fields
):
for vals in result_vals_list:
vals["create_uid"] = request.session.impersonate_from_uid
return result_vals_list

def write(self, vals):
"""Overwrite the write_uid with the impersonating user"""
res = super().write(vals)
if request and request.session.impersonate_from_uid:
self.write_uid = request.session.impersonate_from_uid
if (
request
and request.session.impersonate_from_uid
and "write_uid" in self._fields
):
self._fields["write_uid"].write(self, request.session.impersonate_from_uid)
return res
36 changes: 31 additions & 5 deletions impersonate_login/models/res_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ def _is_impersonate_user(self):
def impersonate_login(self):
if request:
if request.session.impersonate_from_uid:
raise UserError(_("You are already Logged as another user."))
if self.id == request.session.impersonate_from_uid:
return self.back_to_origin_login()
else:
raise UserError(_("You are already Logged as another user."))
if self.id == request.session.uid:
raise UserError(_("It's you."))
if request.env.user._is_impersonate_user():
Expand All @@ -37,9 +40,7 @@ def impersonate_login(self):
.sudo()
.create(
{
"user_id": self.env["res.users"]
.browse(self._uid)
.partner_id.id,
"user_id": self._uid,
"impersonated_partner_id": self.env["res.users"]
.browse(target_uid)
.partner_id.id,
Expand All @@ -52,16 +53,34 @@ def impersonate_login(self):
f"IMPERSONATE: {self._get_partner_name(self._uid)} "
f"Login as {self._get_partner_name(self.id)}"
)

# invalidate session token cache as we've changed the uid
request.env["res.users"].clear_caches()
request.session.session_token = security.compute_session_token(
request.session, request.env
)

# reload the client;
return {
"type": "ir.actions.client",
"tag": "reload",
}

@api.model
def action_impersonate_login(self):
if request:
from_uid = request.session.impersonate_from_uid
if not from_uid:
action = self.env["ir.actions.act_window"]._for_xml_id(
"base.action_res_users"
)
action["views"] = [[self.env.ref("base.view_users_tree").id, "tree"]]
action["domain"] = [
("id", "!=", self.env.user.id),
("share", "=", False),
]
action["target"] = "new"
return action

@api.model
def back_to_origin_login(self):
if request:
Expand All @@ -75,6 +94,7 @@ def back_to_origin_login(self):
"date_end": fields.datetime.now(),
}
)
# invalidate session token cache as we've changed the uid
request.env["res.users"].clear_caches()
request.session.impersonate_from_uid = False
request.session.impersonate_log_id = False
Expand All @@ -85,3 +105,9 @@ def back_to_origin_login(self):
f"IMPERSONATE: {self._get_partner_name(from_uid)} "
f"Logout as {self._get_partner_name(self._uid)}"
)

# reload the client;
return {
"type": "ir.actions.client",
"tag": "reload",
}
4 changes: 3 additions & 1 deletion impersonate_login/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
* Kévin Roche <[email protected]>
- Kévin Roche <[email protected]>
- [360ERP](https://www.360erp.com):
- Andrea Stirpe
23 changes: 16 additions & 7 deletions impersonate_login/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
This module allows to login as another user.
In the chatter, the user who is logged as another user is displayed.
The mails and messages are sent from the orignal user.
A table diplays the impersonated logins in technical.
The user can return to his own user by clicking on the button "Return to my user".
This module is very useful for the support team.
An alternative module will be auth_admin_passkey.
This module allows one user (for example, a member of the support team)
to log in as another user. The impersonation session can be exited by
clicking on the button "Back to Original User".

To ensure that any abuse of this feature will not go unnoticed, the
following measures are in place:

- 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.
-

There is an alternative module to allow logins as another user
(auth_admin_passkey), but it does not support these security mechanisms.
5 changes: 3 additions & 2 deletions impersonate_login/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
1. On the top right corner, click my user and "switch login"
2. Same place to "return to my login"
The impersonating user must belong to group "Impersonate Users".
- In the menu that is displayed when clicking on the user avatar on the top right corner, or in the res.users list, click "Switch Login" toi mpersonate another user.
- On the top-right corner, the button "Back to Original User" is displayed in case the current user is being impersonated.
Loading

0 comments on commit 5c45c07

Please sign in to comment.