Skip to content
Snippets Groups Projects
Commit 8ee75531 authored by Arie Peterson's avatar Arie Peterson
Browse files

Merge branch '198-add-cli-command-for-deleting-old-unused-users' into 'main'

Resolve "Add cli command for deleting old unused users"

Closes #198

See merge request !176
parents a310f431 f3a23533
No related branches found
No related tags found
1 merge request!176Resolve "Add cli command for deleting old unused users"
Pipeline #44905 passed with stages
in 3 minutes and 6 seconds
......@@ -7,6 +7,7 @@ the user entries in the database(s)"""
import sys
import click
import datetime
import ory_hydra_client
import ory_kratos_client
from flask import current_app
......@@ -82,6 +83,42 @@ def list_app():
print(f"App name: {obj.name}\tSlug: {obj.slug},\tURL: {obj.get_url()}\tStatus: {obj.get_status()}")
@user_cli.command("cleanup")
@click.option("--dry-run", is_flag=True, default=False)
def cleanup_users(dry_run):
"""
Remove users that have never been active and are at least six weeks old.
"""
current_app.logger.info("Listing inactive users")
if dry_run:
print("Dry run, so not deleting anything.")
users = KratosUser.find_all(kratos_identity_api)
number_users = 0
number_inactive_users = 0
for user in users:
number_users = number_users + 1
try:
last_recovery = user.metadata_admin.get('last_recovery')
except (KeyError, AttributeError):
last_recovery = None
if last_recovery is not None:
continue
print(user)
print(f" Created at: {user.created_at}")
# For this long period we ignore any timezone difference.
age = datetime.datetime.now(datetime.timezone.utc) - user.created_at
if age > datetime.timedelta(weeks=6):
print("That's more than 6 weeks ago.")
number_inactive_users = number_inactive_users + 1
if not dry_run:
print("Deleting.")
user.delete()
if dry_run:
print(f"Would delete {number_inactive_users} users out of {number_users} total.")
else:
print(f"Deleted {number_inactive_users} users out of {number_users} total.")
@app_cli.command(
"delete",
)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment