Skip to content

Commit

Permalink
feat(sec): log IP addresses of login/logout activities (frappe#21844)
Browse files Browse the repository at this point in the history
  • Loading branch information
ankush committed Jul 28, 2023
1 parent ef51dde commit e31038d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
5 changes: 4 additions & 1 deletion frappe/core/doctype/activity_log/activity_log.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
// For license information, please see license.txt

frappe.ui.form.on("Activity Log", {
refresh: function () {},
refresh: function (frm) {
// Nothing in this form is supposed to be editable.
frm.disable_form();
},
});
8 changes: 7 additions & 1 deletion frappe/core/doctype/activity_log/activity_log.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"column_break_5",
"additional_info",
"communication_date",
"ip_address",
"column_break_7",
"operation",
"status",
Expand Down Expand Up @@ -148,12 +149,17 @@
"fieldtype": "Data",
"in_list_view": 1,
"label": "Full Name"
},
{
"fieldname": "ip_address",
"fieldtype": "Data",
"label": "IP Address"
}
],
"icon": "fa fa-comment",
"index_web_pages_for_search": 1,
"links": [],
"modified": "2022-09-13 15:19:42.474114",
"modified": "2023-07-28 13:26:32.281278",
"modified_by": "Administrator",
"module": "Core",
"name": "Activity Log",
Expand Down
6 changes: 6 additions & 0 deletions frappe/core/doctype/activity_log/activity_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class ActivityLog(Document):
communication_date: DF.Datetime | None
content: DF.TextEditor | None
full_name: DF.Data | None
ip_address: DF.Data | None
link_doctype: DF.Link | None
link_name: DF.DynamicLink | None
operation: DF.Literal["", "Login", "Logout"]
Expand All @@ -40,6 +41,7 @@ def before_insert(self):
def validate(self):
self.set_status()
set_timeline_doc(self)
self.set_ip_address()

def set_status(self):
if not self.is_new():
Expand All @@ -48,6 +50,10 @@ def set_status(self):
if self.reference_doctype and self.reference_name:
self.status = "Linked"

def set_ip_address(self):
if self.operation in ("Login", "Logout"):
self.ip_address = getattr(frappe.local, "request_ip")

@staticmethod
def clear_old_logs(days=None):
if not days:
Expand Down

0 comments on commit e31038d

Please sign in to comment.