Skip to content

Commit

Permalink
feat: add some safeguards to user deletion (LAN-847)
Browse files Browse the repository at this point in the history
  • Loading branch information
barredterra committed Aug 31, 2024
1 parent 089dd5b commit 67f93b5
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions landa/organization_management/user/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,29 @@ def delete_activity_logs(user: str):

def delete_or_disable_inactive_users():
"""Delete or disable all users that have not been active for 18 months."""
from datetime import datetime

from frappe.permissions import get_roles
from frappe.utils.data import add_months, getdate

cutoff_date = add_months(getdate(), -18)
for user in frappe.get_all(
users_to_delete = frappe.get_all(
"User",
filters={
"last_active": ("<", cutoff_date),
"name": ("not in", STANDARD_USERS),
},
filters=[
["last_active", "is", "set"],
["last_active", "<", cutoff_date],
["name", "not in", STANDARD_USERS],
],
pluck="name",
):
)

assert getdate(cutoff_date).year <= datetime.now().year - 1
assert (
len(users_to_delete) / frappe.db.count("User", filters={"name": ("not in", STANDARD_USERS)})
< 0.3
)

for user in users_to_delete:
if "System Manager" in get_roles(user):
continue

Expand Down

0 comments on commit 67f93b5

Please sign in to comment.