impersonate_user: new module
This commit is contained in:
parent
3fd59f5ba1
commit
aed5ad9aed
5 changed files with 110 additions and 0 deletions
1
impersonate_user/__init__.py
Normal file
1
impersonate_user/__init__.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
from . import models
|
||||
35
impersonate_user/__manifest__.py
Normal file
35
impersonate_user/__manifest__.py
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
#
|
||||
# Bemade Inc.
|
||||
#
|
||||
# Copyright (C) 2023-June Bemade Inc. (<https://www.bemade.org>).
|
||||
# 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,
|
||||
}
|
||||
1
impersonate_user/models/__init__.py
Normal file
1
impersonate_user/models/__init__.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
from . import res_users
|
||||
45
impersonate_user/models/res_users.py
Normal file
45
impersonate_user/models/res_users.py
Normal file
|
|
@ -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
|
||||
28
impersonate_user/views/impersonation_views.xml
Normal file
28
impersonate_user/views/impersonation_views.xml
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
<odoo>
|
||||
<record id="view_users_form_impersonation" model="ir.ui.view">
|
||||
<field name="name">res.users.form.impersonation</field>
|
||||
<field name="model">res.users</field>
|
||||
<field name="inherit_id" ref="base.view_users_form" />
|
||||
<field name="arch" type="xml">
|
||||
<header position="inside">
|
||||
<field name="is_impersonated" invisible="True" />
|
||||
<button
|
||||
name="impersonate_user"
|
||||
type="object"
|
||||
string="Impersonate"
|
||||
class="btn-secondary"
|
||||
invisible="is_impersonated"
|
||||
groups="base.group_system"
|
||||
/>
|
||||
<button
|
||||
name="unimpersonate"
|
||||
type="object"
|
||||
string="Unimpersonate"
|
||||
class="btn-secondary"
|
||||
invisible="not is_impersonated"
|
||||
/>
|
||||
</header>
|
||||
</field>
|
||||
</record>
|
||||
<!-- TODO: Add a modification to the user profile view (from hr) to unimpersonate-->
|
||||
</odoo>
|
||||
Loading…
Reference in a new issue