Skip to content

Commit

Permalink
Merge pull request #7 from wahni-green/impersonation
Browse files Browse the repository at this point in the history
feat: user impersonation
  • Loading branch information
rtdany10 authored Nov 18, 2023
2 parents 363a9bc + a655a6a commit aaa77ea
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
9 changes: 8 additions & 1 deletion genie/genie/doctype/genie_settings/genie_settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"field_order": [
"enable_ticket_raising",
"column_break_nuxg",
"enable_user_impersonation",
"support_portal_section",
"support_url",
"save_recording",
Expand Down Expand Up @@ -73,11 +74,17 @@
"fieldname": "max_recording_size",
"fieldtype": "Int",
"label": "Max Recording Size(MB)"
},
{
"default": "0",
"fieldname": "enable_user_impersonation",
"fieldtype": "Check",
"label": "Enable User Impersonation"
}
],
"issingle": 1,
"links": [],
"modified": "2023-10-15 14:47:21.063993",
"modified": "2023-11-18 11:41:53.318912",
"modified_by": "Administrator",
"module": "Genie",
"name": "Genie Settings",
Expand Down
2 changes: 1 addition & 1 deletion genie/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
26 changes: 26 additions & 0 deletions genie/public/js/impersonation.js
Original file line number Diff line number Diff line change
@@ -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 = `<a href=${r.message}>this link</a>`;
frappe.msgprint(
__("Please open {0} in an incognito window to impersonate {1}", [impersonate_url, frm.doc.name]),
);
}
}
});
}, __("Password"));
}
}
});
19 changes: 19 additions & 0 deletions genie/utils/impersonation.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit aaa77ea

Please # to comment.