Skip to content

Commit

Permalink
[16.0][MIG] impersonate_login: backport to V16
Browse files Browse the repository at this point in the history
  • Loading branch information
astirpe committed Aug 30, 2024
1 parent 07b7060 commit c6ee905
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 35 deletions.
10 changes: 5 additions & 5 deletions impersonate_login/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ Impersonate Login
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fserver--auth-lightgray.png?logo=github
:target: https://github.com/OCA/server-auth/tree/17.0/impersonate_login
:target: https://github.com/OCA/server-auth/tree/16.0/impersonate_login
:alt: OCA/server-auth
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/server-auth-17-0/server-auth-17-0-impersonate_login
:target: https://translation.odoo-community.org/projects/server-auth-16-0/server-auth-16-0-impersonate_login
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/builds?repo=OCA/server-auth&target_branch=17.0
:target: https://runboat.odoo-community.org/builds?repo=OCA/server-auth&target_branch=16.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|
Expand Down Expand Up @@ -70,7 +70,7 @@ Bug Tracker
Bugs are tracked on `GitHub Issues <https://github.com/OCA/server-auth/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
`feedback <https://github.com/OCA/server-auth/issues/new?body=module:%20impersonate_login%0Aversion:%2017.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
`feedback <https://github.com/OCA/server-auth/issues/new?body=module:%20impersonate_login%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Expand Down Expand Up @@ -111,6 +111,6 @@ Current `maintainer <https://odoo-community.org/page/maintainer-role>`__:

|maintainer-Kev-Roche|

