diff --git a/impersonate_user/__init__.py b/impersonate_user/__init__.py new file mode 100644 index 0000000..0650744 --- /dev/null +++ b/impersonate_user/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/impersonate_user/__manifest__.py b/impersonate_user/__manifest__.py new file mode 100644 index 0000000..6bdda91 --- /dev/null +++ b/impersonate_user/__manifest__.py @@ -0,0 +1,35 @@ +# +# Bemade Inc. +# +# Copyright (C) 2023-June Bemade Inc. (). +# Author: Marc Durepos (Contact : marc@bemade.org) +# +# This program is under the terms of the GNU Lesser General Public License, +# version 3. +# +# For full license details, see https://www.gnu.org/licenses/lgpl-3.0.en.html. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +# DEALINGS IN THE SOFTWARE. +# +{ + "name": "Impersonate User", + "version": "17.0.0.0.1", + "summary": "Allow administrators to impersonate other users", + "category": "Administration", + "author": "Bemade Inc.", + "website": "http://www.bemade.org", + "license": "LGPL-3", + "depends": ["base"], + "data": [ + "views/impersonation_views.xml", + ], + "assets": {}, + "installable": True, + "auto_install": False, +} diff --git a/impersonate_user/models/__init__.py b/impersonate_user/models/__init__.py new file mode 100644 index 0000000..8835165 --- /dev/null +++ b/impersonate_user/models/__init__.py @@ -0,0 +1 @@ +from . import res_users diff --git a/impersonate_user/models/res_users.py b/impersonate_user/models/res_users.py new file mode 100644 index 0000000..4e85214 --- /dev/null +++ b/impersonate_user/models/res_users.py @@ -0,0 +1,45 @@ +from odoo import models, fields +from odoo.exceptions import AccessError +from odoo.http import request + + +class ResUsers(models.Model): + _inherit = "res.users" + + is_impersonated = fields.Boolean( + compute="_compute_is_impersonated", + ) + + def impersonate_user(self): + self.ensure_one() + if not self.env.user.has_group("base.group_system"): + raise AccessError(_("Only administrators can impersonate users.")) + request.session["original_uid"] = request.uid + request.session.uid = self.id + return { + "type": "ir.actions.act_url", + "url": self.env["ir.config_parameter"].get_param("web.base.url") + "/web", + "target": "self", + } + + def unimpersonate(self): + self.ensure_one() + original_uid = request.session.pop("original_uid", False) + if original_uid: + request.session.uid = original_uid + return { + "type": "ir.actions.act_url", + "url": self.env["ir.config_parameter"].get_param("web.base.url") + + "/web", + "target": "self", + } + else: + raise AccessError(_("This user is not impersonated.")) + + def _compute_is_impersonated(self): + original_uid = request.session.get("original_uid", False) + if not original_uid: + self.write({"is_impersonated": False}) + else: + for rec in self: + rec.is_impersonated = rec.id == request.session.uid diff --git a/impersonate_user/views/impersonation_views.xml b/impersonate_user/views/impersonation_views.xml new file mode 100644 index 0000000..f02e768 --- /dev/null +++ b/impersonate_user/views/impersonation_views.xml @@ -0,0 +1,28 @@ + + + res.users.form.impersonation + res.users + + +
+ +
+
+
+ +