From 9a0d421f66b80acaa9c1a4d21ee117c3f811081a Mon Sep 17 00:00:00 2001 From: Dany Robert Date: Sat, 18 Nov 2023 06:15:17 +0000 Subject: [PATCH] feat: user impersonation --- .../genie_settings/genie_settings.json | 9 ++++++- genie/hooks.py | 2 +- genie/public/js/impersonation.js | 26 +++++++++++++++++++ genie/utils/impersonation.py | 19 ++++++++++++++ 4 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 genie/public/js/impersonation.js create mode 100644 genie/utils/impersonation.py diff --git a/genie/genie/doctype/genie_settings/genie_settings.json b/genie/genie/doctype/genie_settings/genie_settings.json index 19ba5f1..27d1a6d 100644 --- a/genie/genie/doctype/genie_settings/genie_settings.json +++ b/genie/genie/doctype/genie_settings/genie_settings.json @@ -8,6 +8,7 @@ "field_order": [ "enable_ticket_raising", "column_break_nuxg", + "enable_user_impersonation", "support_portal_section", "support_url", "support_api_token", @@ -49,11 +50,17 @@ "fieldtype": "Table", "label": "Ticket Details", "options": "Support Ticket Details" + }, + { + "default": "0", + "fieldname": "enable_user_impersonation", + "fieldtype": "Check", + "label": "Enable User Impersonation" } ], "issingle": 1, "links": [], - "modified": "2023-10-13 19:54:22.523137", + "modified": "2023-11-18 11:41:53.318912", "modified_by": "Administrator", "module": "Genie", "name": "Genie Settings", diff --git a/genie/hooks.py b/genie/hooks.py index f8b0876..43e0e22 100644 --- a/genie/hooks.py +++ b/genie/hooks.py @@ -29,7 +29,7 @@ # page_js = {"page" : "public/js/file.js"} # include js in doctype views -# doctype_js = {"doctype" : "public/js/doctype.js"} +doctype_js = {"User": "public/js/impersonation.js"} # doctype_list_js = {"doctype" : "public/js/doctype_list.js"} # doctype_tree_js = {"doctype" : "public/js/doctype_tree.js"} # doctype_calendar_js = {"doctype" : "public/js/doctype_calendar.js"} diff --git a/genie/public/js/impersonation.js b/genie/public/js/impersonation.js new file mode 100644 index 0000000..de9edc5 --- /dev/null +++ b/genie/public/js/impersonation.js @@ -0,0 +1,26 @@ +// Copyright (c) 2023, Wahni IT Solutions Pvt. Ltd. and Contributors +// MIT License. See license.txt + + +frappe.ui.form.on("User", { + refresh: function(frm) { + if (!frm.doc.__islocal && frm.doc.enabled && frappe.user.has_role("System Manager")) { + frm.add_custom_button(__("Impersonate"), function() { + frappe.call({ + method: "genie.utils.impersonation.generate_impersonation_url", + args: { + "user": frm.doc.name + }, + callback: function(r) { + if (!r.exc && r.message) { + let impersonate_url = `this link`; + frappe.msgprint( + __("Please open {0} in an incognito window to impersonate {1}", [impersonate_url, frm.doc.name]), + ); + } + } + }); + }, __("Password")); + } + } +}); \ No newline at end of file diff --git a/genie/utils/impersonation.py b/genie/utils/impersonation.py new file mode 100644 index 0000000..cfa7ecb --- /dev/null +++ b/genie/utils/impersonation.py @@ -0,0 +1,19 @@ + +# Copyright (c) 2023, Wahni IT Solutions Pvt. Ltd. and contributors +# For license information, please see license.txt + +import frappe +from frappe import _ +from frappe.www.login import _generate_temporary_login_link + + +@frappe.whitelist() +def generate_impersonation_url(user): + frappe.only_for("System Manager") + + if not frappe.db.get_single_value("Genie Settings", "enable_user_impersonation"): + frappe.throw(_("User Impersonation is disabled")) + if user == "Administrator": + frappe.throw(_("You cannot impersonate Administrator")) + + return _generate_temporary_login_link(user, 1) \ No newline at end of file