This module is part of the `OCA/server-auth <https://github.com/OCA/server-auth/tree/17.0/impersonate_login>`_ project on GitHub.
This module is part of the `OCA/server-auth <https://github.com/OCA/server-auth/tree/16.0/impersonate_login>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
2 changes: 1 addition & 1 deletion impersonate_login/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{
"name": "Impersonate Login",
"summary": "tools",
"version": "17.0.1.0.0",
"version": "16.0.1.0.0",
"category": "Tools",
"website": "https://github.com/OCA/server-auth",
"author": "Akretion, Odoo Community Association (OCA)",
Expand Down
4 changes: 2 additions & 2 deletions impersonate_login/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
import logging


def pre_init_hook(env):
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")
env.cr.execute(
cr.execute(
"ALTER TABLE mail_message "
"ADD COLUMN IF NOT EXISTS "
"impersonated_author_id INTEGER"
Expand Down
27 changes: 26 additions & 1 deletion impersonate_login/models/mail_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ class Message(models.Model):

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

@api.depends("author_id")
Expand Down Expand Up @@ -51,4 +53,27 @@ def _compute_message_body(self):
if rec.body and additional_info:
rec.body = f"<b>{additional_info}</b><br/>{rec.body}"

Check warning on line 54 in impersonate_login/models/mail_message.py

View check run for this annotation

Codecov / codecov/patch

impersonate_login/models/mail_message.py#L54

Added line #L54 was not covered by tests
else:
rec.body = additional_info
rec.body = rec.body

Check warning on line 56 in impersonate_login/models/mail_message.py

View check run for this annotation

Codecov / codecov/patch

impersonate_login/models/mail_message.py#L56

Added line #L56 was not covered by tests

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

Check warning on line 75 in impersonate_login/models/mail_message.py

View check run for this annotation

Codecov / codecov/patch

impersonate_login/models/mail_message.py#L75

Added line #L75 was not covered by tests
else:
rec.body = f"{start_with}{rec.body}"
else:
rec.body = rec.body
4 changes: 2 additions & 2 deletions impersonate_login/models/res_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def impersonate_login(self):
f"Login as {self._get_partner_name(self.id)}"
)
# invalidate session token cache as we've changed the uid
request.env.registry.clear_cache()
request.env["res.users"].clear_caches()
request.session.session_token = security.compute_session_token(
request.session, request.env
)
Expand Down Expand Up @@ -100,7 +100,7 @@ def back_to_origin_login(self):
}
)
# invalidate session token cache as we've changed the uid
request.env.registry.clear_cache()
request.env["res.users"].clear_caches()
request.session.impersonate_from_uid = False
request.session.impersonate_log_id = False
request.session.session_token = security.compute_session_token(
Expand Down
3 changes: 0 additions & 3 deletions impersonate_login/pyproject.toml

This file was deleted.

17 changes: 7 additions & 10 deletions impersonate_login/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@

/*
:Author: David Goodger (goodger@python.org)
:Id: $Id: html4css1.css 9511 2024-01-13 09:50:07Z milde $
:Id: $Id: html4css1.css 8954 2022-01-20 10:10:25Z milde $
:Copyright: This stylesheet has been placed in the public domain.

Default cascading style sheet for the HTML output of Docutils.
Despite the name, some widely supported CSS2 features are used.

See https://docutils.sourceforge.io/docs/howto/html-stylesheets.html for how to
customize this style sheet.
Expand Down Expand Up @@ -275,7 +274,7 @@
margin-left: 2em ;
margin-right: 2em }

pre.code .ln { color: gray; } /* line numbers */
pre.code .ln { color: grey; } /* line numbers */
pre.code, code { background-color: #eeeeee }
pre.code .comment, code .comment { color: #5C6576 }
pre.code .keyword, code .keyword { color: #3B0D06; font-weight: bold }
Expand All @@ -301,7 +300,7 @@
span.pre {
white-space: pre }

span.problematic, pre.problematic {
span.problematic {
color: red }

span.section-subtitle {
Expand Down Expand Up @@ -369,7 +368,7 @@ <h1 class="title">Impersonate Login</h1>
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:21edb5cefd3ad559bf6ceb133f51ab242539b8bd46591d8c3f578962fd7ab904
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<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/17.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-17-0/server-auth-17-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=17.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<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/16.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-16-0/server-auth-16-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=16.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>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”.</p>
Expand Down Expand Up @@ -418,7 +417,7 @@ <h1><a class="toc-backref" href="#toc-entry-3">Bug Tracker</a></h1>
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/server-auth/issues">GitHub Issues</a>.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
<a class="reference external" href="https://github.com/OCA/server-auth/issues/new?body=module:%20impersonate_login%0Aversion:%2017.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<a class="reference external" href="https://github.com/OCA/server-auth/issues/new?body=module:%20impersonate_login%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<p>Do not contact contributors directly about support or help with technical issues.</p>
</div>
<div class="section" id="credits">
Expand All @@ -442,15 +441,13 @@ <h2><a class="toc-backref" href="#toc-entry-6">Contributors</a></h2>
<div class="section" id="maintainers">
<h2><a class="toc-backref" href="#toc-entry-7">Maintainers</a></h2>
<p>This module is maintained by the OCA.</p>
<a class="reference external image-reference" href="https://odoo-community.org">
<img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" />
</a>
<a class="reference external image-reference" href="https://odoo-community.org"><img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" /></a>
<p>OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.</p>
<p>Current <a class="reference external" href="https://odoo-community.org/page/maintainer-role">maintainer</a>:</p>
<p><a class="reference external image-reference" href="https://github.com/Kev-Roche"><img alt="Kev-Roche" src="https://github.com/Kev-Roche.png?size=40px" /></a></p>
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/server-auth/tree/17.0/impersonate_login">OCA/server-auth</a> project on GitHub.</p>
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/server-auth/tree/16.0/impersonate_login">OCA/server-auth</a> project on GitHub.</p>
<p>You are welcome to contribute. To learn how please visit <a class="reference external" href="https://odoo-community.org/page/Contribute">https://odoo-community.org/page/Contribute</a>.</p>
</div>
</div>
Expand Down
17 changes: 6 additions & 11 deletions impersonate_login/tests/test_impersonate_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,18 +195,16 @@ def test_03_create_uid(self):
self._impersonate_user(self.demo_user)

response = self.url_open(
"/web/dataset/call_kw/res.partner/web_save",
"/web/dataset/call_kw/res.partner/create",
data=json.dumps(
{
"params": {
"model": "res.partner",
"method": "web_save",
"method": "create",
"args": [
[],
{
"name": "Contact123",
},
{},
],
"kwargs": {},
},
Expand All @@ -216,8 +214,7 @@ def test_03_create_uid(self):
)
self.assertEqual(response.status_code, 200)
data = response.json()
result = data["result"]
contact_id = result[0]["id"]
contact_id = data["result"]

contact = self.env["res.partner"].browse(contact_id)
self.assertEqual(contact.name, "Contact123")
Expand All @@ -236,18 +233,17 @@ def test_04_write_uid(self):
self._impersonate_user(self.demo_user)

response = self.url_open(
"/web/dataset/call_kw/res.partner/web_save",
"/web/dataset/call_kw/res.partner/write",
data=json.dumps(
{
"params": {
"model": "res.partner",
"method": "web_save",
"method": "write",
"args": [
[contact.id],
{
"ref": "abc",
},
{},
],
"kwargs": {},
},
Expand All @@ -258,8 +254,7 @@ def test_04_write_uid(self):
self.assertEqual(response.status_code, 200)
data = response.json()
result = data["result"]
contact_id = result[0]["id"]

self.assertEqual(contact.id, contact_id)
self.assertEqual(result, True)
self.assertEqual(contact.ref, "abc")
self.assertEqual(contact.write_uid, self.admin_user)
1 change: 1 addition & 0 deletions setup/impersonate_login/odoo/addons/impersonate_login
6 changes: 6 additions & 0 deletions setup/impersonate_login/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)

0 comments on commit c6ee905

Please sign in to comment.